opt: query data

fix: webdav backup

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-04-10 00:00:37 +08:00
parent 36ff4a0ed3
commit cef7bfd534
5 changed files with 68 additions and 39 deletions

View File

@@ -2,25 +2,52 @@ abstract class LoadingState<T> {
const LoadingState(); const LoadingState();
factory LoadingState.loading() = Loading; factory LoadingState.loading() = Loading;
// factory LoadingState.empty() = Empty;
factory LoadingState.success(T response) = Success<T>; factory LoadingState.success(T response) = Success<T>;
factory LoadingState.error(String errMsg) = Error; factory LoadingState.error(String errMsg) = Error;
} }
class Loading extends LoadingState<Never> { class Loading extends LoadingState<Never> {
const Loading(); Loading._internal();
}
// class Empty extends LoadingState<Never> { static final Loading _instance = Loading._internal();
// const Empty();
// } factory Loading() => _instance;
}
class Success<T> extends LoadingState<T> { class Success<T> extends LoadingState<T> {
final T response; final T response;
const Success(this.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<Never> { class Error extends LoadingState<Never> {
final String errMsg; final String errMsg;
const Error(this.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;
} }

View File

@@ -39,9 +39,7 @@ abstract class CommonController extends GetxController
Future<LoadingState> customGetData(); Future<LoadingState> customGetData();
List? handleListResponse(List currentList, List dataList) { void handleListResponse(List dataList) {}
return null;
}
bool customHandleResponse(Success response) { bool customHandleResponse(Success response) {
return false; return false;
@@ -51,28 +49,28 @@ abstract class CommonController extends GetxController
return false; return false;
} }
// void handleSuccess(List currentList, List dataList) {}
Future queryData([bool isRefresh = true]) async { Future queryData([bool isRefresh = true]) async {
if (isLoading || (isRefresh.not && isEnd)) return; if (isLoading || (isRefresh.not && isEnd)) return;
isLoading = true; isLoading = true;
LoadingState response = await customGetData(); LoadingState response = await customGetData();
if (response is Success) { if (response is Success) {
if (!customHandleResponse(response)) { if (!customHandleResponse(response)) {
List dataList = (response.response as List?) ?? []; List? dataList = response.response;
if (dataList.isEmpty) { if (dataList.isNullOrEmpty) {
isEnd = true; 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++; currentPage++;
} else { } else {

View File

@@ -15,15 +15,14 @@ class SysMsgController extends CommonController {
} }
@override @override
List? handleListResponse(List currentList, List dataList) { void handleListResponse(List dataList) {
if (cursor == -1) { 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) { if (isEnd.not && dataList.length + 1 < pageSize) {
isEnd = true; isEnd = true;
} }
return null;
} }
Future msgSysUpdateCursor(int? cursor) async { Future msgSysUpdateCursor(int? cursor) async {

View File

@@ -28,14 +28,21 @@ class RcmdController extends CommonController {
} }
@override @override
List? handleListResponse(List currentList, List dataList) { void handleListResponse(List dataList) {
bool shouldSaveLast = if (enableSaveLastData &&
enableSaveLastData && currentPage == 0 && currentList.isNotEmpty; currentPage == 0 &&
if (shouldSaveLast && currentList.length > 500) { 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); currentList.removeRange(50, currentList.length);
} }
lastRefreshAt = shouldSaveLast && savedRcmdTip ? dataList.length : null; dataList.addAll(currentList);
return shouldSaveLast ? (dataList..addAll(currentList)) : null; }
}
} }
@override @override

View File

@@ -1,5 +1,4 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:PiliPlus/common/widgets/pair.dart'; import 'package:PiliPlus/common/widgets/pair.dart';
import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/storage.dart';
@@ -60,10 +59,9 @@ class WebDav {
try { try {
String data = await GStorage.exportAllSettings(); String data = await GStorage.exportAllSettings();
final path = '$_webdavDirectory/piliplus_settings.json'; final path = '$_webdavDirectory/piliplus_settings.json';
final file = File(path); try {
if (await file.exists()) { await _client!.remove(path);
await file.delete(); } catch (_) {}
}
await _client!.write(path, utf8.encode(data)); await _client!.write(path, utf8.encode(data));
SmartDialog.showToast('备份成功'); SmartDialog.showToast('备份成功');
} catch (e) { } catch (e) {