Skip to content

Commit

Permalink
add bloc for torrent
Browse files Browse the repository at this point in the history
  • Loading branch information
ALEZ-DEV committed Nov 10, 2023
1 parent e35bd3c commit ba465c7
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ target/

# Generated messages
*/**/messages/

.vscode
24 changes: 18 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions lib/blocs/get_torrents_bloc/get_torrents_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'dart:developer' as dev;

import 'package:bloc/bloc.dart';
import 'package:fixnum/fixnum.dart';
import 'package:meta/meta.dart';

import 'package:anime_kanri/messages/nyaa_search.pb.dart' as nyaa_search;
import 'package:rinf/rinf.dart';

part 'get_torrents_event.dart';
part 'get_torrents_state.dart';

class GetTorrentsBloc extends Bloc<GetTorrentsEvent, GetTorrentsState> {
GetTorrentsBloc() : super(GetTorrentsInitial()) {
on<GetTorrents>((event, emit) async {
emit(GetTorrentsLoading());
dev.log('start loading');
try {
if (event.search.isNotEmpty) {
final requestMessage = nyaa_search.ReadRequest(
searchInput: event.search,
pageInput: Int64(event.page),
);
final rustRequest = RustRequest(
resource: nyaa_search.ID,
operation: RustOperation.Read,
message: requestMessage.writeToBuffer(),
);

final rustResponse = await requestToRust(rustRequest);

if (rustResponse.message == null) {
emit(GetTorrentsFailure('Rust Message is null'));
return;
}

final responseMessage = nyaa_search.ReadResponse.fromBuffer(
rustResponse.message!,
);

dev.log('successfully load');
emit(GetTorrentsSuccess(responseMessage));
} else {
emit(GetTorrentsInitial());
}
} catch (e) {
dev.log(e.toString());
emit(GetTorrentsFailure(e.toString()));
rethrow;
}
});
}
}
11 changes: 11 additions & 0 deletions lib/blocs/get_torrents_bloc/get_torrents_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'get_torrents_bloc.dart';

@immutable
sealed class GetTorrentsEvent {}

class GetTorrents extends GetTorrentsEvent {
GetTorrents(this.search, this.page);

final String search;
final int page;
}
20 changes: 20 additions & 0 deletions lib/blocs/get_torrents_bloc/get_torrents_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
part of 'get_torrents_bloc.dart';

@immutable
sealed class GetTorrentsState {}

final class GetTorrentsInitial extends GetTorrentsState {}

final class GetTorrentsLoading extends GetTorrentsState {}

final class GetTorrentsFailure extends GetTorrentsState {
GetTorrentsFailure(this.errorMsg);

final String errorMsg;
}

final class GetTorrentsSuccess extends GetTorrentsState {
GetTorrentsSuccess(this.searchInfo);

final nyaa_search.ReadResponse searchInfo;
}
33 changes: 21 additions & 12 deletions lib/main_app.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:anime_kanri/blocs/get_torrents_bloc/get_torrents_bloc.dart';
import 'package:flutter/material.dart';

import 'package:anime_kanri/theme/theme.dart' as AnimeKanri;
import 'package:anime_kanri/screens/screens.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class MainApp extends StatefulWidget {
const MainApp({super.key});
Expand All @@ -27,20 +29,27 @@ class _MainAppState extends State<MainApp> {
theme: AnimeKanri.Theme.light,
darkTheme: AnimeKanri.Theme.dark,
home: Scaffold(
body: Row(
children: [
NavigationRail(
selectedIndex: _selectedIndexPage,
destinations: Screens.pagesRailDestinations,
onDestinationSelected: _changeDestination,
),
Expanded(
flex: 1,
child: SizedBox(
child: Screens.pages[_selectedIndexPage],
),
body: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => GetTorrentsBloc(),
),
],
child: Row(
children: [
NavigationRail(
selectedIndex: _selectedIndexPage,
destinations: Screens.pagesRailDestinations,
onDestinationSelected: _changeDestination,
),
Expanded(
flex: 1,
child: SizedBox(
child: Screens.pages[_selectedIndexPage],
),
),
],
),
),
),
);
Expand Down
70 changes: 67 additions & 3 deletions lib/screens/torrent_search_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:anime_kanri/widget/torrent_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class TorrentSearchScreen extends StatelessWidget {
import 'package:anime_kanri/blocs/get_torrents_bloc/get_torrents_bloc.dart';
import 'package:anime_kanri/messages/nyaa_search.pb.dart' as nyaa_rsearch;

class TorrentSearchScreen extends StatefulWidget {
const TorrentSearchScreen({super.key});

static NavigationRailDestination navigationDestination =
Expand All @@ -9,10 +14,69 @@ class TorrentSearchScreen extends StatelessWidget {
label: Text('Search for Torrents'),
);

@override
State<TorrentSearchScreen> createState() => _TorrentSearchScreenState();
}

class _TorrentSearchScreenState extends State<TorrentSearchScreen> {
final TextEditingController _searchController = TextEditingController();

@override
void initState() {
super.initState();
}

void _searchTorrent(String search, int page) {
context.read<GetTorrentsBloc>().add(GetTorrents(search, page));
}

@override
Widget build(BuildContext context) {
return const Center(
child: Text('there will be torrents'),
return Column(
children: [
Container(
color: Theme.of(context).navigationRailTheme.backgroundColor,
child: Padding(
padding: const EdgeInsets.all(10),
child: TextField(
controller: _searchController,
onSubmitted: (search) {
_searchTorrent(search, 1);
},
),
),
),
Expanded(
child: BlocBuilder<GetTorrentsBloc, GetTorrentsState>(
bloc: context.read<GetTorrentsBloc>(),
builder: (context, state) {
if (state is GetTorrentsFailure) {
return const Center(
child: Text('Somthing goes wrong'),
);
} else if (state is GetTorrentsSuccess) {
final List<nyaa_rsearch.Torrent> torrents =
state.searchInfo.torrents;

return ListView.builder(
itemCount: torrents.length,
itemBuilder: (context, index) => TorrentItem(
torrent: torrents[index],
),
);
} else if (state is GetTorrentsLoading) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return const Center(
child: Text('search something'),
);
}
},
),
),
],
);
}
}
73 changes: 73 additions & 0 deletions lib/widget/torrent_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';

import 'package:anime_kanri/messages/nyaa_search.pb.dart' as nyaa_rsearch;
import 'package:flutter/services.dart';

class TorrentItem extends StatelessWidget {
const TorrentItem({required this.torrent, super.key});

final nyaa_rsearch.Torrent torrent;

@override
Widget build(BuildContext context) {
return Card(
elevation: 0,
color: Theme.of(context).colorScheme.surfaceVariant,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
torrent.name,
overflow: TextOverflow.clip,
),
Text(
'Seeders : ${torrent.seeders.toString()}',
style: const TextStyle(
color: Colors.green,
),
),
Text(
'Leechers : ${torrent.leechers.toString()}',
style: const TextStyle(
color: Colors.red,
),
),
Text(
'Approved : ${torrent.approved.toString()}',
style: const TextStyle(
color: Colors.green,
),
),
],
),
Column(
children: [
TextButton(
onPressed: () async {
await Clipboard.setData(
ClipboardData(text: torrent.magnetLink),
);
},
child: const Text('Copy Magnetlink'),
),
TextButton(
onPressed: () async {
await Clipboard.setData(
ClipboardData(text: torrent.torrentFile),
);
},
child: const Text('Copy Torrent link'),
),
],
)
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion native/hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ lazy_static = "1.4.0"
tokio = { version = "1.28.2", features = ["sync", "macros"] }
prost = "0.12.0"
sample_crate = { path = "../sample_crate" }
nyaa-rsearch = "0.1.2"
nyaa-rsearch = "0.1.3"
Loading

0 comments on commit ba465c7

Please sign in to comment.