diff --git a/lib/http/search.dart b/lib/http/search.dart index dfd3f8db7..3d469a95e 100644 --- a/lib/http/search.dart +++ b/lib/http/search.dart @@ -94,16 +94,18 @@ class SearchHttp { if (categoryId != null) 'category_id': categoryId, }; var res = await Request().get(Api.searchByType, data: reqData); - if (res.data['code'] == 0 && res.data['data']['numPages'] > 0) { + if (res.data['code'] == 0) { dynamic data; try { switch (searchType) { case SearchType.video: List blackMidsList = localCache .get(LocalCacheKey.blackMidsList, defaultValue: []); - for (var i in res.data['data']['result']) { - // 屏蔽推广和拉黑用户 - i['available'] = !blackMidsList.contains(i['mid']); + if (res.data['data']['result'] != null) { + for (var i in res.data['data']['result']) { + // 屏蔽推广和拉黑用户 + i['available'] = !blackMidsList.contains(i['mid']); + } } data = SearchVideoModel.fromJson(res.data['data']); break; @@ -120,11 +122,7 @@ class SearchHttp { data = SearchArticleModel.fromJson(res.data['data']); break; } - if (data.list.isNotEmpty) { - return LoadingState.success(data.list); - } else { - return LoadingState.empty(); - } + return LoadingState.success(data); } catch (err) { print(err); return LoadingState.error(err.toString()); diff --git a/lib/models/search/result.dart b/lib/models/search/result.dart index a68159404..cb5f7988c 100644 --- a/lib/models/search/result.dart +++ b/lib/models/search/result.dart @@ -2,12 +2,19 @@ import 'package:PiliPalaX/utils/em.dart'; import 'package:PiliPalaX/utils/utils.dart'; class SearchVideoModel { - SearchVideoModel({this.list}); + SearchVideoModel({ + this.numResults, + this.list, + }); + + int? numResults; List? list; + SearchVideoModel.fromJson(Map json) { + numResults = (json['numResults'] as num?)?.toInt(); list = json['result'] - .where((e) => e['available'] == true) - .map((e) => SearchVideoItemModel.fromJson(e)) + ?.where((e) => e['available'] == true) + ?.map((e) => SearchVideoItemModel.fromJson(e)) .toList(); } } @@ -144,11 +151,18 @@ class Owner { } class SearchUserModel { - SearchUserModel({this.list}); + SearchUserModel({ + this.numResults, + this.list, + }); + + int? numResults; List? list; + SearchUserModel.fromJson(Map json) { + numResults = (json['numResults'] as num?)?.toInt(); list = json['result'] - .map((e) => SearchUserItemModel.fromJson(e)) + ?.map((e) => SearchUserItemModel.fromJson(e)) .toList(); } } @@ -211,11 +225,18 @@ class SearchUserItemModel { } class SearchLiveModel { - SearchLiveModel({this.list}); + SearchLiveModel({ + this.numResults, + this.list, + }); + + int? numResults; List? list; + SearchLiveModel.fromJson(Map json) { + numResults = (json['numResults'] as num?)?.toInt(); list = json['result'] - .map((e) => SearchLiveItemModel.fromJson(e)) + ?.map((e) => SearchLiveItemModel.fromJson(e)) .toList(); } } @@ -285,9 +306,16 @@ class SearchLiveItemModel { } class SearchMBangumiModel { - SearchMBangumiModel({this.list}); + SearchMBangumiModel({ + this.numResults, + this.list, + }); + + int? numResults; List? list; + SearchMBangumiModel.fromJson(Map json) { + numResults = (json['numResults'] as num?)?.toInt(); list = json['result'] != null ? json['result'] .map( @@ -382,11 +410,16 @@ class SearchMBangumiItemModel { } class SearchArticleModel { - SearchArticleModel({this.list}); + SearchArticleModel({ + this.numResults, + this.list, + }); + int? numResults; List? list; SearchArticleModel.fromJson(Map json) { + numResults = (json['numResults'] as num?)?.toInt(); list = json['result'] != null ? json['result'] .map( diff --git a/lib/pages/search_panel/controller.dart b/lib/pages/search_panel/controller.dart index 51695cc55..06a158b44 100644 --- a/lib/pages/search_panel/controller.dart +++ b/lib/pages/search_panel/controller.dart @@ -1,5 +1,6 @@ import 'package:PiliPalaX/http/loading_state.dart'; import 'package:PiliPalaX/pages/common/common_controller.dart'; +import 'package:PiliPalaX/pages/search_result/controller.dart'; import 'package:get/get.dart'; import 'package:PiliPalaX/http/search.dart'; import 'package:PiliPalaX/models/common/search_type.dart'; @@ -7,7 +8,7 @@ import 'package:PiliPalaX/utils/id_utils.dart'; import 'package:PiliPalaX/utils/utils.dart'; class SearchPanelController extends CommonController { - SearchPanelController({this.keyword, this.searchType}); + SearchPanelController({this.keyword, this.searchType, this.tag}); String? keyword; SearchType? searchType; // 结果排序方式 搜索类型为视频、专栏及相簿时 @@ -18,6 +19,9 @@ class SearchPanelController extends CommonController { int? orderSort; int? userType; int? categoryId; + String? tag; + late final searchResultController = + Get.find(tag: tag); @override void onInit() { @@ -26,8 +30,30 @@ class SearchPanelController extends CommonController { } @override - void handleSuccess(List currentList, List dataList) { - onPushDetail(dataList); + bool customHandleResponse(Success response) { + searchResultController.count[SearchType.values.indexOf(searchType!)] = + response.response.numResults; + if (response.response.list != null) { + List currentList = currentPage != 1 && loadingState.value is Success + ? (loadingState.value as Success).response + : []; + List dataList = currentPage == 1 + ? response.response.list + : currentList + response.response.list; + if (dataList.isNotEmpty) { + loadingState.value = LoadingState.success(dataList); + } else { + loadingState.value = LoadingState.empty(); + } + if (currentPage == 1) { + onPushDetail(response.response.list); + } + } else { + if (currentPage == 1) { + loadingState.value = LoadingState.empty(); + } + } + return true; } void onPushDetail(resultList) async { diff --git a/lib/pages/search_panel/view.dart b/lib/pages/search_panel/view.dart index c95ba496b..441398d3e 100644 --- a/lib/pages/search_panel/view.dart +++ b/lib/pages/search_panel/view.dart @@ -42,6 +42,7 @@ class _SearchPanelState extends State SearchPanelController( keyword: widget.keyword, searchType: widget.searchType, + tag: widget.tag, ), tag: widget.searchType!.type + widget.keyword!, ); diff --git a/lib/pages/search_result/controller.dart b/lib/pages/search_result/controller.dart index 9914d82b0..c6b4e4c64 100644 --- a/lib/pages/search_result/controller.dart +++ b/lib/pages/search_result/controller.dart @@ -4,6 +4,8 @@ class SearchResultController extends GetxController { String? keyword; int tabIndex = 0; + RxList count = List.generate(5, (_) => -1).toList().obs; + @override void onInit() { super.onInit(); diff --git a/lib/pages/search_result/view.dart b/lib/pages/search_result/view.dart index 57a6beca6..05140a7e6 100644 --- a/lib/pages/search_result/view.dart +++ b/lib/pages/search_result/view.dart @@ -13,19 +13,22 @@ class SearchResultPage extends StatefulWidget { class _SearchResultPageState extends State with TickerProviderStateMixin { - late SearchResultController? _searchResultController; + late SearchResultController _searchResultController; late TabController _tabController; + final String _tag = DateTime.now().millisecondsSinceEpoch.toString(); @override void initState() { super.initState(); - _searchResultController = Get.put(SearchResultController(), - tag: DateTime.now().millisecondsSinceEpoch.toString()); + _searchResultController = Get.put( + SearchResultController(), + tag: _tag, + ); _tabController = TabController( vsync: this, length: SearchType.values.length, - initialIndex: _searchResultController!.tabIndex, + initialIndex: _searchResultController.tabIndex, ); } @@ -52,7 +55,7 @@ class _SearchResultPageState extends State child: SizedBox( width: double.infinity, child: Text( - '${_searchResultController!.keyword}', + '${_searchResultController.keyword}', style: Theme.of(context).textTheme.titleMedium, ), ), @@ -72,9 +75,19 @@ class _SearchResultPageState extends State ), child: TabBar( controller: _tabController, - tabs: [ - for (var i in SearchType.values) Tab(text: i.label), - ], + tabs: SearchType.values + .map( + (item) => Obx( + () { + int count = _searchResultController + .count[SearchType.values.indexOf(item)]; + return Tab( + text: + '${item.label}${count != -1 ? ' ${count > 99 ? '99+' : count}' : ''}'); + }, + ), + ) + .toList(), isScrollable: true, indicatorWeight: 0, indicatorPadding: @@ -90,14 +103,14 @@ class _SearchResultPageState extends State unselectedLabelColor: Theme.of(context).colorScheme.outline, tabAlignment: TabAlignment.start, onTap: (index) { - if (index == _searchResultController!.tabIndex) { + if (index == _searchResultController.tabIndex) { Get.find( tag: SearchType.values[index].type + - _searchResultController!.keyword!) + _searchResultController.keyword!) .animateToTop(); } - _searchResultController!.tabIndex = index; + _searchResultController.tabIndex = index; }, ), ), @@ -108,9 +121,9 @@ class _SearchResultPageState extends State children: [ for (var i in SearchType.values) ...{ SearchPanel( - keyword: _searchResultController!.keyword, + keyword: _searchResultController.keyword, searchType: i, - tag: DateTime.now().millisecondsSinceEpoch.toString(), + tag: _tag, ) } ],