refactor dyn uplist

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-06-27 09:45:18 +08:00
parent 840f8d5a25
commit 5d15bf5e59
9 changed files with 228 additions and 275 deletions

View File

@@ -1,12 +1,10 @@
import 'dart:async';
import 'package:PiliPlus/http/dynamics.dart';
import 'package:PiliPlus/http/follow.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/common/dynamic/dynamics_type.dart';
import 'package:PiliPlus/models/dynamics/up.dart';
import 'package:PiliPlus/models_new/follow/data.dart';
import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/pages/common/common_data_controller.dart';
import 'package:PiliPlus/pages/dynamics_tab/controller.dart';
import 'package:PiliPlus/services/account_service.dart';
import 'package:PiliPlus/utils/accounts.dart';
@@ -17,24 +15,20 @@ import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class DynamicsController extends GetxController
with GetSingleTickerProviderStateMixin, ScrollOrRefreshMixin, AccountMixin {
@override
final ScrollController scrollController = ScrollController();
class DynamicsController
extends CommonDataController<FollowUpModel, FollowUpModel>
with GetSingleTickerProviderStateMixin, AccountMixin {
late final TabController tabController;
late final RxInt mid = (-1).obs;
late int currentMid = -1;
final Set<int> tempBannedList = <int>{};
Set<int> tempBannedList = <int>{};
final Rx<LoadingState<FollowUpModel>> upState =
LoadingState<FollowUpModel>.loading().obs;
late int _upPage = 1;
late bool _upEnd = false;
String? _offset;
late int _page = 1;
late bool _isEnd = false;
Set<UpItem>? _cacheUpList;
late final _showAllUp = Pref.dynamicsShowAllFollowedUp;
late int hostMid = -1, currentMid = -1;
late bool showLiveUp = Pref.expandDynLivePanel;
late final _showAllUp = Pref.dynamicsShowAllFollowedUp;
final upPanelPosition = Pref.upPanelPosition;
@@ -55,154 +49,56 @@ class DynamicsController extends GetxController
void onInit() {
super.onInit();
tabController = TabController(
length: DynamicsTabType.values.length,
vsync: this,
length: DynamicsTabType.values.length,
initialIndex: Pref.defaultDynamicTypeIndex,
);
queryFollowUp();
queryData();
}
void onLoadMoreUp() {
if (_showAllUp) {
queryAllUp();
} else {
queryUpList();
}
}
Future<void> queryUpList() async {
if (isQuerying || _upEnd) return;
isQuerying = true;
final res = await DynamicsHttp.dynUpList(upState.value.data.offset);
if (res case Success(:final response)) {
if (response.hasMore == false || response.offset.isNullOrEmpty) {
_upEnd = true;
}
final upData = upState.value.data
..hasMore = response.hasMore
..offset = response.offset;
final list = response.upList;
if (list != null && list.isNotEmpty) {
upData.upList.addAll(list);
upState.refresh();
}
}
isQuerying = false;
}
Future<void> queryAllUp() async {
if (isQuerying || _upEnd) return;
isQuerying = true;
final res = await FollowHttp.followings(
vmid: Accounts.main.mid,
pn: _upPage,
orderType: 'attention',
ps: 50,
);
if (res case Success(:final response)) {
_upPage++;
final list = response.list;
if (list.isEmpty) {
_upEnd = true;
}
upState
..value.data.upList.addAll(
list..removeWhere((e) => _cacheUpList?.contains(e) == true),
)
..refresh();
}
isQuerying = false;
}
late bool isQuerying = false;
Future<void> queryFollowUp() async {
if (isQuerying) return;
isQuerying = true;
if (!accountService.isLogin.value) {
upState.value = const Error(null);
isQuerying = false;
return;
}
// reset
_upEnd = false;
if (_showAllUp) _upPage = 1;
final res = await Future.wait([
DynamicsHttp.followUp(),
if (_showAllUp)
FollowHttp.followings(
vmid: Accounts.main.mid,
pn: _upPage,
orderType: 'attention',
ps: 50,
),
]);
final first = res.first;
if (first case final Success<FollowUpModel> i) {
final data = i.response;
final second = res.elementAtOrNull(1);
if (second case final Success<FollowData> j) {
final data1 = j.response;
final list1 = data1.list;
_upPage++;
if (list1.isEmpty || list1.length >= (data1.total ?? 0)) {
_upEnd = true;
}
final list = data.upList;
list.addAll(list1..removeWhere((_cacheUpList = list.toSet()).contains));
}
if (!_showAllUp) {
if (data.hasMore == false || data.offset.isNullOrEmpty) {
_upEnd = true;
}
}
upState.value = Success(data);
} else {
upState.value = const Error(null);
}
isQuerying = false;
void _jumpToTab(int mid) {
tabController.index = mid == -1 ? 0 : 4;
}
void onSelectUp(int mid) {
if (this.mid.value == mid) {
tabController.index = (mid == -1 ? 0 : 4);
if (currentMid == mid) {
_jumpToTab(mid);
if (mid == -1) {
queryFollowUp();
singleRefresh();
}
controller?.onReload();
return;
}
this.mid.value = mid;
tabController.index = (mid == -1 ? 0 : 4);
if (mid != -1) {
hostMid = mid;
try {
Get.find<DynamicsTabController>(
tag: DynamicsTabType.up.name,
).onReload();
} catch (_) {}
}
currentMid = mid;
_jumpToTab(mid);
}
Future<void> singleRefresh() {
if (_showAllUp) {
_page = 1;
_cacheUpList = null;
}
_offset = null;
_isEnd = false;
return super.onRefresh();
}
@override
Future<void> onRefresh() {
_refreshFollowUp();
singleRefresh();
return controller!.onRefresh();
}
void _refreshFollowUp() {
if (_showAllUp) {
_upPage = 1;
_cacheUpList = null;
}
queryFollowUp();
}
@override
void animateToTop() {
controller?.animateToTop();
@@ -234,10 +130,71 @@ class DynamicsController extends GetxController
@override
void onClose() {
tabController.dispose();
scrollController.dispose();
super.onClose();
}
@override
void onChangeAccount(bool isLogin) => _refreshFollowUp();
void onChangeAccount(bool isLogin) => onReload();
@override
Future<LoadingState<FollowUpModel>> customGetData() {
if (_offset == null) {
return DynamicsHttp.followUp();
}
if (_showAllUp) {
return DynamicsHttp.followings(
vmid: Accounts.main.mid,
pn: _page,
orderType: 'attention',
ps: 50,
);
} else {
return DynamicsHttp.dynUpList(_offset);
}
}
@override
Future<void> queryData([bool isRefresh = true]) {
if (!isRefresh && _isEnd) return Future.value();
return super.queryData(isRefresh);
}
@override
bool customHandleResponse(bool isRefresh, Success<FollowUpModel> response) {
final res = response.response;
if (_showAllUp) {
if (res.upList?.isNotEmpty != true) {
_isEnd = true;
}
} else {
_offset = res.offset;
if (res.hasMore != true || _offset.isNullOrEmpty) {
_isEnd = true;
}
}
if (isRefresh) {
if (_showAllUp) {
_offset = '';
_cacheUpList = res.upList?.toSet();
}
loadingState.value = response;
} else {
if (_showAllUp) {
_page++;
}
if (res.upList case final upList? when upList.isNotEmpty) {
if (_showAllUp && _cacheUpList != null) {
upList.removeWhere(_cacheUpList!.contains);
}
loadingState
..value.data.addAllUpList(upList)
..refresh();
}
}
return true;
}
}