Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

refactor: query logic in popular page (#770)

ErBW_s9f37550df419
diff --git a/lib/pages/popular/popular_controller.dart b/lib/pages/popular/popular_controller.dart
index 21b67606..0b75d0ae 100644
--- a/lib/pages/popular/popular_controller.dart
+++ b/lib/pages/popular/popular_controller.dart
@@ -20,6 +20,9 @@ abstract class _PopularController with Store {
@observable
ObservableList<BangumiItem> bangumiList = ObservableList.of([]);
+ @observable
+ ObservableList<BangumiItem> trendList = ObservableList.of([]);
+
double scrollOffset = 0.0;
@observable
@@ -36,19 +39,23 @@ abstract class _PopularController with Store {
currentTag = s;
}
- Future<bool> queryBangumiFeed({String type = 'add'}) async {
+ void clearBangumiList() {
+ bangumiList.clear();
+ }
+
+ Future<void> queryBangumiByTrend({String type = 'add'}) async {
if (type == 'init') {
- bangumiList.clear();
+ trendList.clear();
}
isLoadingMore = true;
var result =
- await BangumiHTTP.getBangumiTrendsList(offset: bangumiList.length);
- bangumiList.addAll(result);
+ await BangumiHTTP.getBangumiTrendsList(offset: trendList.length);
+ trendList.addAll(result);
isLoadingMore = false;
- return true;
+ isTimeOut = trendList.isEmpty;
}
- Future<bool> queryBangumiList({String type = 'add'}) async {
+ Future<void> queryBangumiByTag({String type = 'add'}) async {
if (type == 'init') {
bangumiList.clear();
}
@@ -56,30 +63,19 @@ abstract class _PopularController with Store {
int randomNumber = Random().nextInt(8000) + 1;
var tag = currentTag;
var result = await BangumiHTTP.getBangumiList(rank: randomNumber, tag: tag);
- if (currentTag == tag) {
- bangumiList.addAll(result);
- isLoadingMore = false;
- isTimeOut = bangumiList.isEmpty;
- return true;
- }
- return false;
- }
-
- Future<bool> queryBangumiByRefresh() async {
- if (currentTag.isEmpty) {
- return await queryBangumiFeed(type: 'init');
- }
- return await queryBangumiList(type: 'init');
+ bangumiList.addAll(result);
+ isLoadingMore = false;
+ isTimeOut = bangumiList.isEmpty;
}
- Future<void> queryBangumi(String keyword) async {
+ Future<void> searchBangumi(String keyword) async {
currentTag = '';
isLoadingMore = true;
if (RegExp(r'^[0-9]+$').hasMatch(keyword)) {
// 纯数字时调用ID查询
final id = int.parse(keyword);
final BangumiItem? item = await BangumiHTTP.getBangumiInfoByID(id);
-
+
bangumiList.clear();
if (item != null) {
bangumiList.add(item); // 单个结果转为列表
@@ -91,5 +87,6 @@ abstract class _PopularController with Store {
bangumiList.addAll(result);
}
isLoadingMore = false;
+ isTimeOut = bangumiList.isEmpty;
}
}
diff --git a/lib/pages/popular/popular_controller.g.dart b/lib/pages/popular/popular_controller.g.dart
index b1226764..5810c2e7 100644
--- a/lib/pages/popular/popular_controller.g.dart
+++ b/lib/pages/popular/popular_controller.g.dart
@@ -41,6 +41,22 @@ mixin _$PopularController on _PopularController, Store {
});
}
+ late final _$trendListAtom =
+ Atom(name: '_PopularController.trendList', context: context);
+
+ @override
+ ObservableList<BangumiItem> get trendList {
+ _$trendListAtom.reportRead();
+ return super.trendList;
+ }
+
+ @override
+ set trendList(ObservableList<BangumiItem> value) {
+ _$trendListAtom.reportWrite(value, super.trendList, () {
+ super.trendList = value;
+ });
+ }
+
late final _$isLoadingMoreAtom =
Atom(name: '_PopularController.isLoadingMore', context: context);
@@ -78,6 +94,7 @@ mixin _$PopularController on _PopularController, Store {
return '''
currentTag: ${currentTag},
bangumiList: ${bangumiList},
+trendList: ${trendList},
isLoadingMore: ${isLoadingMore},
isTimeOut: ${isTimeOut}
''';
diff --git a/lib/pages/popular/popular_page.dart b/lib/pages/popular/popular_page.dart
index e17cf201..a5cce683 100644
--- a/lib/pages/popular/popular_page.dart
+++ b/lib/pages/popular/popular_page.dart
@@ -40,8 +40,8 @@ class _PopularPageState extends State<PopularPage>
void initState() {
super.initState();
scrollController.addListener(scrollListener);
- if (popularController.bangumiList.isEmpty) {
- popularController.queryBangumiFeed();
+ if (popularController.trendList.isEmpty) {
+ popularController.queryBangumiByTrend();
}
showSearchBar = popularController.searchKeyword.isNotEmpty;
}
@@ -67,9 +67,9 @@ class _PopularPageState extends State<PopularPage>
if (popularController.searchKeyword == '') {
KazumiLogger().log(Level.info, 'Popular is loading more');
if (popularController.currentTag != '') {
- popularController.queryBangumiList();
+ popularController.queryBangumiByTag();
} else {
- popularController.queryBangumiFeed();
+ popularController.queryBangumiByTrend();
}
}
}
@@ -99,170 +99,154 @@ class _PopularPageState extends State<PopularPage>
}
onBackPressed(context);
},
- child: RefreshIndicator(
- onRefresh: () async {
- await popularController.queryBangumiByRefresh();
- },
- child: Scaffold(
- appBar: SysAppBar(
- needTopOffset: false,
- // default 56 + 10
- leadingWidth: 66,
- leading: (navigationBarState.isBottom)
- ? Row(
- children: [
- const SizedBox(
- width: 10,
+ child: Scaffold(
+ appBar: SysAppBar(
+ needTopOffset: false,
+ // default 56 + 10
+ leadingWidth: 66,
+ leading: (navigationBarState.isBottom)
+ ? Row(
+ children: [
+ const SizedBox(
+ width: 10,
+ ),
+ ClipOval(
+ child: InkWell(
+ customBorder: const CircleBorder(),
+ onTap: () {
+ Modular.to.pushNamed('/settings/history');
+ },
+ child: Image.asset(
+ 'assets/images/logo/logo_android.png',
),
- ClipOval(
- child: InkWell(
- customBorder: const CircleBorder(),
- onTap: () {
- Modular.to.pushNamed('/settings/history');
- },
- child: Image.asset(
- 'assets/images/logo/logo_android.png',
- ),
- ),
- )
- ],
+ ),
)
- : null,
- backgroundColor: Colors.transparent,
- actions: [
- IconButton(
- onPressed: () async {
- if (!showSearchBar) {
- setState(() {
- showSearchBar = true;
- });
- _focusNode.requestFocus();
- } else {
- if (popularController.searchKeyword == '') {
- _focusNode.unfocus();
- setState(() {
- showSearchBar = false;
- });
- popularController.setCurrentTag('');
- await popularController.queryBangumiFeed(
- type: 'init');
- } else {
- popularController.setSearchKeyword('');
- setState(() {
- showSearchBar = true;
- });
- _focusNode.requestFocus();
- }
- }
- },
- icon: showSearchBar
- ? const Icon(Icons.close)
- : const Icon(Icons.search))
- ],
- title: Stack(
- children: [
- Positioned.fill(
- child: GestureDetector(
- behavior: HitTestBehavior.translucent,
- onPanStart: (_) => windowManager.startDragging(),
- child: Container(),
- ),
- ),
- Padding(
- padding:
- EdgeInsets.only(top: (Utils.isDesktop()) ? 8 : 0),
- child: Visibility(
- visible: showSearchBar,
- child: searchBar(),
- ),
- ),
- ],
+ ],
+ )
+ : null,
+ backgroundColor: Colors.transparent,
+ actions: [
+ IconButton(
+ onPressed: () async {
+ if (!showSearchBar) {
+ setState(() {
+ showSearchBar = true;
+ });
+ _focusNode.requestFocus();
+ } else {
+ if (popularController.searchKeyword == '') {
+ _focusNode.unfocus();
+ setState(() {
+ showSearchBar = false;
+ });
+ popularController.setCurrentTag('');
+ popularController.clearBangumiList();
+ } else {
+ popularController.setSearchKeyword('');
+ setState(() {
+ showSearchBar = true;
+ });
+ _focusNode.requestFocus();
+ }
+ }
+ },
+ icon: showSearchBar
+ ? const Icon(Icons.close)
+ : const Icon(Icons.search))
+ ],
+ title: Stack(
+ children: [
+ Positioned.fill(
+ child: GestureDetector(
+ behavior: HitTestBehavior.translucent,
+ onPanStart: (_) => windowManager.startDragging(),
+ child: Container(),
+ ),
),
- ),
- body: Column(
- children: [
- SizedBox(
- height: showTagFilter ? 50 : 0,
- child: tagFilter(),
+ Padding(
+ padding: EdgeInsets.only(top: (Utils.isDesktop()) ? 8 : 0),
+ child: Visibility(
+ visible: showSearchBar,
+ child: searchBar(),
),
- Expanded(
- child: CustomScrollView(
- controller: scrollController,
- slivers: [
- SliverToBoxAdapter(
- child: Observer(
- builder: (_) => Padding(
- padding: const EdgeInsets.only(
- top: 0, bottom: 10, left: 0),
- child: popularController.isLoadingMore
- ? const LinearProgressIndicator()
- : const SizedBox(
- height: 4.0,
- ),
- ),
- ),
+ ),
+ ],
+ ),
+ ),
+ body: Column(
+ children: [
+ SizedBox(
+ height: showTagFilter ? 50 : 0,
+ child: tagFilter(),
+ ),
+ Expanded(
+ child: CustomScrollView(
+ controller: scrollController,
+ slivers: [
+ SliverToBoxAdapter(
+ child: Observer(
+ builder: (_) => Padding(
+ padding: const EdgeInsets.only(
+ top: 0, bottom: 10, left: 0),
+ child: popularController.isLoadingMore
+ ? const LinearProgressIndicator()
+ : const SizedBox(
+ height: 4.0,
+ ),
),
- SliverPadding(
- padding: const EdgeInsets.fromLTRB(
- StyleString.cardSpace,
- 0,
- StyleString.cardSpace,
- 0),
- sliver: Observer(builder: (context) {
- if (popularController.bangumiList.isEmpty &&
- popularController.isTimeOut) {
- return SliverToBoxAdapter(
- child: SizedBox(
- height: 400,
- child: GeneralErrorWidget(
- errMsg: '什么都没有找到 (´;ω;`)',
- actions: [
- GeneralErrorButton(
- onPressed: () {
- popularController
- .queryBangumiList();
- },
- text: '点击重试',
- ),
- ],
- ),
- ),
- );
- }
- if (popularController.bangumiList.isEmpty &&
- !popularController.isTimeOut) {
- return SliverToBoxAdapter(
- child: SizedBox(
- height:
- (MediaQuery.of(context).size.height /
- 2),
- child: const Column(
- crossAxisAlignment:
- CrossAxisAlignment.center,
- mainAxisAlignment:
- MainAxisAlignment.center,
- children: [
- CircularProgressIndicator(),
- ],
- )),
- );
- }
- return contentGrid(
- popularController.bangumiList, orientation);
- })),
- ],
+ ),
),
- ),
- ],
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- scrollController.jumpTo(0.0);
- },
- child: const Icon(Icons.arrow_upward),
- )
- // backgroundColor: themedata.colorScheme.primaryContainer,
+ SliverPadding(
+ padding: const EdgeInsets.fromLTRB(
+ StyleString.cardSpace, 0, StyleString.cardSpace, 0),
+ sliver: Observer(builder: (context) {
+ if (popularController.isTimeOut) {
+ return SliverToBoxAdapter(
+ child: SizedBox(
+ height: 400,
+ child: GeneralErrorWidget(
+ errMsg: '什么都没有找到 (´;ω;`)',
+ actions: [
+ GeneralErrorButton(
+ onPressed: () {
+ if (popularController.searchKeyword !=
+ '') {
+ popularController.searchBangumi(
+ popularController.searchKeyword);
+ } else if (popularController
+ .trendList.isEmpty) {
+ popularController
+ .queryBangumiByTrend();
+ } else {
+ popularController.queryBangumiByTag();
+ }
+ },
+ text: '点击重试',
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+ return contentGrid(
+ popularController.bangumiList.isEmpty &&
+ !popularController.isLoadingMore
+ ? popularController.trendList
+ : popularController.bangumiList,
+ orientation);
+ })),
+ ],
+ ),
),
+ ],
+ ),
+ floatingActionButton: FloatingActionButton(
+ onPressed: () {
+ scrollController.jumpTo(0.0);
+ },
+ child: const Icon(Icons.arrow_upward),
+ ),
+ // backgroundColor: themedata.colorScheme.primaryContainer,
),
);
});
@@ -333,8 +317,7 @@ class _PopularPageState extends State<PopularPage>
onPressed: () async {
scrollController.jumpTo(0.0);
popularController.setCurrentTag('');
- await popularController.queryBangumiFeed(
- type: 'init');
+ popularController.clearBangumiList();
},
)
: FilledButton.tonal(
@@ -347,7 +330,7 @@ class _PopularPageState extends State<PopularPage>
showSearchBar = false;
});
popularController.setCurrentTag(filter);
- await popularController.queryBangumiList(
+ await popularController.queryBangumiByTag(
type: 'init');
},
),
@@ -385,10 +368,11 @@ class _PopularPageState extends State<PopularPage>
onSubmitted: (t) async {
popularController.setSearchKeyword(t);
if (t != '') {
- await popularController.queryBangumi(popularController.searchKeyword);
+ await popularController
+ .searchBangumi(popularController.searchKeyword);
} else {
popularController.setCurrentTag('');
- await popularController.queryBangumiFeed(type: 'init');
+ popularController.clearBangumiList();
}
},
);
diff --git a/lib/request/bangumi.dart b/lib/request/bangumi.dart
index 52a39720..b9e22202 100644
--- a/lib/request/bangumi.dart
+++ b/lib/request/bangumi.dart
@@ -140,7 +140,7 @@ class BangumiHTTP {
}
static Future<List<BangumiItem>> getBangumiTrendsList(
- {int type = 2, int limit = 20, int offset = 0}) async {
+ {int type = 2, int limit = 24, int offset = 0}) async {
List<BangumiItem> bangumiList = [];
var params = <String, dynamic>{
'type': type,