photo_preview_controller.dart 15 KB

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