replace bloc with watch_it

This commit is contained in:
shenlong-tanwen
2024-05-10 02:00:00 +05:30
parent aa5673bae3
commit fb6253d2d1
29 changed files with 663 additions and 239 deletions
@@ -5,7 +5,7 @@ import 'package:immich_mobile/domain/models/store.model.dart';
abstract class IStoreRepository {
FutureOr<T?> getValue<T>(StoreKey key);
FutureOr<void> setValue<T>(StoreKey<T> key, T value);
FutureOr<void> setValue<T>(StoreKey key, T value);
FutureOr<void> deleteValue(StoreKey key);
@@ -0,0 +1,10 @@
import 'package:immich_mobile/domain/models/store.model.dart';
enum AppSettings<T> {
appTheme<int>(StoreKey.appTheme, 10);
const AppSettings(this.storeKey, this.defaultValue);
final StoreKey storeKey;
final T defaultValue;
}
+3 -6
View File
@@ -1,10 +1,7 @@
/// Key for each possible value in the `Store`.
/// Defines the data type for each value
enum StoreKey<T> {
// Server endpoint related stores
accessToken<String>(0, type: String),
serverEndpoint<String>(1, type: String),
;
enum StoreKey {
appTheme(1000, type: int);
const StoreKey(this.id, {required this.type});
final int id;
@@ -45,7 +42,7 @@ class StoreValue {
}
}
static StoreValue of<T>(StoreKey<T> key, T? value) {
static StoreValue of<T>(StoreKey key, T? value) {
int? i;
String? s;
@@ -20,7 +20,7 @@ class StoreDriftRepository implements IStoreRepository {
}
@override
FutureOr<void> setValue<T>(StoreKey<T> key, T value) {
FutureOr<void> setValue<T>(StoreKey key, T value) {
return db.transaction(() async {
final storeValue = StoreValue.of(key, value);
await db.into(db.store).insertOnConflictUpdate(StoreCompanion.insert(
-29
View File
@@ -1,29 +0,0 @@
import 'package:get_it/get_it.dart';
import 'package:immich_mobile/domain/interfaces/log.interface.dart';
import 'package:immich_mobile/domain/interfaces/store.interface.dart';
import 'package:immich_mobile/domain/repositories/database.repository.dart';
import 'package:immich_mobile/domain/repositories/log.repository.dart';
import 'package:immich_mobile/domain/repositories/store.repository.dart';
import 'package:immich_mobile/domain/store_manager.dart';
/// Ambient instance
final getIt = GetIt.instance;
class ServiceLocator {
const ServiceLocator._internal();
static void configureServices() {
// Register DB
getIt.registerSingleton<DriftDatabaseRepository>(DriftDatabaseRepository());
_registerCoreServices();
}
static void _registerCoreServices() {
// Init store
getIt
.registerFactory<IStoreRepository>(() => StoreDriftRepository(getIt()));
getIt.registerSingleton<StoreManager>(StoreManager(getIt()));
// Logs
getIt.registerFactory<ILogRepository>(() => LogDriftRepository(getIt()));
}
}
@@ -0,0 +1,22 @@
import 'package:immich_mobile/domain/models/app_setting.model.dart';
import 'package:immich_mobile/domain/store_manager.dart';
class AppSettingsService {
final StoreManager store;
const AppSettingsService(this.store);
T getSetting<T>(AppSettings<T> setting) {
return store.get(setting.storeKey, setting.defaultValue);
}
void setSetting<T>(AppSettings<T> setting, T value) {
store.put(setting.storeKey, value);
}
Stream<T> watchSetting<T>(AppSettings<T> setting) {
return store
.watch<T>(setting.storeKey)
.map((value) => value ?? setting.defaultValue);
}
}
+5 -5
View File
@@ -55,11 +55,11 @@ class StoreManager with LogContext {
}
/// Returns the stored value for the given key (possibly null)
T? tryGet<T>(StoreKey<T> key) => _cache[key.id] as T?;
T? tryGet<T>(StoreKey key) => _cache[key.id] as T?;
/// Returns the stored value for the given key or if null the [defaultValue]
/// Throws a [StoreKeyNotFoundException] if both are null
T get<T>(StoreKey<T> key, [T? defaultValue]) {
T get<T>(StoreKey key, [T? defaultValue]) {
final value = _cache[key.id] ?? defaultValue;
if (value == null) {
throw StoreKeyNotFoundException(key);
@@ -68,17 +68,17 @@ class StoreManager with LogContext {
}
/// Watches a specific key for changes
Stream<T?> watch<T>(StoreKey<T> key) => _db.watchValue(key);
Stream<T?> watch<T>(StoreKey key) => _db.watchValue(key);
/// Stores the value synchronously in the cache and asynchronously in the DB
FutureOr<void> put<T>(StoreKey<T> key, T value) async {
FutureOr<void> put<T>(StoreKey key, T value) async {
if (_cache[key.id] == value) return Future.value();
_cache[key.id] = value;
return await _db.setValue(key, value);
}
/// Removes the value synchronously from the cache and asynchronously from the DB
Future<void> delete<T>(StoreKey<T> key) async {
Future<void> delete(StoreKey key) async {
if (_cache[key.id] == null) return Future.value();
_cache.remove(key.id);
return await _db.deleteValue(key);