diff --git a/lib/http/loading_state.dart b/lib/http/loading_state.dart index 048d3003..229c5610 100644 --- a/lib/http/loading_state.dart +++ b/lib/http/loading_state.dart @@ -2,25 +2,52 @@ abstract class LoadingState { const LoadingState(); factory LoadingState.loading() = Loading; - // factory LoadingState.empty() = Empty; factory LoadingState.success(T response) = Success; factory LoadingState.error(String errMsg) = Error; } class Loading extends LoadingState { - const Loading(); -} + Loading._internal(); -// class Empty extends LoadingState { -// const Empty(); -// } + static final Loading _instance = Loading._internal(); + + factory Loading() => _instance; +} class Success extends LoadingState { final T response; const Success(this.response); + + @override + bool operator ==(Object other) { + if (identical(this, other)) { + return true; + } + if (other is Success) { + return response == other.response; + } + return false; + } + + @override + int get hashCode => response.hashCode; } class Error extends LoadingState { final String errMsg; const Error(this.errMsg); + + @override + bool operator ==(Object other) { + if (identical(this, other)) { + return true; + } + if (other is Error) { + return errMsg == other.errMsg; + } + return false; + } + + @override + int get hashCode => errMsg.hashCode; } diff --git a/lib/pages/common/common_controller.dart b/lib/pages/common/common_controller.dart index d2664ec3..d2a897cf 100644 --- a/lib/pages/common/common_controller.dart +++ b/lib/pages/common/common_controller.dart @@ -39,9 +39,7 @@ abstract class CommonController extends GetxController Future customGetData(); - List? handleListResponse(List currentList, List dataList) { - return null; - } + void handleListResponse(List dataList) {} bool customHandleResponse(Success response) { return false; @@ -51,28 +49,28 @@ abstract class CommonController extends GetxController return false; } - // void handleSuccess(List currentList, List dataList) {} - Future queryData([bool isRefresh = true]) async { if (isLoading || (isRefresh.not && isEnd)) return; isLoading = true; LoadingState response = await customGetData(); if (response is Success) { if (!customHandleResponse(response)) { - List dataList = (response.response as List?) ?? []; - if (dataList.isEmpty) { + List? dataList = response.response; + if (dataList.isNullOrEmpty) { isEnd = true; + if (isRefresh) { + loadingState.value = response; + } + return; + } + handleListResponse(dataList!); + if (isRefresh) { + loadingState.value = LoadingState.success(dataList); + } else if (loadingState.value is Success) { + List currentList = (loadingState.value as Success).response ?? []; + currentList.addAll(dataList); + loadingState.value = LoadingState.success(currentList); } - List currentList = loadingState.value is Success - ? (loadingState.value as Success).response - : []; - List? handleList = handleListResponse(currentList, dataList); - loadingState.value = isRefresh - ? handleList != null - ? LoadingState.success(handleList) - : response - : LoadingState.success(currentList + dataList); - // handleSuccess(currentList, response.response); } currentPage++; } else { diff --git a/lib/pages/msg_feed_top/sys_msg/controller.dart b/lib/pages/msg_feed_top/sys_msg/controller.dart index 8cfbff11..a2fa1326 100644 --- a/lib/pages/msg_feed_top/sys_msg/controller.dart +++ b/lib/pages/msg_feed_top/sys_msg/controller.dart @@ -15,15 +15,14 @@ class SysMsgController extends CommonController { } @override - List? handleListResponse(List currentList, List dataList) { + void handleListResponse(List dataList) { if (cursor == -1) { - msgSysUpdateCursor(dataList.firstOrNull?.cursor); + msgSysUpdateCursor(dataList.first?.cursor); } - cursor = dataList.lastOrNull?.cursor ?? -1; + cursor = dataList.last?.cursor ?? -1; if (isEnd.not && dataList.length + 1 < pageSize) { isEnd = true; } - return null; } Future msgSysUpdateCursor(int? cursor) async { diff --git a/lib/pages/rcmd/controller.dart b/lib/pages/rcmd/controller.dart index c15a888a..a578a078 100644 --- a/lib/pages/rcmd/controller.dart +++ b/lib/pages/rcmd/controller.dart @@ -28,14 +28,21 @@ class RcmdController extends CommonController { } @override - List? handleListResponse(List currentList, List dataList) { - bool shouldSaveLast = - enableSaveLastData && currentPage == 0 && currentList.isNotEmpty; - if (shouldSaveLast && currentList.length > 500) { - currentList.removeRange(50, currentList.length); + void handleListResponse(List dataList) { + if (enableSaveLastData && + currentPage == 0 && + loadingState.value is Success) { + List? currentList = (loadingState.value as Success).response; + if (currentList?.isNotEmpty == true) { + if (savedRcmdTip) { + lastRefreshAt = dataList.length; + } + if (currentList!.length > 500) { + currentList.removeRange(50, currentList.length); + } + dataList.addAll(currentList); + } } - lastRefreshAt = shouldSaveLast && savedRcmdTip ? dataList.length : null; - return shouldSaveLast ? (dataList..addAll(currentList)) : null; } @override diff --git a/lib/pages/webdav/webdav.dart b/lib/pages/webdav/webdav.dart index 7ab6525e..f1a8e6d7 100644 --- a/lib/pages/webdav/webdav.dart +++ b/lib/pages/webdav/webdav.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:io'; import 'package:PiliPlus/common/widgets/pair.dart'; import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/storage.dart'; @@ -60,10 +59,9 @@ class WebDav { try { String data = await GStorage.exportAllSettings(); final path = '$_webdavDirectory/piliplus_settings.json'; - final file = File(path); - if (await file.exists()) { - await file.delete(); - } + try { + await _client!.remove(path); + } catch (_) {} await _client!.write(path, utf8.encode(data)); SmartDialog.showToast('备份成功'); } catch (e) {