Only show deviceFolder on UI if coverFile is found

This commit is contained in:
Neeraj Gupta 2022-09-01 01:20:11 +05:30
parent 718fcdf6cb
commit decbfe0145
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
3 changed files with 31 additions and 13 deletions

View file

@ -3,6 +3,7 @@ import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/models/device_folder.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_load_result.dart';
import 'package:photos/services/local/local_sync_util.dart';
import 'package:sqflite/sqlite_api.dart';
@ -283,15 +284,22 @@ extension DeviceFiles on FilesDB {
return FileLoadResult(files, files.length == limit);
}
Future<List<DeviceCollection>> getDeviceCollections() async {
debugPrint("Fetching DeviceCollections From DB");
Future<List<DeviceCollection>> getDeviceCollections({
bool includeCoverThumbnail = false,
}) async {
debugPrint(
"Fetching DeviceCollections From DB with thumnail = $includeCoverThumbnail");
try {
final db = await database;
final fileRows = await db.rawQuery(
'''SELECT * FROM FILES where local_id in (select cover_id from device_collections) group by local_id;
final coverFiles = <File>[];
if (includeCoverThumbnail) {
final fileRows = await db.rawQuery(
'''SELECT * FROM FILES where local_id in (select cover_id from device_collections) group by local_id;
''',
);
final files = convertToFiles(fileRows);
);
final files = convertToFiles(fileRows);
coverFiles.addAll(files);
}
final deviceCollectionRows = await db.rawQuery(
'''SELECT * from device_collections''',
);
@ -305,11 +313,19 @@ extension DeviceFiles on FilesDB {
coverId: row["cover_id"],
shouldBackup: (row["should_backup"] ?? _sqlBoolFalse) == _sqlBoolTrue,
);
deviceCollection.thumbnail = files.firstWhere(
(element) => element.localID == deviceCollection.coverId,
orElse: () => null,
);
deviceCollections.add(deviceCollection);
if (includeCoverThumbnail) {
deviceCollection.thumbnail = coverFiles.firstWhere(
(element) => element.localID == deviceCollection.coverId,
orElse: () => null,
);
if (deviceCollection.thumbnail == null) {
_logger.warning(
'Failed to find coverThum for ${deviceCollection.name}');
continue;
}
} else {
deviceCollections.add(deviceCollection);
}
}
return deviceCollections;
} catch (e) {

View file

@ -41,7 +41,9 @@ class _BackupFolderSelectionPageState extends State<BackupFolderSelectionPage> {
@override
void initState() {
FilesDB.instance.getDeviceCollections().then((files) async {
FilesDB.instance
.getDeviceCollections(includeCoverThumbnail: true)
.then((files) async {
_pathIDToItemCount =
await FilesDB.instance.getDevicePathIDToImportedFileCount();
setState(() {

View file

@ -93,7 +93,7 @@ class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget>
final userID = Configuration.instance.getUserID();
final List<DeviceFolder> folders = [];
final List<DeviceCollection> deviceCollections =
await filesDB.getDeviceCollections();
await filesDB.getDeviceCollections(includeCoverThumbnail: true);
final latestLocalFiles = await filesDB.getLatestLocalFiles();
for (final file in latestLocalFiles) {
folders.add(DeviceFolder(file.deviceFolder, file.deviceFolder, file));