diff --git a/lib/db/files_db.dart b/lib/db/files_db.dart index 4d2ef368d..46e609b2a 100644 --- a/lib/db/files_db.dart +++ b/lib/db/files_db.dart @@ -461,6 +461,7 @@ class FilesDB { final rows = await db.query( table, columns: [columnDeviceFolder], + where: '$columnLocalID IS NOT NULL', distinct: true, ); List result = List(); @@ -486,6 +487,22 @@ class FilesDB { } } + Future getLastCreatedFileInPath(String path) async { + final db = await instance.database; + final rows = await db.query( + table, + where: '$columnDeviceFolder = ? AND $columnIsDeleted = 0', + whereArgs: [path], + orderBy: '$columnCreationTime DESC', + limit: 1, + ); + if (rows.isNotEmpty) { + return _getFileFromRow(rows[0]); + } else { + return null; + } + } + Future getLastModifiedFileInCollection(int collectionID) async { final db = await instance.database; final rows = await db.query( diff --git a/lib/ui/collections_gallery_widget.dart b/lib/ui/collections_gallery_widget.dart index 9fb25a0db..742f6a119 100644 --- a/lib/ui/collections_gallery_widget.dart +++ b/lib/ui/collections_gallery_widget.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -110,26 +109,17 @@ class _CollectionsGalleryWidgetState extends State Future _getCollections() async { final filesDB = FilesDB.instance; - final filesRepo = FileRepository.instance; final collectionsService = CollectionsService.instance; final userID = Configuration.instance.getUserID(); final folders = List(); - final files = filesRepo.hasLoadedFiles - ? filesRepo.files - : await filesRepo.loadFiles(); - final thumbnailPathMap = Map(); - for (final file in files) { - final path = file.deviceFolder; - if (file.localID != null) { - if (thumbnailPathMap[path] == null) { - thumbnailPathMap[path] = file; - } - } - } - for (final path in thumbnailPathMap.keys) { + final paths = await filesDB.getLocalPaths(); + for (final path in paths) { final folderName = p.basename(path); - folders.add(DeviceFolder(folderName, path, thumbnailPathMap[path])); + folders.add(DeviceFolder( + folderName, path, await filesDB.getLastCreatedFileInPath(path))); } + folders.sort((first, second) => + second.thumbnail.creationTime.compareTo(first.thumbnail.creationTime)); final collectionsWithThumbnail = List(); final collections = collectionsService.getCollections(); final ownedCollectionIDs = List();