refa: query data (#659)

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
dom
2025-04-10 21:43:01 +08:00
committed by GitHub
parent 99b19e7b03
commit e1b73f4766
128 changed files with 1493 additions and 1987 deletions

View File

@@ -1,12 +1,14 @@
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/http/live.dart';
import 'package:PiliPlus/models/live/follow.dart';
import 'package:PiliPlus/pages/common/common_controller.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 CommonController {
class LiveController
extends CommonListController<List<LiveItemModel>?, LiveItemModel> {
@override
void onInit() {
super.onInit();
@@ -17,7 +19,8 @@ class LiveController extends CommonController {
}
@override
Future<LoadingState> customGetData() => LiveHttp.liveList(pn: currentPage);
Future<LoadingState<List<LiveItemModel>?>> customGetData() =>
LiveHttp.liveList(pn: currentPage);
@override
Future onRefresh() {
@@ -41,20 +44,34 @@ class LiveController extends CommonController {
}
dynamic res = await LiveHttp.liveFollowing(pn: followPage, ps: 20);
if (res['status']) {
followPage++;
liveCount.value = res['data'].liveCount;
List list = res['data']
.list
.where((LiveFollowingItemModel item) =>
LiveFollowingModel data = res['data'];
liveCount.value = data.liveCount ?? 0;
List<LiveFollowingItemModel>? dataList = data.list
?.where((LiveFollowingItemModel item) =>
item.liveStatus == 1 && item.recordLiveTime == 0)
.toList();
if (isRefresh.not && followListState.value is Success) {
list.insertAll(0, (followListState.value as Success).response);
if (dataList.isNullOrEmpty) {
followEnd = true;
if (isRefresh) {
followListState.value = LoadingState.success(dataList);
}
return;
}
followEnd = list.length >= liveCount.value ||
list.isEmpty ||
(res['data'].list as List?).isNullOrEmpty;
followListState.value = LoadingState.success(list);
if (isRefresh) {
if (dataList!.length >= liveCount.value) {
followEnd = true;
}
followListState.value = LoadingState.success(dataList);
} else if (loadingState.value is Success) {
List<LiveFollowingItemModel> list =
(loadingState.value as Success).response;
list.addAll(dataList!);
if (list.length >= liveCount.value) {
followEnd = true;
}
loadingState.refresh();
}
followPage++;
} else {
followListState.value = LoadingState.error(res['msg']);
}

View File

@@ -3,6 +3,7 @@ import 'package:PiliPlus/common/widgets/network_img_layer.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:PiliPlus/common/widgets/self_sized_horizontal_list.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/live/item.dart';
import 'package:PiliPlus/pages/common/common_page.dart';
import 'package:PiliPlus/pages/live/controller.dart';
import 'package:PiliPlus/pages/live/widgets/live_item.dart';
@@ -66,17 +67,7 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
top: StyleString.cardSpace,
bottom: MediaQuery.paddingOf(context).bottom + 80,
),
sliver: Obx(
() => controller.loadingState.value is Loading ||
controller.loadingState.value is Success
? contentGrid(controller.loadingState.value)
: HttpError(
errMsg: controller.loadingState.value is Error
? (controller.loadingState.value as Error).errMsg
: '没有相关数据',
callback: controller.onReload,
),
),
sliver: Obx(() => _buildBody(controller.loadingState.value)),
),
],
),
@@ -84,33 +75,49 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
);
}
Widget contentGrid(LoadingState loadingState) {
return SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
// 行间距
mainAxisSpacing: StyleString.cardSpace,
// 列间距
crossAxisSpacing: StyleString.cardSpace,
// 最大宽度
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent: MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (loadingState is Success &&
index == loadingState.response.length - 1) {
controller.onLoadMore();
}
return loadingState is Success
? LiveCardV(
liveItem: loadingState.response[index],
)
: const VideoCardVSkeleton();
},
childCount: loadingState is Success ? loadingState.response.length : 10,
),
);
Widget _buildBody(LoadingState<List<LiveItemModel>?> loadingState) {
return switch (loadingState) {
Loading() => SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.cardSpace,
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent: MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(context, index) {
return const VideoCardVSkeleton();
},
childCount: 10,
),
),
Success() => loadingState.response?.isNotEmpty == true
? SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.cardSpace,
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent: MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(context, index) {
if (index == loadingState.response!.length - 1) {
controller.onLoadMore();
}
return LiveCardV(liveItem: loadingState.response![index]);
},
childCount: loadingState.response!.length,
),
)
: scrollErrorWidget(callback: controller.onReload),
Error() => HttpError(
errMsg: loadingState.errMsg,
callback: controller.onReload,
),
_ => throw UnimplementedError(),
};
}
Widget _buildFollowList() {