photo_preview_controller.dart 15 KB

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