calendar_preview_controller.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import 'dart:async';
  2. import 'package:clean/base/base_controller.dart';
  3. import 'package:clean/module/calendar/selected_preview/calendar_selected_preview_view.dart';
  4. import 'package:flutter/Material.dart';
  5. import 'package:flutter_card_swiper/flutter_card_swiper.dart';
  6. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  7. import 'package:get/get.dart';
  8. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  9. import '../../../data/repositories/user_repository.dart';
  10. import '../../../dialog/photo_delete_finish_dialog.dart';
  11. import '../../../dialog/photo_deleting_dialog.dart';
  12. import '../../../dialog/play_video_dialog.dart';
  13. import '../../../utils/file_size_calculator_util.dart';
  14. import '../../../utils/toast_util.dart';
  15. import '../../image_picker/image_picker_util.dart';
  16. import '../../people_photo/photo_group.dart';
  17. import '../../store/store_view.dart';
  18. class CalendarPreviewController extends BaseController
  19. with GetSingleTickerProviderStateMixin {
  20. final Rx<PhotoGroup> photoGroup =
  21. PhotoGroup(isSelected: false, images: []).obs;
  22. late String? currentImageId;
  23. final CardSwiperController cardSwiperController = CardSwiperController();
  24. final RxBool isSwiperEnd = false.obs;
  25. RxInt groupIndex = 0.obs;
  26. late AnimationController animationController;
  27. RxBool animationIsComplete = false.obs;
  28. void playVideo(AssetEntity asset) async {
  29. playVideoDialog(asset);
  30. }
  31. @override
  32. void onInit() {
  33. super.onInit();
  34. _getArgs(); // 获取传递的参数
  35. animationController = AnimationController(
  36. vsync: this,
  37. duration: const Duration(seconds: 3),
  38. );
  39. WidgetsBinding.instance.addPostFrameCallback((_) {
  40. if (currentImageId != null) {
  41. for (int i = 0; i < photoGroup.value.images.length; i++) {
  42. if (photoGroup.value.images[i].id == currentImageId) {
  43. debugPrint(
  44. 'photoGroups[i].id ${photoGroup.value.images[i].id},i $i');
  45. groupIndex.value = i;
  46. cardSwiperController.moveTo(i);
  47. break;
  48. }
  49. }
  50. }
  51. });
  52. animationController.addStatusListener((status) {
  53. if (status == AnimationStatus.forward) {
  54. // 延迟一秒
  55. Future.delayed(Duration(seconds: 1), () {
  56. animationIsComplete.value = true;
  57. });
  58. }
  59. if (status == AnimationStatus.completed) {
  60. Future.delayed(Duration(seconds: 1), () {
  61. CalendarSelectedPreviewPage.start(photoGroup.value);
  62. });
  63. }
  64. });
  65. }
  66. // 获取参数
  67. void _getArgs() {
  68. photoGroup.value = parameters?['photoGroup'];
  69. currentImageId = parameters?['currentImageId'];
  70. }
  71. void clickUnselect() {
  72. debugPrint("clickUnselect");
  73. cardSwiperController.swipe(CardSwiperDirection.right);
  74. }
  75. FutureOr<bool> onSwipe(
  76. int previousIndex,
  77. int? currentIndex,
  78. CardSwiperDirection direction,
  79. ) {
  80. debugPrint(
  81. 'onSwipe The card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
  82. );
  83. if (currentIndex != null) {
  84. groupIndex.value = currentIndex;
  85. }
  86. // 如果direction是left,
  87. if (direction == CardSwiperDirection.left) {
  88. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  89. if (!photoGroup.value.selectedPhotosIds
  90. .contains(photoGroup.value.images[previousIndex].id)) {
  91. debugPrint(
  92. 'add photoGroups[groupIndex.value].id ${photoGroup.value.images[previousIndex].id}');
  93. photoGroup.value.selectedPhotosIds
  94. .add(photoGroup.value.images[previousIndex].id);
  95. }
  96. } else if (direction == CardSwiperDirection.right) {
  97. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  98. if (photoGroup.value.selectedPhotosIds
  99. .contains(photoGroup.value.images[previousIndex].id)) {
  100. debugPrint(
  101. 'remove photoGroups[groupIndex.value].id ${photoGroup.value.images[previousIndex].id}');
  102. photoGroup.value.selectedPhotosIds
  103. .remove(photoGroup.value.images[previousIndex].id);
  104. }
  105. }
  106. updateSelectedFilesSize();
  107. return true;
  108. }
  109. bool onSwiperUndo(
  110. int? previousIndex,
  111. int currentIndex,
  112. CardSwiperDirection direction,
  113. ) {
  114. debugPrint(
  115. 'onSwiperUndo The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  116. groupIndex.value = currentIndex;
  117. // 撤销之前左滑的操作
  118. if (direction == CardSwiperDirection.left) {
  119. debugPrint(
  120. 'photoGroups[groupIndex.value].id ${photoGroup.value.images[groupIndex.value].id}');
  121. if (photoGroup.value.selectedPhotosIds
  122. .contains(photoGroup.value.images[groupIndex.value].id)) {
  123. photoGroup.value.selectedPhotosIds
  124. .remove(photoGroup.value.images[groupIndex.value].id);
  125. }
  126. }
  127. updateSelectedFilesSize();
  128. return true;
  129. }
  130. onSwiperEnd() {
  131. isSwiperEnd.value = true;
  132. debugPrint('onSwiperEnd');
  133. // 延迟500ms
  134. Future.delayed(Duration(milliseconds: 200), () {
  135. animationController.forward(from: 0);
  136. });
  137. PhotoManager.clearFileCache();
  138. // PhotoSelectedPreviewPage.start(photosType, selectedPhotosIds);
  139. }
  140. void recoverSelectPhoto() {
  141. debugPrint('PhotoPreviewController recoverSelectPhoto');
  142. cardSwiperController.undo();
  143. }
  144. void clickSelect() {
  145. cardSwiperController.swipe(CardSwiperDirection.left);
  146. }
  147. void clickBack() {
  148. Get.back();
  149. }
  150. clickDelete() async {
  151. debugPrint('CalendarPreviewController clickDelete');
  152. if (userRepository.isVip()) {
  153. if (photoGroup.value.selectedPhotosIds.isNotEmpty) {
  154. photoDeletingDialog();
  155. // 获取要删除的资产
  156. final assetsToDelete = photoGroup.value.images
  157. .where(
  158. (asset) => photoGroup.value.selectedPhotosIds.contains(asset.id),
  159. )
  160. .toList();
  161. // 调用方法会返回被删除的资源,如果全部失败会返回空列表。
  162. final List<String> result = await PhotoManager.editor.deleteWithIds(
  163. assetsToDelete.map((e) => e.id).toList(),
  164. );
  165. // 比较result和selectedPhotosIds,如果result和selectedPhotosIds相等,说明删除成功,走下面的逻辑
  166. // 如果不相等,说明有删除失败的,走else逻辑
  167. if (result.length == photoGroup.value.selectedPhotosIds.length) {
  168. ImagePickerUtil.updatePhotoData(photoGroup.value.selectedPhotosIds);
  169. cleanSelections();
  170. ToastUtil.show('Delete success');
  171. Future.delayed(Duration(seconds: 2), () {
  172. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  173. photoDeleteFinishDialog();
  174. });
  175. } else {
  176. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  177. // 删除失败
  178. ToastUtil.show("Delete failed");
  179. }
  180. }
  181. } else {
  182. StorePage.start();
  183. }
  184. }
  185. //删除成功清除选中的图片
  186. void cleanSelections() async {
  187. photoGroup.value.images.removeWhere(
  188. (element) => photoGroup.value.selectedPhotosIds.contains(element.id));
  189. photoGroup.value.selectedPhotosIds.clear();
  190. photoGroup.refresh();
  191. if (photoGroup.value.images.isEmpty) {
  192. return;
  193. }
  194. isSwiperEnd.value = false;
  195. if (photoGroup.value.images.isNotEmpty) {
  196. WidgetsBinding.instance.addPostFrameCallback((_) {
  197. groupIndex.value = 0;
  198. cardSwiperController.moveTo(0);
  199. });
  200. }
  201. updateSelectedFilesSize();
  202. }
  203. // 将selectedFilesSize转成String类型,然后单位转换,如果超过1MB,则转成MB,超过1GB,则转成GB,否则KB
  204. String get selectedFilesSizeString {
  205. final double sizeInKB = photoGroup.value.selectedTotalSize.value;
  206. if (sizeInKB >= 1024 * 1024) {
  207. // 先检查最大单位(GB)
  208. return "${(sizeInKB / (1024 * 1024)).toStringAsFixed(2)}GB";
  209. } else if (sizeInKB >= 1024) {
  210. // 然后检查MB
  211. return "${(sizeInKB / 1024).toStringAsFixed(2)}MB";
  212. } else {
  213. // 最后是KB
  214. return "${sizeInKB.toStringAsFixed(2)}KB";
  215. }
  216. }
  217. Future<void> updateSelectedFilesSize() async {
  218. // 如果没有选中的文件,直接返回
  219. if (photoGroup.value.selectedCount == 0) {
  220. photoGroup.value.selectedTotalSize.value = 0;
  221. return;
  222. }
  223. FileSizeCalculatorUtil.calculateTotalSize(
  224. assetIds: photoGroup.value.selectedPhotosIds,
  225. updateValue: (double totalSize) {
  226. photoGroup.value.selectedTotalSize.value = totalSize;
  227. },
  228. );
  229. PhotoManager.clearFileCache();
  230. }
  231. void restoreSelections() {
  232. if (photoGroup.value.images.isEmpty) {
  233. clickBack();
  234. return;
  235. }
  236. isSwiperEnd.value = false;
  237. if (photoGroup.value.images.isNotEmpty) {
  238. WidgetsBinding.instance.addPostFrameCallback((_) {
  239. groupIndex.value = 0;
  240. cardSwiperController.moveTo(0);
  241. });
  242. }
  243. updateSelectedFilesSize();
  244. }
  245. @override
  246. void onClose() {
  247. debugPrint('CalendarPreviewController onClose');
  248. animationController.dispose();
  249. super.onClose();
  250. isSwiperEnd.value = false;
  251. // 清理操作,释放资源
  252. cardSwiperController.dispose();
  253. // 清理缓存
  254. PhotoManager.clearFileCache();
  255. }
  256. }