Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/pages/info/info_controller.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -71,6 +72,21 @@ abstract class _InfoController with Store {
});
}

Future<int> getEpisodesNum(String url, String pluginName) async {
final PluginsController pluginsController =
Modular.get<PluginsController>();
List<Road> 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<void> queryRoads(String url, String pluginName) async {
final PluginsController pluginsController =
Modular.get<PluginsController>();
Expand All @@ -81,6 +97,7 @@ abstract class _InfoController with Store {
if (plugin.name == pluginName) {
videoPageController.roadList
.addAll(await plugin.querychapterRoads(url));
break;
}
}
KazumiLogger()
Expand Down
133 changes: 88 additions & 45 deletions lib/pages/info/info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _InfoPageState extends State<InfoPage>
final PluginsController pluginsController = Modular.get<PluginsController>();
final PopularController popularController = Modular.get<PopularController>();
late TabController tabController;

final Map<String, Map<String,int>> pluginToEpisodesNum = {};
/// Concurrent query manager
late QueryManager queryManager;

Expand All @@ -43,13 +43,23 @@ class _InfoPageState extends State<InfoPage>
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();
Expand Down Expand Up @@ -172,49 +182,7 @@ class _InfoPageState extends State<InfoPage>
))
.toList(),
),
Expanded(
child: Observer(
builder: (context) => TabBarView(
controller: tabController,
children: List.generate(
pluginsController.pluginList.length, (pluginIndex) {
var plugin = pluginsController.pluginList[pluginIndex];
var cardList = <Widget>[];
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(
Expand All @@ -240,4 +208,79 @@ class _InfoPageState extends State<InfoPage>
),
);
}

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 = <Widget>[];
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<void> 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<String, int> 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(() {});
}
}
break;
}
}
}
}
}
}
7 changes: 5 additions & 2 deletions lib/request/query_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class QueryManager {

Future<void> querySource(String keyword) async {
_controller = StreamController();
int count = pluginsController.pluginList.length;
infoController.pluginSearchResponseList.clear();

for (Plugin plugin in pluginsController.pluginList) {
Expand All @@ -31,15 +32,17 @@ 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;
}
}

Expand Down