From 5d127062f4dd54dedb14f796f49c5fbfe3ff293a Mon Sep 17 00:00:00 2001 From: lauirf <592184299@qq.com> Date: Sat, 11 Jan 2025 00:57:45 +0800 Subject: [PATCH 1/2] show episode num --- lib/pages/info/info_controller.dart | 17 ++++ lib/pages/info/info_page.dart | 135 ++++++++++++++++++---------- lib/request/query_manager.dart | 8 +- 3 files changed, 113 insertions(+), 47 deletions(-) diff --git a/lib/pages/info/info_controller.dart b/lib/pages/info/info_controller.dart index 28d2802d..1eae311d 100644 --- a/lib/pages/info/info_controller.dart +++ b/lib/pages/info/info_controller.dart @@ -1,5 +1,6 @@ import 'package:kazumi/bean/dialog/dialog_helper.dart'; import 'package:kazumi/modules/bangumi/bangumi_item.dart'; +import 'package:kazumi/modules/roads/road_module.dart'; import 'package:kazumi/pages/collect/collect_controller.dart'; import 'package:kazumi/plugins/plugins_controller.dart'; import 'package:kazumi/pages/video/video_controller.dart'; @@ -71,6 +72,21 @@ abstract class _InfoController with Store { }); } + Future getEpisodesNum(String url, String pluginName) async { + final PluginsController pluginsController = + Modular.get(); + List roads = []; + for (Plugin plugin in pluginsController.pluginList) { + if (plugin.name == pluginName) { + roads = await plugin.querychapterRoads(url); + break; + } + } + return roads.isNotEmpty && roads[0].data.isNotEmpty + ? roads[0].data.length + : 0; + } + Future queryRoads(String url, String pluginName) async { final PluginsController pluginsController = Modular.get(); @@ -81,6 +97,7 @@ abstract class _InfoController with Store { if (plugin.name == pluginName) { videoPageController.roadList .addAll(await plugin.querychapterRoads(url)); + break; } } KazumiLogger() diff --git a/lib/pages/info/info_page.dart b/lib/pages/info/info_page.dart index 6a69f02d..bf2a7154 100644 --- a/lib/pages/info/info_page.dart +++ b/lib/pages/info/info_page.dart @@ -32,7 +32,7 @@ class _InfoPageState extends State final PluginsController pluginsController = Modular.get(); final PopularController popularController = Modular.get(); late TabController tabController; - + final Map> pluginToEpisodesNum = {}; /// Concurrent query manager late QueryManager queryManager; @@ -43,13 +43,23 @@ class _InfoPageState extends State queryBangumiInfoByID(infoController.bangumiItem.id); } queryManager = QueryManager(); - queryManager.querySource(popularController.keyword); tabController = TabController(length: pluginsController.pluginList.length, vsync: this); + tabController.addListener(() { + onPluginChange(); + }); + queryManager.querySource(popularController.keyword).then((_) { + if(mounted){ + onPluginChange(first: true); + } + }); } @override void dispose() { + tabController.removeListener(() { + onPluginChange(); + }); queryManager.cancel(); infoController.characterList.clear(); infoController.commentsList.clear(); @@ -172,49 +182,7 @@ class _InfoPageState extends State )) .toList(), ), - Expanded( - child: Observer( - builder: (context) => TabBarView( - controller: tabController, - children: List.generate( - pluginsController.pluginList.length, (pluginIndex) { - var plugin = pluginsController.pluginList[pluginIndex]; - var cardList = []; - for (var searchResponse - in infoController.pluginSearchResponseList) { - if (searchResponse.pluginName == plugin.name) { - for (var searchItem in searchResponse.data) { - cardList.add(Card( - color: Colors.transparent, - child: ListTile( - tileColor: Colors.transparent, - title: Text(searchItem.name), - onTap: () async { - KazumiDialog.showLoading(msg: '获取中'); - videoPageController.currentPlugin = plugin; - videoPageController.title = searchItem.name; - videoPageController.src = searchItem.src; - try { - await infoController.queryRoads( - searchItem.src, plugin.name); - KazumiDialog.dismiss(); - Modular.to.pushNamed('/video/'); - } catch (e) { - KazumiLogger() - .log(Level.error, e.toString()); - KazumiDialog.dismiss(); - } - }, - ), - )); - } - } - } - return ListView(children: cardList); - }), - ), - ), - ) + pluginTabBarView(), ], ), floatingActionButton: FloatingActionButton( @@ -240,4 +208,81 @@ class _InfoPageState extends State ), ); } + + Widget pluginTabBarView() { + return Expanded( + child: Observer( + builder: (context) => TabBarView( + controller: tabController, + children: + List.generate(pluginsController.pluginList.length, (pluginIndex) { + var plugin = pluginsController.pluginList[pluginIndex]; + var cardList = []; + for (var searchResponse + in infoController.pluginSearchResponseList) { + if (searchResponse.pluginName == plugin.name) { + for (var searchItem in searchResponse.data) { + final num = pluginToEpisodesNum.containsKey(plugin.name) && + pluginToEpisodesNum[plugin.name]! + .containsKey(searchItem.src) + ? pluginToEpisodesNum[plugin.name]![searchItem.src] + : 0; + cardList.add(Card( + color: Colors.transparent, + child: ListTile( + tileColor: Colors.transparent, + title: Text('${searchItem.name} (${num==0?"loading...":num})'), + onTap: () async { + KazumiDialog.showLoading(msg: '获取中'); + videoPageController.currentPlugin = plugin; + videoPageController.title = searchItem.name; + videoPageController.src = searchItem.src; + try { + await infoController.queryRoads( + searchItem.src, plugin.name); + KazumiDialog.dismiss(); + Modular.to.pushNamed('/video/'); + } catch (e) { + KazumiLogger().log(Level.error, e.toString()); + KazumiDialog.dismiss(); + } + }, + ), + )); + } + } + } + return ListView(children: cardList); + }), + ), + ), + ); + } + + Future onPluginChange({first = false}) async { + if (tabController.indexIsChanging||first) { + var pluginIndex = tabController.index; + var plugin = pluginsController.pluginList[pluginIndex]; + var name = plugin.name; + if (!pluginToEpisodesNum.containsKey(name)) { + Map episodesNumList = {}; + pluginToEpisodesNum[name] = episodesNumList; + for (var searchResponse in infoController.pluginSearchResponseList) { + if (searchResponse.pluginName == plugin.name) { + for (var searchItem in searchResponse.data) { + final num = + await infoController.getEpisodesNum(searchItem.src, name); + if (mounted) { + episodesNumList[searchItem.src] = num; + setState(() {}); + } + } + } + } + if (mounted) { + setState(() {}); + } + } + } + } } diff --git a/lib/request/query_manager.dart b/lib/request/query_manager.dart index 50349371..45b2b759 100644 --- a/lib/request/query_manager.dart +++ b/lib/request/query_manager.dart @@ -12,6 +12,7 @@ class QueryManager { Future querySource(String keyword) async { _controller = StreamController(); + int count = pluginsController.pluginList.length; infoController.pluginSearchResponseList.clear(); for (Plugin plugin in pluginsController.pluginList) { @@ -31,15 +32,18 @@ class QueryManager { _controller.add(result); }).catchError((error) { if (_isCancelled) return; - + infoController.pluginSearchStatus[plugin.name] = 'error'; + --count; + if (count == 0) return; }); } await for (var result in _controller.stream) { if (_isCancelled) break; - infoController.pluginSearchResponseList.add(result); + --count; + if (count == 0) break; } } From 6ba79061419592325cb7e09fb0436be2fce79e56 Mon Sep 17 00:00:00 2001 From: lauirf <592184299@qq.com> Date: Sat, 11 Jan 2025 01:21:25 +0800 Subject: [PATCH 2/2] fix code --- lib/pages/info/info_page.dart | 4 +--- lib/request/query_manager.dart | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/pages/info/info_page.dart b/lib/pages/info/info_page.dart index bf2a7154..2c8bfbed 100644 --- a/lib/pages/info/info_page.dart +++ b/lib/pages/info/info_page.dart @@ -277,11 +277,9 @@ class _InfoPageState extends State setState(() {}); } } + break; } } - if (mounted) { - setState(() {}); - } } } } diff --git a/lib/request/query_manager.dart b/lib/request/query_manager.dart index 45b2b759..11db8700 100644 --- a/lib/request/query_manager.dart +++ b/lib/request/query_manager.dart @@ -32,7 +32,6 @@ class QueryManager { _controller.add(result); }).catchError((error) { if (_isCancelled) return; - infoController.pluginSearchStatus[plugin.name] = 'error'; --count; if (count == 0) return;