locations_single_photo_controller.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import 'package:clean/base/base_controller.dart';
  2. import 'package:clean/data/bean/photos_type.dart';
  3. import 'package:clean/module/image_picker/image_picker_util.dart';
  4. import 'package:clean/module/locations_photo/locations_photo_controller.dart';
  5. import 'package:clean/module/people_photo/photo_group.dart';
  6. import 'package:clean/module/photo_preview/photo_preview_view.dart';
  7. import 'package:clean/utils/toast_util.dart';
  8. import 'package:get/get.dart';
  9. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  10. class LocationsSinglePhotoController extends BaseController {
  11. late final PhotoGroup photoGroup;
  12. final RxInt selectedFileCount = 0.obs;
  13. final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
  14. final RxDouble selectedFilesSize = 0.0.obs;
  15. @override
  16. void onInit() {
  17. // TODO: implement onInit
  18. super.onInit();
  19. _getArgs();
  20. loadLocationsSinglePhoto();
  21. restoreSelections();
  22. }
  23. void clickImage(String location, int imageIndex) {
  24. final group = getGroupByLocation(location);
  25. final image = group.images[imageIndex];
  26. PhotoPreviewPage.start(PhotosType.locationPhotos, image.id);
  27. }
  28. void toggleImageSelection(String groupTitle, int imageIndex) {
  29. final group = getGroupByLocation(groupTitle);
  30. final image = group.images[imageIndex];
  31. final selected = !group.selectedImages[imageIndex];
  32. group.selectedImages[imageIndex] = selected;
  33. _updateSelectedScreenshotsPhotosIds(image.id, selected);
  34. group.isSelected.value = group.selectedImages.every((selected) => selected);
  35. updateSelectedFilesSize();
  36. selectedFileCount.value = ImagePickerUtil.selectedLocationPhotosIds.length;
  37. }
  38. Future<void> updateSelectedFilesSize() async {
  39. double totalSize = 0;
  40. for (var id in ImagePickerUtil.selectedLocationPhotosIds) {
  41. final entity = await AssetEntity.fromId(id);
  42. if (entity != null) {
  43. final file = await entity.file;
  44. if (file != null) {
  45. totalSize += await file.length();
  46. }
  47. }
  48. }
  49. selectedFilesSize.value = totalSize / 1024; // Convert to KB
  50. }
  51. void _updateSelectedScreenshotsPhotosIds(String photoId, bool isSelected) {
  52. if (isSelected) {
  53. ImagePickerUtil.selectedLocationPhotosIds.add(photoId);
  54. } else {
  55. ImagePickerUtil.selectedLocationPhotosIds.remove(photoId);
  56. }
  57. }
  58. // 获取参数
  59. void _getArgs() {
  60. photoGroup = parameters?['PhotoGroup'] as PhotoGroup;
  61. }
  62. void loadLocationsSinglePhoto() {
  63. photoGroups.clear();
  64. if (ImagePickerUtil.locationPhotos.isEmpty) {
  65. print('locationPhotos.isEmpty');
  66. return;
  67. }
  68. photoGroups.add(photoGroup);
  69. }
  70. void restoreSelections() async {
  71. final selectedIds = ImagePickerUtil.selectedLocationPhotosIds.toSet();
  72. for (var group in photoGroups) {
  73. for (int i = 0; i < group.images.length; i++) {
  74. group.selectedImages[i] = selectedIds.contains(group.images[i].id);
  75. }
  76. group.isSelected.value = group.selectedImages.every((selected) => selected);
  77. }
  78. await updateSelectedFilesSize();
  79. selectedFileCount.value = selectedIds.length;
  80. }
  81. void toggleGroupSelection(String location) {
  82. final group = getGroupByLocation(location);
  83. final newValue = !group.isSelected.value;
  84. group.toggleSelectAll(newValue);
  85. for (var image in group.images) {
  86. _updateSelectedScreenshotsPhotosIds(image.id, newValue);
  87. }
  88. updateSelectedFilesSize();
  89. selectedFileCount.value = ImagePickerUtil.selectedLocationPhotosIds.length;
  90. }
  91. // 通过位置获取照片组
  92. PhotoGroup getGroupByLocation(String? location) {
  93. return photoGroups.firstWhere((group) => group.location == location);
  94. }
  95. clickDelete() async {
  96. print('clickDelete');
  97. if (ImagePickerUtil.selectedLocationPhotosIds.isNotEmpty) {
  98. final assetsToDelete = photoGroups.expand((group) =>
  99. group.images.where((asset) => ImagePickerUtil.selectedLocationPhotosIds.contains(asset.id))
  100. ).toList();
  101. final List<String> result = await PhotoManager.editor.deleteWithIds(
  102. assetsToDelete.map((e) => e.id).toList(),
  103. );
  104. print('PhotoPreviewController result $result');
  105. if (result.length == ImagePickerUtil.selectedLocationPhotosIds.length) {
  106. if (photoGroup.images.length == ImagePickerUtil.selectedLocationPhotosIds.length) {
  107. ImagePickerUtil.locationPhotos.remove(photoGroup.location);
  108. } else {
  109. photoGroup.images.removeWhere((element) => ImagePickerUtil.selectedLocationPhotosIds.contains(element.id));
  110. }
  111. ImagePickerUtil.updatePhotoGroupDate(PhotosType.locationPhotos, ImagePickerUtil.selectedLocationPhotosIds);
  112. ToastUtil.show("Delete success");
  113. } else {
  114. // 删除失败
  115. ToastUtil.show("Delete failed");
  116. }
  117. }
  118. }
  119. }