From b187d0fd594f832a36296760a67d6124c4c04663 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Wed, 15 Mar 2023 15:13:23 +0800 Subject: [PATCH] feat: fit for file confirm dialog queue --- flutter/lib/models/file_model.dart | 70 ++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/flutter/lib/models/file_model.dart b/flutter/lib/models/file_model.dart index 953625c96..5a73d777e 100644 --- a/flutter/lib/models/file_model.dart +++ b/flutter/lib/models/file_model.dart @@ -45,7 +45,7 @@ class FileModel { late final GetSessionID getSessionID; String get sessionID => getSessionID(); - late final _FileDialogEventLoop evtLoop; + late final FileDialogEventLoop evtLoop; FileModel(this.parent) { getSessionID = () => parent.target?.id ?? ""; @@ -65,7 +65,7 @@ class FileModel { jobController: jobController, fileFetcher: fileFetcher, getOtherSideDirectoryData: () => localController.directoryData()); - evtLoop = _FileDialogEventLoop(); + evtLoop = FileDialogEventLoop(); } Future onReady() async { @@ -94,11 +94,20 @@ class FileModel { fileFetcher.tryCompleteTask(evt['value'], evt['is_local']); } + Future postOverrideFileConfirm(Map evt) async { + evtLoop.push_event( + _FileDialogEvent(WeakReference(this), FileDialogType.overwrite, evt)); + } + Future overrideFileConfirm(Map evt, - {bool? overrideConfirm}) async { - final resp = overrideConfirm ?? - await showFileConfirmDialog( - translate("Overwrite"), "${evt['read_path']}", true); + {bool? overrideConfirm, bool skip = false}) async { + // If `skip == true`, it means to skip this file without showing dialog. + // Because `resp` may be null after the user operation or the last remembered operation, + // and we should distinguish them. + final resp = overrideConfirm ?? (!skip + ? await showFileConfirmDialog( + translate("Overwrite"), "${evt['read_path']}", true) + : null); final id = int.tryParse(evt['id']) ?? 0; if (false == resp) { final jobIndex = jobController.getJob(id); @@ -117,6 +126,10 @@ class FileModel { // overwrite need_override = true; } + // Update the loop config. + if (fileConfirmCheckboxRemember) { + evtLoop.setSkip(!need_override); + } await bind.sessionSetConfirmOverrideFile( id: sessionID, actId: id, @@ -125,6 +138,10 @@ class FileModel { remember: fileConfirmCheckboxRemember, isUpload: evt['is_upload'] == "true"); } + // Update the loop config. + if (fileConfirmCheckboxRemember) { + evtLoop.setOverrideConfirm(resp); + } } bool fileConfirmCheckboxRemember = false; @@ -1178,12 +1195,12 @@ List _sortList(List list, SortBy sortType, bool ascending) { /// /// [Visibility] /// The `_FileDialogType` and `_DialogEvent` are invisible for other models. -enum _FileDialogType { overwrite, unknown } +enum FileDialogType { overwrite, unknown } -class _FileDialogEvent - extends BaseEvent<_FileDialogType, Map> { +class _FileDialogEvent extends BaseEvent> { WeakReference fileModel; bool? _overrideConfirm; + bool _skip = false; _FileDialogEvent(this.fileModel, super.type, super.data); @@ -1191,40 +1208,55 @@ class _FileDialogEvent _overrideConfirm = confirm; } + void setSkip(bool skip) { + _skip = skip; + } + @override - EventCallback>? findCallback(_FileDialogType type) { + EventCallback>? findCallback(FileDialogType type) { final model = fileModel.target; if (model == null) { return null; } switch (type) { - case _FileDialogType.overwrite: + case FileDialogType.overwrite: return (data) async { - return await model.overrideFileConfirm(data, overrideConfirm: _overrideConfirm); + return await model.overrideFileConfirm(data, + overrideConfirm: _overrideConfirm, skip: _skip); }; default: - return null; + debugPrint("Unknown event type: $type with $data"); + return null; } } } -class _FileDialogEventLoop extends BaseEventLoop<_FileDialogType, Map> { - +class FileDialogEventLoop + extends BaseEventLoop> { bool? overrideConfirm; + bool _skip = false; @override - Future onPreConsume(BaseEvent<_FileDialogType, Map> evt) async { + Future onPreConsume( + BaseEvent> evt) async { var event = evt as _FileDialogEvent; event.setOverrideConfirm(overrideConfirm); + event.setSkip(_skip); + debugPrint("FileDialogEventLoop: consuming"); } - + @override Future onEventsClear() { overrideConfirm = null; + _skip = false; return super.onEventsClear(); } - void setOverrideConfirm(bool confirm) { + void setOverrideConfirm(bool? confirm) { overrideConfirm = confirm; } -} \ No newline at end of file + + void setSkip(bool skip) { + _skip = skip; + } +}