Skip to content

Commit 180589b

Browse files
alextran1502savely-krasovsky
authored andcommitted
fix(mobile): mobile migration logic (immich-app#17865)
* fix(mobile): mobile migration logic * add exception * remove unused comment * finalize
1 parent 0511530 commit 180589b

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

mobile/lib/utils/migration.dart

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import 'dart:async';
44
import 'dart:io';
55

6-
import 'package:flutter/widgets.dart';
6+
import 'package:flutter/foundation.dart';
77
import 'package:immich_mobile/domain/models/store.model.dart';
88
import 'package:immich_mobile/entities/album.entity.dart';
99
import 'package:immich_mobile/entities/android_device_asset.entity.dart';
@@ -17,6 +17,8 @@ import 'package:immich_mobile/infrastructure/entities/store.entity.dart';
1717
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
1818
import 'package:immich_mobile/utils/diff.dart';
1919
import 'package:isar/isar.dart';
20+
// ignore: import_rule_photo_manager
21+
import 'package:photo_manager/photo_manager.dart';
2022

2123
const int targetVersion = 10;
2224

@@ -69,14 +71,45 @@ Future<void> _migrateDeviceAsset(Isar db) async {
6971
: (await db.iOSDeviceAssets.where().findAll())
7072
.map((i) => _DeviceAsset(assetId: i.id, hash: i.hash))
7173
.toList();
72-
final localAssets = (await db.assets
73-
.where()
74-
.anyOf(ids, (query, id) => query.localIdEqualTo(id.assetId))
75-
.findAll())
76-
.map((a) => _DeviceAsset(assetId: a.localId!, dateTime: a.fileModifiedAt))
77-
.toList();
78-
debugPrint("Device Asset Ids length - ${ids.length}");
79-
debugPrint("Local Asset Ids length - ${localAssets.length}");
74+
75+
final PermissionState ps = await PhotoManager.requestPermissionExtend();
76+
if (!ps.hasAccess) {
77+
if (kDebugMode) {
78+
debugPrint(
79+
"[MIGRATION] Photo library permission not granted. Skipping device asset migration.",
80+
);
81+
}
82+
83+
return;
84+
}
85+
86+
List<_DeviceAsset> localAssets = [];
87+
final List<AssetPathEntity> paths =
88+
await PhotoManager.getAssetPathList(onlyAll: true);
89+
90+
if (paths.isEmpty) {
91+
localAssets = (await db.assets
92+
.where()
93+
.anyOf(ids, (query, id) => query.localIdEqualTo(id.assetId))
94+
.findAll())
95+
.map(
96+
(a) => _DeviceAsset(assetId: a.localId!, dateTime: a.fileModifiedAt),
97+
)
98+
.toList();
99+
} else {
100+
final AssetPathEntity albumWithAll = paths.first;
101+
final int assetCount = await albumWithAll.assetCountAsync;
102+
103+
final List<AssetEntity> allDeviceAssets =
104+
await albumWithAll.getAssetListRange(start: 0, end: assetCount);
105+
106+
localAssets = allDeviceAssets
107+
.map((a) => _DeviceAsset(assetId: a.id, dateTime: a.modifiedDateTime))
108+
.toList();
109+
}
110+
111+
debugPrint("[MIGRATION] Device Asset Ids length - ${ids.length}");
112+
debugPrint("[MIGRATION] Local Asset Ids length - ${localAssets.length}");
80113
ids.sort((a, b) => a.assetId.compareTo(b.assetId));
81114
localAssets.sort((a, b) => a.assetId.compareTo(b.assetId));
82115
final List<DeviceAssetEntity> toAdd = [];
@@ -95,15 +128,27 @@ Future<void> _migrateDeviceAsset(Isar db) async {
95128
return false;
96129
},
97130
onlyFirst: (deviceAsset) {
98-
debugPrint(
99-
'DeviceAsset not found in local assets: ${deviceAsset.assetId}',
100-
);
131+
if (kDebugMode) {
132+
debugPrint(
133+
'[MIGRATION] Local asset not found in DeviceAsset: ${deviceAsset.assetId}',
134+
);
135+
}
101136
},
102137
onlySecond: (asset) {
103-
debugPrint('Local asset not found in DeviceAsset: ${asset.assetId}');
138+
if (kDebugMode) {
139+
debugPrint(
140+
'[MIGRATION] Local asset not found in DeviceAsset: ${asset.assetId}',
141+
);
142+
}
104143
},
105144
);
106-
debugPrint("Total number of device assets migrated - ${toAdd.length}");
145+
146+
if (kDebugMode) {
147+
debugPrint(
148+
"[MIGRATION] Total number of device assets migrated - ${toAdd.length}",
149+
);
150+
}
151+
107152
await db.writeTxn(() async {
108153
await db.deviceAssetEntitys.putAll(toAdd);
109154
});

0 commit comments

Comments
 (0)