mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
split singleton into a different class
This commit is contained in:
@@ -39,7 +39,7 @@ class BackgroundWorkerFgService {
|
|||||||
_foregroundHostApi.saveNotificationMessage(title, body);
|
_foregroundHostApi.saveNotificationMessage(title, body);
|
||||||
|
|
||||||
Future<void> configure({int? minimumDelaySeconds, bool? requireCharging}) {
|
Future<void> configure({int? minimumDelaySeconds, bool? requireCharging}) {
|
||||||
final backup = MetadataRepository.instance.appConfig.backup;
|
final backup = MetadataStore.appConfig.backup;
|
||||||
return _foregroundHostApi.configure(
|
return _foregroundHostApi.configure(
|
||||||
BackgroundWorkerSettings(
|
BackgroundWorkerSettings(
|
||||||
minimumDelaySeconds: minimumDelaySeconds ?? backup.triggerDelay,
|
minimumDelaySeconds: minimumDelaySeconds ?? backup.triggerDelay,
|
||||||
@@ -69,7 +69,7 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi {
|
|||||||
BackgroundWorkerFlutterApi.setUp(this);
|
BackgroundWorkerFlutterApi.setUp(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get _isBackupEnabled => MetadataRepository.instance.appConfig.backup.enabled;
|
bool get _isBackupEnabled => MetadataStore.appConfig.backup.enabled;
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -6,12 +6,7 @@ import 'package:immich_mobile/extensions/string_extensions.dart';
|
|||||||
import 'package:immich_mobile/infrastructure/entities/metadata.entity.drift.dart';
|
import 'package:immich_mobile/infrastructure/entities/metadata.entity.drift.dart';
|
||||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||||
|
|
||||||
class MetadataRepository extends DriftDatabaseRepository {
|
abstract final class MetadataStore {
|
||||||
final Drift _db;
|
|
||||||
final Map<MetadataKey, Object> _cache = {};
|
|
||||||
|
|
||||||
MetadataRepository._(this._db) : super(_db);
|
|
||||||
|
|
||||||
static MetadataRepository? _instance;
|
static MetadataRepository? _instance;
|
||||||
|
|
||||||
static MetadataRepository get instance {
|
static MetadataRepository get instance {
|
||||||
@@ -22,26 +17,36 @@ class MetadataRepository extends DriftDatabaseRepository {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
AppConfig _appConfig = const .new();
|
|
||||||
AppConfig get appConfig => _appConfig;
|
|
||||||
|
|
||||||
SystemConfig _systemConfig = const .new();
|
|
||||||
SystemConfig get systemConfig => _systemConfig;
|
|
||||||
|
|
||||||
static Future<MetadataRepository> ensureInitialized(Drift db) async {
|
static Future<MetadataRepository> ensureInitialized(Drift db) async {
|
||||||
if (_instance == null) {
|
if (_instance == null) {
|
||||||
final instance = MetadataRepository._(db);
|
final instance = MetadataRepository(db);
|
||||||
await instance._hydrate();
|
await instance._hydrate();
|
||||||
_instance = instance;
|
_instance = instance;
|
||||||
}
|
}
|
||||||
return _instance!;
|
return _instance!;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> refresh() async {
|
static AppConfig get appConfig => instance.appConfig;
|
||||||
instance._cache.clear();
|
static SystemConfig get systemConfig => instance.systemConfig;
|
||||||
instance._appConfig = const .new();
|
}
|
||||||
instance._systemConfig = const .new();
|
|
||||||
await instance._hydrate();
|
class MetadataRepository extends DriftDatabaseRepository {
|
||||||
|
final Drift _db;
|
||||||
|
final Map<MetadataKey, Object> _cache = {};
|
||||||
|
|
||||||
|
MetadataRepository(this._db) : super(_db);
|
||||||
|
|
||||||
|
AppConfig _appConfig = const .new();
|
||||||
|
AppConfig get appConfig => _appConfig;
|
||||||
|
|
||||||
|
SystemConfig _systemConfig = const .new();
|
||||||
|
SystemConfig get systemConfig => _systemConfig;
|
||||||
|
|
||||||
|
Future<void> refresh() async {
|
||||||
|
_cache.clear();
|
||||||
|
_appConfig = const .new();
|
||||||
|
_systemConfig = const .new();
|
||||||
|
await _hydrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _hydrate() async => _hydrateCache(await _db.select(_db.metadataEntity).get());
|
Future<void> _hydrate() async => _hydrateCache(await _db.select(_db.metadataEntity).get());
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class _DriftBackupAlbumSelectionPageState extends ConsumerState<DriftBackupAlbum
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final isBackupEnabled = MetadataRepository.instance.appConfig.backup.enabled;
|
final isBackupEnabled = MetadataStore.appConfig.backup.enabled;
|
||||||
await ref.read(driftBackupProvider.notifier).getBackupStatus(user.id);
|
await ref.read(driftBackupProvider.notifier).getBackupStatus(user.id);
|
||||||
final currentTotalAssetCount = ref.read(driftBackupProvider.select((p) => p.totalCount));
|
final currentTotalAssetCount = ref.read(driftBackupProvider.select((p) => p.totalCount));
|
||||||
final totalChanged = currentTotalAssetCount != _initialTotalAssetCount;
|
final totalChanged = currentTotalAssetCount != _initialTotalAssetCount;
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class DriftBackupOptionsPage extends ConsumerWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await ref.read(driftBackupProvider.notifier).getBackupStatus(currentUser.id);
|
await ref.read(driftBackupProvider.notifier).getBackupStatus(currentUser.id);
|
||||||
final isBackupEnabled = MetadataRepository.instance.appConfig.backup.enabled;
|
final isBackupEnabled = MetadataStore.appConfig.backup.enabled;
|
||||||
if (!isBackupEnabled) {
|
if (!isBackupEnabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -341,7 +341,7 @@ class SplashScreenPageState extends ConsumerState<SplashScreenPage> {
|
|||||||
await backgroundManager.hashAssets();
|
await backgroundManager.hashAssets();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MetadataRepository.instance.appConfig.backup.syncAlbums) {
|
if (MetadataStore.appConfig.backup.syncAlbums) {
|
||||||
await backgroundManager.syncLinkedAlbum();
|
await backgroundManager.syncLinkedAlbum();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -370,7 +370,7 @@ class SplashScreenPageState extends ConsumerState<SplashScreenPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _resumeBackup(DriftBackupNotifier notifier) async {
|
Future<void> _resumeBackup(DriftBackupNotifier notifier) async {
|
||||||
final isEnableBackup = MetadataRepository.instance.appConfig.backup.enabled;
|
final isEnableBackup = MetadataStore.appConfig.backup.enabled;
|
||||||
|
|
||||||
if (isEnableBackup) {
|
if (isEnableBackup) {
|
||||||
final currentUser = Store.tryGet(StoreKey.currentUser);
|
final currentUser = Store.tryGet(StoreKey.currentUser);
|
||||||
|
|||||||
@@ -188,6 +188,4 @@ ImageProvider? getThumbnailImageProvider(BaseAsset asset, {Size size = kThumbnai
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool _shouldUseLocalAsset(BaseAsset asset) =>
|
bool _shouldUseLocalAsset(BaseAsset asset) =>
|
||||||
asset.hasLocal &&
|
asset.hasLocal && (!asset.hasRemote || !MetadataStore.appConfig.image.preferRemote) && !asset.isEdited;
|
||||||
(!asset.hasRemote || !MetadataRepository.instance.appConfig.image.preferRemote) &&
|
|
||||||
!asset.isEdited;
|
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ class LocalFullImageProvider extends CancellableImageProvider<LocalFullImageProv
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final loadOriginal = MetadataRepository.instance.appConfig.image.loadOriginal;
|
final loadOriginal = MetadataStore.appConfig.image.loadOriginal;
|
||||||
final devicePixelRatio = PlatformDispatcher.instance.views.first.devicePixelRatio;
|
final devicePixelRatio = PlatformDispatcher.instance.views.first.devicePixelRatio;
|
||||||
var request = this.request = LocalImageRequest(
|
var request = this.request = LocalImageRequest(
|
||||||
localId: key.id,
|
localId: key.id,
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class RemoteFullImageProvider extends CancellableImageProvider<RemoteFullImagePr
|
|||||||
edited: key.edited,
|
edited: key.edited,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
final loadOriginal = assetType == AssetType.image && MetadataRepository.instance.appConfig.image.loadOriginal;
|
final loadOriginal = assetType == AssetType.image && MetadataStore.appConfig.image.loadOriginal;
|
||||||
yield* loadRequest(previewRequest, decode, isFinal: !loadOriginal);
|
yield* loadRequest(previewRequest, decode, isFinal: !loadOriginal);
|
||||||
|
|
||||||
if (!loadOriginal) {
|
if (!loadOriginal) {
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
|||||||
await _apiService.updateHeaders();
|
await _apiService.updateHeaders();
|
||||||
|
|
||||||
final serverEndpoint = Store.get(StoreKey.serverEndpoint);
|
final serverEndpoint = Store.get(StoreKey.serverEndpoint);
|
||||||
final headerMap = MetadataRepository.instance.systemConfig.network.customHeaders;
|
final headerMap = MetadataStore.systemConfig.network.customHeaders;
|
||||||
final customHeaders = headerMap.isEmpty ? null : jsonEncode(headerMap);
|
final customHeaders = headerMap.isEmpty ? null : jsonEncode(headerMap);
|
||||||
await _widgetService.writeCredentials(serverEndpoint, accessToken, customHeaders);
|
await _widgetService.writeCredentials(serverEndpoint, accessToken, customHeaders);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import 'package:immich_mobile/domain/models/config/app_config.dart';
|
|||||||
import 'package:immich_mobile/domain/models/config/system_config.dart';
|
import 'package:immich_mobile/domain/models/config/system_config.dart';
|
||||||
import 'package:immich_mobile/infrastructure/repositories/metadata.repository.dart';
|
import 'package:immich_mobile/infrastructure/repositories/metadata.repository.dart';
|
||||||
|
|
||||||
final metadataProvider = Provider.autoDispose<MetadataRepository>((_) => MetadataRepository.instance);
|
final metadataProvider = Provider.autoDispose<MetadataRepository>((_) => MetadataStore.instance);
|
||||||
|
|
||||||
final appConfigProvider = Provider.autoDispose<AppConfig>((ref) {
|
final appConfigProvider = Provider.autoDispose<AppConfig>((ref) {
|
||||||
final repo = ref.watch(metadataProvider);
|
final repo = ref.watch(metadataProvider);
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class ApiService {
|
|||||||
if (serverEndpoint != null && serverEndpoint.isNotEmpty) {
|
if (serverEndpoint != null && serverEndpoint.isNotEmpty) {
|
||||||
urls.add(serverEndpoint);
|
urls.add(serverEndpoint);
|
||||||
}
|
}
|
||||||
final network = MetadataRepository.instance.systemConfig.network;
|
final network = MetadataStore.systemConfig.network;
|
||||||
final localEndpoint = network.localEndpoint;
|
final localEndpoint = network.localEndpoint;
|
||||||
if (localEndpoint != null) {
|
if (localEndpoint != null) {
|
||||||
urls.add(localEndpoint);
|
urls.add(localEndpoint);
|
||||||
@@ -189,7 +189,7 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Map<String, String> getRequestHeaders() {
|
static Map<String, String> getRequestHeaders() {
|
||||||
return MetadataRepository.instance.systemConfig.network.customHeaders;
|
return MetadataStore.systemConfig.network.customHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiClient get apiClient => _apiClient;
|
ApiClient get apiClient => _apiClient;
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ class AuthService {
|
|||||||
_log.severe("Error clearing local data", error, stackTrace);
|
_log.severe("Error clearing local data", error, stackTrace);
|
||||||
});
|
});
|
||||||
|
|
||||||
await MetadataRepository.instance.write(MetadataKey.backupEnabled, false);
|
await MetadataStore.instance.write(MetadataKey.backupEnabled, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -357,7 +357,7 @@ class BackgroundUploadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool _shouldRequireWiFi(LocalAsset asset) {
|
bool _shouldRequireWiFi(LocalAsset asset) {
|
||||||
final backup = MetadataRepository.instance.appConfig.backup;
|
final backup = MetadataStore.appConfig.backup;
|
||||||
if (asset.isVideo && backup.useCellularForVideos) return false;
|
if (asset.isVideo && backup.useCellularForVideos) return false;
|
||||||
if (!asset.isVideo && backup.useCellularForPhotos) return false;
|
if (!asset.isVideo && backup.useCellularForPhotos) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import 'package:immich_mobile/domain/models/asset/asset_metadata.model.dart';
|
|||||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||||
import 'package:immich_mobile/entities/store.entity.dart';
|
import 'package:immich_mobile/entities/store.entity.dart';
|
||||||
import 'package:immich_mobile/extensions/platform_extensions.dart';
|
|
||||||
import 'package:immich_mobile/extensions/network_capability_extensions.dart';
|
import 'package:immich_mobile/extensions/network_capability_extensions.dart';
|
||||||
|
import 'package:immich_mobile/extensions/platform_extensions.dart';
|
||||||
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
||||||
import 'package:immich_mobile/infrastructure/repositories/backup.repository.dart';
|
import 'package:immich_mobile/infrastructure/repositories/backup.repository.dart';
|
||||||
import 'package:immich_mobile/infrastructure/repositories/metadata.repository.dart';
|
import 'package:immich_mobile/infrastructure/repositories/metadata.repository.dart';
|
||||||
@@ -449,7 +449,7 @@ class ForegroundUploadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool _shouldRequireWiFi(LocalAsset asset) {
|
bool _shouldRequireWiFi(LocalAsset asset) {
|
||||||
final backup = MetadataRepository.instance.appConfig.backup;
|
final backup = MetadataStore.appConfig.backup;
|
||||||
if (asset.isVideo && backup.useCellularForVideos) return false;
|
if (asset.isVideo && backup.useCellularForVideos) return false;
|
||||||
if (!asset.isVideo && backup.useCellularForPhotos) return false;
|
if (!asset.isVideo && backup.useCellularForPhotos) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ abstract final class Bootstrap {
|
|||||||
|
|
||||||
await StoreService.init(storeRepository: storeRepo, listenUpdates: listenStoreUpdates);
|
await StoreService.init(storeRepository: storeRepo, listenUpdates: listenStoreUpdates);
|
||||||
|
|
||||||
final metadataRepo = await MetadataRepository.ensureInitialized(drift);
|
final metadataRepo = await MetadataStore.ensureInitialized(drift);
|
||||||
|
|
||||||
await LogService.init(
|
await LogService.init(
|
||||||
logRepository: LogRepository(logDb),
|
logRepository: LogRepository(logDb),
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ class LoginForm extends HookConsumerWidget {
|
|||||||
await backgroundManager.syncRemote();
|
await backgroundManager.syncRemote();
|
||||||
await backgroundManager.hashAssets();
|
await backgroundManager.hashAssets();
|
||||||
|
|
||||||
if (MetadataRepository.instance.appConfig.backup.syncAlbums) {
|
if (MetadataStore.appConfig.backup.syncAlbums) {
|
||||||
await backgroundManager.syncLinkedAlbum();
|
await backgroundManager.syncLinkedAlbum();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ void main() {
|
|||||||
|
|
||||||
setUpAll(() async {
|
setUpAll(() async {
|
||||||
ctx = MediumRepositoryContext();
|
ctx = MediumRepositoryContext();
|
||||||
sut = await MetadataRepository.ensureInitialized(ctx.db);
|
sut = MetadataRepository(ctx.db);
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDownAll(() async {
|
tearDownAll(() async {
|
||||||
@@ -23,7 +23,7 @@ void main() {
|
|||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
await ctx.db.delete(ctx.db.metadataEntity).go();
|
await ctx.db.delete(ctx.db.metadataEntity).go();
|
||||||
await MetadataRepository.refresh();
|
await sut.refresh();
|
||||||
});
|
});
|
||||||
|
|
||||||
group('defaults', () {
|
group('defaults', () {
|
||||||
@@ -78,7 +78,7 @@ void main() {
|
|||||||
// Cache hasn't seen this row yet — view still returns the default.
|
// Cache hasn't seen this row yet — view still returns the default.
|
||||||
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
||||||
|
|
||||||
await MetadataRepository.refresh();
|
await sut.refresh();
|
||||||
expect(sut.appConfig.theme.mode, ThemeMode.dark);
|
expect(sut.appConfig.theme.mode, ThemeMode.dark);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ void main() {
|
|||||||
await ctx.db.delete(ctx.db.metadataEntity).go();
|
await ctx.db.delete(ctx.db.metadataEntity).go();
|
||||||
expect(sut.appConfig.theme.mode, ThemeMode.dark);
|
expect(sut.appConfig.theme.mode, ThemeMode.dark);
|
||||||
|
|
||||||
await MetadataRepository.refresh();
|
await sut.refresh();
|
||||||
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
await MetadataRepository.refresh();
|
await sut.refresh();
|
||||||
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
expect(sut.appConfig.theme.mode, ThemeMode.system);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ void main() {
|
|||||||
);
|
);
|
||||||
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||||
await MetadataRepository.ensureInitialized(db);
|
await MetadataStore.ensureInitialized(db);
|
||||||
|
|
||||||
await Store.put(StoreKey.serverEndpoint, 'http://test-server.com');
|
await Store.put(StoreKey.serverEndpoint, 'http://test-server.com');
|
||||||
await Store.put(StoreKey.deviceId, 'test-device-id');
|
await Store.put(StoreKey.deviceId, 'test-device-id');
|
||||||
|
|||||||
Reference in New Issue
Block a user