photo_preview_controller.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.loadPeoplePhoto();
  129. controller.restoreSelections();
  130. break;
  131. case PhotosType.screenshots:
  132. ImagePickerUtil.selectedScreenshotPhotosIds
  133. .assignAll(selectedPhotosIds);
  134. ScreenShotsController controller = Get.find<ScreenShotsController>();
  135. controller.loadScreenshots();
  136. controller.restoreSelections();
  137. break;
  138. case PhotosType.similarPhotos:
  139. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  140. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  141. controller.loadSimilarPhotos();
  142. controller.restoreSelections();
  143. break;
  144. case PhotosType.locationPhotos:
  145. ImagePickerUtil.selectedLocationPhotosIds.assignAll(selectedPhotosIds);
  146. LocationsSinglePhotoController controller =
  147. Get.find<LocationsSinglePhotoController>();
  148. controller.loadLocationsSinglePhoto();
  149. controller.restoreSelections();
  150. break;
  151. case PhotosType.blurryPhotos:
  152. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  153. ScreenShotsController controller = Get.find<ScreenShotsController>();
  154. controller.loadScreenshots();
  155. controller.restoreSelections();
  156. break;
  157. }
  158. }
  159. void clickUnselect() {
  160. print("clickUnselect");
  161. cardSwiperController.value.swipe(CardSwiperDirection.right);
  162. }
  163. FutureOr<bool> onSwipe(
  164. int previousIndex,
  165. int? currentIndex,
  166. CardSwiperDirection direction,
  167. ) {
  168. print(
  169. 'The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  170. );
  171. if (currentIndex != null) {
  172. groupIndex.value = currentIndex;
  173. }
  174. // 如果direction是left,
  175. if (direction == CardSwiperDirection.left) {
  176. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  177. if (!selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  178. print(
  179. 'add photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  180. selectedPhotosIds.add(photoGroups[previousIndex].id);
  181. }
  182. } else if (direction == CardSwiperDirection.right) {
  183. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  184. if (selectedPhotosIds.contains(photoGroups[previousIndex].id)) {
  185. print(
  186. 'remove photoGroups[groupIndex.value].id ${photoGroups[previousIndex].id}');
  187. selectedPhotosIds.remove(photoGroups[previousIndex].id);
  188. }
  189. }
  190. selectedFileCount.value = selectedPhotosIds.length;
  191. updateSelectedFilesSize();
  192. return true;
  193. }
  194. bool onSwiperUndo(
  195. int? previousIndex,
  196. int currentIndex,
  197. CardSwiperDirection direction,
  198. ) {
  199. print(
  200. 'The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  201. groupIndex.value = currentIndex;
  202. // 撤销之前左滑的操作
  203. if (direction == CardSwiperDirection.left) {
  204. print(
  205. 'photoGroups[groupIndex.value].id ${photoGroups[groupIndex.value].id}');
  206. if (selectedPhotosIds.contains(photoGroups[groupIndex.value].id)) {
  207. selectedPhotosIds.remove(photoGroups[groupIndex.value].id);
  208. }
  209. }
  210. selectedFileCount.value = selectedPhotosIds.length;
  211. updateSelectedFilesSize();
  212. return true;
  213. }
  214. onSwiperEnd() {
  215. isSwiperEnd.value = true;
  216. print('onSwiperEnd');
  217. }
  218. clickDelete() async{
  219. print('clickDelete');
  220. switch(photosType){
  221. case PhotosType.peoplePhotos:
  222. ImagePickerUtil.selectedPeoplePhotosIds.assignAll(selectedPhotosIds);
  223. break;
  224. case PhotosType.screenshots:
  225. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  226. break;
  227. case PhotosType.similarPhotos:
  228. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  229. break;
  230. case PhotosType.locationPhotos:
  231. ImagePickerUtil.selectedLocationPhotosIds.assignAll(selectedPhotosIds);
  232. break;
  233. case PhotosType.blurryPhotos:
  234. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  235. break;
  236. }
  237. if (selectedPhotosIds.isNotEmpty) {
  238. // 获取要删除的资产
  239. final assetsToDelete = photoGroups.where(
  240. (asset) => selectedPhotosIds.contains(asset.id),
  241. ).toList();
  242. // 调用方法会返回被删除的资源,如果全部失败会返回空列表。
  243. final List<String> result = await PhotoManager.editor.deleteWithIds(
  244. assetsToDelete.map((e) => e.id).toList(),
  245. );
  246. print ('PhotoPreviewController result $result');
  247. }
  248. switch(photosType){
  249. case PhotosType.peoplePhotos:
  250. ImagePickerUtil.updatePhotoGroupDate(PhotosType.peoplePhotos);
  251. PeoplePhotoController controller = Get.find<PeoplePhotoController>();
  252. controller.loadPeoplePhoto();
  253. controller.restoreSelections();
  254. break;
  255. case PhotosType.screenshots:
  256. ImagePickerUtil.updatePhotoGroupDate(PhotosType.screenshots);
  257. ScreenShotsController controller = Get.find<ScreenShotsController>();
  258. controller.loadScreenshots();
  259. controller.restoreSelections();
  260. break;
  261. case PhotosType.similarPhotos:
  262. ImagePickerUtil.updatePhotoGroupDate(PhotosType.similarPhotos);
  263. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  264. controller.loadSimilarPhotos();
  265. break;
  266. case PhotosType.locationPhotos:
  267. ImagePickerUtil.updatePhotoGroupDate(PhotosType.locationPhotos);
  268. LocationsSinglePhotoController controller = Get.find<LocationsSinglePhotoController>();
  269. controller.loadLocationsSinglePhoto();
  270. break;
  271. case PhotosType.blurryPhotos:
  272. ImagePickerUtil.updatePhotoGroupDate(PhotosType.blurryPhotos);
  273. ScreenShotsController controller = Get.find<ScreenShotsController>();
  274. controller.loadScreenshots();
  275. break;
  276. }
  277. _initData();
  278. }
  279. }