photo_preview_controller.dart 13 KB

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