refactor: live

This commit is contained in:
bggRGjQaUbCoE
2024-09-08 13:36:56 +08:00
parent a74f0b55dd
commit 56278678cf
3 changed files with 40 additions and 94 deletions

View File

@@ -1,3 +1,5 @@
import 'package:PiliPalaX/http/loading_state.dart';
import '../models/live/item.dart'; import '../models/live/item.dart';
import '../models/live/room_info.dart'; import '../models/live/room_info.dart';
import '../models/live/room_info_h5.dart'; import '../models/live/room_info_h5.dart';
@@ -5,23 +7,21 @@ import 'api.dart';
import 'init.dart'; import 'init.dart';
class LiveHttp { class LiveHttp {
static Future liveList( static Future<LoadingState> liveList(
{int? vmid, int? pn, int? ps, String? orderType}) async { {int? vmid, int? pn, int? ps, String? orderType}) async {
var res = await Request().get(Api.liveList, var res = await Request().get(Api.liveList,
data: {'page': pn, 'page_size': 30, 'platform': 'web'}); data: {'page': pn, 'page_size': 30, 'platform': 'web'});
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { List<LiveItemModel> list = res.data['data']['list']
'status': true, .map<LiveItemModel>((e) => LiveItemModel.fromJson(e))
'data': res.data['data']['list'] .toList();
.map<LiveItemModel>((e) => LiveItemModel.fromJson(e)) if (list.isNotEmpty) {
.toList() return LoadingState.success(list);
}; } else {
return LoadingState.empty();
}
} else { } else {
return { return LoadingState.error(res.data['message']);
'status': false,
'data': [],
'msg': res.data['message'],
};
} }
} }

View File

@@ -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:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:PiliPalaX/http/live.dart'; import 'package:PiliPalaX/http/live.dart';
import 'package:PiliPalaX/models/live/item.dart';
import 'package:PiliPalaX/utils/storage.dart';
class LiveController extends GetxController { class LiveController extends CommonController {
final ScrollController scrollController = ScrollController();
int count = 12; int count = 12;
int _currentPage = 1;
RxInt crossAxisCount = 2.obs;
RxList<LiveItemModel> liveList = <LiveItemModel>[].obs;
bool flag = false;
List<OverlayEntry?> popupDialog = <OverlayEntry?>[]; List<OverlayEntry?> popupDialog = <OverlayEntry?>[];
Box setting = GStorage.setting;
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
queryData();
} }
// 获取推荐 @override
Future queryLiveList(type) async { Future<LoadingState> customGetData() => LiveHttp.liveList(
// if (type == 'init') { pn: currentPage,
// _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();
}
} }

View File

@@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:easy_debounce/easy_throttle.dart'; import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
@@ -25,7 +26,6 @@ class LivePage extends StatefulWidget {
class _LivePageState extends State<LivePage> class _LivePageState extends State<LivePage>
with AutomaticKeepAliveClientMixin { with AutomaticKeepAliveClientMixin {
final LiveController _liveController = Get.put(LiveController()); final LiveController _liveController = Get.put(LiveController());
late Future _futureBuilderFuture;
@override @override
bool get wantKeepAlive => true; bool get wantKeepAlive => true;
@@ -33,7 +33,6 @@ class _LivePageState extends State<LivePage>
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_futureBuilderFuture = _liveController.queryLiveList('init');
StreamController<bool> mainStream = StreamController<bool> mainStream =
Get.find<MainController>().bottomBarStream; Get.find<MainController>().bottomBarStream;
StreamController<bool> searchBarStream = StreamController<bool> searchBarStream =
@@ -44,7 +43,7 @@ class _LivePageState extends State<LivePage>
_liveController.scrollController.position.maxScrollExtent - 200) { _liveController.scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('liveList', const Duration(milliseconds: 200), EasyThrottle.throttle('liveList', const Duration(milliseconds: 200),
() { () {
_liveController.onLoad(); _liveController.onLoadMore();
}); });
} }
@@ -64,7 +63,6 @@ class _LivePageState extends State<LivePage>
@override @override
void dispose() { void dispose() {
_liveController.scrollController.removeListener(() {}); _liveController.scrollController.removeListener(() {});
_liveController.scrollController.dispose();
super.dispose(); super.dispose();
} }
@@ -90,35 +88,20 @@ class _LivePageState extends State<LivePage>
// 单列布局 EdgeInsets.zero // 单列布局 EdgeInsets.zero
padding: padding:
const EdgeInsets.fromLTRB(0, StyleString.cardSpace, 0, 0), const EdgeInsets.fromLTRB(0, StyleString.cardSpace, 0, 0),
sliver: FutureBuilder( sliver: Obx(
future: _futureBuilderFuture, () => _liveController.loadingState.value is Loading ||
builder: (context, snapshot) { _liveController.loadingState.value is Success
if (snapshot.connectionState == ConnectionState.done) { ? contentGrid(_liveController.loadingState.value)
if (snapshot.data == null) { : HttpError(
return const SliverToBoxAdapter(child: SizedBox()); errMsg: _liveController.loadingState.value is Error
} ? (_liveController.loadingState.value as Error)
Map data = snapshot.data as Map; .errMsg
if (data['status']) { : '没有相关数据',
return SliverLayoutBuilder(
builder: (context, boxConstraints) {
return Obx(() => contentGrid(
_liveController, _liveController.liveList));
});
} else {
return HttpError(
errMsg: data['msg'],
fn: () { fn: () {
setState(() { _liveController.loadingState.value =
_futureBuilderFuture = LoadingState.loading();
_liveController.queryLiveList('init'); _liveController.onRefresh();
}); }),
},
);
}
} else {
return contentGrid(_liveController, []);
}
},
), ),
), ),
], ],
@@ -141,7 +124,7 @@ class _LivePageState extends State<LivePage>
); );
} }
Widget contentGrid(ctr, liveList) { Widget contentGrid(LoadingState loadingState) {
return SliverGrid( return SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio( gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace, mainAxisSpacing: StyleString.cardSpace,
@@ -152,12 +135,12 @@ class _LivePageState extends State<LivePage>
), ),
delegate: SliverChildBuilderDelegate( delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) { (BuildContext context, int index) {
return liveList!.isNotEmpty return loadingState is Success
? LiveCardV( ? LiveCardV(
liveItem: liveList[index], liveItem: loadingState.response[index],
longPress: () { longPress: () {
_liveController.popupDialog _liveController.popupDialog
.add(_createPopupDialog(liveList[index])); .add(_createPopupDialog(loadingState.response[index]));
Overlay.of(context) Overlay.of(context)
.insert(_liveController.popupDialog.last!); .insert(_liveController.popupDialog.last!);
}, },
@@ -165,7 +148,7 @@ class _LivePageState extends State<LivePage>
) )
: const VideoCardVSkeleton(); : const VideoCardVSkeleton();
}, },
childCount: liveList!.isNotEmpty ? liveList!.length : 10, childCount: loadingState is Success ? loadingState.response.length : 10,
), ),
); );
} }