mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-20 11:08:03 +08:00
129
lib/pages/pgc/controller.dart
Normal file
129
lib/pages/pgc/controller.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
import 'package:PiliPlus/http/fav.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/http/pgc.dart';
|
||||
import 'package:PiliPlus/models/common/home_tab_type.dart';
|
||||
import 'package:PiliPlus/models_new/fav/fav_pgc/data.dart';
|
||||
import 'package:PiliPlus/models_new/fav/fav_pgc/list.dart';
|
||||
import 'package:PiliPlus/models_new/pgc/pgc_index_result/list.dart';
|
||||
import 'package:PiliPlus/models_new/pgc/pgc_timeline/result.dart';
|
||||
import 'package:PiliPlus/pages/common/common_list_controller.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class PgcController
|
||||
extends CommonListController<List<PgcIndexItem>?, PgcIndexItem> {
|
||||
PgcController({required this.tabType});
|
||||
final HomeTabType tabType;
|
||||
|
||||
int? mid;
|
||||
late final RxBool isLogin;
|
||||
late final showPgcTimeline =
|
||||
tabType == HomeTabType.bangumi && GStorage.showPgcTimeline;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
mid = Accounts.main.mid;
|
||||
isLogin = (mid != 0).obs;
|
||||
|
||||
queryData();
|
||||
queryPgcFollow();
|
||||
if (showPgcTimeline) {
|
||||
queryPgcTimeline();
|
||||
}
|
||||
if (isLogin.value) {
|
||||
followController = ScrollController();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onRefresh() {
|
||||
if (isLogin.value) {
|
||||
followPage = 1;
|
||||
followEnd = false;
|
||||
}
|
||||
queryPgcFollow();
|
||||
if (showPgcTimeline) {
|
||||
queryPgcTimeline();
|
||||
}
|
||||
return super.onRefresh();
|
||||
}
|
||||
|
||||
// follow
|
||||
late int followPage = 1;
|
||||
late RxInt followCount = (-1).obs;
|
||||
late bool followLoading = false;
|
||||
late bool followEnd = false;
|
||||
late Rx<LoadingState<List<FavPgcItemModel>?>> followState =
|
||||
LoadingState<List<FavPgcItemModel>?>.loading().obs;
|
||||
ScrollController? followController;
|
||||
|
||||
// timeline
|
||||
late Rx<LoadingState<List<Result>?>> timelineState =
|
||||
LoadingState<List<Result>?>.loading().obs;
|
||||
|
||||
Future<void> queryPgcTimeline() async {
|
||||
final res = await PgcHttp.pgcTimeline(types: 1, before: 6, after: 6);
|
||||
timelineState.value = res;
|
||||
}
|
||||
|
||||
// 我的订阅
|
||||
Future<void> queryPgcFollow([bool isRefresh = true]) async {
|
||||
if (!isLogin.value || followLoading || (!isRefresh && followEnd)) {
|
||||
return;
|
||||
}
|
||||
followLoading = true;
|
||||
var res = await FavHttp.favPgc(
|
||||
mid: mid,
|
||||
type: tabType == HomeTabType.bangumi ? 1 : 2,
|
||||
pn: followPage,
|
||||
);
|
||||
|
||||
if (res.isSuccess) {
|
||||
FavPgcData data = res.data;
|
||||
List<FavPgcItemModel>? list = data.list;
|
||||
followCount.value = data.total ?? -1;
|
||||
|
||||
if (list.isNullOrEmpty) {
|
||||
followEnd = true;
|
||||
if (isRefresh) {
|
||||
followState.value = Success(list);
|
||||
}
|
||||
followLoading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isRefresh) {
|
||||
if (list!.length >= followCount.value) {
|
||||
followEnd = true;
|
||||
}
|
||||
followState.value = Success(list);
|
||||
followController?.animToTop();
|
||||
} else if (followState.value.isSuccess) {
|
||||
final currentList = followState.value.data!..addAll(list!);
|
||||
if (currentList.length >= followCount.value) {
|
||||
followEnd = true;
|
||||
}
|
||||
followState.refresh();
|
||||
}
|
||||
followPage++;
|
||||
} else if (isRefresh) {
|
||||
followState.value = res as Error;
|
||||
}
|
||||
followLoading = false;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LoadingState<List<PgcIndexItem>?>> customGetData() => PgcHttp.pgcIndex(
|
||||
page: page,
|
||||
indexType: tabType == HomeTabType.cinema ? 102 : null,
|
||||
);
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
followController?.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user