photo_preview_controller.dart 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/locations_photo/locations_single_photo_controller.dart';
  6. import 'package:clean/module/people_photo/people_photo_controller.dart';
  7. import 'package:clean/module/people_photo/photo_group.dart';
  8. import 'package:clean/module/screenshots_blurry/screenshots_controller.dart';
  9. import 'package:clean/module/similar_photo/similar_photo_controller.dart';
  10. import 'package:clean/utils/file_utils.dart';
  11. import 'package:flutter/Material.dart';
  12. import 'package:flutter_card_swiper/flutter_card_swiper.dart';
  13. import 'package:get/get.dart';
  14. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  15. class PhotoPreviewController extends BaseController {
  16. Rx<CardSwiperController> cardSwiperController = CardSwiperController().obs;
  17. final RxList<AssetEntity> photoGroups = <AssetEntity>[].obs;
  18. final RxSet<String> selectedPhotosIds = <String>{}.obs;
  19. final RxBool isSwiperEnd = false.obs;
  20. RxInt groupIndex = 0.obs;
  21. late PhotosType photosType;
  22. late String? currentImageId;
  23. RxDouble selectedFilesSize = 0.0.obs;
  24. RxInt selectedFileCount = 0.obs;
  25. @override
  26. void onInit() {
  27. super.onInit();
  28. isSwiperEnd.value = false;
  29. print('PhotoPreviewController onInit');
  30. _getArgs(); // 获取传递的参数
  31. _initData(); // 初始化数据
  32. WidgetsBinding.instance.addPostFrameCallback((_) {
  33. if (currentImageId != null) {
  34. for (int i = 0; i < photoGroups.length; i++) {
  35. if (photoGroups[i].id == currentImageId) {
  36. print('photoGroups[i].id ${photoGroups[i].id},i $i');
  37. groupIndex.value = i;
  38. cardSwiperController.value.moveTo(i);
  39. break;
  40. }
  41. }
  42. }
  43. });
  44. }
  45. // 获取参数
  46. void _getArgs() {
  47. photosType = parameters?['photosType'];
  48. currentImageId = parameters?['currentImageId'];
  49. }
  50. void _initData() {
  51. photoGroups.clear();
  52. selectedPhotosIds.clear();
  53. switch (photosType) {
  54. case PhotosType.peoplePhotos:
  55. photoGroups.assignAll(ImagePickerUtil.peoplePhotos);
  56. selectedPhotosIds.assignAll(ImagePickerUtil.selectedPeoplePhotosIds);
  57. selectedFileCount.value =
  58. ImagePickerUtil.selectedPeoplePhotosIds.length;
  59. break;
  60. case PhotosType.screenshots:
  61. photoGroups.assignAll(ImagePickerUtil.screenshotPhotos);
  62. selectedPhotosIds
  63. .assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  64. selectedFileCount.value =
  65. ImagePickerUtil.selectedScreenshotPhotosIds.length;
  66. break;
  67. case PhotosType.similarPhotos:
  68. for (var group in ImagePickerUtil.similarPhotos) {
  69. photoGroups.addAll(group);
  70. }
  71. selectedPhotosIds.assignAll(ImagePickerUtil.selectedSimilarPhotosIds);
  72. selectedFileCount.value =
  73. ImagePickerUtil.selectedSimilarPhotosIds.length;
  74. break;
  75. case PhotosType.locationPhotos:
  76. for (var group in ImagePickerUtil.locationPhotos.values) {
  77. photoGroups.addAll(group);
  78. }
  79. selectedPhotosIds.assignAll(ImagePickerUtil.selectedLocationPhotosIds);
  80. selectedFileCount.value =
  81. ImagePickerUtil.selectedLocationPhotosIds.length;
  82. break;
  83. case PhotosType.blurryPhotos:
  84. photoGroups.assignAll(ImagePickerUtil.blurryPhotos);
  85. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  86. selectedFileCount.value =
  87. ImagePickerUtil.selectedBlurryPhotosIds.length;
  88. break;
  89. }
  90. updateSelectedFilesSize();
  91. }
  92. Future<void> updateSelectedFilesSize() async {
  93. double totalSize = 0;
  94. // 通过selectedPhotosIds获取选中的图片,然后计算大小
  95. for (var id in selectedPhotosIds) {
  96. final entity = await AssetEntity.fromId(id);
  97. if (entity != null) {
  98. final file = await entity.file;
  99. if (file != null) {
  100. totalSize += await file.length();
  101. }
  102. }
  103. }
  104. selectedFilesSize.value = totalSize / 1024; // Convert to KB
  105. }
  106. void recoverSelectPhoto() {
  107. cardSwiperController.value.undo();
  108. }
  109. void clickSelect() {
  110. cardSwiperController.value.swipe(CardSwiperDirection.left);
  111. }
  112. @override
  113. void onClose() {
  114. super.onClose();
  115. // 清理操作,释放资源
  116. cardSwiperController.value.dispose();
  117. }
  118. void clickBack() {
  119. _saveSelectedPhotos(photosType);
  120. Get.back();
  121. }
  122. // // 保存选择的图片ID列表
  123. void _saveSelectedPhotos(PhotosType type) {
  124. switch (type) {
  125. case PhotosType.peoplePhotos:
  126. ImagePickerUtil.selectedPeoplePhotosIds.assignAll(selectedPhotosIds);
  127. PeoplePhotoController controller = Get.find<PeoplePhotoController>();
  128. controller.restoreSelections();
  129. break;
  130. case PhotosType.screenshots:
  131. ImagePickerUtil.selectedScreenshotPhotosIds
  132. .assignAll(selectedPhotosIds);
  133. ScreenShotsController controller = Get.find<ScreenShotsController>();
  134. controller.restoreSelections();
  135. break;
  136. case PhotosType.similarPhotos:
  137. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  138. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  139. controller.restoreSelections();
  140. break;
  141. case PhotosType.locationPhotos:
  142. ImagePickerUtil.selectedLocationPhotosIds.assignAll(selectedPhotosIds);
  143. LocationsSinglePhotoController controller =
  144. Get.find<LocationsSinglePhotoController>();
  145. controller.restoreSelections();
  146. break;
  147. case PhotosType.blurryPhotos:
  148. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  149. ScreenShotsController controller = Get.find<ScreenShotsController>();
  150. controller.restoreSelections();
  151. break;
  152. }
  153. }
  154. void clickUnselect() {
  155. print("clickUnselect");
  156. cardSwiperController.value.swipe(CardSwiperDirection.right);
  157. }
  158. FutureOr<bool> onSwipe(
  159. int previousIndex,
  160. int? currentIndex,
  161. CardSwiperDirection direction,
  162. ) {
  163. print(
  164. 'The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  165. );
  166. if (currentIndex != null) {
  167. groupIndex.value = currentIndex;
  168. }
  169. // 如果direction是left,
  170. if (direction == CardSwiperDirection.left) {
  171. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  172. if (!selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  173. print(
  174. 'add photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  175. selectedPhotosIds.add(photoGroups[previousIndex].id);
  176. }
  177. } else if (direction == CardSwiperDirection.right) {
  178. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  179. if (selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  180. print(
  181. 'remove photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  182. selectedPhotosIds.remove(photoGroups[previousIndex].id);
  183. }
  184. }
  185. selectedFileCount.value = selectedPhotosIds.length;
  186. updateSelectedFilesSize();
  187. return true;
  188. }
  189. bool onSwiperUndo(
  190. int? previousIndex,
  191. int currentIndex,
  192. CardSwiperDirection direction,
  193. ) {
  194. print(
  195. 'The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  196. groupIndex.value = currentIndex;
  197. // 撤销之前左滑的操作
  198. if (direction == CardSwiperDirection.left) {
  199. print(
  200. 'photoGroups[groupIndex.value].id ${photoGroups[groupIndex.value].id}');
  201. if (selectedPhotosIds.contains(photoGroups[groupIndex.value].id)) {
  202. selectedPhotosIds.remove(photoGroups[groupIndex.value].id);
  203. }
  204. }
  205. selectedFileCount.value = selectedPhotosIds.length;
  206. updateSelectedFilesSize();
  207. return true;
  208. }
  209. onSwiperEnd() {
  210. isSwiperEnd.value = true;
  211. print('onSwiperEnd');
  212. }
  213. clickDelete() async{
  214. print('clickDelete');
  215. switch(photosType){
  216. case PhotosType.peoplePhotos:
  217. ImagePickerUtil.selectedPeoplePhotosIds.assignAll(selectedPhotosIds);
  218. break;
  219. case PhotosType.screenshots:
  220. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  221. break;
  222. case PhotosType.similarPhotos:
  223. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  224. break;
  225. case PhotosType.locationPhotos:
  226. ImagePickerUtil.selectedLocationPhotosIds.assignAll(selectedPhotosIds);
  227. break;
  228. case PhotosType.blurryPhotos:
  229. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  230. break;
  231. }
  232. if (selectedPhotosIds.isNotEmpty) {
  233. // 获取要删除的资产
  234. final assetsToDelete = photoGroups.where(
  235. (asset) => selectedPhotosIds.contains(asset.id),
  236. ).toList();
  237. // 调用方法会返回被删除的资源,如果全部失败会返回空列表。
  238. final List<String> result = await PhotoManager.editor.deleteWithIds(
  239. assetsToDelete.map((e) => e.id).toList(),
  240. );
  241. print ('PhotoPreviewController result $result');
  242. }
  243. switch(photosType){
  244. case PhotosType.peoplePhotos:
  245. ImagePickerUtil.updatePhotoGroupDate(PhotosType.peoplePhotos);
  246. break;
  247. case PhotosType.screenshots:
  248. ImagePickerUtil.updatePhotoGroupDate(PhotosType.screenshots);
  249. break;
  250. case PhotosType.similarPhotos:
  251. ImagePickerUtil.updatePhotoGroupDate(PhotosType.similarPhotos);
  252. break;
  253. case PhotosType.locationPhotos:
  254. ImagePickerUtil.updatePhotoGroupDate(PhotosType.locationPhotos);
  255. break;
  256. case PhotosType.blurryPhotos:
  257. ImagePickerUtil.updatePhotoGroupDate(PhotosType.blurryPhotos);
  258. break;
  259. }
  260. _initData();
  261. }
  262. }