diff --git a/mobile/apps/photos/lib/db/local/mappers.dart b/mobile/apps/photos/lib/db/local/mappers.dart index 16c4d8fec0..d17cf11ba4 100644 --- a/mobile/apps/photos/lib/db/local/mappers.dart +++ b/mobile/apps/photos/lib/db/local/mappers.dart @@ -39,8 +39,8 @@ class LocalDBMappers { isFavorite: (row['is_fav'] as int) == 1, title: row['title'] as String?, relativePath: row['relative_path'] as String?, - createDateSecond: (row['created_at'] as int), - modifiedDateSecond: (row['modified_at'] as int), + createDateSecond: (row['created_at'] as int) ~/ 1000000, + modifiedDateSecond: (row['modified_at'] as int) ~/ 1000000, mimeType: row['mime_type'] as String?, latitude: row['latitude'] as double?, longitude: row['longitude'] as double?, diff --git a/mobile/apps/photos/lib/db/local/schema.dart b/mobile/apps/photos/lib/db/local/schema.dart index a215a084bc..9afbf6a551 100644 --- a/mobile/apps/photos/lib/db/local/schema.dart +++ b/mobile/apps/photos/lib/db/local/schema.dart @@ -39,11 +39,13 @@ const String deviceCollectionWithOneAssetQuery = ''' WITH latest_per_path AS ( SELECT dpa.path_id, - MAX(a.created_at) as max_created + MAX(a.created_at) as max_created, + count(*) as asset_count FROM device_path_assets dpa JOIN assets a ON dpa.asset_id = a.id + GROUP BY dpa.path_id ), @@ -51,7 +53,8 @@ ranked_assets AS ( SELECT dpa.path_id, a.*, - ROW_NUMBER() OVER (PARTITION BY dpa.path_id ORDER BY a.id) as rn + ROW_NUMBER() OVER (PARTITION BY dpa.path_id ORDER BY a.id) as rn, + lpp.asset_count FROM device_path_assets dpa JOIN @@ -61,11 +64,14 @@ ranked_assets AS ( ) SELECT dp.*, - ra.* + ra.*, + pc.* FROM device_path dp JOIN ranked_assets ra ON dp.path_id = ra.path_id AND ra.rn = 1 +LEFT JOIN path_backup_config pc + on dp.path_id = pc.device_path_id '''; class LocalAssertsParam { diff --git a/mobile/apps/photos/lib/db/local/table/device_albums.dart b/mobile/apps/photos/lib/db/local/table/device_albums.dart new file mode 100644 index 0000000000..993001efeb --- /dev/null +++ b/mobile/apps/photos/lib/db/local/table/device_albums.dart @@ -0,0 +1,33 @@ +import "package:photo_manager/photo_manager.dart"; +import "package:photos/db/local/db.dart"; +import "package:photos/db/local/mappers.dart"; +import "package:photos/db/local/schema.dart"; +import "package:photos/models/device_collection.dart"; +import "package:photos/models/file/file.dart"; +import "package:photos/models/upload_strategy.dart"; + +extension DeviceAlbums on LocalDB { + Future> getDeviceCollections() async { + final List collections = []; + final rows = await sqliteDB.getAll(deviceCollectionWithOneAssetQuery); + for (final row in rows) { + final path = LocalDBMappers.assetPath(row); + AssetEntity? asset; + if (row['id'] != null) { + asset = LocalDBMappers.asset(row); + } + + collections.add( + DeviceCollection( + path, + count: row['asset_count'] as int, + thumbnail: asset != null ? EnteFile.fromAssetSync(asset) : null, + shouldBackup: (row['should_backup'] as int) == 1, + uploadStrategy: UploadStrategy.values[row['upload_strategy'] as int], + ), + ); + } + + return collections; + } +} diff --git a/mobile/apps/photos/lib/models/device_collection.dart b/mobile/apps/photos/lib/models/device_collection.dart index 898d118074..3d3d019fd2 100644 --- a/mobile/apps/photos/lib/models/device_collection.dart +++ b/mobile/apps/photos/lib/models/device_collection.dart @@ -1,13 +1,12 @@ +import "package:photo_manager/photo_manager.dart"; import 'package:photos/models/file/file.dart'; import 'package:photos/models/upload_strategy.dart'; class DeviceCollection { - final String id; - final String name; + AssetPathEntity assetPathEntity; final int count; final bool shouldBackup; UploadStrategy uploadStrategy; - final String? coverId; int? collectionID; EnteFile? thumbnail; @@ -15,10 +14,16 @@ class DeviceCollection { return collectionID != null && collectionID! != -1; } + String get name { + return assetPathEntity.name; + } + + String get id { + return assetPathEntity.id; + } + DeviceCollection( - this.id, - this.name, { - this.coverId, + this.assetPathEntity, { this.count = 0, this.collectionID, this.thumbnail, diff --git a/mobile/apps/photos/lib/services/local/device_albums.dart b/mobile/apps/photos/lib/services/local/device_albums.dart index 9d3916ac09..69f1cc8f8b 100644 --- a/mobile/apps/photos/lib/services/local/device_albums.dart +++ b/mobile/apps/photos/lib/services/local/device_albums.dart @@ -1,3 +1,4 @@ +import "package:photos/db/local/table/device_albums.dart"; import "package:photos/models/device_collection.dart"; import "package:photos/models/file/file.dart"; import "package:photos/service_locator.dart"; @@ -5,23 +6,7 @@ import "package:photos/services/local/import/local_import.dart"; extension DeviceAlbums on LocalImportService { Future> getDeviceCollections() async { - final cache = await getLocalAssetsCache(); - final pathToLatestAsset = cache.getPathToLatestAsset(); - final List collections = []; - for (final path in cache.assetPaths.values) { - final asset = pathToLatestAsset[path.id]; - if (asset != null) { - collections.add( - DeviceCollection( - path.id, - path.name, - count: cache.pathToAssetIDs[path.id]?.length ?? 0, - thumbnail: asset, - ), - ); - } - } - return collections; + return localDB.getDeviceCollections(); } Future> getAlbumFiles(String pathID) async { diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/device_folder_page.dart b/mobile/apps/photos/lib/ui/viewer/gallery/device_folder_page.dart index 9b89472670..da1838cadf 100644 --- a/mobile/apps/photos/lib/ui/viewer/gallery/device_folder_page.dart +++ b/mobile/apps/photos/lib/ui/viewer/gallery/device_folder_page.dart @@ -38,7 +38,7 @@ class DeviceFolderPage extends StatelessWidget { final gallery = Gallery( asyncLoader: (creationStartTime, creationEndTime, {limit, asc}) async { final files = await localDB.getPathAssets( - deviceCollection.id, + deviceCollection.assetPathEntity.id, params: LocalAssertsParam( limit: limit, isAsc: asc ?? false, @@ -56,7 +56,7 @@ class DeviceFolderPage extends StatelessWidget { EventType.deletedFromEverywhere, EventType.hide, }, - tagPrefix: "device_folder:" + deviceCollection.name, + tagPrefix: "device_folder:" + deviceCollection.assetPathEntity.name, selectedFiles: _selectedFiles, header: Configuration.instance.hasConfiguredAccount() ? BackupHeaderWidget(deviceCollection) @@ -69,7 +69,7 @@ class DeviceFolderPage extends StatelessWidget { preferredSize: const Size.fromHeight(50.0), child: GalleryAppBarWidget( GalleryType.localFolder, - deviceCollection.name, + deviceCollection.assetPathEntity.name, _selectedFiles, deviceCollection: deviceCollection, ),