mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
refactor: fan
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:PiliPalaX/http/fan.dart';
|
||||
import 'package:PiliPalaX/http/loading_state.dart';
|
||||
import 'package:PiliPalaX/pages/common/common_controller.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:PiliPalaX/http/fan.dart';
|
||||
import 'package:PiliPalaX/models/fans/result.dart';
|
||||
import 'package:PiliPalaX/utils/storage.dart';
|
||||
|
||||
class FansController extends GetxController {
|
||||
class FansController extends CommonController {
|
||||
Box userInfoCache = GStorage.userInfo;
|
||||
int pn = 1;
|
||||
int ps = 20;
|
||||
int total = 0;
|
||||
RxList<FansItemModel> fansList = <FansItemModel>[].obs;
|
||||
late int? mid;
|
||||
late String? name;
|
||||
dynamic userInfo;
|
||||
RxString loadingText = '加载中...'.obs;
|
||||
RxBool isOwner = false.obs;
|
||||
bool isEnd = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -26,40 +24,44 @@ class FansController extends GetxController {
|
||||
: userInfo?.mid;
|
||||
isOwner.value = mid == userInfo?.mid;
|
||||
name = Get.parameters['name'] ?? userInfo?.uname;
|
||||
|
||||
queryData();
|
||||
}
|
||||
|
||||
Future queryFans(type) async {
|
||||
if (type == 'init' || type == 'refresh') {
|
||||
pn = 1;
|
||||
loadingText.value == '加载中...';
|
||||
}
|
||||
if (loadingText.value == '没有更多了') {
|
||||
return;
|
||||
}
|
||||
var res = await FanHttp.fans(
|
||||
vmid: mid,
|
||||
pn: pn,
|
||||
ps: ps,
|
||||
orderType: 'attention',
|
||||
);
|
||||
if (res['status']) {
|
||||
if (type == 'init') {
|
||||
fansList.value = res['data'].list;
|
||||
total = res['data'].total;
|
||||
} else if (type == 'onLoad') {
|
||||
fansList.addAll(res['data'].list);
|
||||
}
|
||||
print('fansList: ${fansList.length}, total: $total');
|
||||
if ((pn == 1 && total < ps) || res['data'].list.isEmpty) {
|
||||
loadingText.value = '没有更多了';
|
||||
}
|
||||
pn += 1;
|
||||
if (total > ps && pn == 2) {
|
||||
queryFans('onLoad');
|
||||
}
|
||||
} else {
|
||||
SmartDialog.showToast(res['msg']);
|
||||
}
|
||||
return res;
|
||||
@override
|
||||
Future onRefresh() {
|
||||
isEnd = false;
|
||||
return super.onRefresh();
|
||||
}
|
||||
|
||||
@override
|
||||
Future queryData([bool isRefresh = true]) {
|
||||
if (isEnd) {
|
||||
return Future.value();
|
||||
}
|
||||
return super.queryData(isRefresh);
|
||||
}
|
||||
|
||||
@override
|
||||
bool customHandleResponse(Success response) {
|
||||
if ((currentPage == 1 && response.response.total < ps) ||
|
||||
response.response.list.isEmpty) {
|
||||
isEnd = true;
|
||||
}
|
||||
List currentList = loadingState.value is Success
|
||||
? (loadingState.value as Success).response
|
||||
: [];
|
||||
loadingState.value = currentPage == 1
|
||||
? LoadingState.success(response.response.list)
|
||||
: LoadingState.success(currentList + response.response.list);
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LoadingState> customGetData() => FanHttp.fans(
|
||||
vmid: mid,
|
||||
pn: currentPage,
|
||||
ps: ps,
|
||||
orderType: 'attention',
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user