From 35ca95230fc4ab9127b9b8ca2ee7e8a9f5938702 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Sun, 8 Sep 2024 12:41:49 +0800 Subject: [PATCH] refactor: zone --- lib/http/video.dart | 36 ++++--- lib/models/common/rank_type.dart | 40 ++++---- lib/pages/rank/zone/controller.dart | 51 +++------- lib/pages/rank/zone/view.dart | 145 ++++++++++++++-------------- 4 files changed, 118 insertions(+), 154 deletions(-) diff --git a/lib/http/video.dart b/lib/http/video.dart index 78b866c0..a3a3ce2e 100644 --- a/lib/http/video.dart +++ b/lib/http/video.dart @@ -782,27 +782,25 @@ class VideoHttp { } // 视频排行 - static Future getRankVideoList(int rid) async { - try { - var rankApi = "${Api.getRankApi}?rid=$rid&type=all"; - var res = await Request().get(rankApi); - if (res.data['code'] == 0) { - List list = []; - List blackMidsList = localCache - .get(LocalCacheKey.blackMidsList, defaultValue: [-1]) - .map((e) => e as int) - .toList(); - for (var i in res.data['data']['list']) { - if (!blackMidsList.contains(i['owner']['mid'])) { - list.add(HotVideoItemModel.fromJson(i)); - } + static Future getRankVideoList(int rid) async { + var rankApi = "${Api.getRankApi}?rid=$rid&type=all"; + var res = await Request().get(rankApi); + if (res.data['code'] == 0) { + List list = []; + List blackMidsList = + localCache.get(LocalCacheKey.blackMidsList, defaultValue: []); + for (var i in res.data['data']['list']) { + if (!blackMidsList.contains(i['owner']['mid'])) { + list.add(HotVideoItemModel.fromJson(i)); } - return {'status': true, 'data': list}; - } else { - return {'status': false, 'data': [], 'msg': res.data['message']}; } - } catch (err) { - return {'status': false, 'data': [], 'msg': err}; + if (list.isNotEmpty) { + return LoadingState.success(list); + } else { + return LoadingState.empty(); + } + } else { + return LoadingState.error(res.data['message']); } } } diff --git a/lib/models/common/rank_type.dart b/lib/models/common/rank_type.dart index 710100e9..4fab35a7 100644 --- a/lib/models/common/rank_type.dart +++ b/lib/models/common/rank_type.dart @@ -81,7 +81,7 @@ List tabsConfig = [ ), 'label': '全站', 'type': RandType.all, - 'ctr': Get.put(ZoneController(), tag: '0'), + 'ctr': Get.put(ZoneController(zoneID: 0), tag: '0'), 'page': const ZonePage(rid: 0), }, { @@ -91,7 +91,7 @@ List tabsConfig = [ ), 'label': '国创', 'type': RandType.creation, - 'ctr': Get.put(ZoneController(), tag: '168'), + 'ctr': Get.put(ZoneController(zoneID: 168), tag: '168'), 'page': const ZonePage(rid: 168), }, { @@ -101,7 +101,7 @@ List tabsConfig = [ ), 'label': '动画', 'type': RandType.animation, - 'ctr': Get.put(ZoneController(), tag: '1'), + 'ctr': Get.put(ZoneController(zoneID: 1), tag: '1'), 'page': const ZonePage(rid: 1), }, { @@ -111,7 +111,7 @@ List tabsConfig = [ ), 'label': '音乐', 'type': RandType.music, - 'ctr': Get.put(ZoneController(), tag: '3'), + 'ctr': Get.put(ZoneController(zoneID: 3), tag: '3'), 'page': const ZonePage(rid: 3), }, { @@ -121,7 +121,7 @@ List tabsConfig = [ ), 'label': '舞蹈', 'type': RandType.dance, - 'ctr': Get.put(ZoneController(), tag: '129'), + 'ctr': Get.put(ZoneController(zoneID: 129), tag: '129'), 'page': const ZonePage(rid: 129), }, { @@ -131,7 +131,7 @@ List tabsConfig = [ ), 'label': '游戏', 'type': RandType.game, - 'ctr': Get.put(ZoneController(), tag: '4'), + 'ctr': Get.put(ZoneController(zoneID: 4), tag: '4'), 'page': const ZonePage(rid: 4), }, { @@ -141,7 +141,7 @@ List tabsConfig = [ ), 'label': '知识', 'type': RandType.knowledge, - 'ctr': Get.put(ZoneController(), tag: '36'), + 'ctr': Get.put(ZoneController(zoneID: 36), tag: '36'), 'page': const ZonePage(rid: 36), }, { @@ -151,7 +151,7 @@ List tabsConfig = [ ), 'label': '科技', 'type': RandType.technology, - 'ctr': Get.put(ZoneController(), tag: '188'), + 'ctr': Get.put(ZoneController(zoneID: 188), tag: '188'), 'page': const ZonePage(rid: 188), }, { @@ -161,7 +161,7 @@ List tabsConfig = [ ), 'label': '运动', 'type': RandType.sport, - 'ctr': Get.put(ZoneController(), tag: '234'), + 'ctr': Get.put(ZoneController(zoneID: 234), tag: '234'), 'page': const ZonePage(rid: 234), }, { @@ -171,7 +171,7 @@ List tabsConfig = [ ), 'label': '汽车', 'type': RandType.car, - 'ctr': Get.put(ZoneController(), tag: '223'), + 'ctr': Get.put(ZoneController(zoneID: 223), tag: '223'), 'page': const ZonePage(rid: 223), }, { @@ -181,7 +181,7 @@ List tabsConfig = [ ), 'label': '生活', 'type': RandType.life, - 'ctr': Get.put(ZoneController(), tag: '160'), + 'ctr': Get.put(ZoneController(zoneID: 160), tag: '160'), 'page': const ZonePage(rid: 160), }, { @@ -191,7 +191,7 @@ List tabsConfig = [ ), 'label': '美食', 'type': RandType.food, - 'ctr': Get.put(ZoneController(), tag: '211'), + 'ctr': Get.put(ZoneController(zoneID: 211), tag: '211'), 'page': const ZonePage(rid: 211), }, { @@ -201,7 +201,7 @@ List tabsConfig = [ ), 'label': '动物', 'type': RandType.animal, - 'ctr': Get.put(ZoneController(), tag: '217'), + 'ctr': Get.put(ZoneController(zoneID: 217), tag: '217'), 'page': const ZonePage(rid: 217), }, { @@ -211,7 +211,7 @@ List tabsConfig = [ ), 'label': '鬼畜', 'type': RandType.madness, - 'ctr': Get.put(ZoneController(), tag: '119'), + 'ctr': Get.put(ZoneController(zoneID: 119), tag: '119'), 'page': const ZonePage(rid: 119), }, { @@ -221,7 +221,7 @@ List tabsConfig = [ ), 'label': '时尚', 'type': RandType.fashion, - 'ctr': Get.put(ZoneController(), tag: '155'), + 'ctr': Get.put(ZoneController(zoneID: 155), tag: '155'), 'page': const ZonePage(rid: 155), }, { @@ -231,7 +231,7 @@ List tabsConfig = [ ), 'label': '娱乐', 'type': RandType.entertainment, - 'ctr': Get.put(ZoneController(), tag: '5'), + 'ctr': Get.put(ZoneController(zoneID: 5), tag: '5'), 'page': const ZonePage(rid: 5), }, { @@ -241,7 +241,7 @@ List tabsConfig = [ ), 'label': '影视', 'type': RandType.film, - 'ctr': Get.put(ZoneController(), tag: '181'), + 'ctr': Get.put(ZoneController(zoneID: 181), tag: '181'), 'page': const ZonePage(rid: 181), }, { @@ -251,7 +251,7 @@ List tabsConfig = [ ), 'label': '纪录', 'type': RandType.documentary, - 'ctr': Get.put(ZoneController(), tag: '177'), + 'ctr': Get.put(ZoneController(zoneID: 177), tag: '177'), 'page': const ZonePage(rid: 177), }, { @@ -261,7 +261,7 @@ List tabsConfig = [ ), 'label': '电影', 'type': RandType.movie, - 'ctr': Get.put(ZoneController(), tag: '23'), + 'ctr': Get.put(ZoneController(zoneID: 23), tag: '23'), 'page': const ZonePage(rid: 23), }, { @@ -271,7 +271,7 @@ List tabsConfig = [ ), 'label': '剧集', 'type': RandType.teleplay, - 'ctr': Get.put(ZoneController(), tag: '11'), + 'ctr': Get.put(ZoneController(zoneID: 11), tag: '11'), 'page': const ZonePage(rid: 11), } ]; diff --git a/lib/pages/rank/zone/controller.dart b/lib/pages/rank/zone/controller.dart index 83b276a8..559a2a57 100644 --- a/lib/pages/rank/zone/controller.dart +++ b/lib/pages/rank/zone/controller.dart @@ -1,48 +1,19 @@ -import 'package:PiliPalaX/utils/extension.dart'; -import 'package:get/get.dart'; +import 'package:PiliPalaX/http/loading_state.dart'; +import 'package:PiliPalaX/pages/common/common_controller.dart'; import 'package:flutter/material.dart'; import 'package:PiliPalaX/http/video.dart'; -import 'package:PiliPalaX/models/model_hot_video_item.dart'; -class ZoneController extends GetxController { - final ScrollController scrollController = ScrollController(); - RxList videoList = [].obs; - bool isLoadingMore = false; - bool flag = false; +class ZoneController extends CommonController { + ZoneController({required this.zoneID}); List popupDialog = []; - int zoneID = 0; + int zoneID; - // 获取推荐 - Future queryRankFeed(type, rid) async { - zoneID = rid; - var res = await VideoHttp.getRankVideoList(zoneID); - if (res['status']) { - if (type == 'init') { - videoList.value = res['data']; - } else if (type == 'onRefresh') { - videoList.clear(); - videoList.addAll(res['data']); - } else if (type == 'onLoad') { - videoList.clear(); - videoList.addAll(res['data']); - } - } - isLoadingMore = false; - return res; + @override + void onInit() { + super.onInit(); + queryData(); } - // 下拉刷新 - Future onRefresh() async { - queryRankFeed('onRefresh', zoneID); - } - - // 上拉加载 - Future onLoad() async { - queryRankFeed('onLoad', zoneID); - } - - // 返回顶部并刷新 - void animateToTop() { - scrollController.animToTop(); - } + @override + Future customGetData() => VideoHttp.getRankVideoList(zoneID); } diff --git a/lib/pages/rank/zone/view.dart b/lib/pages/rank/zone/view.dart index 12d56b91..30b0b1f6 100644 --- a/lib/pages/rank/zone/view.dart +++ b/lib/pages/rank/zone/view.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:PiliPalaX/http/loading_state.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:get/get.dart'; @@ -26,7 +27,6 @@ class ZonePage extends StatefulWidget { class _ZonePageState extends State with AutomaticKeepAliveClientMixin { late ZoneController _zoneController; - Future? _futureBuilderFuture; @override bool get wantKeepAlive => true; @@ -34,22 +34,14 @@ class _ZonePageState extends State @override void initState() { super.initState(); - _zoneController = Get.put(ZoneController(), tag: widget.rid.toString()); - _futureBuilderFuture = _zoneController.queryRankFeed('init', widget.rid); + _zoneController = + Get.put(ZoneController(zoneID: widget.rid), tag: widget.rid.toString()); StreamController mainStream = Get.find().bottomBarStream; StreamController searchBarStream = Get.find().searchBarStream; _zoneController.scrollController.addListener( () { - if (_zoneController.scrollController.position.pixels >= - _zoneController.scrollController.position.maxScrollExtent - 200) { - if (!_zoneController.isLoadingMore) { - _zoneController.isLoadingMore = true; - _zoneController.onLoad(); - } - } - final ScrollDirection direction = _zoneController.scrollController.position.userScrollDirection; if (direction == ScrollDirection.forward) { @@ -66,7 +58,6 @@ class _ZonePageState extends State @override void dispose() { _zoneController.scrollController.removeListener(() {}); - _zoneController.scrollController.dispose(); super.dispose(); } @@ -82,71 +73,30 @@ class _ZonePageState extends State slivers: [ SliverPadding( // 单列布局 EdgeInsets.zero - padding: const EdgeInsets.fromLTRB( - StyleString.cardSpace, StyleString.safeSpace, 0, 0), - sliver: FutureBuilder( - future: _futureBuilderFuture, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - Map data = snapshot.data as Map; - if (data['status']) { - return Obx( - () => SliverGrid( - gridDelegate: SliverGridDelegateWithExtentAndRatio( - mainAxisSpacing: StyleString.safeSpace, - crossAxisSpacing: StyleString.safeSpace, - maxCrossAxisExtent: Grid.maxRowWidth * 2, - childAspectRatio: StyleString.aspectRatio * 2.4, - mainAxisExtent: 13), - delegate: SliverChildBuilderDelegate((context, index) { - return VideoCardH( - videoItem: _zoneController.videoList[index], - showPubdate: true, - longPress: () { - _zoneController.popupDialog.add( - _createPopupDialog( - _zoneController.videoList[index])); - Overlay.of(context) - .insert(_zoneController.popupDialog.last!); - }, - longPressEnd: _removePopupDialog, - ); - }, childCount: _zoneController.videoList.length), - ), - ); - } else { - return HttpError( - errMsg: data['msg'], - fn: () { - setState(() { - _futureBuilderFuture = - _zoneController.queryRankFeed('init', widget.rid); - }); - }, - ); - } - } else { - // 骨架屏 - return SliverGrid( - gridDelegate: SliverGridDelegateWithExtentAndRatio( - mainAxisSpacing: StyleString.safeSpace, - crossAxisSpacing: StyleString.safeSpace, - maxCrossAxisExtent: Grid.maxRowWidth * 2, - childAspectRatio: StyleString.aspectRatio * 2.4, - mainAxisExtent: 0), - delegate: SliverChildBuilderDelegate((context, index) { - return const VideoCardHSkeleton(); - }, childCount: 10), - ); - } - }, + padding: EdgeInsets.fromLTRB( + StyleString.cardSpace, + StyleString.safeSpace, + 0, + MediaQuery.of(context).padding.bottom + 10, + ), + sliver: Obx( + () => _zoneController.loadingState.value is Loading + ? _buildSkeleton() + : _zoneController.loadingState.value is Success + ? _buildBody( + _zoneController.loadingState.value as Success) + : HttpError( + errMsg: _zoneController.loadingState.value is Error + ? (_zoneController.loadingState.value as Error) + .errMsg + : '没有相关数据', + fn: () { + _zoneController.loadingState.value = + LoadingState.loading(); + _zoneController.onRefresh(); + }), ), ), - SliverToBoxAdapter( - child: SizedBox( - height: MediaQuery.of(context).padding.bottom + 10, - ), - ) ], ), ); @@ -165,4 +115,49 @@ class _ZonePageState extends State ), ); } + + Widget _buildSkeleton() { + return SliverGrid( + gridDelegate: SliverGridDelegateWithExtentAndRatio( + mainAxisSpacing: StyleString.safeSpace, + crossAxisSpacing: StyleString.safeSpace, + maxCrossAxisExtent: Grid.maxRowWidth * 2, + childAspectRatio: StyleString.aspectRatio * 2.4, + mainAxisExtent: 0, + ), + delegate: SliverChildBuilderDelegate( + (context, index) { + return const VideoCardHSkeleton(); + }, + childCount: 10, + ), + ); + } + + Widget _buildBody(Success loadingState) { + return SliverGrid( + gridDelegate: SliverGridDelegateWithExtentAndRatio( + mainAxisSpacing: StyleString.safeSpace, + crossAxisSpacing: StyleString.safeSpace, + maxCrossAxisExtent: Grid.maxRowWidth * 2, + childAspectRatio: StyleString.aspectRatio * 2.4, + mainAxisExtent: 13, + ), + delegate: SliverChildBuilderDelegate( + (context, index) { + return VideoCardH( + videoItem: loadingState.response[index], + showPubdate: true, + longPress: () { + _zoneController.popupDialog + .add(_createPopupDialog(loadingState.response[index])); + Overlay.of(context).insert(_zoneController.popupDialog.last!); + }, + longPressEnd: _removePopupDialog, + ); + }, + childCount: loadingState.response.length, + ), + ); + } }