refactor: zone

This commit is contained in:
bggRGjQaUbCoE
2024-09-08 12:41:49 +08:00
parent d3a7f5fa1c
commit 35ca95230f
4 changed files with 118 additions and 154 deletions

View File

@@ -782,27 +782,25 @@ class VideoHttp {
} }
// 视频排行 // 视频排行
static Future getRankVideoList(int rid) async { static Future<LoadingState> getRankVideoList(int rid) async {
try { var rankApi = "${Api.getRankApi}?rid=$rid&type=all";
var rankApi = "${Api.getRankApi}?rid=$rid&type=all"; var res = await Request().get(rankApi);
var res = await Request().get(rankApi); if (res.data['code'] == 0) {
if (res.data['code'] == 0) { List<HotVideoItemModel> list = [];
List<HotVideoItemModel> list = []; List<int> blackMidsList =
List<int> blackMidsList = localCache localCache.get(LocalCacheKey.blackMidsList, defaultValue: <int>[]);
.get(LocalCacheKey.blackMidsList, defaultValue: [-1]) for (var i in res.data['data']['list']) {
.map<int>((e) => e as int) if (!blackMidsList.contains(i['owner']['mid'])) {
.toList(); list.add(HotVideoItemModel.fromJson(i));
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) { if (list.isNotEmpty) {
return {'status': false, 'data': [], 'msg': err}; return LoadingState.success(list);
} else {
return LoadingState.empty();
}
} else {
return LoadingState.error(res.data['message']);
} }
} }
} }

View File

@@ -81,7 +81,7 @@ List tabsConfig = [
), ),
'label': '全站', 'label': '全站',
'type': RandType.all, 'type': RandType.all,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '0'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 0), tag: '0'),
'page': const ZonePage(rid: 0), 'page': const ZonePage(rid: 0),
}, },
{ {
@@ -91,7 +91,7 @@ List tabsConfig = [
), ),
'label': '国创', 'label': '国创',
'type': RandType.creation, 'type': RandType.creation,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '168'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 168), tag: '168'),
'page': const ZonePage(rid: 168), 'page': const ZonePage(rid: 168),
}, },
{ {
@@ -101,7 +101,7 @@ List tabsConfig = [
), ),
'label': '动画', 'label': '动画',
'type': RandType.animation, 'type': RandType.animation,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '1'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 1), tag: '1'),
'page': const ZonePage(rid: 1), 'page': const ZonePage(rid: 1),
}, },
{ {
@@ -111,7 +111,7 @@ List tabsConfig = [
), ),
'label': '音乐', 'label': '音乐',
'type': RandType.music, 'type': RandType.music,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '3'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 3), tag: '3'),
'page': const ZonePage(rid: 3), 'page': const ZonePage(rid: 3),
}, },
{ {
@@ -121,7 +121,7 @@ List tabsConfig = [
), ),
'label': '舞蹈', 'label': '舞蹈',
'type': RandType.dance, 'type': RandType.dance,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '129'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 129), tag: '129'),
'page': const ZonePage(rid: 129), 'page': const ZonePage(rid: 129),
}, },
{ {
@@ -131,7 +131,7 @@ List tabsConfig = [
), ),
'label': '游戏', 'label': '游戏',
'type': RandType.game, 'type': RandType.game,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '4'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 4), tag: '4'),
'page': const ZonePage(rid: 4), 'page': const ZonePage(rid: 4),
}, },
{ {
@@ -141,7 +141,7 @@ List tabsConfig = [
), ),
'label': '知识', 'label': '知识',
'type': RandType.knowledge, 'type': RandType.knowledge,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '36'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 36), tag: '36'),
'page': const ZonePage(rid: 36), 'page': const ZonePage(rid: 36),
}, },
{ {
@@ -151,7 +151,7 @@ List tabsConfig = [
), ),
'label': '科技', 'label': '科技',
'type': RandType.technology, 'type': RandType.technology,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '188'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 188), tag: '188'),
'page': const ZonePage(rid: 188), 'page': const ZonePage(rid: 188),
}, },
{ {
@@ -161,7 +161,7 @@ List tabsConfig = [
), ),
'label': '运动', 'label': '运动',
'type': RandType.sport, 'type': RandType.sport,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '234'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 234), tag: '234'),
'page': const ZonePage(rid: 234), 'page': const ZonePage(rid: 234),
}, },
{ {
@@ -171,7 +171,7 @@ List tabsConfig = [
), ),
'label': '汽车', 'label': '汽车',
'type': RandType.car, 'type': RandType.car,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '223'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 223), tag: '223'),
'page': const ZonePage(rid: 223), 'page': const ZonePage(rid: 223),
}, },
{ {
@@ -181,7 +181,7 @@ List tabsConfig = [
), ),
'label': '生活', 'label': '生活',
'type': RandType.life, 'type': RandType.life,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '160'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 160), tag: '160'),
'page': const ZonePage(rid: 160), 'page': const ZonePage(rid: 160),
}, },
{ {
@@ -191,7 +191,7 @@ List tabsConfig = [
), ),
'label': '美食', 'label': '美食',
'type': RandType.food, 'type': RandType.food,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '211'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 211), tag: '211'),
'page': const ZonePage(rid: 211), 'page': const ZonePage(rid: 211),
}, },
{ {
@@ -201,7 +201,7 @@ List tabsConfig = [
), ),
'label': '动物', 'label': '动物',
'type': RandType.animal, 'type': RandType.animal,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '217'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 217), tag: '217'),
'page': const ZonePage(rid: 217), 'page': const ZonePage(rid: 217),
}, },
{ {
@@ -211,7 +211,7 @@ List tabsConfig = [
), ),
'label': '鬼畜', 'label': '鬼畜',
'type': RandType.madness, 'type': RandType.madness,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '119'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 119), tag: '119'),
'page': const ZonePage(rid: 119), 'page': const ZonePage(rid: 119),
}, },
{ {
@@ -221,7 +221,7 @@ List tabsConfig = [
), ),
'label': '时尚', 'label': '时尚',
'type': RandType.fashion, 'type': RandType.fashion,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '155'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 155), tag: '155'),
'page': const ZonePage(rid: 155), 'page': const ZonePage(rid: 155),
}, },
{ {
@@ -231,7 +231,7 @@ List tabsConfig = [
), ),
'label': '娱乐', 'label': '娱乐',
'type': RandType.entertainment, 'type': RandType.entertainment,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '5'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 5), tag: '5'),
'page': const ZonePage(rid: 5), 'page': const ZonePage(rid: 5),
}, },
{ {
@@ -241,7 +241,7 @@ List tabsConfig = [
), ),
'label': '影视', 'label': '影视',
'type': RandType.film, 'type': RandType.film,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '181'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 181), tag: '181'),
'page': const ZonePage(rid: 181), 'page': const ZonePage(rid: 181),
}, },
{ {
@@ -251,7 +251,7 @@ List tabsConfig = [
), ),
'label': '纪录', 'label': '纪录',
'type': RandType.documentary, 'type': RandType.documentary,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '177'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 177), tag: '177'),
'page': const ZonePage(rid: 177), 'page': const ZonePage(rid: 177),
}, },
{ {
@@ -261,7 +261,7 @@ List tabsConfig = [
), ),
'label': '电影', 'label': '电影',
'type': RandType.movie, 'type': RandType.movie,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '23'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 23), tag: '23'),
'page': const ZonePage(rid: 23), 'page': const ZonePage(rid: 23),
}, },
{ {
@@ -271,7 +271,7 @@ List tabsConfig = [
), ),
'label': '剧集', 'label': '剧集',
'type': RandType.teleplay, 'type': RandType.teleplay,
'ctr': Get.put<ZoneController>(ZoneController(), tag: '11'), 'ctr': Get.put<ZoneController>(ZoneController(zoneID: 11), tag: '11'),
'page': const ZonePage(rid: 11), 'page': const ZonePage(rid: 11),
} }
]; ];

View File

@@ -1,48 +1,19 @@
import 'package:PiliPalaX/utils/extension.dart'; import 'package:PiliPalaX/http/loading_state.dart';
import 'package:get/get.dart'; import 'package:PiliPalaX/pages/common/common_controller.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:PiliPalaX/http/video.dart'; import 'package:PiliPalaX/http/video.dart';
import 'package:PiliPalaX/models/model_hot_video_item.dart';
class ZoneController extends GetxController { class ZoneController extends CommonController {
final ScrollController scrollController = ScrollController(); ZoneController({required this.zoneID});
RxList<HotVideoItemModel> videoList = <HotVideoItemModel>[].obs;
bool isLoadingMore = false;
bool flag = false;
List<OverlayEntry?> popupDialog = <OverlayEntry?>[]; List<OverlayEntry?> popupDialog = <OverlayEntry?>[];
int zoneID = 0; int zoneID;
// 获取推荐 @override
Future queryRankFeed(type, rid) async { void onInit() {
zoneID = rid; super.onInit();
var res = await VideoHttp.getRankVideoList(zoneID); queryData();
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
Future onRefresh() async { Future<LoadingState> customGetData() => VideoHttp.getRankVideoList(zoneID);
queryRankFeed('onRefresh', zoneID);
}
// 上拉加载
Future onLoad() async {
queryRankFeed('onLoad', zoneID);
}
// 返回顶部并刷新
void animateToTop() {
scrollController.animToTop();
}
} }

View File

@@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@@ -26,7 +27,6 @@ class ZonePage extends StatefulWidget {
class _ZonePageState extends State<ZonePage> class _ZonePageState extends State<ZonePage>
with AutomaticKeepAliveClientMixin { with AutomaticKeepAliveClientMixin {
late ZoneController _zoneController; late ZoneController _zoneController;
Future? _futureBuilderFuture;
@override @override
bool get wantKeepAlive => true; bool get wantKeepAlive => true;
@@ -34,22 +34,14 @@ class _ZonePageState extends State<ZonePage>
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_zoneController = Get.put(ZoneController(), tag: widget.rid.toString()); _zoneController =
_futureBuilderFuture = _zoneController.queryRankFeed('init', widget.rid); Get.put(ZoneController(zoneID: widget.rid), tag: widget.rid.toString());
StreamController<bool> mainStream = StreamController<bool> mainStream =
Get.find<MainController>().bottomBarStream; Get.find<MainController>().bottomBarStream;
StreamController<bool> searchBarStream = StreamController<bool> searchBarStream =
Get.find<HomeController>().searchBarStream; Get.find<HomeController>().searchBarStream;
_zoneController.scrollController.addListener( _zoneController.scrollController.addListener(
() { () {
if (_zoneController.scrollController.position.pixels >=
_zoneController.scrollController.position.maxScrollExtent - 200) {
if (!_zoneController.isLoadingMore) {
_zoneController.isLoadingMore = true;
_zoneController.onLoad();
}
}
final ScrollDirection direction = final ScrollDirection direction =
_zoneController.scrollController.position.userScrollDirection; _zoneController.scrollController.position.userScrollDirection;
if (direction == ScrollDirection.forward) { if (direction == ScrollDirection.forward) {
@@ -66,7 +58,6 @@ class _ZonePageState extends State<ZonePage>
@override @override
void dispose() { void dispose() {
_zoneController.scrollController.removeListener(() {}); _zoneController.scrollController.removeListener(() {});
_zoneController.scrollController.dispose();
super.dispose(); super.dispose();
} }
@@ -82,71 +73,30 @@ class _ZonePageState extends State<ZonePage>
slivers: [ slivers: [
SliverPadding( SliverPadding(
// 单列布局 EdgeInsets.zero // 单列布局 EdgeInsets.zero
padding: const EdgeInsets.fromLTRB( padding: EdgeInsets.fromLTRB(
StyleString.cardSpace, StyleString.safeSpace, 0, 0), StyleString.cardSpace,
sliver: FutureBuilder( StyleString.safeSpace,
future: _futureBuilderFuture, 0,
builder: (context, snapshot) { MediaQuery.of(context).padding.bottom + 10,
if (snapshot.connectionState == ConnectionState.done) { ),
Map data = snapshot.data as Map; sliver: Obx(
if (data['status']) { () => _zoneController.loadingState.value is Loading
return Obx( ? _buildSkeleton()
() => SliverGrid( : _zoneController.loadingState.value is Success
gridDelegate: SliverGridDelegateWithExtentAndRatio( ? _buildBody(
mainAxisSpacing: StyleString.safeSpace, _zoneController.loadingState.value as Success)
crossAxisSpacing: StyleString.safeSpace, : HttpError(
maxCrossAxisExtent: Grid.maxRowWidth * 2, errMsg: _zoneController.loadingState.value is Error
childAspectRatio: StyleString.aspectRatio * 2.4, ? (_zoneController.loadingState.value as Error)
mainAxisExtent: 13), .errMsg
delegate: SliverChildBuilderDelegate((context, index) { : '没有相关数据',
return VideoCardH( fn: () {
videoItem: _zoneController.videoList[index], _zoneController.loadingState.value =
showPubdate: true, LoadingState.loading();
longPress: () { _zoneController.onRefresh();
_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),
);
}
},
), ),
), ),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).padding.bottom + 10,
),
)
], ],
), ),
); );
@@ -165,4 +115,49 @@ class _ZonePageState extends State<ZonePage>
), ),
); );
} }
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,
),
);
}
} }