3
3
import 'dart:async' ;
4
4
import 'dart:io' ;
5
5
6
- import 'package:flutter/widgets .dart' ;
6
+ import 'package:flutter/foundation .dart' ;
7
7
import 'package:immich_mobile/domain/models/store.model.dart' ;
8
8
import 'package:immich_mobile/entities/album.entity.dart' ;
9
9
import 'package:immich_mobile/entities/android_device_asset.entity.dart' ;
@@ -17,6 +17,8 @@ import 'package:immich_mobile/infrastructure/entities/store.entity.dart';
17
17
import 'package:immich_mobile/infrastructure/entities/user.entity.dart' ;
18
18
import 'package:immich_mobile/utils/diff.dart' ;
19
19
import 'package:isar/isar.dart' ;
20
+ // ignore: import_rule_photo_manager
21
+ import 'package:photo_manager/photo_manager.dart' ;
20
22
21
23
const int targetVersion = 10 ;
22
24
@@ -69,14 +71,45 @@ Future<void> _migrateDeviceAsset(Isar db) async {
69
71
: (await db.iOSDeviceAssets.where ().findAll ())
70
72
.map ((i) => _DeviceAsset (assetId: i.id, hash: i.hash))
71
73
.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 }" );
80
113
ids.sort ((a, b) => a.assetId.compareTo (b.assetId));
81
114
localAssets.sort ((a, b) => a.assetId.compareTo (b.assetId));
82
115
final List <DeviceAssetEntity > toAdd = [];
@@ -95,15 +128,27 @@ Future<void> _migrateDeviceAsset(Isar db) async {
95
128
return false ;
96
129
},
97
130
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
+ }
101
136
},
102
137
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
+ }
104
143
},
105
144
);
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
+
107
152
await db.writeTxn (() async {
108
153
await db.deviceAssetEntitys.putAll (toAdd);
109
154
});
0 commit comments