ソースを参照

[fix]新增对文件大小进行缓存

云天逵 11 ヶ月 前
コミット
db2336e590

+ 53 - 13
lib/base/base_photo_controller.dart

@@ -30,24 +30,38 @@ abstract class BasePhotoController extends BaseController {
   }
 
   // 获取选中的文件大小
-  Future<void> getSelectedFilesSize() async {
+  Future<void> updateSelectedFilesSize() async {
+    // 如果没有选中的文件,直接返回
+    if (selectedFileCount.value == 0) {
+      selectedFilesSize.value = 0;
+      return;
+    }
     double totalSize = 0;
     for (var group in photoGroups) {
       for (int i = 0; i < group.images.length; i++) {
         if (group.selectedImages[i]) {
-          final file = await group.images[i].file;
-          if (file != null) {
-            totalSize += file.lengthSync();
+          // 检查缓存
+          final assetId = group.images[i].id;
+          if (ImagePickerUtil.fileSizeCache.containsKey(assetId)) {
+            totalSize += ImagePickerUtil.fileSizeCache[assetId]!;
+          } else {
+            final file = await group.images[i].file;
+            if (file != null) {
+              final size = (await file.length()) / 1024; // 转换为KB
+              totalSize += size;
+              ImagePickerUtil.fileSizeCache[assetId] = size;
+            }
           }
         }
       }
     }
-    selectedFilesSize.value = totalSize / 1024; // Convert to KB
+    selectedFilesSize.value = totalSize;
     PhotoManager.clearFileCache();
   }
 
   // 切换图片选中状态
-  void toggleImageSelection(List<AssetEntity> groupTitle, int imageIndex) {
+  Future<void> toggleImageSelection(
+      List<AssetEntity> groupTitle, int imageIndex) async {
     print("BasePhotoController toggleImageSelection");
     final group = getGroupByImages(groupTitle);
     final image = group.images[imageIndex];
@@ -59,7 +73,31 @@ abstract class BasePhotoController extends BaseController {
     group.isSelected.value = group.selectedImages.every((selected) => selected);
 
     selectedFileCount.value = selectedPhotosIds.length;
-    getSelectedFilesSize();
+
+    // 更新文件大小
+    if (selected) {
+      // 如果选中,增加文件大小
+      if (ImagePickerUtil.fileSizeCache.containsKey(image.id)) {
+        selectedFilesSize.value += ImagePickerUtil.fileSizeCache[image.id]!;
+      } else {
+        final file = await image.file;
+        if (file != null) {
+          selectedFilesSize.value += (await file.length()) / 1024; // 转换为KB
+        }
+      }
+    } else {
+      // 如果取消选中,减少文件大小
+      if (ImagePickerUtil.fileSizeCache.containsKey(image.id)) {
+        selectedFilesSize.value -= ImagePickerUtil.fileSizeCache[image.id]!;
+      } else {
+        final file = await image.file;
+        if (file != null) {
+          selectedFilesSize.value -= (await file.length()) / 1024; // 转换为KB
+        }
+      }
+    }
+
+    PhotoManager.clearFileCache();
   }
 
   void clickImage(List<AssetEntity> images, int imageIndex, PhotosType type) {
@@ -71,8 +109,6 @@ abstract class BasePhotoController extends BaseController {
 
   // 切换图片组选中状态
   void toggleGroupSelection(List<AssetEntity> imagesList) {
-
-
     final group = getGroupByImages(imagesList);
     final newValue = !group.isSelected.value;
     group.toggleSelectAll(newValue);
@@ -81,7 +117,7 @@ abstract class BasePhotoController extends BaseController {
       updateSelectedPhotosIds(image.id, newValue);
     }
     selectedFileCount.value = selectedPhotosIds.length;
-    getSelectedFilesSize();
+    updateSelectedFilesSize();
   }
 
   PhotoGroup getGroupByImages(List<AssetEntity> images) {
@@ -113,7 +149,7 @@ abstract class BasePhotoController extends BaseController {
       selectedFilesSize.value = 0;
       return;
     }
-    await getSelectedFilesSize();
+    await updateSelectedFilesSize();
   }
 
   @override
@@ -150,7 +186,6 @@ abstract class BasePhotoController extends BaseController {
 
           selectedPhotosIds.clear();
 
-
           ToastUtil.show("Delete success");
           Future.delayed(Duration(seconds: 2), () {
             SmartDialog.dismiss(tag: 'photoDeletingDialog');
@@ -159,7 +194,6 @@ abstract class BasePhotoController extends BaseController {
         } else {
           SmartDialog.dismiss(tag: 'photoDeletingDialog');
           ToastUtil.show("Delete failed");
-
         }
       }
     } else {
@@ -203,4 +237,10 @@ abstract class BasePhotoController extends BaseController {
   List<AssetEntity> getAllPhotos() {
     return photoGroups.expand((group) => group.images).toList();
   }
+
+  @override
+  void onClose() {
+    super.onClose();
+    PhotoManager.clearFileCache();
+  }
 }

+ 2 - 7
lib/module/home/home_controller.dart

@@ -86,15 +86,10 @@ class HomeController extends BaseController {
         ImagePickerUtil.locationPhotos.isEmpty ||
         ImagePickerUtil.screenshotPhotos.isEmpty) {
       try {
-        final List<AssetEntity>? result = await AssetPicker.pickAssets(
-          Get.context!,
-        );
+        final List<AssetEntity> result = await ImagePickerUtil.loadAssetsAndroid();
         ImagePickerUtil.peoplePhotos.value = result ?? [];
 
-        if (result == null) {
-        } else {
-          ImagePickerUtil.locationPhotos['location'] = result ?? [];
-        }
+        ImagePickerUtil.locationPhotos['location'] = result ?? [];
 
         ImagePickerUtil.screenshotPhotos.value = result ?? [];
 

+ 33 - 0
lib/module/image_picker/image_picker_util.dart

@@ -51,6 +51,9 @@ class ImagePickerUtil {
   static final Rx<int> peopleSize = 0.obs;
   static final Rx<int> similarPhotosSize = 0.obs;
 
+  // 用来缓存文件大小
+  static final Map<String, double> fileSizeCache = {};
+
   // 清除所有照片数据
   static void clearAllPhotos() {
     screenshotPhotos.clear();
@@ -273,4 +276,34 @@ class ImagePickerUtil {
       return '${(sizeInGB / 1024).toStringAsFixed(1)} ';
     }
   }
+
+
+  static Future<List<AssetEntity>> loadAssetsAndroid({bool sortByDate = true}) async {
+    final PermissionState result = await PhotoManager.requestPermissionExtend();
+    if (!result.isAuth) return [];
+
+    // 选择相册
+    final List<AssetPathEntity> albums = await PhotoManager.getAssetPathList(
+      type: RequestType.image,
+      filterOption: FilterOptionGroup(
+        orders: [
+          // 根据创建日期排序,降序(最新的在前)
+          OrderOption(
+            type: OrderOptionType.createDate,
+            asc: !sortByDate, // 是否升序排列
+          ),
+        ],
+      ),
+    );
+
+    if (albums.isEmpty) return [];
+
+    // 获取图片资源
+    final List<AssetEntity> assets = await albums.first.getAssetListPaged(
+      page: 0,
+      size: 10000, // 获取 10000 张图片
+    );
+
+    return assets;
+  }
 }

+ 20 - 6
lib/module/photo_preview/photo_preview_controller.dart

@@ -185,19 +185,33 @@ class PhotoPreviewController extends BaseController
   }
 
   Future<void> updateSelectedFilesSize() async {
+    // 如果没有选中的文件,直接返回
+    if (selectedPhotosIds.isEmpty) {
+      selectedFilesSize.value = 0;
+      return;
+    }
     double totalSize = 0;
     // 通过selectedPhotosIds获取选中的图片,然后计算大小
     for (var id in selectedPhotosIds) {
-      final entity = await AssetEntity.fromId(id);
-      if (entity != null) {
-        final file = await entity.file;
-        if (file != null) {
-          totalSize += await file.length();
+      // 检查缓存
+      if (ImagePickerUtil.fileSizeCache.containsKey(id)) {
+        totalSize += ImagePickerUtil.fileSizeCache[id]!;
+      } else {
+        final entity = await AssetEntity.fromId(id);
+        if (entity != null) {
+          final file = await entity.file;
+          if (file != null) {
+            double size = await file.length() / 1024; // 转换为KB
+            totalSize += size;
+            // 缓存文件大小
+            ImagePickerUtil.fileSizeCache[id] = size;
+          }
         }
       }
     }
 
-    selectedFilesSize.value = totalSize / 1024; // Convert to KB
+
+    selectedFilesSize.value = totalSize; // 更新总大小
     PhotoManager.clearFileCache();
   }
 

+ 1 - 1
lib/module/similar_photo/similar_photo_controller.dart

@@ -92,7 +92,7 @@ class SimilarPhotoController extends BasePhotoController {
             updateSelectedPhotosIds(image.id, false);
           }
           selectedFileCount.value = selectedPhotosIds.length;
-          getSelectedFilesSize();
+          updateSelectedFilesSize();
 
           Future.delayed(Duration(seconds: 2), () {
             SmartDialog.dismiss(tag: 'photoDeletingDialog');