mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
refactor: rcmd hot
This commit is contained in:
63
lib/pages/common/common_controller.dart
Normal file
63
lib/pages/common/common_controller.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user