Преглед на файлове

Merge pull request #408 from ente-io/refactor_part_1

Add base EnteFile and use cacheKey for thumbnailCache
Vishnu Mohandas преди 3 години
родител
ревизия
a128d0d50f
променени са 3 файла, в които са добавени 32 реда и са изтрити 9 реда
  1. 8 8
      lib/core/cache/thumbnail_cache.dart
  2. 11 0
      lib/models/ente_file.dart
  3. 13 1
      lib/models/file.dart

+ 8 - 8
lib/core/cache/thumbnail_cache.dart

@@ -2,38 +2,38 @@ import 'dart:typed_data';
 
 import 'package:photos/core/cache/lru_map.dart';
 import 'package:photos/core/constants.dart';
-import 'package:photos/models/file.dart';
+import 'package:photos/models/ente_file.dart';
 
 class ThumbnailLruCache {
   static final LRUMap<String, Uint8List> _map = LRUMap(1000);
 
-  static Uint8List get(File photo, [int size]) {
+  static Uint8List get(EnteFile enteFile, [int size]) {
     return _map.get(
-      photo.generatedID.toString() +
+      enteFile.cacheKey() +
           "_" +
           (size != null ? size.toString() : kThumbnailLargeSize.toString()),
     );
   }
 
   static void put(
-    File photo,
+    EnteFile enteFile,
     Uint8List imageData, [
     int size,
   ]) {
     _map.put(
-      photo.generatedID.toString() +
+      enteFile.cacheKey() +
           "_" +
           (size != null ? size.toString() : kThumbnailLargeSize.toString()),
       imageData,
     );
   }
 
-  static void clearCache(File file) {
+  static void clearCache(EnteFile enteFile) {
     _map.remove(
-      file.generatedID.toString() + "_" + kThumbnailLargeSize.toString(),
+      enteFile.cacheKey() + "_" + kThumbnailLargeSize.toString(),
     );
     _map.remove(
-      file.generatedID.toString() + "_" + kThumbnailSmallSize.toString(),
+      enteFile.cacheKey() + "_" + kThumbnailSmallSize.toString(),
     );
   }
 }

+ 11 - 0
lib/models/ente_file.dart

@@ -0,0 +1,11 @@
+// EnteFile is base file entry for various type of files
+// like DeviceFile,RemoteFile or TrashedFile
+abstract class EnteFile {
+  // returns cacheKey which should be used while caching entry related to
+  // this file.
+  String cacheKey();
+
+  // returns localIdentifier for the file on the host OS.
+  // Can be null if the file only exist on remote
+  String localIdentifier();
+}

+ 13 - 1
lib/models/file.dart

@@ -6,6 +6,7 @@ import 'package:path/path.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/constants.dart';
+import 'package:photos/models/ente_file.dart';
 import 'package:photos/models/file_type.dart';
 import 'package:photos/models/location.dart';
 import 'package:photos/models/magic_metadata.dart';
@@ -13,7 +14,7 @@ import 'package:photos/services/feature_flag_service.dart';
 import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/exif_util.dart';
 
-class File {
+class File extends EnteFile {
   int generatedID;
   int uploadedFileID;
   int ownerID;
@@ -262,4 +263,15 @@ class File {
         ":generated_" +
         generatedID.toString();
   }
+
+  @override
+  String cacheKey() {
+    // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
+    return localID ?? uploadedFileID?.toString() ?? generatedID?.toString();
+  }
+
+  @override
+  String localIdentifier() {
+    return localID;
+  }
 }