Return unique files while returning unsynced assets

This commit is contained in:
Neeraj Gupta 2022-08-24 16:42:19 +05:30
parent 64efda4284
commit c6c955c889
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 24 additions and 23 deletions

View file

@ -90,7 +90,7 @@ Future<List<LocalPathAsset>> getAllLocalAssets() async {
return localPathAssets; return localPathAssets;
} }
Future<LocalUnSyncResult> getLocalUnsyncedFiles( Future<LocalUnSyncResult> getLocalUnSyncedFiles(
List<LocalPathAsset> assets, List<LocalPathAsset> assets,
// current set of assets available on device // current set of assets available on device
Set<String> existingIDs, // localIDs of files already imported in app Set<String> existingIDs, // localIDs of files already imported in app
@ -109,7 +109,7 @@ Future<LocalUnSyncResult> getLocalUnsyncedFiles(
return LocalUnSyncResult(); return LocalUnSyncResult();
} }
final unSyncedFiles = final unSyncedFiles =
await _convertToFiles(localUnSyncResult.localPathAssets, computer); await _convertToUniqueFilesFiles(localUnSyncResult.localPathAssets);
localUnSyncResult.uniqueLocalFiles = unSyncedFiles; localUnSyncResult.uniqueLocalFiles = unSyncedFiles;
return localUnSyncResult; return localUnSyncResult;
} }
@ -117,7 +117,7 @@ Future<LocalUnSyncResult> getLocalUnsyncedFiles(
// _getUnsyncedAssets performs following operation // _getUnsyncedAssets performs following operation
// Identify // Identify
LocalUnSyncResult _getUnsyncedAssets(Map<String, dynamic> args) { LocalUnSyncResult _getUnsyncedAssets(Map<String, dynamic> args) {
final List<LocalPathAsset> localPathAssets = args['assets']; final List<LocalPathAsset> onDeviceLocalPathAsset = args['assets'];
final Set<String> existingIDs = args['existingIDs']; final Set<String> existingIDs = args['existingIDs'];
final Set<String> invalidIDs = args['invalidIDs']; final Set<String> invalidIDs = args['invalidIDs'];
final Map<String, Set<String>> pathToLocalIDs = args['pathToLocalIDs']; final Map<String, Set<String>> pathToLocalIDs = args['pathToLocalIDs'];
@ -126,23 +126,24 @@ LocalUnSyncResult _getUnsyncedAssets(Map<String, dynamic> args) {
<String, Set<String>>{}; <String, Set<String>>{};
final List<LocalPathAsset> unsyncedAssets = []; final List<LocalPathAsset> unsyncedAssets = [];
for (final localPathAsset in localPathAssets) { for (final localPathAsset in onDeviceLocalPathAsset) {
String pathID = localPathAsset.pathID; String pathID = localPathAsset.pathID;
// Start identifying pathID to localID mapping changes which needs to be // Start identifying pathID to localID mapping changes which needs to be
// synced // synced
Set<String> existingPathToLocalIDs = pathToLocalIDs[pathID] ?? <String>{}; Set<String> candidateLocalIDsForRemoval =
pathToLocalIDs[pathID] ?? <String>{};
Set<String> missingLocalIDsInPath = <String>{}; Set<String> missingLocalIDsInPath = <String>{};
for (final String localID in localPathAsset.localIDs) { for (final String localID in localPathAsset.localIDs) {
if (existingPathToLocalIDs.contains(localID)) { if (candidateLocalIDsForRemoval.contains(localID)) {
// remove the localID after checking. Any pending existing ID indicates // remove the localID after checking. Any pending existing ID indicates
// the the local file was removed from the path. // the the local file was removed from the path.
existingPathToLocalIDs.remove(localID); candidateLocalIDsForRemoval.remove(localID);
} else { } else {
missingLocalIDsInPath.add(localID); missingLocalIDsInPath.add(localID);
} }
} }
if (existingPathToLocalIDs.isNotEmpty) { if (candidateLocalIDsForRemoval.isNotEmpty) {
removedPathToLocalIDs[pathID] = existingPathToLocalIDs; removedPathToLocalIDs[pathID] = candidateLocalIDsForRemoval;
} }
if (missingLocalIDsInPath.isNotEmpty) { if (missingLocalIDsInPath.isNotEmpty) {
newPathToLocalIDs[pathID] = missingLocalIDsInPath; newPathToLocalIDs[pathID] = missingLocalIDsInPath;
@ -151,8 +152,10 @@ LocalUnSyncResult _getUnsyncedAssets(Map<String, dynamic> args) {
localPathAsset.localIDs.removeAll(existingIDs); localPathAsset.localIDs.removeAll(existingIDs);
localPathAsset.localIDs.removeAll(invalidIDs); localPathAsset.localIDs.removeAll(invalidIDs);
if (localPathAsset.localIDs.isNotEmpty) {
unsyncedAssets.add(localPathAsset); unsyncedAssets.add(localPathAsset);
} }
}
return LocalUnSyncResult( return LocalUnSyncResult(
localPathAssets: unsyncedAssets, localPathAssets: unsyncedAssets,
newPathToLocalIDs: newPathToLocalIDs, newPathToLocalIDs: newPathToLocalIDs,
@ -160,24 +163,22 @@ LocalUnSyncResult _getUnsyncedAssets(Map<String, dynamic> args) {
); );
} }
Future<List<File>> _convertToFiles( Future<List<File>> _convertToUniqueFilesFiles(
List<LocalPathAsset> assets, List<LocalPathAsset> assets,
Computer computer,
) async { ) async {
final Map<String, AssetEntity> assetIDToEntityMap = {}; final Set<String> alreadySeenLocalIDs = <String>{};
final List<File> files = []; final List<File> files = [];
for (LocalPathAsset localPathAsset in assets) { for (LocalPathAsset localPathAsset in assets) {
String localPathName = localPathAsset.pathName;
String pathID = localPathAsset.pathID;
for (final String localID in localPathAsset.localIDs) { for (final String localID in localPathAsset.localIDs) {
if (!assetIDToEntityMap.containsKey(localID)) { if (!alreadySeenLocalIDs.contains(localID)) {
assetIDToEntityMap[localID] = await AssetEntity.fromId(localID); var assetEntity = await AssetEntity.fromId(localID);
}
files.add( files.add(
File.fromAsset( File.fromAsset(localPathName, assetEntity, devicePathID: pathID),
localPathAsset.pathName,
assetIDToEntityMap[localID],
devicePathID: localPathAsset.pathID,
),
); );
alreadySeenLocalIDs.add(localID);
}
} }
} }
return files; return files;

View file

@ -157,7 +157,7 @@ class LocalSyncService {
final Map<String, Set<String>> pathToLocalIDs = final Map<String, Set<String>> pathToLocalIDs =
await _db.getDevicePathIDToLocalIDMap(); await _db.getDevicePathIDToLocalIDMap();
final invalidIDs = _getInvalidFileIDs().toSet(); final invalidIDs = _getInvalidFileIDs().toSet();
final localUnSyncResult = await getLocalUnsyncedFiles( final localUnSyncResult = await getLocalUnSyncedFiles(
localAssets, localAssets,
existingLocalFileIDs, existingLocalFileIDs,
pathToLocalIDs, pathToLocalIDs,