import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/http/live.dart'; import 'package:PiliPlus/models/live/follow.dart'; import 'package:PiliPlus/models/live/item.dart'; import 'package:PiliPlus/pages/common/common_list_controller.dart'; import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:get/get_rx/src/rx_types/rx_types.dart'; class LiveController extends CommonListController?, LiveItemModel> { @override void onInit() { super.onInit(); queryData(); if (isLogin.value) { fetchLiveFollowing(); } } @override Future?>> customGetData() => LiveHttp.liveList(pn: currentPage); @override Future onRefresh() { fetchLiveFollowing(); return super.onRefresh(); } late RxBool isLogin = Accounts.main.isLogin.obs; late Rx followListState = LoadingState.loading().obs; late int followPage = 1; late bool followEnd = false; late RxInt liveCount = 0.obs; Future fetchLiveFollowing([bool isRefresh = true]) async { if (!isLogin.value || (isRefresh.not && followEnd)) { return; } if (isRefresh) { followPage = 1; followEnd = false; } dynamic res = await LiveHttp.liveFollowing(pn: followPage, ps: 20); if (res['status']) { LiveFollowingModel data = res['data']; liveCount.value = data.liveCount ?? 0; List? dataList = data.list ?.where((LiveFollowingItemModel item) => item.liveStatus == 1 && item.recordLiveTime == 0) .toList(); if (dataList.isNullOrEmpty) { followEnd = true; if (isRefresh) { followListState.value = LoadingState.success(dataList); } return; } if (isRefresh) { if (dataList!.length >= liveCount.value) { followEnd = true; } followListState.value = LoadingState.success(dataList); } else if (followListState.value is Success) { List list = (followListState.value as Success).response; list.addAll(dataList!); if (list.length >= liveCount.value) { followEnd = true; } followListState.refresh(); } followPage++; } else if (isRefresh) { followListState.value = LoadingState.error(res['msg']); } } }