From 94cb4e7d92122d7af922da60e9059b29519a46a2 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Mon, 25 Nov 2024 01:48:49 +0800 Subject: [PATCH] Fix missing files after changing included dirs --- app/lib/widget/settings/account/bloc.dart | 18 +++++++++++++++++- app/lib/widget/settings/account_settings.dart | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/lib/widget/settings/account/bloc.dart b/app/lib/widget/settings/account/bloc.dart index 76370dc4..fedd5f13 100644 --- a/app/lib/widget/settings/account/bloc.dart +++ b/app/lib/widget/settings/account/bloc.dart @@ -6,6 +6,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { required Account account, required this.prefController, required this.accountPrefController, + required this.npDb, this.highlight, }) : super(_State.init( account: account, @@ -88,11 +89,11 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { Future _onSetAccount(_SetAccount ev, Emitter<_State> emit) async { _log.info(ev); + final revert = state.account; emit(state.copyWith( account: ev.account, shouldReload: true, )); - final revert = state.account; try { final accounts = prefController.accountsValue; if (accounts.contains(ev.account)) { @@ -112,6 +113,20 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { _log.severe("[_onSetAccount] Failed while setAccounts3: ${ev.account}"); throw const _WritePrefError(); } + // clear files in db + for (final r in { + ...revert.roots, + accountPrefController.shareFolderValue, + }) { + _log.info("[_onSetAccount] Delete dir: $r"); + try { + final f = File(path: file_util.unstripPath(revert, r)); + await npDb.truncateDir(account: revert.toDb(), dir: f.toDbKey()); + await npDb.deleteFile(account: revert.toDb(), file: f.toDbKey()); + } catch (e, stackTrace) { + _log.severe("[_onSetAccount] Failed deleting dir: $r", e, stackTrace); + } + } } catch (_) { emit(state.copyWith(account: revert)); rethrow; @@ -155,6 +170,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { final PrefController prefController; final AccountPrefController accountPrefController; final AccountSettingsOption? highlight; + final NpDb npDb; final _subscriptions = []; var _isHandlingError = false; diff --git a/app/lib/widget/settings/account_settings.dart b/app/lib/widget/settings/account_settings.dart index 0e23e436..9f8c94b1 100644 --- a/app/lib/widget/settings/account_settings.dart +++ b/app/lib/widget/settings/account_settings.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:copy_with/copy_with.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:kiwi/kiwi.dart'; import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; @@ -10,6 +11,10 @@ import 'package:nc_photos/bloc_util.dart'; import 'package:nc_photos/controller/account_controller.dart'; import 'package:nc_photos/controller/account_pref_controller.dart'; import 'package:nc_photos/controller/pref_controller.dart'; +import 'package:nc_photos/db/entity_converter.dart'; +import 'package:nc_photos/di_container.dart'; +import 'package:nc_photos/entity/file.dart'; +import 'package:nc_photos/entity/file_util.dart' as file_util; import 'package:nc_photos/entity/person.dart'; import 'package:nc_photos/exception_event.dart'; import 'package:nc_photos/help_utils.dart' as help_util; @@ -23,6 +28,7 @@ import 'package:nc_photos/widget/settings/settings_list_caption.dart'; import 'package:nc_photos/widget/share_folder_picker.dart'; import 'package:nc_photos/widget/simple_input_dialog.dart'; import 'package:np_codegen/np_codegen.dart'; +import 'package:np_db/np_db.dart'; import 'package:np_ui/np_ui.dart'; import 'package:to_string/to_string.dart'; @@ -75,6 +81,7 @@ class AccountSettings extends StatelessWidget { account: accountController.account, prefController: context.read(), accountPrefController: accountController.accountPrefController, + npDb: KiwiContainer().resolve().npDb, highlight: highlight, ), child: const _WrappedAccountSettings(),