From 56278678cf0491d50a292f40dbfb1644ddea01e8 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Sun, 8 Sep 2024 13:36:56 +0800 Subject: [PATCH] refactor: live --- lib/http/live.dart | 24 +++++++------- lib/pages/live/controller.dart | 53 +++++-------------------------- lib/pages/live/view.dart | 57 ++++++++++++---------------------- 3 files changed, 40 insertions(+), 94 deletions(-) diff --git a/lib/http/live.dart b/lib/http/live.dart index e624120e..c5bc0451 100644 --- a/lib/http/live.dart +++ b/lib/http/live.dart @@ -1,3 +1,5 @@ +import 'package:PiliPalaX/http/loading_state.dart'; + import '../models/live/item.dart'; import '../models/live/room_info.dart'; import '../models/live/room_info_h5.dart'; @@ -5,23 +7,21 @@ import 'api.dart'; import 'init.dart'; class LiveHttp { - static Future liveList( + static Future liveList( {int? vmid, int? pn, int? ps, String? orderType}) async { var res = await Request().get(Api.liveList, data: {'page': pn, 'page_size': 30, 'platform': 'web'}); if (res.data['code'] == 0) { - return { - 'status': true, - 'data': res.data['data']['list'] - .map((e) => LiveItemModel.fromJson(e)) - .toList() - }; + List list = res.data['data']['list'] + .map((e) => LiveItemModel.fromJson(e)) + .toList(); + if (list.isNotEmpty) { + return LoadingState.success(list); + } else { + return LoadingState.empty(); + } } else { - return { - 'status': false, - 'data': [], - 'msg': res.data['message'], - }; + return LoadingState.error(res.data['message']); } } diff --git a/lib/pages/live/controller.dart b/lib/pages/live/controller.dart index 252202f9..b7bdb9e5 100644 --- a/lib/pages/live/controller.dart +++ b/lib/pages/live/controller.dart @@ -1,57 +1,20 @@ -import 'package:PiliPalaX/utils/extension.dart'; +import 'package:PiliPalaX/http/loading_state.dart'; +import 'package:PiliPalaX/pages/common/common_controller.dart'; import 'package:flutter/material.dart'; -import 'package:get/get.dart'; -import 'package:hive/hive.dart'; import 'package:PiliPalaX/http/live.dart'; -import 'package:PiliPalaX/models/live/item.dart'; -import 'package:PiliPalaX/utils/storage.dart'; -class LiveController extends GetxController { - final ScrollController scrollController = ScrollController(); +class LiveController extends CommonController { int count = 12; - int _currentPage = 1; - RxInt crossAxisCount = 2.obs; - RxList liveList = [].obs; - bool flag = false; List popupDialog = []; - Box setting = GStorage.setting; @override void onInit() { super.onInit(); + queryData(); } - // 获取推荐 - Future queryLiveList(type) async { - // if (type == 'init') { - // _currentPage = 1; - // } - var res = await LiveHttp.liveList( - pn: _currentPage, - ); - if (res['status']) { - if (type == 'init') { - liveList.value = res['data']; - } else if (type == 'onLoad') { - liveList.addAll(res['data']); - } - _currentPage += 1; - } - return res; - } - - // 下拉刷新 - Future onRefresh() async { - queryLiveList('init'); - } - - // 上拉加载 - Future onLoad() async { - queryLiveList('onLoad'); - } - - // 返回顶部并刷新 - void animateToTop() { - scrollController.animToTop(); - } + @override + Future customGetData() => LiveHttp.liveList( + pn: currentPage, + ); } diff --git a/lib/pages/live/view.dart b/lib/pages/live/view.dart index e6b3cc2c..82440f2d 100644 --- a/lib/pages/live/view.dart +++ b/lib/pages/live/view.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:PiliPalaX/http/loading_state.dart'; import 'package:easy_debounce/easy_throttle.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -25,7 +26,6 @@ class LivePage extends StatefulWidget { class _LivePageState extends State with AutomaticKeepAliveClientMixin { final LiveController _liveController = Get.put(LiveController()); - late Future _futureBuilderFuture; @override bool get wantKeepAlive => true; @@ -33,7 +33,6 @@ class _LivePageState extends State @override void initState() { super.initState(); - _futureBuilderFuture = _liveController.queryLiveList('init'); StreamController mainStream = Get.find().bottomBarStream; StreamController searchBarStream = @@ -44,7 +43,7 @@ class _LivePageState extends State _liveController.scrollController.position.maxScrollExtent - 200) { EasyThrottle.throttle('liveList', const Duration(milliseconds: 200), () { - _liveController.onLoad(); + _liveController.onLoadMore(); }); } @@ -64,7 +63,6 @@ class _LivePageState extends State @override void dispose() { _liveController.scrollController.removeListener(() {}); - _liveController.scrollController.dispose(); super.dispose(); } @@ -90,35 +88,20 @@ class _LivePageState extends State // 单列布局 EdgeInsets.zero padding: const EdgeInsets.fromLTRB(0, StyleString.cardSpace, 0, 0), - sliver: FutureBuilder( - future: _futureBuilderFuture, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - if (snapshot.data == null) { - return const SliverToBoxAdapter(child: SizedBox()); - } - Map data = snapshot.data as Map; - if (data['status']) { - return SliverLayoutBuilder( - builder: (context, boxConstraints) { - return Obx(() => contentGrid( - _liveController, _liveController.liveList)); - }); - } else { - return HttpError( - errMsg: data['msg'], + sliver: Obx( + () => _liveController.loadingState.value is Loading || + _liveController.loadingState.value is Success + ? contentGrid(_liveController.loadingState.value) + : HttpError( + errMsg: _liveController.loadingState.value is Error + ? (_liveController.loadingState.value as Error) + .errMsg + : '没有相关数据', fn: () { - setState(() { - _futureBuilderFuture = - _liveController.queryLiveList('init'); - }); - }, - ); - } - } else { - return contentGrid(_liveController, []); - } - }, + _liveController.loadingState.value = + LoadingState.loading(); + _liveController.onRefresh(); + }), ), ), ], @@ -141,7 +124,7 @@ class _LivePageState extends State ); } - Widget contentGrid(ctr, liveList) { + Widget contentGrid(LoadingState loadingState) { return SliverGrid( gridDelegate: SliverGridDelegateWithExtentAndRatio( mainAxisSpacing: StyleString.cardSpace, @@ -152,12 +135,12 @@ class _LivePageState extends State ), delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { - return liveList!.isNotEmpty + return loadingState is Success ? LiveCardV( - liveItem: liveList[index], + liveItem: loadingState.response[index], longPress: () { _liveController.popupDialog - .add(_createPopupDialog(liveList[index])); + .add(_createPopupDialog(loadingState.response[index])); Overlay.of(context) .insert(_liveController.popupDialog.last!); }, @@ -165,7 +148,7 @@ class _LivePageState extends State ) : const VideoCardVSkeleton(); }, - childCount: liveList!.isNotEmpty ? liveList!.length : 10, + childCount: loadingState is Success ? loadingState.response.length : 10, ), ); }