Convert fetched fileIDs to File objects

This commit is contained in:
vishnukvmd 2021-09-14 23:58:14 +05:30
parent dcab8b8028
commit f38259bae9
3 changed files with 63 additions and 17 deletions

View file

@ -840,6 +840,25 @@ class FilesDB {
return rows.isNotEmpty;
}
Future<Map<int, File>> getFilesFromIDs(List<int> ids) async {
String inParam = "";
for (final id in ids) {
inParam += "'" + id.toString() + "',";
}
inParam = inParam.substring(0, inParam.length - 1);
final db = await instance.database;
final results = await db.query(
table,
where: '$columnUploadedFileID IN ($inParam)',
);
final files = _convertToFiles(results);
final result = <int, File>{};
for (final file in files) {
result[file.uploadedFileID] = file;
}
return result;
}
List<File> _convertToFiles(List<Map<String, dynamic>> results) {
final List<File> files = [];
for (final result in results) {

View file

@ -1,38 +1,50 @@
import 'dart:convert';
class DuplicateFiles {
final List<Duplicates> duplicates;
DuplicateFiles(this.duplicates);
import 'package:photos/models/file.dart';
factory DuplicateFiles.fromMap(Map<String, dynamic> map) {
return DuplicateFiles(
List<Duplicates>.from(
map['duplicates']?.map((x) => Duplicates.fromMap(x))),
class DuplicateFilesResponse {
final List<DuplicateItems> duplicates;
DuplicateFilesResponse(this.duplicates);
factory DuplicateFilesResponse.fromMap(Map<String, dynamic> map) {
return DuplicateFilesResponse(
List<DuplicateItems>.from(
map['duplicates']?.map((x) => DuplicateItems.fromMap(x))),
);
}
factory DuplicateFiles.fromJson(String source) =>
DuplicateFiles.fromMap(json.decode(source));
factory DuplicateFilesResponse.fromJson(String source) =>
DuplicateFilesResponse.fromMap(json.decode(source));
@override
String toString() => 'DuplicateFiles(duplicates: $duplicates)';
}
class Duplicates {
class DuplicateItems {
final List<int> fileIDs;
final int size;
Duplicates(this.fileIDs, this.size);
DuplicateItems(this.fileIDs, this.size);
factory Duplicates.fromMap(Map<String, dynamic> map) {
return Duplicates(
factory DuplicateItems.fromMap(Map<String, dynamic> map) {
return DuplicateItems(
List<int>.from(map['fileIDs']),
map['size'],
);
}
factory Duplicates.fromJson(String source) =>
Duplicates.fromMap(json.decode(source));
factory DuplicateItems.fromJson(String source) =>
DuplicateItems.fromMap(json.decode(source));
@override
String toString() => 'Duplicates(fileIDs: $fileIDs, size: $size)';
}
class DuplicateFiles {
final List<File> files;
final int size;
DuplicateFiles(this.files, this.size);
@override
String toString() => 'DuplicateFiles(files: $files, size: $size)';
}

View file

@ -17,6 +17,7 @@ import 'package:photos/events/sync_status_update_event.dart';
import 'package:photos/events/trigger_logout_event.dart';
import 'package:photos/models/backup_status.dart';
import 'package:photos/models/duplicate_files.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/services/local_sync_service.dart';
import 'package:photos/services/notification_service.dart';
@ -184,7 +185,7 @@ class SyncService {
);
}
Future<DuplicateFiles> getDuplicateFiles() async {
Future<List<DuplicateFiles>> getDuplicateFiles() async {
try {
final response = await _dio.get(
Configuration.instance.getHttpEndpoint() + "/files/duplicates",
@ -194,7 +195,21 @@ class SyncService {
},
),
);
return DuplicateFiles.fromMap(response.data);
final dupes = DuplicateFilesResponse.fromMap(response.data);
final ids = <int>[];
for (final dupe in dupes.duplicates) {
ids.addAll(dupe.fileIDs);
}
final fileMap = await FilesDB.instance.getFilesFromIDs(ids);
final result = <DuplicateFiles>[];
for (final dupe in dupes.duplicates) {
final files = <File>[];
for (final id in dupe.fileIDs) {
files.add(fileMap[id]);
}
result.add(DuplicateFiles(files, dupe.size));
}
return result;
} catch (e) {
_logger.severe(e);
rethrow;