mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: fav detail page: multi select opt: reply item opt: load more Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
78 lines
2.0 KiB
Dart
78 lines
2.0 KiB
Dart
import 'package:PiliPalaX/http/loading_state.dart';
|
|
import 'package:PiliPalaX/utils/extension.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
abstract class CommonController extends GetxController {
|
|
final ScrollController scrollController = ScrollController();
|
|
|
|
int currentPage = 1;
|
|
bool isLoading = false;
|
|
bool isEnd = false;
|
|
Rx<LoadingState> loadingState = LoadingState.loading().obs;
|
|
|
|
Future<LoadingState> customGetData();
|
|
|
|
List? handleListResponse(List currentList, List dataList) {
|
|
return null;
|
|
}
|
|
|
|
bool customHandleResponse(Success response) {
|
|
return false;
|
|
}
|
|
|
|
// void handleSuccess(List currentList, List dataList) {}
|
|
|
|
Future queryData([bool isRefresh = true]) async {
|
|
if (isLoading || (isRefresh.not && isEnd)) return;
|
|
isLoading = true;
|
|
LoadingState response = await customGetData();
|
|
if (response is Success) {
|
|
if (!customHandleResponse(response)) {
|
|
isEnd = response.response.isEmpty;
|
|
List currentList = loadingState.value is Success
|
|
? (loadingState.value as Success).response
|
|
: [];
|
|
List? handleList = handleListResponse(currentList, response.response);
|
|
loadingState.value = isRefresh
|
|
? handleList != null
|
|
? LoadingState.success(handleList)
|
|
: response
|
|
: LoadingState.success(currentList + response.response);
|
|
// handleSuccess(currentList, response.response);
|
|
}
|
|
currentPage++;
|
|
} else {
|
|
if (isRefresh) {
|
|
loadingState.value = response;
|
|
}
|
|
}
|
|
isLoading = false;
|
|
}
|
|
|
|
Future onRefresh() async {
|
|
currentPage = 1;
|
|
isEnd = false;
|
|
await queryData();
|
|
}
|
|
|
|
Future onLoadMore() async {
|
|
await queryData(false);
|
|
}
|
|
|
|
void animateToTop() {
|
|
scrollController.animToTop();
|
|
}
|
|
|
|
void onReload() {
|
|
loadingState.value = LoadingState.loading();
|
|
onRefresh();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
scrollController.dispose();
|
|
super.onClose();
|
|
}
|
|
}
|