feat: fit for file confirm dialog queue

This commit is contained in:
Kingtous 2023-03-15 15:13:23 +08:00
parent 6725c9544b
commit b187d0fd59

View File

@ -45,7 +45,7 @@ class FileModel {
late final GetSessionID getSessionID; late final GetSessionID getSessionID;
String get sessionID => getSessionID(); String get sessionID => getSessionID();
late final _FileDialogEventLoop evtLoop; late final FileDialogEventLoop evtLoop;
FileModel(this.parent) { FileModel(this.parent) {
getSessionID = () => parent.target?.id ?? ""; getSessionID = () => parent.target?.id ?? "";
@ -65,7 +65,7 @@ class FileModel {
jobController: jobController, jobController: jobController,
fileFetcher: fileFetcher, fileFetcher: fileFetcher,
getOtherSideDirectoryData: () => localController.directoryData()); getOtherSideDirectoryData: () => localController.directoryData());
evtLoop = _FileDialogEventLoop(); evtLoop = FileDialogEventLoop();
} }
Future<void> onReady() async { Future<void> onReady() async {
@ -94,11 +94,20 @@ class FileModel {
fileFetcher.tryCompleteTask(evt['value'], evt['is_local']); fileFetcher.tryCompleteTask(evt['value'], evt['is_local']);
} }
Future<void> postOverrideFileConfirm(Map<String, dynamic> evt) async {
evtLoop.push_event(
_FileDialogEvent(WeakReference(this), FileDialogType.overwrite, evt));
}
Future<void> overrideFileConfirm(Map<String, dynamic> evt, Future<void> overrideFileConfirm(Map<String, dynamic> evt,
{bool? overrideConfirm}) async { {bool? overrideConfirm, bool skip = false}) async {
final resp = overrideConfirm ?? // If `skip == true`, it means to skip this file without showing dialog.
await showFileConfirmDialog( // Because `resp` may be null after the user operation or the last remembered operation,
translate("Overwrite"), "${evt['read_path']}", true); // 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; final id = int.tryParse(evt['id']) ?? 0;
if (false == resp) { if (false == resp) {
final jobIndex = jobController.getJob(id); final jobIndex = jobController.getJob(id);
@ -117,6 +126,10 @@ class FileModel {
// overwrite // overwrite
need_override = true; need_override = true;
} }
// Update the loop config.
if (fileConfirmCheckboxRemember) {
evtLoop.setSkip(!need_override);
}
await bind.sessionSetConfirmOverrideFile( await bind.sessionSetConfirmOverrideFile(
id: sessionID, id: sessionID,
actId: id, actId: id,
@ -125,6 +138,10 @@ class FileModel {
remember: fileConfirmCheckboxRemember, remember: fileConfirmCheckboxRemember,
isUpload: evt['is_upload'] == "true"); isUpload: evt['is_upload'] == "true");
} }
// Update the loop config.
if (fileConfirmCheckboxRemember) {
evtLoop.setOverrideConfirm(resp);
}
} }
bool fileConfirmCheckboxRemember = false; bool fileConfirmCheckboxRemember = false;
@ -1178,12 +1195,12 @@ List<Entry> _sortList(List<Entry> list, SortBy sortType, bool ascending) {
/// ///
/// [Visibility] /// [Visibility]
/// The `_FileDialogType` and `_DialogEvent` are invisible for other models. /// The `_FileDialogType` and `_DialogEvent` are invisible for other models.
enum _FileDialogType { overwrite, unknown } enum FileDialogType { overwrite, unknown }
class _FileDialogEvent class _FileDialogEvent extends BaseEvent<FileDialogType, Map<String, dynamic>> {
extends BaseEvent<_FileDialogType, Map<String, dynamic>> {
WeakReference<FileModel> fileModel; WeakReference<FileModel> fileModel;
bool? _overrideConfirm; bool? _overrideConfirm;
bool _skip = false;
_FileDialogEvent(this.fileModel, super.type, super.data); _FileDialogEvent(this.fileModel, super.type, super.data);
@ -1191,40 +1208,55 @@ class _FileDialogEvent
_overrideConfirm = confirm; _overrideConfirm = confirm;
} }
void setSkip(bool skip) {
_skip = skip;
}
@override @override
EventCallback<Map<String, dynamic>>? findCallback(_FileDialogType type) { EventCallback<Map<String, dynamic>>? findCallback(FileDialogType type) {
final model = fileModel.target; final model = fileModel.target;
if (model == null) { if (model == null) {
return null; return null;
} }
switch (type) { switch (type) {
case _FileDialogType.overwrite: case FileDialogType.overwrite:
return (data) async { return (data) async {
return await model.overrideFileConfirm(data, overrideConfirm: _overrideConfirm); return await model.overrideFileConfirm(data,
overrideConfirm: _overrideConfirm, skip: _skip);
}; };
default: default:
return null; debugPrint("Unknown event type: $type with $data");
return null;
} }
} }
} }
class _FileDialogEventLoop extends BaseEventLoop<_FileDialogType, Map<String, dynamic>> { class FileDialogEventLoop
extends BaseEventLoop<FileDialogType, Map<String, dynamic>> {
bool? overrideConfirm; bool? overrideConfirm;
bool _skip = false;
@override @override
Future<void> onPreConsume(BaseEvent<_FileDialogType, Map<String, dynamic>> evt) async { Future<void> onPreConsume(
BaseEvent<FileDialogType, Map<String, dynamic>> evt) async {
var event = evt as _FileDialogEvent; var event = evt as _FileDialogEvent;
event.setOverrideConfirm(overrideConfirm); event.setOverrideConfirm(overrideConfirm);
event.setSkip(_skip);
debugPrint("FileDialogEventLoop: consuming<jobId:${evt.data['id']} overrideConfirm: $overrideConfirm, skip:$_skip>");
} }
@override @override
Future<void> onEventsClear() { Future<void> onEventsClear() {
overrideConfirm = null; overrideConfirm = null;
_skip = false;
return super.onEventsClear(); return super.onEventsClear();
} }
void setOverrideConfirm(bool confirm) { void setOverrideConfirm(bool? confirm) {
overrideConfirm = confirm; overrideConfirm = confirm;
} }
void setSkip(bool skip) {
_skip = skip;
}
} }