mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
64 lines
1.6 KiB
Dart
64 lines
1.6 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;
|
|
Rx<LoadingState> loadingState = LoadingState.loading().obs;
|
|
|
|
Future<LoadingState> customGetData();
|
|
|
|
List? handleResponse(List currentList, List dataList) {
|
|
return null;
|
|
}
|
|
|
|
void handleSuccess(List currentList, List dataList) {}
|
|
|
|
Future queryData([bool isRefresh = true]) async {
|
|
if (isLoading) return;
|
|
isLoading = true;
|
|
LoadingState response = await customGetData();
|
|
if (response is Success) {
|
|
currentPage++;
|
|
List currentList = loadingState.value is Success
|
|
? (loadingState.value as Success).response
|
|
: [];
|
|
List? handleList = handleResponse(currentList, response.response);
|
|
loadingState.value = isRefresh
|
|
? handleList != null
|
|
? LoadingState.success(handleList)
|
|
: response
|
|
: LoadingState.success(currentList + response.response);
|
|
handleSuccess(currentList, response.response);
|
|
} else {
|
|
if (isRefresh) {
|
|
loadingState.value = response;
|
|
}
|
|
}
|
|
isLoading = false;
|
|
}
|
|
|
|
Future onRefresh() async {
|
|
currentPage = 1;
|
|
await queryData();
|
|
}
|
|
|
|
Future onLoadMore() async {
|
|
await queryData(false);
|
|
}
|
|
|
|
void animateToTop() {
|
|
scrollController.animToTop();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
scrollController.dispose();
|
|
super.onClose();
|
|
}
|
|
}
|