From 19fbc11c339605b39bcf7056c13d794cfa72b6c8 Mon Sep 17 00:00:00 2001 From: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Date: Wed, 3 Jun 2026 12:15:19 +0530 Subject: [PATCH] extract CachedKeyValueRepository --- .../cached_key_value_repository.dart | 37 ++++++++++++++ .../repositories/settings.repository.dart | 50 +++++++++---------- 2 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 mobile/lib/infrastructure/repositories/cached_key_value_repository.dart diff --git a/mobile/lib/infrastructure/repositories/cached_key_value_repository.dart b/mobile/lib/infrastructure/repositories/cached_key_value_repository.dart new file mode 100644 index 0000000000..92cb2e7c3b --- /dev/null +++ b/mobile/lib/infrastructure/repositories/cached_key_value_repository.dart @@ -0,0 +1,37 @@ +import 'package:collection/collection.dart'; +import 'package:drift/drift.dart'; +// ignore: depend_on_referenced_packages +import 'package:meta/meta.dart'; + +abstract class CachedKeyValueRepository { + CachedKeyValueRepository(this._snapshot); + + S _snapshot; + S get snapshot => _snapshot; + @protected + set snapshot(S value) => _snapshot = value; + + List get keys; + + Object decodeValue(K key, String raw); + + S buildSnapshot(Map overrides); + + Selectable<({String key, String value})> selectable(); + + Future refresh() async => _snapshot = _build(await selectable().get()); + + Stream watchSnapshot() => selectable().watch().map((rows) => _snapshot = _build(rows)); + + S _build(List<({String key, String value})> rows) { + final overrides = {}; + for (final row in rows) { + final key = keys.firstWhereOrNull((k) => k.name == row.key); + if (key == null) { + continue; + } + overrides[key] = decodeValue(key, row.value); + } + return buildSnapshot(overrides); + } +} diff --git a/mobile/lib/infrastructure/repositories/settings.repository.dart b/mobile/lib/infrastructure/repositories/settings.repository.dart index 56066f543a..8fc8749c0c 100644 --- a/mobile/lib/infrastructure/repositories/settings.repository.dart +++ b/mobile/lib/infrastructure/repositories/settings.repository.dart @@ -1,14 +1,14 @@ -import 'package:collection/collection.dart'; import 'package:drift/drift.dart'; import 'package:immich_mobile/domain/models/config/app_config.dart'; import 'package:immich_mobile/domain/models/settings_key.dart'; import 'package:immich_mobile/infrastructure/entities/settings.entity.drift.dart'; +import 'package:immich_mobile/infrastructure/repositories/cached_key_value_repository.dart'; import 'package:immich_mobile/infrastructure/repositories/db.repository.dart'; -class SettingsRepository extends DriftDatabaseRepository { +class SettingsRepository extends CachedKeyValueRepository { final Drift _db; - SettingsRepository._(this._db) : super(_db); + SettingsRepository._(this._db) : super(const .new()); static SettingsRepository? _instance; @@ -20,9 +20,6 @@ class SettingsRepository extends DriftDatabaseRepository { return instance; } - AppConfig _appConfig = const .new(); - AppConfig get appConfig => _appConfig; - static Future ensureInitialized(Drift db) async { if (_instance == null) { final instance = SettingsRepository._(db); @@ -32,7 +29,20 @@ class SettingsRepository extends DriftDatabaseRepository { return _instance!; } - Future refresh() async => _applyOverrides(await _db.select(_db.settingsEntity).get()); + @override + List get keys => SettingsKey.values; + + @override + Object decodeValue(SettingsKey key, String raw) => key.decode(raw); + + @override + AppConfig buildSnapshot(Map overrides) => AppConfig.fromEntries(overrides); + + @override + Selectable<({String key, String value})> selectable() => + _db.select(_db.settingsEntity).map((row) => (key: row.key, value: row.value)); + + AppConfig get appConfig => snapshot; Future clear(Iterable keys) async { if (keys.isEmpty) { @@ -42,13 +52,15 @@ class SettingsRepository extends DriftDatabaseRepository { final names = keys.map((key) => key.name).toList(); await (_db.delete(_db.settingsEntity)..where((row) => row.key.isIn(names))).go(); + var config = snapshot; for (final key in keys) { - _appConfig = _appConfig.write(key, defaultConfig.read(key)); + config = config.write(key, defaultConfig.read(key)); } + snapshot = config; } Future write(SettingsKey key, U value) async { - if (value == _appConfig.read(key)) { + if (value == snapshot.read(key)) { return; } @@ -61,24 +73,8 @@ class SettingsRepository extends DriftDatabaseRepository { .insertOnConflictUpdate( SettingsEntityCompanion.insert(key: key.name, value: key.encode(value), updatedAt: Value(DateTime.now())), ); - _appConfig = _appConfig.write(key, value); + snapshot = snapshot.write(key, value); } - Stream watchConfig() => _db.select(_db.settingsEntity).watch().map((rows) { - _applyOverrides(rows); - return _appConfig; - }); - - void _applyOverrides(List rows) { - _appConfig = AppConfig.fromEntries( - rows.fold({}, (overrides, row) { - final metadataKey = SettingsKey.values.firstWhereOrNull((key) => key.name == row.key); - if (metadataKey == null) { - return overrides; - } - - return {...overrides, metadataKey: metadataKey.decode(row.value)}; - }), - ); - } + Stream watchConfig() => watchSnapshot(); }