2023-03-03 15:38:30 -07:00
|
|
|
// ignore_for_file: deprecated_member_use_from_same_package
|
|
|
|
|
2023-03-22 18:36:44 -07:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
2023-03-03 15:38:30 -07:00
|
|
|
import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/backup_album.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/duplicated_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_duplicated_assets.model.dart';
|
2023-03-22 18:36:44 -07:00
|
|
|
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/immich_logger_message.model.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-03-03 15:38:30 -07:00
|
|
|
import 'package:immich_mobile/shared/services/asset_cache.service.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:isar/isar.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
|
|
|
|
Future<void> migrateHiveToStoreIfNecessary() async {
|
2023-03-18 07:55:11 -07:00
|
|
|
await _migrateHiveBoxIfNecessary(userInfoBox, _migrateHiveUserInfoBox);
|
|
|
|
await _migrateHiveBoxIfNecessary(
|
|
|
|
backgroundBackupInfoBox,
|
|
|
|
_migrateHiveBackgroundBackupInfoBox,
|
|
|
|
);
|
|
|
|
await _migrateHiveBoxIfNecessary(hiveBackupInfoBox, _migrateBackupInfoBox);
|
|
|
|
await _migrateHiveBoxIfNecessary(
|
|
|
|
duplicatedAssetsBox,
|
|
|
|
_migrateDuplicatedAssetsBox,
|
|
|
|
);
|
2023-03-22 18:36:44 -07:00
|
|
|
await _migrateHiveBoxIfNecessary(
|
|
|
|
hiveGithubReleaseInfoBox,
|
|
|
|
_migrateReleaseInfoBox,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _migrateHiveBoxIfNecessary(hiveLoginInfoBox, _migrateLoginInfoBox);
|
|
|
|
await _migrateHiveBoxIfNecessary(
|
|
|
|
immichLoggerBox,
|
|
|
|
(Box<ImmichLoggerMessage> box) => box.deleteFromDisk(),
|
|
|
|
);
|
|
|
|
await _migrateHiveBoxIfNecessary(userSettingInfoBox, _migrateAppSettingsBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<void> _migrateReleaseInfoBox(Box box) =>
|
|
|
|
_migrateKey(box, githubReleaseInfoKey, StoreKey.githubReleaseInfo);
|
|
|
|
|
|
|
|
Future<void> _migrateLoginInfoBox(Box<HiveSavedLoginInfo> box) async {
|
|
|
|
final HiveSavedLoginInfo? info = box.get(savedLoginInfoKey);
|
|
|
|
if (info != null) {
|
|
|
|
await Store.put(StoreKey.serverUrl, info.serverUrl);
|
|
|
|
await Store.put(StoreKey.accessToken, info.accessToken);
|
|
|
|
}
|
2023-03-18 07:55:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _migrateHiveUserInfoBox(Box box) async {
|
|
|
|
await _migrateKey(box, userIdKey, StoreKey.userRemoteId);
|
|
|
|
await _migrateKey(box, assetEtagKey, StoreKey.assetETag);
|
2023-03-22 18:36:44 -07:00
|
|
|
if (Store.tryGet(StoreKey.deviceId) == null) {
|
|
|
|
await _migrateKey(box, deviceIdKey, StoreKey.deviceId);
|
|
|
|
}
|
|
|
|
await _migrateKey(box, serverEndpointKey, StoreKey.serverEndpoint);
|
2023-03-18 07:55:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _migrateHiveBackgroundBackupInfoBox(Box box) async {
|
|
|
|
await _migrateKey(box, backupFailedSince, StoreKey.backupFailedSince);
|
|
|
|
await _migrateKey(box, backupRequireWifi, StoreKey.backupRequireWifi);
|
|
|
|
await _migrateKey(box, backupRequireCharging, StoreKey.backupRequireCharging);
|
|
|
|
await _migrateKey(box, backupTriggerDelay, StoreKey.backupTriggerDelay);
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:36:44 -07:00
|
|
|
FutureOr<void> _migrateBackupInfoBox(Box<HiveBackupAlbums> box) {
|
2023-03-18 07:55:11 -07:00
|
|
|
final HiveBackupAlbums? infos = box.get(backupInfoKey);
|
|
|
|
if (infos != null) {
|
2023-03-22 18:36:44 -07:00
|
|
|
final Isar? db = Isar.getInstance();
|
|
|
|
if (db == null) {
|
|
|
|
throw Exception("_migrateBackupInfoBox could not load database");
|
|
|
|
}
|
2023-03-18 07:55:11 -07:00
|
|
|
List<BackupAlbum> albums = [];
|
|
|
|
for (int i = 0; i < infos.selectedAlbumIds.length; i++) {
|
|
|
|
final album = BackupAlbum(
|
|
|
|
infos.selectedAlbumIds[i],
|
|
|
|
infos.lastSelectedBackupTime[i],
|
|
|
|
BackupSelection.select,
|
|
|
|
);
|
|
|
|
albums.add(album);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < infos.excludedAlbumsIds.length; i++) {
|
|
|
|
final album = BackupAlbum(
|
|
|
|
infos.excludedAlbumsIds[i],
|
|
|
|
infos.lastExcludedBackupTime[i],
|
|
|
|
BackupSelection.exclude,
|
|
|
|
);
|
|
|
|
albums.add(album);
|
|
|
|
}
|
2023-03-22 18:36:44 -07:00
|
|
|
return db.writeTxn(() => db.backupAlbums.putAll(albums));
|
2023-03-18 07:55:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:36:44 -07:00
|
|
|
FutureOr<void> _migrateDuplicatedAssetsBox(Box<HiveDuplicatedAssets> box) {
|
2023-03-18 07:55:11 -07:00
|
|
|
final HiveDuplicatedAssets? duplicatedAssets = box.get(duplicatedAssetsKey);
|
|
|
|
if (duplicatedAssets != null) {
|
2023-03-22 18:36:44 -07:00
|
|
|
final Isar? db = Isar.getInstance();
|
|
|
|
if (db == null) {
|
|
|
|
throw Exception("_migrateBackupInfoBox could not load database");
|
|
|
|
}
|
2023-03-18 07:55:11 -07:00
|
|
|
final duplicatedAssetIds = duplicatedAssets.duplicatedAssetIds
|
|
|
|
.map((id) => DuplicatedAsset(id))
|
|
|
|
.toList();
|
2023-03-22 18:36:44 -07:00
|
|
|
return db.writeTxn(() => db.duplicatedAssets.putAll(duplicatedAssetIds));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _migrateAppSettingsBox(Box box) async {
|
|
|
|
for (AppSettingsEnum s in AppSettingsEnum.values) {
|
|
|
|
await _migrateKey(box, s.hiveKey, s.storeKey);
|
2023-03-18 07:55:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _migrateHiveBoxIfNecessary<T>(
|
|
|
|
String boxName,
|
2023-03-22 18:36:44 -07:00
|
|
|
FutureOr<void> Function(Box<T>) migrate,
|
2023-03-18 07:55:11 -07:00
|
|
|
) async {
|
2023-02-09 10:32:08 -07:00
|
|
|
try {
|
2023-03-18 07:55:11 -07:00
|
|
|
if (await Hive.boxExists(boxName)) {
|
2023-03-22 18:36:44 -07:00
|
|
|
final box = await Hive.openBox<T>(boxName);
|
|
|
|
await migrate(box);
|
|
|
|
await box.deleteFromDisk();
|
2023-02-09 10:32:08 -07:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-03-18 07:55:11 -07:00
|
|
|
debugPrint("Error while migrating $boxName $e");
|
2023-02-09 10:32:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:36:44 -07:00
|
|
|
FutureOr<void> _migrateKey<T>(Box box, String hiveKey, StoreKey<T> key) {
|
|
|
|
final T? value = box.get(hiveKey);
|
2023-02-09 10:32:08 -07:00
|
|
|
if (value != null) {
|
2023-03-22 18:36:44 -07:00
|
|
|
return Store.put(key, value);
|
2023-02-09 10:32:08 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-03 15:38:30 -07:00
|
|
|
|
|
|
|
Future<void> migrateJsonCacheIfNecessary() async {
|
|
|
|
await AlbumCacheService().invalidate();
|
|
|
|
await SharedAlbumCacheService().invalidate();
|
|
|
|
await AssetCacheService().invalidate();
|
|
|
|
}
|