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. 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 _assignPhotosAndSelections(BasePhotoController controller) {
  92. listAssetEntity.assignAll(controller.getAllPhotos());
  93. selectedPhotosIds.assignAll(controller.selectedPhotosIds);
  94. selectedFileCount.value = selectedPhotosIds.length;
  95. }
  96. void _initData() {
  97. listAssetEntity.clear();
  98. selectedPhotosIds.clear();
  99. switch (photosType) {
  100. case PhotosType.peoplePhotos:
  101. final PeoplePhotoController controller =
  102. Get.find<PeoplePhotoController>();
  103. _assignPhotosAndSelections(controller);
  104. break;
  105. case PhotosType.screenshots:
  106. final ScreenShotsController controller =
  107. Get.find<ScreenShotsController>();
  108. _assignPhotosAndSelections(controller);
  109. break;
  110. case PhotosType.similarPhotos:
  111. final SimilarPhotoController controller =
  112. Get.find<SimilarPhotoController>();
  113. _assignPhotosAndSelections(controller);
  114. break;
  115. case PhotosType.locationPhotos:
  116. final LocationsSinglePhotoController controller =
  117. Get.find<LocationsSinglePhotoController>();
  118. _assignPhotosAndSelections(controller);
  119. break;
  120. case PhotosType.blurryPhotos:
  121. final ScreenShotsController controller =
  122. Get.find<ScreenShotsController>();
  123. _assignPhotosAndSelections(controller);
  124. break;
  125. }
  126. updateSelectedFilesSize();
  127. }
  128. //删除成功清除选中的图片
  129. void cleanSelections() async {
  130. listAssetEntity
  131. .removeWhere((element) => selectedPhotosIds.contains(element.id));
  132. selectedPhotosIds.clear();
  133. if (listAssetEntity.isEmpty) {
  134. return;
  135. }
  136. isSwiperEnd.value = false;
  137. if (listAssetEntity.isNotEmpty) {
  138. WidgetsBinding.instance.addPostFrameCallback((_) {
  139. groupIndex.value = 0;
  140. cardSwiperController.value.moveTo(0);
  141. });
  142. }
  143. updateSelectedFilesSize();
  144. selectedFileCount.value = selectedPhotosIds.length;
  145. }
  146. // 恢复状态
  147. void restoreSelections() {
  148. if (listAssetEntity.isEmpty) {
  149. clickBack();
  150. return;
  151. }
  152. isSwiperEnd.value = false;
  153. if (listAssetEntity.isNotEmpty) {
  154. WidgetsBinding.instance.addPostFrameCallback((_) {
  155. groupIndex.value = 0;
  156. cardSwiperController.value.moveTo(0);
  157. });
  158. }
  159. updateSelectedFilesSize();
  160. selectedFileCount.value = selectedPhotosIds.length;
  161. }
  162. Future<void> updateSelectedFilesSize() async {
  163. // 如果没有选中的文件,直接返回
  164. if (selectedPhotosIds.isEmpty) {
  165. selectedFilesSize.value = 0;
  166. return;
  167. }
  168. double totalSize = 0;
  169. // 通过selectedPhotosIds获取选中的图片,然后计算大小
  170. for (var id in selectedPhotosIds) {
  171. // 检查缓存
  172. if (ImagePickerUtil.fileSizeCache.containsKey(id)) {
  173. totalSize += ImagePickerUtil.fileSizeCache[id]!;
  174. } else {
  175. final entity = await AssetEntity.fromId(id);
  176. if (entity != null) {
  177. final file = await entity.file;
  178. if (file != null) {
  179. double size = await file.length() / 1024; // 转换为KB
  180. totalSize += size;
  181. // 缓存文件大小
  182. ImagePickerUtil.fileSizeCache[id] = size;
  183. }
  184. }
  185. }
  186. }
  187. selectedFilesSize.value = totalSize; // 更新总大小
  188. PhotoManager.clearFileCache();
  189. }
  190. void updateSelections(Set<String> selectedIds) {
  191. print(
  192. 'PhotoPreviewController updateSelections photosType $photosType selectedIds $selectedIds');
  193. selectedPhotosIds.assignAll(selectedIds);
  194. restoreSelections();
  195. }
  196. void recoverSelectPhoto() {
  197. print('PhotoPreviewController recoverSelectPhoto');
  198. cardSwiperController.value.undo();
  199. }
  200. void clickSelect() {
  201. cardSwiperController.value.swipe(CardSwiperDirection.left);
  202. }
  203. @override
  204. void onClose() {
  205. print('PhotoPreviewController onClose');
  206. animationController.dispose();
  207. super.onClose();
  208. isSwiperEnd.value = false;
  209. // 清理操作,释放资源
  210. cardSwiperController.value.dispose();
  211. // 清理缓存
  212. PhotoManager.clearFileCache();
  213. }
  214. void clickBack() {
  215. _saveSelectedPhotos(photosType);
  216. Get.back();
  217. }
  218. // // 保存选择的图片ID列表
  219. void _saveSelectedPhotos(PhotosType type) {
  220. switch (type) {
  221. case PhotosType.peoplePhotos:
  222. PeoplePhotoController controller = Get.find<PeoplePhotoController>();
  223. controller.updateSelections(selectedPhotosIds);
  224. controller.restoreSelections();
  225. break;
  226. case PhotosType.screenshots:
  227. ScreenShotsController controller = Get.find<ScreenShotsController>();
  228. controller.updateSelections(selectedPhotosIds);
  229. controller.restoreSelections();
  230. break;
  231. case PhotosType.similarPhotos:
  232. SimilarPhotoController controller = Get.find<SimilarPhotoController>();
  233. controller.updateSelections(selectedPhotosIds);
  234. controller.restoreSelections();
  235. break;
  236. case PhotosType.locationPhotos:
  237. LocationsSinglePhotoController controller =
  238. Get.find<LocationsSinglePhotoController>();
  239. controller.updateSelections(selectedPhotosIds);
  240. controller.restoreSelections();
  241. break;
  242. case PhotosType.blurryPhotos:
  243. ScreenShotsController controller = Get.find<ScreenShotsController>();
  244. controller.updateSelections(selectedPhotosIds);
  245. controller.restoreSelections();
  246. break;
  247. }
  248. }
  249. void clickUnselect() {
  250. print("clickUnselect");
  251. cardSwiperController.value.swipe(CardSwiperDirection.right);
  252. }
  253. FutureOr<bool> onSwipe(
  254. int previousIndex,
  255. int? currentIndex,
  256. CardSwiperDirection direction,
  257. ) {
  258. print(
  259. 'The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  260. );
  261. if (currentIndex != null) {
  262. groupIndex.value = currentIndex;
  263. }
  264. // 如果direction是left,
  265. if (direction == CardSwiperDirection.left) {
  266. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  267. if (!selectedPhotosIds.contains(listAssetEntity[previousIndex].id)) {
  268. print(
  269. 'add photoGroups[groupIndex.value].id ${listAssetEntity[previousIndex].id}');
  270. selectedPhotosIds.add(listAssetEntity[previousIndex].id);
  271. }
  272. } else if (direction == CardSwiperDirection.right) {
  273. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  274. if (selectedPhotosIds.contains(listAssetEntity[previousIndex].id)) {
  275. print(
  276. 'remove photoGroups[groupIndex.value].id ${listAssetEntity[previousIndex].id}');
  277. selectedPhotosIds.remove(listAssetEntity[previousIndex].id);
  278. }
  279. }
  280. selectedFileCount.value = selectedPhotosIds.length;
  281. updateSelectedFilesSize();
  282. return true;
  283. }
  284. bool onSwiperUndo(
  285. int? previousIndex,
  286. int currentIndex,
  287. CardSwiperDirection direction,
  288. ) {
  289. print(
  290. 'The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  291. groupIndex.value = currentIndex;
  292. // 撤销之前左滑的操作
  293. if (direction == CardSwiperDirection.left) {
  294. print(
  295. 'photoGroups[groupIndex.value].id ${listAssetEntity[groupIndex.value].id}');
  296. if (selectedPhotosIds.contains(listAssetEntity[groupIndex.value].id)) {
  297. selectedPhotosIds.remove(listAssetEntity[groupIndex.value].id);
  298. }
  299. }
  300. selectedFileCount.value = selectedPhotosIds.length;
  301. updateSelectedFilesSize();
  302. PhotoManager.clearFileCache();
  303. return true;
  304. }
  305. onSwiperEnd() {
  306. isSwiperEnd.value = true;
  307. print('onSwiperEnd');
  308. // 延迟500ms
  309. Future.delayed(Duration(milliseconds: 200), () {
  310. animationController.forward(from: 0);
  311. });
  312. PhotoManager.clearFileCache();
  313. // PhotoSelectedPreviewPage.start(photosType, selectedPhotosIds);
  314. }
  315. clickDelete() async {
  316. print('clickDelete');
  317. if (userRepository.isVip()) {
  318. switch (photosType) {
  319. case PhotosType.peoplePhotos:
  320. ImagePickerUtil.selectedPeoplePhotosIds.assignAll(selectedPhotosIds);
  321. break;
  322. case PhotosType.screenshots:
  323. ImagePickerUtil.selectedScreenshotPhotosIds
  324. .assignAll(selectedPhotosIds);
  325. break;
  326. case PhotosType.similarPhotos:
  327. ImagePickerUtil.selectedSimilarPhotosIds.assignAll(selectedPhotosIds);
  328. break;
  329. case PhotosType.locationPhotos:
  330. ImagePickerUtil.selectedLocationPhotosIds
  331. .assignAll(selectedPhotosIds);
  332. break;
  333. case PhotosType.blurryPhotos:
  334. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  335. break;
  336. }
  337. if (selectedPhotosIds.isNotEmpty) {
  338. photoDeletingDialog();
  339. // 获取要删除的资产
  340. final assetsToDelete = listAssetEntity
  341. .where(
  342. (asset) => selectedPhotosIds.contains(asset.id),
  343. )
  344. .toList();
  345. // 调用方法会返回被删除的资源,如果全部失败会返回空列表。
  346. final List<String> result = await PhotoManager.editor.deleteWithIds(
  347. assetsToDelete.map((e) => e.id).toList(),
  348. );
  349. print('PhotoPreviewController result $result');
  350. // 比较result和selectedPhotosIds,如果result和selectedPhotosIds相等,说明删除成功,走下面的逻辑
  351. // 如果不相等,说明有删除失败的,走else逻辑
  352. if (result.length == selectedPhotosIds.length) {
  353. switch (photosType) {
  354. case PhotosType.peoplePhotos:
  355. ImagePickerUtil.updatePhotoGroupDate(
  356. photosType, ImagePickerUtil.selectedPeoplePhotosIds);
  357. break;
  358. case PhotosType.screenshots:
  359. ImagePickerUtil.updatePhotoGroupDate(
  360. photosType, ImagePickerUtil.selectedScreenshotPhotosIds);
  361. break;
  362. case PhotosType.similarPhotos:
  363. ImagePickerUtil.updatePhotoGroupDate(
  364. photosType, ImagePickerUtil.selectedSimilarPhotosIds);
  365. break;
  366. case PhotosType.locationPhotos:
  367. ImagePickerUtil.updatePhotoGroupDate(
  368. photosType, ImagePickerUtil.selectedLocationPhotosIds);
  369. break;
  370. case PhotosType.blurryPhotos:
  371. ImagePickerUtil.updatePhotoGroupDate(
  372. photosType, ImagePickerUtil.selectedBlurryPhotosIds);
  373. break;
  374. }
  375. cleanSelections();
  376. ToastUtil.show('Delete success');
  377. Future.delayed(Duration(seconds: 2), () {
  378. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  379. photoDeleteFinishDialog();
  380. });
  381. } else {
  382. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  383. // 删除失败
  384. ToastUtil.show("Delete failed");
  385. }
  386. }
  387. } else {
  388. StorePage.start();
  389. }
  390. // 清理缓存
  391. PhotoManager.clearFileCache();
  392. }
  393. // 将selectedFilesSize转成String类型,然后单位转换,如果超过1MB,则转成MB,超过1GB,则转成GB,否则KB
  394. String get selectedFilesSizeString {
  395. if (selectedFilesSize.value > 1024) {
  396. return "${(selectedFilesSize.value / 1024).toStringAsFixed(2)}MB";
  397. } else if (selectedFilesSize.value > 1024 * 1024) {
  398. return "${(selectedFilesSize.value / 1024 / 1024).toStringAsFixed(2)}GB";
  399. } else {
  400. return "${(selectedFilesSize.value).toStringAsFixed(2)}KB";
  401. }
  402. }
  403. }