瀏覽代碼

Fix thumbnail issue within device folder section

Vishnu Mohandas 4 年之前
父節點
當前提交
f17d7027c8
共有 2 個文件被更改,包括 23 次插入16 次删除
  1. 17 0
      lib/db/files_db.dart
  2. 6 16
      lib/ui/collections_gallery_widget.dart

+ 17 - 0
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<String> result = List<String>();
@@ -486,6 +487,22 @@ class FilesDB {
     }
   }
 
+  Future<File> 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<File> getLastModifiedFileInCollection(int collectionID) async {
     final db = await instance.database;
     final rows = await db.query(

+ 6 - 16
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<CollectionsGalleryWidget>
 
   Future<CollectionItems> _getCollections() async {
     final filesDB = FilesDB.instance;
-    final filesRepo = FileRepository.instance;
     final collectionsService = CollectionsService.instance;
     final userID = Configuration.instance.getUserID();
     final folders = List<DeviceFolder>();
-    final files = filesRepo.hasLoadedFiles
-        ? filesRepo.files
-        : await filesRepo.loadFiles();
-    final thumbnailPathMap = Map<String, File>();
-    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<CollectionWithThumbnail>();
     final collections = collectionsService.getCollections();
     final ownedCollectionIDs = List<int>();