Browse Source

[fit]同步删除后的大小

云天逵 1 year ago
parent
commit
78583a045a

+ 0 - 2
lib/base/base_photo_controller.dart

@@ -190,7 +190,6 @@ abstract class BasePhotoController extends BaseController {
                   (ImagePickerUtil.screenshotsSize.value - deleteSize);
               break;
             case PhotosType.similarPhotos:
-
               ImagePickerUtil.similarPhotosSize.value =
                   (ImagePickerUtil.similarPhotosSize.value - deleteSize);
               break;
@@ -201,7 +200,6 @@ abstract class BasePhotoController extends BaseController {
             case PhotosType.blurryPhotos:
               ImagePickerUtil.blurrySize.value =
                   (ImagePickerUtil.blurrySize.value - deleteSize);
-
               break;
           }
           for (var group in photoGroups) {

+ 1 - 0
lib/base/base_photos_controller.dart

@@ -52,6 +52,7 @@ abstract class BasePhotosController extends BaseController {
       if (result.length == photoGroup.value.selectedPhotosIds.length) {
         debugPrint(
             "BasePhotosController $runtimeType delete ${photoGroup.value.selectedPhotosIds}");
+
         ImagePickerUtil.updatePhotoData(photoGroup.value.selectedPhotosIds);
         cleanSelections();
         ToastUtil.show('Delete success');

+ 16 - 35
lib/handler/photo_scan_handler.dart

@@ -13,6 +13,7 @@ import 'package:photo_classifier/photo_classifier.dart';
 import 'package:wechat_assets_picker/wechat_assets_picker.dart';
 
 import '../../module/image_picker/image_picker_util.dart';
+import '../utils/file_size_calculator_util.dart';
 
 
 class PhotoScanHandler {
@@ -24,10 +25,7 @@ class PhotoScanHandler {
   var errorMessage = ''.obs;
 
   static var progress = Rxn<ClassificationProgress>();
-  static var similarResult = <ClassifiedImageGroup>[].obs;
-  static var peopleResult = <ClassifiedImage>[].obs;
-  static var screenshotResult = <ClassifiedImage>[].obs;
-  static var blurryResult = <ClassifiedImage>[].obs;
+
 
   StreamSubscription<ClassificationEvent?>? _subscription;
 
@@ -176,6 +174,11 @@ class PhotoScanHandler {
     ImagePickerUtil.screenshotPhotos.value = result ?? [];
     ImagePickerUtil.similarPhotos.add(result ?? []);
     ImagePickerUtil.blurryPhotos.value = result ?? [];
+     FileSizeCalculatorUtil.calculateTotalSize(
+        assetIds: ImagePickerUtil.blurryPhotos.map((e) => e.id).toSet(),
+        updateValue: (double totalSize) {
+          ImagePickerUtil.blurrySize.value = totalSize.toInt()*1024;
+        });
 
     // result = await ImagePickerUtil.loadAssetsPaged(page: 1, pageSize: 9);
     // ImagePickerUtil.similarPhotos.add(result ?? []);
@@ -230,10 +233,10 @@ class PhotoScanHandler {
     isClassifying.value = true;
     errorMessage.value = '';
     progress.value = null;
-    similarResult.clear();
-    peopleResult.clear();
-    screenshotResult.clear();
-    blurryResult.clear();
+    ImagePickerUtil.similarResult.clear();
+    ImagePickerUtil.peopleResult.clear();
+    ImagePickerUtil.screenshotResult.clear();
+    ImagePickerUtil.blurryResult.clear();
     try {
       await classifier.configureClassifier(
         batchSize: 200,
@@ -247,7 +250,7 @@ class PhotoScanHandler {
           progress.value = event.progress;
           final result = event.result;
           if (result != null) {
-           await loadData(result);
+           await ImagePickerUtil.loadData(result);
           }
           if (event.progress?.isCompleted == true) {
             isClassifying.value = false;
@@ -284,10 +287,10 @@ class PhotoScanHandler {
 
   void resetAll() {
     progress.value = null;
-    similarResult.clear();
-    peopleResult.clear();
-    screenshotResult.clear();
-    blurryResult.clear();
+    ImagePickerUtil.similarResult.clear();
+    ImagePickerUtil.peopleResult.clear();
+    ImagePickerUtil.screenshotResult.clear();
+    ImagePickerUtil.blurryResult.clear();
     errorMessage.value = '';
     isClassifying.value = false;
   }
@@ -331,28 +334,6 @@ class PhotoScanHandler {
 
   }
 
-  loadData(ClassificationResult result) async {
-
-      if(result.similarGroups!=null){
-        similarResult.assignAll(result.similarGroups??[]);
-        await ImagePickerUtil.newUpdatePhotoGroups("similar", similarResult);
-      }
-
-      if(result.peopleImages!=null){
-        peopleResult.addAll(result.peopleImages ?? []);
-        await ImagePickerUtil.newUpdatePhotoGroup("people", peopleResult);
-      }
 
-      if(result.screenshotImages!=null) {
-        screenshotResult.addAll(result.screenshotImages ?? []);
-        await ImagePickerUtil.newUpdatePhotoGroup(
-            "screenshots", screenshotResult);
-      }
-      if(result.blurryImages!=null){
-        blurryResult.addAll(result.blurryImages ?? []);
-        await ImagePickerUtil.newUpdatePhotoGroup("blurry", blurryResult);
-      }
-
-  }
 
 }

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

@@ -62,8 +62,10 @@ class ImagePickerUtil {
   static final Rx<int> peopleSize = 0.obs;
   static final Rx<int> similarPhotosSize = 0.obs;
 
-  // // 用来缓存文件大小
-  // static final Map<String, double> fileSizeCache = {};
+  static var similarResult = <ClassifiedImageGroup>[].obs;
+  static var peopleResult = <ClassifiedImage>[].obs;
+  static var screenshotResult = <ClassifiedImage>[].obs;
+  static var blurryResult = <ClassifiedImage>[].obs;
 
   // 清除所有照片数据
   static void clearAllPhotos() {
@@ -74,16 +76,38 @@ class ImagePickerUtil {
     peoplePhotos.clear();
   }
 
+  static loadData(ClassificationResult result) async {
+    if (result.similarGroups != null) {
+      similarResult.assignAll(result.similarGroups ?? []);
+      await newUpdatePhotoGroups("similar", similarResult);
+    }
 
+    if (result.peopleImages != null) {
+      peopleResult.addAll(result.peopleImages ?? []);
+      await newUpdatePhotoGroup("people", peopleResult);
+    }
 
-  static Future<void> updatePhotoGroupDate(PhotosType photosType,Set<String> selectedPhotosIds1) async {
+    if (result.screenshotImages != null) {
+      screenshotResult.addAll(result.screenshotImages ?? []);
+      await newUpdatePhotoGroup("screenshots", screenshotResult);
+    }
+    if (result.blurryImages != null) {
+      blurryResult.addAll(result.blurryImages ?? []);
+      await newUpdatePhotoGroup("blurry", blurryResult);
+    }
+  }
+
+  static Future<void> updatePhotoGroupDate(
+      PhotosType photosType, Set<String> selectedPhotosIds1) async {
     // // 1. 统一移除相关的照片列表
-    debugPrint("updatePhotoGroupDate1 ScreenshotsController screenshotController $selectedPhotosIds1");
+    debugPrint(
+        "updatePhotoGroupDate1 ScreenshotsController screenshotController $selectedPhotosIds1");
     // 2. 移除控制器中的数据
     Set<String> selectedPhotosIds = Set.from(selectedPhotosIds1);
     updatePhotoData(selectedPhotosIds);
     //
-    debugPrint("updatePhotoGroupDate2 ScreenshotsController screenshotController $selectedPhotosIds");
+    debugPrint(
+        "updatePhotoGroupDate2 ScreenshotsController screenshotController $selectedPhotosIds");
     // 3. 根据photosType来处理不同的控制器和photoGroups
     switch (photosType) {
       case PhotosType.screenshots:
@@ -94,9 +118,11 @@ class ImagePickerUtil {
         break;
 
       case PhotosType.similarPhotos:
-        SimilarPhotoController similarController = Get.find<SimilarPhotoController>();
+        SimilarPhotoController similarController =
+            Get.find<SimilarPhotoController>();
         removeImagesAndEmptyGroups(similarController, selectedPhotosIds);
-        selectedSimilarPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+        selectedSimilarPhotosIds
+            .removeWhere((element) => selectedPhotosIds.contains(element));
         similarController.restoreSelections();
         break;
 
@@ -224,6 +250,23 @@ class ImagePickerUtil {
       }
     }
 
+    peopleResult
+        .removeWhere((element) => selectedPhotosIds.contains(element.assetsId));
+    peopleSize.value =
+        peopleResult.fold(0, (sum, image) => sum + image.fileSize);
+    screenshotResult
+        .removeWhere((element) => selectedPhotosIds.contains(element.assetsId));
+    screenshotsSize.value =
+        screenshotResult.fold(0, (sum, image) => sum + image.fileSize);
+    blurryResult
+        .removeWhere((element) => selectedPhotosIds.contains(element.assetsId));
+    blurrySize.value =
+        blurryResult.fold(0, (sum, image) => sum + image.fileSize);
+    similarResult.map((group) => group.images.removeWhere(
+        (element) => selectedPhotosIds.contains(element.assetsId)));
+    similarPhotoCount.value =
+        similarResult.fold(0, (sum, group) => sum + group.groupImageCount);
+
     debugPrint("ImagePickerUtil3  updatePhotoData $selectedPhotosIds");
     CalendarState.removePhotosData(selectedPhotosIds);
   }
@@ -271,46 +314,46 @@ class ImagePickerUtil {
   }
 
   static Future<void> newUpdatePhotoGroups(
-      String type,List<ClassifiedImageGroup> photoGroup
-      )async{
-    if(type=='similar') {
+      String type, List<ClassifiedImageGroup> photoGroup) async {
+    if (type == 'similar') {
       // similarPhotos.clear();
       for (var group in photoGroup) {
         similarPhotos.add(await _newConvertToAssetEntities(group.images));
-        similarPhotosSize.value +=  group.groupFileSize;
+        similarPhotosSize.value += group.groupFileSize;
         similarPhotoCount.value += group.groupImageCount;
       }
     }
   }
 
-  static Future<void> newUpdatePhotoGroup( String type,
-      List<ClassifiedImage> photoGroup) async {
+  static Future<void> newUpdatePhotoGroup(
+      String type, List<ClassifiedImage> photoGroup) async {
+    switch (type) {
+      case 'screenshots':
+        screenshotPhotos.clear();
+        screenshotPhotos.value = await _newConvertToAssetEntities(photoGroup);
+        screenshotsSize.value =
+            photoGroup.fold(0, (sum, image) => sum + image.fileSize);
 
-      switch (type) {
-        case 'screenshots':
-          screenshotPhotos.clear();
-          screenshotPhotos.value = await _newConvertToAssetEntities(photoGroup);
-          screenshotsSize.value = photoGroup.fold(0, (sum, image) => sum + image.fileSize);
-
-          break;
+        break;
 
         // case 'location':
         //   String location = group['name'] as String;
         //   locationPhotos[location] = await _convertToAssetEntities(photos);
         //   locationsSize.value = totalSize;
-          break;
-        case 'people':
-          peoplePhotos.clear();
-          peoplePhotos.assignAll(await _newConvertToAssetEntities(photoGroup));
-          peopleSize.value = photoGroup.fold(0, (sum, image) => sum + image.fileSize);
-          break;
-        case 'blurry':
-          blurryPhotos.clear();
-          blurryPhotos.value = await _newConvertToAssetEntities(photoGroup);
-          blurrySize.value = photoGroup.fold(0, (sum, image) => sum + image.fileSize);
-          break;
-      }
-
+        break;
+      case 'people':
+        peoplePhotos.clear();
+        peoplePhotos.assignAll(await _newConvertToAssetEntities(photoGroup));
+        peopleSize.value =
+            photoGroup.fold(0, (sum, image) => sum + image.fileSize);
+        break;
+      case 'blurry':
+        blurryPhotos.clear();
+        blurryPhotos.value = await _newConvertToAssetEntities(photoGroup);
+        blurrySize.value =
+            photoGroup.fold(0, (sum, image) => sum + image.fileSize);
+        break;
+    }
   }
 
   // 将原始照片数据转换为 AssetEntity 列表
@@ -427,6 +470,7 @@ class ImagePickerUtil {
       return '${(sizeInGB / 1024).toStringAsFixed(1)} ';
     }
   }
+
   static Future<List<AssetEntity>> loadAssetsPaged({
     int page = 0,
     int pageSize = 9,
@@ -460,6 +504,7 @@ class ImagePickerUtil {
 
     return assets;
   }
+
   // 加载图片资源
   static Future<List<AssetEntity>> loadAssets({bool sortByDate = true}) async {
     final PermissionState result = await PhotoManager.requestPermissionExtend();

+ 2 - 1
lib/module/people_photo/people_photo_state.dart

@@ -13,10 +13,11 @@ class PeoplePhotoState {
         .removeWhere((element) => selectedPhotosIds.contains(element.id));
     peoplePhotoGroup.selectedPhotosIds
         .removeWhere((element) => selectedPhotosIds.contains(element));
-
     // 如果images为空,删除该 album
     if (peoplePhotoGroup.images.isEmpty) {
       return;
     }
   }
+
+
 }