Better way to store session variables

This commit is contained in:
Ming Ming 2023-08-24 23:32:23 +08:00
parent c07819fb0e
commit 7236e033d9
4 changed files with 53 additions and 31 deletions

View file

@ -4,6 +4,7 @@ import 'package:nc_photos/controller/account_pref_controller.dart';
import 'package:nc_photos/controller/collections_controller.dart'; import 'package:nc_photos/controller/collections_controller.dart';
import 'package:nc_photos/controller/persons_controller.dart'; import 'package:nc_photos/controller/persons_controller.dart';
import 'package:nc_photos/controller/server_controller.dart'; import 'package:nc_photos/controller/server_controller.dart';
import 'package:nc_photos/controller/session_controller.dart';
import 'package:nc_photos/controller/sync_controller.dart'; import 'package:nc_photos/controller/sync_controller.dart';
import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/di_container.dart';
@ -20,6 +21,8 @@ class AccountController {
_personsController = null; _personsController = null;
_syncController?.dispose(); _syncController?.dispose();
_syncController = null; _syncController = null;
_sessionController?.dispose();
_sessionController = null;
} }
Account get account => _account!; Account get account => _account!;
@ -52,10 +55,14 @@ class AccountController {
account: _account!, account: _account!,
); );
SessionController get sessionController =>
_sessionController ??= SessionController();
Account? _account; Account? _account;
CollectionsController? _collectionsController; CollectionsController? _collectionsController;
ServerController? _serverController; ServerController? _serverController;
AccountPrefController? _accountPrefController; AccountPrefController? _accountPrefController;
PersonsController? _personsController; PersonsController? _personsController;
SyncController? _syncController; SyncController? _syncController;
SessionController? _sessionController;
} }

View file

@ -0,0 +1,22 @@
import 'package:np_codegen/np_codegen.dart';
import 'package:rxdart/rxdart.dart';
/// Manage volatile global variables bonded to an account session, when the
/// session ends (i.e., sign out), the variables are cleared
@npLog
class SessionController {
SessionController();
void dispose() {
_hasFiredMetadataTaskController.close();
}
ValueStream<bool> get hasFiredMetadataTask =>
_hasFiredMetadataTaskController.stream;
void setFiredMetadataTask(bool value) {
_hasFiredMetadataTaskController.add(value);
}
final _hasFiredMetadataTaskController = BehaviorSubject.seeded(false);
}

View file

@ -1,5 +0,0 @@
class Primitive<T> {
Primitive(this.value);
T value;
}

View file

@ -12,7 +12,6 @@ import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart'; import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
import 'package:nc_photos/bloc/progress.dart'; import 'package:nc_photos/bloc/progress.dart';
import 'package:nc_photos/bloc/scan_account_dir.dart'; import 'package:nc_photos/bloc/scan_account_dir.dart';
import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/compute_queue.dart';
@ -31,7 +30,6 @@ import 'package:nc_photos/language_util.dart' as language_util;
import 'package:nc_photos/metadata_task_manager.dart'; import 'package:nc_photos/metadata_task_manager.dart';
import 'package:nc_photos/object_extension.dart'; import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/platform/k.dart' as platform_k; import 'package:nc_photos/platform/k.dart' as platform_k;
import 'package:nc_photos/primitive.dart';
import 'package:nc_photos/service.dart' as service; import 'package:nc_photos/service.dart' as service;
import 'package:nc_photos/share_handler.dart'; import 'package:nc_photos/share_handler.dart';
import 'package:nc_photos/snack_bar_manager.dart'; import 'package:nc_photos/snack_bar_manager.dart';
@ -183,7 +181,7 @@ class _HomePhotosState extends State<HomePhotos>
ScrollConfiguration.of(context).copyWith(scrollbars: false), ScrollConfiguration.of(context).copyWith(scrollbars: false),
child: RefreshIndicator( child: RefreshIndicator(
onRefresh: () async { onRefresh: () async {
_onRefreshSelected(); _onRefreshSelected(context);
await _waitRefresh(); await _waitRefresh();
}, },
child: CustomScrollView( child: CustomScrollView(
@ -381,8 +379,11 @@ class _HomePhotosState extends State<HomePhotos>
} }
} }
void _onRefreshSelected() { void _onRefreshSelected(BuildContext context) {
_hasFiredMetadataTask.value = false; context
.read<AccountController>()
.sessionController
.setFiredMetadataTask(false);
_reqRefresh(); _reqRefresh();
} }
@ -484,7 +485,7 @@ class _HomePhotosState extends State<HomePhotos>
void _onPrefUpdated(PrefUpdatedEvent ev) { void _onPrefUpdated(PrefUpdatedEvent ev) {
if (ev.key == PrefKey.enableExif) { if (ev.key == PrefKey.enableExif) {
if (ev.value == true) { if (ev.value == true) {
_tryStartMetadataTask(ignoreFired: true); _tryStartMetadataTask(context, ignoreFired: true);
} else { } else {
_stopMetadataTask(); _stopMetadataTask();
} }
@ -505,19 +506,28 @@ class _HomePhotosState extends State<HomePhotos>
ImageProcessorUploadSuccessEvent ev) { ImageProcessorUploadSuccessEvent ev) {
_log.info( _log.info(
"[_onImageProcessorUploadSuccessEvent] Scheduling metadata task after next refresh"); "[_onImageProcessorUploadSuccessEvent] Scheduling metadata task after next refresh");
_hasFiredMetadataTask.value = false; context
.read<AccountController>()
.sessionController
.setFiredMetadataTask(false);
} }
void _onBackToTop(HomePhotosBackToTopEvent ev) { void _onBackToTop(HomePhotosBackToTopEvent ev) {
_scrollController.jumpTo(0); _scrollController.jumpTo(0);
} }
Future<void> _tryStartMetadataTask({ Future<void> _tryStartMetadataTask(
BuildContext context, {
bool ignoreFired = false, bool ignoreFired = false,
}) async { }) async {
if (_bloc.state is ScanAccountDirBlocSuccess && if (_bloc.state is ScanAccountDirBlocSuccess &&
Pref().isEnableExifOr() && Pref().isEnableExifOr() &&
(!_hasFiredMetadataTask.value || ignoreFired)) { (ignoreFired ||
!context
.read<AccountController>()
.sessionController
.hasFiredMetadataTask
.value)) {
try { try {
final c = KiwiContainer().resolve<DiContainer>(); final c = KiwiContainer().resolve<DiContainer>();
final missingMetadataCount = await c.sqliteDb.use((db) async { final missingMetadataCount = await c.sqliteDb.use((db) async {
@ -536,7 +546,10 @@ class _HomePhotosState extends State<HomePhotos>
} }
} }
_hasFiredMetadataTask.value = true; context
.read<AccountController>()
.sessionController
.setFiredMetadataTask(true);
} catch (e, stackTrace) { } catch (e, stackTrace) {
_log.shout("[_tryStartMetadataTask] Failed starting metadata task", e, _log.shout("[_tryStartMetadataTask] Failed starting metadata task", e,
stackTrace); stackTrace);
@ -592,7 +605,7 @@ class _HomePhotosState extends State<HomePhotos>
_isScrollbarVisible = true; _isScrollbarVisible = true;
context.read<AccountController>().syncController.requestSync( context.read<AccountController>().syncController.requestSync(
widget.account, _accountPrefController.personProvider.value); widget.account, _accountPrefController.personProvider.value);
_tryStartMetadataTask(); _tryStartMetadataTask(context);
} }
}); });
} }
@ -683,21 +696,6 @@ class _HomePhotosState extends State<HomePhotos>
double _calcAppBarExtent(BuildContext context) => double _calcAppBarExtent(BuildContext context) =>
MediaQuery.of(context).padding.top + kToolbarHeight; MediaQuery.of(context).padding.top + kToolbarHeight;
Primitive<bool> get _hasFiredMetadataTask {
final name = bloc_util.getInstNameForRootAwareAccount(
"HomePhotosState.hasFiredMetadataTask", widget.account);
try {
_log.fine("[_hasFiredMetadataTask] Resolving for '$name'");
return KiwiContainer().resolve<Primitive<bool>>(name);
} catch (_) {
_log.info(
"[_hasFiredMetadataTask] New instance for account: ${widget.account}");
final obj = Primitive(false);
KiwiContainer().registerInstance<Primitive<bool>>(obj, name: name);
return obj;
}
}
late final _bloc = ScanAccountDirBloc.of(widget.account); late final _bloc = ScanAccountDirBloc.of(widget.account);
late final _queryProgressBloc = ProgressBloc(); late final _queryProgressBloc = ProgressBloc();
late final _accountPrefController = late final _accountPrefController =