photo_preview_controller.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 '../../dialog/play_video_dialog.dart';
  27. import '../../utils/file_size_calculator_util.dart';
  28. class PhotoPreviewController extends BaseController
  29. with GetSingleTickerProviderStateMixin {
  30. Rx<CardSwiperController> cardSwiperController = CardSwiperController().obs;
  31. final RxList<AssetEntity> listAssetEntity = <AssetEntity>[].obs;
  32. final RxSet<String> selectedPhotosIds = <String>{}.obs;
  33. final RxBool isSwiperEnd = false.obs;
  34. RxInt groupIndex = 0.obs;
  35. late PhotosType photosType;
  36. late String? currentImageId;
  37. RxDouble selectedFilesSize = 0.0.obs;
  38. RxInt selectedFileCount = 0.obs;
  39. late AnimationController animationController;
  40. RxBool animationIsComplete = false.obs;
  41. @override
  42. void onInit() {
  43. super.onInit();
  44. animationController = AnimationController(
  45. vsync: this,
  46. duration: const Duration(seconds: 3),
  47. );
  48. print('PhotoPreviewController onInit');
  49. _getArgs(); // 获取传递的参数
  50. _initData(); // 初始化数据
  51. WidgetsBinding.instance.addPostFrameCallback((_) {
  52. if (currentImageId != null) {
  53. for (int i = 0; i < listAssetEntity.length; i++) {
  54. if (listAssetEntity[i].id == currentImageId) {
  55. print('photoGroups[i].id ${listAssetEntity[i].id},i $i');
  56. groupIndex.value = i;
  57. cardSwiperController.value.moveTo(i);
  58. break;
  59. }
  60. }
  61. }
  62. });
  63. animationController.addStatusListener((status) {
  64. if (status == AnimationStatus.forward) {
  65. // 延迟一秒
  66. Future.delayed(Duration(seconds: 1), () {
  67. animationIsComplete.value = true;
  68. });
  69. }
  70. if (status == AnimationStatus.completed) {
  71. Future.delayed(Duration(seconds: 1), () {
  72. PhotoSelectedPreviewPage.start(photosType, selectedPhotosIds);
  73. });
  74. }
  75. });
  76. }
  77. @override
  78. void onReady() {
  79. // TODO: implement onReady
  80. super.onReady();
  81. print('PhotoPreviewController onReady');
  82. if (isFirstOpenPhotoPreview()) {
  83. photoPreviewTipDialog(clickCallback: () {
  84. setFirstOpenPhotoPreview(false);
  85. });
  86. }
  87. }
  88. // 获取参数
  89. void _getArgs() {
  90. photosType = parameters?['photosType'];
  91. currentImageId = parameters?['currentImageId'];
  92. }
  93. void _assignPhotosAndSelections(BasePhotoController controller) {
  94. listAssetEntity.assignAll(controller.getAllPhotos());
  95. selectedPhotosIds.assignAll(controller.selectedPhotosIds);
  96. selectedFileCount.value = selectedPhotosIds.length;
  97. }
  98. void _initData() {
  99. listAssetEntity.clear();
  100. selectedPhotosIds.clear();
  101. switch (photosType) {
  102. case PhotosType.peoplePhotos:
  103. final PeoplePhotoController controller =
  104. Get.find<PeoplePhotoController>();
  105. _assignPhotosAndSelections(controller);
  106. break;
  107. case PhotosType.screenshots:
  108. final ScreenShotsController controller =
  109. Get.find<ScreenShotsController>();
  110. _assignPhotosAndSelections(controller);
  111. break;
  112. case PhotosType.similarPhotos:
  113. final SimilarPhotoController controller =
  114. Get.find<SimilarPhotoController>();
  115. _assignPhotosAndSelections(controller);
  116. break;
  117. case PhotosType.locationPhotos:
  118. final LocationsSinglePhotoController controller =
  119. Get.find<LocationsSinglePhotoController>();
  120. _assignPhotosAndSelections(controller);
  121. break;
  122. case PhotosType.blurryPhotos:
  123. final ScreenShotsController controller =
  124. Get.find<ScreenShotsController>();
  125. _assignPhotosAndSelections(controller);
  126. break;
  127. }
  128. updateSelectedFilesSize();
  129. }
  130. //删除成功清除选中的图片
  131. void cleanSelections() async {
  132. debugPrint('PhotoPreviewController cleanSelections');
  133. listAssetEntity
  134. .removeWhere((element) => selectedPhotosIds.contains(element.id));
  135. selectedPhotosIds.clear();
  136. if (listAssetEntity.isEmpty) {
  137. return;
  138. }
  139. isSwiperEnd.value = false;
  140. if (listAssetEntity.isNotEmpty) {
  141. WidgetsBinding.instance.addPostFrameCallback((_) {
  142. groupIndex.value = 0;
  143. cardSwiperController.value.moveTo(0);
  144. });
  145. }
  146. updateSelectedFilesSize();
  147. selectedFileCount.value = selectedPhotosIds.length;
  148. }
  149. // 恢复状态
  150. void restoreSelections() {
  151. if (listAssetEntity.isEmpty) {
  152. clickBack();
  153. return;
  154. }
  155. isSwiperEnd.value = false;
  156. if (listAssetEntity.isNotEmpty) {
  157. WidgetsBinding.instance.addPostFrameCallback((_) {
  158. groupIndex.value = 0;
  159. cardSwiperController.value.moveTo(0);
  160. });
  161. }
  162. updateSelectedFilesSize();
  163. selectedFileCount.value = selectedPhotosIds.length;
  164. }
  165. Future<void> updateSelectedFilesSize() async {
  166. if (selectedPhotosIds.isEmpty) {
  167. selectedFilesSize.value = 0;
  168. return;
  169. }
  170. FileSizeCalculatorUtil.calculateTotalSize(
  171. assetIds: selectedPhotosIds,
  172. updateValue: (double totalSize) {
  173. selectedFilesSize.value = totalSize;
  174. },
  175. );
  176. PhotoManager.clearFileCache();
  177. }
  178. void updateSelections(Set<String> selectedIds) {
  179. print(
  180. 'PhotoPreviewController updateSelections photosType $photosType selectedIds $selectedIds');
  181. selectedPhotosIds.assignAll(selectedIds);
  182. restoreSelections();
  183. }
  184. void recoverSelectPhoto() {
  185. print('PhotoPreviewController recoverSelectPhoto');
  186. cardSwiperController.value.undo();
  187. }
  188. void clickSelect() {
  189. cardSwiperController.value.swipe(CardSwiperDirection.left);
  190. }
  191. @override
  192. void onClose() {
  193. print('PhotoPreviewController onClose');
  194. animationController.dispose();
  195. super.onClose();
  196. isSwiperEnd.value = false;
  197. // 清理操作,释放资源
  198. cardSwiperController.value.dispose();
  199. // 清理缓存
  200. PhotoManager.clearFileCache();
  201. }
  202. void clickBack() {
  203. _saveSelectedPhotos(photosType);
  204. Get.back();
  205. }
  206. // // 保存选择的图片ID列表
  207. void _saveSelectedPhotos(PhotosType type) {
  208. switch (type) {
  209. case PhotosType.peoplePhotos:
  210. PeoplePhotoController controller = Get.find<PeoplePhotoController>();
  211. controller.updateSelections(selectedPhotosIds);
  212. controller.restoreSelections();
  213. break;
  214. case PhotosType.screenshots:
  215. ScreenShotsController controller = Get.find<ScreenShotsController>();
  216. controller.updateSelections(selectedPhotosIds);
  217. controller.restoreSelections();
  218. break;
  219. case PhotosType.similarPhotos:
  220. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  221. controller.updateSelections(selectedPhotosIds);
  222. controller.restoreSelections();
  223. break;
  224. case PhotosType.locationPhotos:
  225. LocationsSinglePhotoController controller =
  226. Get.find<LocationsSinglePhotoController>();
  227. controller.updateSelections(selectedPhotosIds);
  228. controller.restoreSelections();
  229. break;
  230. case PhotosType.blurryPhotos:
  231. ScreenShotsController controller = Get.find<ScreenShotsController>();
  232. controller.updateSelections(selectedPhotosIds);
  233. controller.restoreSelections();
  234. break;
  235. }
  236. }
  237. void clickUnselect() {
  238. print("clickUnselect");
  239. cardSwiperController.value.swipe(CardSwiperDirection.right);
  240. }
  241. FutureOr<bool> onSwipe(
  242. int previousIndex,
  243. int? currentIndex,
  244. CardSwiperDirection direction,
  245. ) {
  246. print(
  247. 'onSwipe The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  248. );
  249. if (currentIndex != null) {
  250. groupIndex.value = currentIndex;
  251. }
  252. // 如果direction是left,
  253. if (direction == CardSwiperDirection.left) {
  254. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  255. if (!selectedPhotosIds.contains(listAssetEntity[previousIndex].id)) {
  256. print(
  257. 'add photoGroups[groupIndex.value].id ${listAssetEntity[previousIndex].id}');
  258. selectedPhotosIds.add(listAssetEntity[previousIndex].id);
  259. }
  260. } else if (direction == CardSwiperDirection.right) {
  261. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  262. if (selectedPhotosIds.contains(listAssetEntity[previousIndex].id)) {
  263. print(
  264. 'remove photoGroups[groupIndex.value].id ${listAssetEntity[previousIndex].id}');
  265. selectedPhotosIds.remove(listAssetEntity[previousIndex].id);
  266. }
  267. }
  268. selectedFileCount.value = selectedPhotosIds.length;
  269. updateSelectedFilesSize();
  270. return true;
  271. }
  272. bool onSwiperUndo(
  273. int? previousIndex,
  274. int currentIndex,
  275. CardSwiperDirection direction,
  276. ) {
  277. print(
  278. 'onSwiperUndo The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  279. groupIndex.value = currentIndex;
  280. // 撤销之前左滑的操作
  281. if (direction == CardSwiperDirection.left) {
  282. print(
  283. 'photoGroups[groupIndex.value].id ${listAssetEntity[groupIndex.value].id}');
  284. if (selectedPhotosIds.contains(listAssetEntity[groupIndex.value].id)) {
  285. selectedPhotosIds.remove(listAssetEntity[groupIndex.value].id);
  286. }
  287. }
  288. selectedFileCount.value = selectedPhotosIds.length;
  289. updateSelectedFilesSize();
  290. return true;
  291. }
  292. onSwiperEnd() {
  293. isSwiperEnd.value = true;
  294. print('onSwiperEnd');
  295. // 延迟500ms
  296. Future.delayed(Duration(milliseconds: 200), () {
  297. animationController.forward(from: 0);
  298. });
  299. PhotoManager.clearFileCache();
  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. PhotoManager.clearFileCache();
  379. }
  380. void playVideo(AssetEntity asset) async {
  381. playVideoDialog(asset);
  382. }
  383. // 将selectedFilesSize转成String类型,然后单位转换,如果超过1MB,则转成MB,超过1GB,则转成GB,否则KB
  384. String get selectedFilesSizeString {
  385. final double sizeInKB = selectedFilesSize.value;
  386. if (sizeInKB >= 1024 * 1024) { // 先检查最大单位(GB)
  387. return "${(sizeInKB / (1024 * 1024)).toStringAsFixed(2)}GB";
  388. } else if (sizeInKB >= 1024) { // 然后检查MB
  389. return "${(sizeInKB / 1024).toStringAsFixed(2)}MB";
  390. } else { // 最后是KB
  391. return "${sizeInKB.toStringAsFixed(2)}KB";
  392. }
  393. }
  394. }