From 6c343bf2ed4303da742748a03d5c832eab940f4d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 17 Jun 2024 08:50:30 -0700 Subject: [PATCH] combine album name --- .../models/backup/backup_candidate.model.dart | 14 ++++++--- .../lib/providers/backup/backup.provider.dart | 30 +++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/mobile/lib/models/backup/backup_candidate.model.dart b/mobile/lib/models/backup/backup_candidate.model.dart index cbbc3e7961..0932959088 100644 --- a/mobile/lib/models/backup/backup_candidate.model.dart +++ b/mobile/lib/models/backup/backup_candidate.model.dart @@ -3,19 +3,23 @@ import 'package:photo_manager/photo_manager.dart'; class BackupCandidate { - final String albumName; + final String id; + final List albumName; final AssetEntity asset; BackupCandidate({ + required this.id, required this.albumName, required this.asset, }); BackupCandidate copyWith({ - String? albumName, + String? id, + List? albumName, AssetEntity? asset, }) { return BackupCandidate( + id: id ?? this.id, albumName: albumName ?? this.albumName, asset: asset ?? this.asset, ); @@ -28,9 +32,11 @@ class BackupCandidate { bool operator ==(covariant BackupCandidate other) { if (identical(this, other)) return true; - return other.albumName == albumName && other.asset == asset; + return other.id == id && + other.albumName == albumName && + other.asset == asset; } @override - int get hashCode => albumName.hashCode ^ asset.hashCode; + int get hashCode => id.hashCode ^ albumName.hashCode ^ asset.hashCode; } diff --git a/mobile/lib/providers/backup/backup.provider.dart b/mobile/lib/providers/backup/backup.provider.dart index 315cb53571..37432cf24e 100644 --- a/mobile/lib/providers/backup/backup.provider.dart +++ b/mobile/lib/providers/backup/backup.provider.dart @@ -295,6 +295,7 @@ class BackupNotifier extends StateNotifier { final Set assetsFromSelectedAlbums = {}; final Set assetsFromExcludedAlbums = {}; + /// Extracing assets from selected albums for (final album in state.selectedBackupAlbums) { final assetCount = await album.albumEntity.assetCountAsync; @@ -308,12 +309,17 @@ class BackupNotifier extends StateNotifier { ); final candidate = assets.map( - (e) => BackupCandidate(albumName: album.albumEntity.name, asset: e), + (e) => BackupCandidate( + id: e.id, + albumName: [album.albumEntity.name], + asset: e, + ), ); assetsFromSelectedAlbums.addAll(candidate.toSet()); } + /// Extracing assets from excluded albums for (final album in state.excludedBackupAlbums) { final assetCount = await album.albumEntity.assetCountAsync; @@ -327,16 +333,20 @@ class BackupNotifier extends StateNotifier { ); final candidate = assets.map( - (e) => BackupCandidate(albumName: album.albumEntity.name, asset: e), + (e) => BackupCandidate( + id: e.id, + albumName: [album.albumEntity.name], + asset: e, + ), ); assetsFromExcludedAlbums.addAll(candidate); } - final Set allUniqueAssets = + Set allUniqueAssets = assetsFromSelectedAlbums.difference(assetsFromExcludedAlbums); - final allAssetsInDatabase = await _backupService.getDeviceBackupAsset(); + final allAssetsInDatabase = await _backupService.getDeviceBackupAsset(); if (allAssetsInDatabase == null) { return; } @@ -353,6 +363,16 @@ class BackupNotifier extends StateNotifier { (e) => duplicatedAssetIds.contains(e.asset.id), ); + /// Merge different album name of the same id + allUniqueAssets = allUniqueAssets.map((e) { + final List albumNames = allUniqueAssets + .where((a) => a.id == e.id) + .map((a) => a.albumName) + .expand((e) => e) + .toList(); + return e.copyWith(albumName: albumNames); + }).toSet(); + if (allUniqueAssets.isEmpty) { log.info("No assets are selected for back up"); state = state.copyWith( @@ -372,7 +392,7 @@ class BackupNotifier extends StateNotifier { // Save to persistent storage await _updatePersistentAlbumsSelection(); - debugPrint("backup asset $allUniqueAssets", wrapWidth: 80); + debugPrint("backup asset ${allUniqueAssets.length}", wrapWidth: 80); } /// Get all necessary information for calculating the available albums,