Quellcode durchsuchen

Preload the next memory

Vishnu Mohandas vor 5 Jahren
Ursprung
Commit
590c72f2e5
3 geänderte Dateien mit 29 neuen und 22 gelöschten Zeilen
  1. 2 22
      lib/ui/detail_page.dart
  2. 4 0
      lib/ui/memories_widget.dart
  3. 23 0
      lib/utils/file_util.dart

+ 2 - 22
lib/ui/detail_page.dart

@@ -106,30 +106,10 @@ class _DetailPageState extends State<DetailPage> {
 
   void _preloadFiles(int index) {
     if (index > 0) {
-      _preloadFile(_files[index - 1]);
+      preloadFile(_files[index - 1]);
     }
     if (index < _files.length - 1) {
-      _preloadFile(_files[index + 1]);
-    }
-  }
-
-  void _preloadFile(File file) {
-    if (file.fileType == FileType.video) {
-      return;
-    }
-    if (file.localId == null) {
-      file.getBytes().then((data) {
-        BytesLruCache.put(file, data);
-      });
-    } else {
-      final cachedFile = FileLruCache.get(file);
-      if (cachedFile == null) {
-        file.getAsset().then((asset) {
-          asset.file.then((assetFile) {
-            FileLruCache.put(file, assetFile);
-          });
-        });
-      }
+      preloadFile(_files[index + 1]);
     }
   }
 

+ 4 - 0
lib/ui/memories_widget.dart

@@ -8,6 +8,7 @@ import 'package:photos/ui/thumbnail_widget.dart';
 import 'package:photos/ui/video_widget.dart';
 import 'package:photos/ui/zoomable_image.dart';
 import 'package:photos/utils/date_time_util.dart';
+import 'package:photos/utils/file_util.dart';
 import 'package:photos/utils/share_util.dart';
 
 class MemoriesWidget extends StatefulWidget {
@@ -283,6 +284,9 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
         setState(() {
           _index = index;
         });
+        if (index < widget.memories.length - 1) {
+          preloadFile(widget.memories[index + 1].file);
+        }
         return index;
       },
     );

+ 23 - 0
lib/utils/file_util.dart

@@ -1,6 +1,8 @@
 import 'package:photo_manager/photo_manager.dart';
+import 'package:photos/core/cache/image_cache.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/models/file.dart';
+import 'package:photos/models/file_type.dart';
 
 Future<void> deleteFiles(List<File> files,
     {bool deleteEveryWhere = false}) async {
@@ -12,3 +14,24 @@ Future<void> deleteFiles(List<File> files,
         : await FilesDB.instance.delete(file);
   }
 }
+
+void preloadFile(File file) {
+  if (file.fileType == FileType.video) {
+    return;
+  }
+  if (file.localId == null) {
+    if (BytesLruCache.get(file) == null) {
+      file.getBytes().then((data) {
+        BytesLruCache.put(file, data);
+      });
+    }
+  } else {
+    if (FileLruCache.get(file) == null) {
+      file.getAsset().then((asset) {
+        asset.file.then((assetFile) {
+          FileLruCache.put(file, assetFile);
+        });
+      });
+    }
+  }
+}