Ver código fonte

[fit]修复首页照片bug

云天逵 9 meses atrás
pai
commit
c8820ab594
1 arquivos alterados com 107 adições e 28 exclusões
  1. 107 28
      lib/module/image_picker/image_picker_util.dart

+ 107 - 28
lib/module/image_picker/image_picker_util.dart

@@ -15,6 +15,7 @@ import 'package:get/get.dart';
 import 'package:photo_manager/photo_manager.dart';
 
 import '../calendar/calendar_state.dart';
+import '../home/home_controller.dart';
 
 class ImagePickerUtil {
   ImagePickerUtil._();
@@ -70,59 +71,74 @@ class ImagePickerUtil {
     peoplePhotos.clear();
   }
 
-
-
-  static Future<void> updatePhotoGroupDate(PhotosType photosType,Set<String> selectedPhotosIds1) async {
+  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:
-        ScreenShotsController screenshotController = Get.find<ScreenShotsController>();
+        ScreenShotsController screenshotController =
+            Get.find<ScreenShotsController>();
         removeImagesAndEmptyGroups(screenshotController, selectedPhotosIds);
-        selectedScreenshotPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+        selectedScreenshotPhotosIds
+            .removeWhere((element) => selectedPhotosIds.contains(element));
         screenshotController.restoreSelections();
         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;
 
       case PhotosType.locationPhotos:
-        LocationsSinglePhotoController locationsSingleController = Get.find<LocationsSinglePhotoController>();
-        LocationsPhotoController locationsPhotoController = Get.find<LocationsPhotoController>();
-        removeImagesAndEmptyGroups(locationsSingleController, selectedPhotosIds);
+        LocationsSinglePhotoController locationsSingleController =
+            Get.find<LocationsSinglePhotoController>();
+        LocationsPhotoController locationsPhotoController =
+            Get.find<LocationsPhotoController>();
+        removeImagesAndEmptyGroups(
+            locationsSingleController, selectedPhotosIds);
         removeImagesAndEmptyGroups(locationsPhotoController, selectedPhotosIds);
-        selectedLocationPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+        selectedLocationPhotosIds
+            .removeWhere((element) => selectedPhotosIds.contains(element));
         locationsSingleController.restoreSelections();
         locationsPhotoController.restoreSelections();
         break;
 
       case PhotosType.peoplePhotos:
-        PeoplePhotoController peoplePhotoController = Get.find<PeoplePhotoController>();
+        PeoplePhotoController peoplePhotoController =
+            Get.find<PeoplePhotoController>();
         removeImagesAndEmptyGroups(peoplePhotoController, selectedPhotosIds);
-        selectedPeoplePhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+        selectedPeoplePhotosIds
+            .removeWhere((element) => selectedPhotosIds.contains(element));
         peoplePhotoController.restoreSelections();
         break;
 
       case PhotosType.blurryPhotos:
-        ScreenShotsController blurryController = Get.find<ScreenShotsController>();
+        ScreenShotsController blurryController =
+            Get.find<ScreenShotsController>();
         removeImagesAndEmptyGroups(blurryController, selectedPhotosIds);
-        selectedBlurryPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+        selectedBlurryPhotosIds
+            .removeWhere((element) => selectedPhotosIds.contains(element));
         blurryController.restoreSelections();
         break;
     }
   }
+
   // 移除每组图片里面对应的元素并删除空的组
-  static void removeImagesAndEmptyGroups(dynamic controller, Set<String> selectedPhotosIds) {
+  static void removeImagesAndEmptyGroups(
+      dynamic controller, Set<String> selectedPhotosIds) {
     // 创建photoGroups的副本进行遍历,避免遍历过程中修改photoGroups
     for (var group in List.from(controller.photoGroups)) {
       // 移除所有与selectedPhotosIds匹配的图片
@@ -134,27 +150,90 @@ class ImagePickerUtil {
       }
     }
   }
-  static void updatePhotoData(Set<String> selectedPhotosIds1) {
 
+  static void updatePhotoData(Set<String> selectedPhotosIds1) {
     debugPrint("ImagePickerUtil1 updatePhotoData  $selectedPhotosIds1");
     Set<String> selectedPhotosIds = Set.from(selectedPhotosIds1);
-    screenshotPhotos.removeWhere((element) => selectedPhotosIds.contains(element.id));
+    screenshotPhotos
+        .removeWhere((element) => selectedPhotosIds.contains(element.id));
     for (var group in similarPhotos) {
       group.removeWhere((element) => selectedPhotosIds.contains(element.id));
     }
-    locationPhotos.forEach((key, group) => group.removeWhere((element) => selectedPhotosIds.contains(element.id)));
-    peoplePhotos.removeWhere((element) => selectedPhotosIds.contains(element.id));
-    blurryPhotos.removeWhere((element) => selectedPhotosIds.contains(element.id));
+    locationPhotos.forEach((key, group) =>
+        group.removeWhere((element) => selectedPhotosIds.contains(element.id)));
+    peoplePhotos
+        .removeWhere((element) => selectedPhotosIds.contains(element.id));
+    blurryPhotos
+        .removeWhere((element) => selectedPhotosIds.contains(element.id));
     // 2. 移除空的集合
     similarPhotos.removeWhere((element) => element.isEmpty);
     locationPhotos.removeWhere((key, value) => value.isEmpty);
 
     debugPrint("ImagePickerUtil2 updatePhotoData  $selectedPhotosIds");
-    selectedScreenshotPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
-    selectedSimilarPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
-    selectedLocationPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
-    selectedPeoplePhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
-    selectedBlurryPhotosIds.removeWhere((element) => selectedPhotosIds.contains(element));
+    selectedScreenshotPhotosIds
+        .removeWhere((element) => selectedPhotosIds.contains(element));
+    selectedSimilarPhotosIds
+        .removeWhere((element) => selectedPhotosIds.contains(element));
+    selectedLocationPhotosIds
+        .removeWhere((element) => selectedPhotosIds.contains(element));
+    selectedPeoplePhotosIds
+        .removeWhere((element) => selectedPhotosIds.contains(element));
+    selectedBlurryPhotosIds
+        .removeWhere((element) => selectedPhotosIds.contains(element));
+
+    HomeController homeController = Get.find<HomeController>();
+    if (homeController.locationPhoto.value != null &&
+        selectedPhotosIds.contains(homeController.locationPhoto.value!.id)) {
+      homeController.locationPhoto.value = null;
+      if (locationPhotos.isNotEmpty) {
+        var asset = locationPhotos.values.first.first;
+        homeController.locationPhoto.value = asset;
+      }
+    }
+    if (homeController.screenshotPhoto.value != null &&
+        selectedPhotosIds.contains(homeController.screenshotPhoto.value!.id)) {
+      homeController.screenshotPhoto.value = null;
+      if (screenshotPhotos.isNotEmpty) {
+        var asset = screenshotPhotos.first;
+        homeController.screenshotPhoto.value = asset;
+      }
+    }
+    if (homeController.blurryPhoto.value != null &&
+        selectedPhotosIds.contains(homeController.blurryPhoto.value!.id)) {
+      homeController.blurryPhoto.value = null;
+      if (blurryPhotos.isNotEmpty) {
+        var asset = blurryPhotos.first;
+        homeController.blurryPhoto.value = asset;
+      }
+    }
+    homeController.similarPhotos
+        .removeWhere((element) => selectedPhotosIds.contains(element.id));
+    if (homeController.locationPhoto.value != null &&
+        selectedPhotosIds.contains(homeController.locationPhoto.value!.id)) {
+      homeController.locationPhoto.value = null;
+    }
+    if (similarPhotos.isNotEmpty) {
+      for (var group in ImagePickerUtil.similarPhotos) {
+        for (var asset in group) {
+          homeController.similarPhotos.add(asset);
+          if (homeController.similarPhotos.length == 4) {
+            break;
+          }
+        }
+      }
+    }
+
+    homeController.peoplePhotos
+        .removeWhere((element) => selectedPhotosIds.contains(element.id));
+    if (peoplePhotos.isNotEmpty) {
+      for (var personPhotos in peoplePhotos) {
+        homeController.peoplePhotos.add(personPhotos);
+        if (homeController.peoplePhotos.length == 2) {
+          break;
+        }
+      }
+    }
+
     debugPrint("ImagePickerUtil3  updatePhotoData $selectedPhotosIds");
     CalendarState.removePhotosData(selectedPhotosIds);
   }