photo_preview_controller.dart 12 KB

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