photo_preview_controller.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:async';
  2. import 'package:clean/base/base_controller.dart';
  3. import 'package:clean/data/bean/photos_type.dart';
  4. import 'package:clean/module/image_picker/image_picker_util.dart';
  5. import 'package:clean/module/people_photo/people_photo_controller.dart';
  6. import 'package:clean/module/people_photo/photo_group.dart';
  7. import 'package:clean/module/screenshots_blurry/screenshots_controller.dart';
  8. import 'package:clean/module/similar_photo/similar_photo_controller.dart';
  9. import 'package:flutter/Material.dart';
  10. import 'package:flutter_card_swiper/flutter_card_swiper.dart';
  11. import 'package:get/get.dart';
  12. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  13. class PhotoPreviewController extends BaseController {
  14. Rx<CardSwiperController> cardSwiperController = CardSwiperController().obs;
  15. final RxList<AssetEntity> photoGroups = <AssetEntity>[].obs;
  16. final RxSet<String> selectedPhotosIds = <String>{}.obs;
  17. RxInt groupIndex = 0.obs;
  18. late PhotosType photosType;
  19. late String? currentImageId;
  20. RxDouble selectedFilesSize = 0.0.obs;
  21. RxInt selectedFileCount = 0.obs;
  22. @override
  23. void onInit() {
  24. super.onInit();
  25. print('PhotoPreviewController onInit');
  26. _getArgs(); // 获取传递的参数
  27. _initData(); // 初始化数据
  28. WidgetsBinding.instance.addPostFrameCallback((_) {
  29. if (currentImageId != null) {
  30. for (int i = 0; i < photoGroups.length; i++) {
  31. if (photoGroups[i].id == currentImageId) {
  32. print('photoGroups[i].id ${photoGroups[i].id},i $i');
  33. groupIndex.value = i;
  34. cardSwiperController.value.moveTo(i);
  35. break;
  36. }
  37. }
  38. }
  39. });
  40. }
  41. // 获取参数
  42. void _getArgs() {
  43. photosType = parameters?['photosType'];
  44. currentImageId = parameters?['currentImageId'];
  45. }
  46. void _initData() {
  47. photoGroups.clear();
  48. selectedPhotosIds.clear();
  49. switch(photosType) {
  50. case PhotosType.peoplePhotos:
  51. photoGroups.assignAll(ImagePickerUtil.peoplePhotos);
  52. selectedPhotosIds.assignAll(ImagePickerUtil.selectedPeoplePhotosIds);
  53. selectedFileCount.value = ImagePickerUtil.selectedPeoplePhotosIds.length;
  54. break;
  55. case PhotosType.screenshots:
  56. photoGroups.assignAll(ImagePickerUtil.screenshotPhotos);
  57. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  58. selectedFileCount.value = ImagePickerUtil.selectedScreenshotPhotosIds.length;
  59. break;
  60. case PhotosType.similarPhotos:
  61. for (var group in ImagePickerUtil.similarPhotos) {
  62. photoGroups.addAll(group);
  63. }
  64. selectedPhotosIds.assignAll(ImagePickerUtil.selectedSimilarPhotosIds);
  65. selectedFileCount.value = ImagePickerUtil.selectedSimilarPhotosIds.length;
  66. break;
  67. case PhotosType.locationPhotos:
  68. for (var group in ImagePickerUtil.locationPhotos.values) {
  69. photoGroups.addAll(group);
  70. }
  71. selectedPhotosIds.assignAll(ImagePickerUtil.selectedLocationPhotosIds);
  72. selectedFileCount.value = ImagePickerUtil.selectedLocationPhotosIds.length;
  73. break;
  74. case PhotosType.blurryPhotos:
  75. photoGroups.assignAll(ImagePickerUtil.blurryPhotos);
  76. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  77. selectedFileCount.value = ImagePickerUtil.selectedBlurryPhotosIds.length;
  78. break;
  79. }
  80. updateSelectedFilesSize();
  81. }
  82. Future<void> updateSelectedFilesSize() async {
  83. double totalSize = 0;
  84. // 通过selectedPhotosIds获取选中的图片,然后计算大小
  85. for (var id in selectedPhotosIds) {
  86. final entity = await AssetEntity.fromId(id);
  87. if (entity != null) {
  88. final file = await entity.file;
  89. if (file != null) {
  90. totalSize += await file.length();
  91. }
  92. }
  93. }
  94. selectedFilesSize.value = totalSize / 1024; // Convert to KB
  95. }
  96. void recoverSelectPhoto() {
  97. cardSwiperController.value.undo();
  98. }
  99. void clickSelect() {
  100. cardSwiperController.value.swipe(CardSwiperDirection.left);
  101. }
  102. @override
  103. void onClose() {
  104. super.onClose();
  105. // 清理操作,释放资源
  106. cardSwiperController.value.dispose();
  107. }
  108. void clickBack() {
  109. _saveSelectedPhotos(photosType);
  110. Get.back();
  111. }
  112. // // 保存选择的图片ID列表
  113. void _saveSelectedPhotos(PhotosType type) {
  114. switch (type) {
  115. case PhotosType.peoplePhotos:
  116. ImagePickerUtil.selectedPeoplePhotosIds.assignAll(selectedPhotosIds);
  117. PeoplePhotoController controller = Get.find<PeoplePhotoController>();
  118. controller.restoreSelections();
  119. break;
  120. case PhotosType.screenshots:
  121. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  122. ScreenShotsController controller = Get.find<ScreenShotsController>();
  123. controller.restoreSelections();
  124. break;
  125. case PhotosType.similarPhotos:
  126. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  127. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  128. controller.restoreSelections();
  129. break;
  130. case PhotosType.locationPhotos:
  131. ImagePickerUtil.selectedLocationPhotosIds.assignAll(selectedPhotosIds);
  132. break;
  133. case PhotosType.blurryPhotos:
  134. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  135. ScreenShotsController controller = Get.find<ScreenShotsController>();
  136. controller.restoreSelections();
  137. break;
  138. }
  139. }
  140. void clickUnselect() {
  141. print("clickUnselect");
  142. cardSwiperController.value.swipe(CardSwiperDirection.right);
  143. }
  144. FutureOr<bool> onSwipe(
  145. int previousIndex,
  146. int? currentIndex,
  147. CardSwiperDirection direction,
  148. ) {
  149. print(
  150. 'The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  151. );
  152. if (currentIndex != null) {
  153. groupIndex.value = currentIndex;
  154. }
  155. // 如果direction是left,
  156. if (direction == CardSwiperDirection.left) {
  157. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  158. if (!selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  159. print(
  160. 'add photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  161. selectedPhotosIds.add(photoGroups[previousIndex].id);
  162. }
  163. } else if (direction == CardSwiperDirection.right) {
  164. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  165. if (selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  166. print(
  167. 'remove photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  168. selectedPhotosIds.remove(photoGroups[previousIndex].id);
  169. }
  170. }
  171. selectedFileCount.value = selectedPhotosIds.length;
  172. updateSelectedFilesSize();
  173. return true;
  174. }
  175. bool onSwiperUndo(
  176. int? previousIndex,
  177. int currentIndex,
  178. CardSwiperDirection direction,
  179. ) {
  180. print(
  181. 'The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  182. groupIndex.value = currentIndex;
  183. // 撤销之前左滑的操作
  184. if (direction == CardSwiperDirection.left) {
  185. print(
  186. 'photoGroups[groupIndex.value].id ${photoGroups[groupIndex.value].id}');
  187. if (selectedPhotosIds.contains(photoGroups[groupIndex.value].id)) {
  188. selectedPhotosIds.remove(photoGroups[groupIndex.value].id);
  189. }
  190. }
  191. selectedFileCount.value = selectedPhotosIds.length;
  192. updateSelectedFilesSize();
  193. return true;
  194. }
  195. }