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. '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. // 预加载
  87. // precacheImage(AssetEntityImageProvider(entity), context);
  88. // 如果direction是left,
  89. if (direction == CardSwiperDirection.left) {
  90. // 先看看图片id是不是在selectedPhotosIds里面,如果在,不处理,如果不在,添加到selectedPhotosIds里面
  91. if (!photoGroup.value.selectedPhotosIds
  92. .contains(photoGroup.value.images[previousIndex].id)) {
  93. debugPrint(
  94. 'add photoGroups[groupIndex.value].id ${photoGroup.value.images[previousIndex].id}');
  95. photoGroup.value.selectedPhotosIds
  96. .add(photoGroup.value.images[previousIndex].id);
  97. }
  98. } else if (direction == CardSwiperDirection.right) {
  99. // 先看看图片id是不是在selectedPhotosIds里面,如果在,在selectedPhotosIds移除,不处理,如果不在,不处理
  100. if (photoGroup.value.selectedPhotosIds
  101. .contains(photoGroup.value.images[previousIndex].id)) {
  102. debugPrint(
  103. 'remove photoGroups[groupIndex.value].id ${photoGroup.value.images[previousIndex].id}');
  104. photoGroup.value.selectedPhotosIds
  105. .remove(photoGroup.value.images[previousIndex].id);
  106. }
  107. }
  108. updateSelectedFilesSize();
  109. return true;
  110. }
  111. bool onSwiperUndo(
  112. int? previousIndex,
  113. int currentIndex,
  114. CardSwiperDirection direction,
  115. ) {
  116. debugPrint(
  117. 'The card $currentIndex was swiped back to the ${direction.name}. Now the card $previousIndex is on top');
  118. groupIndex.value = currentIndex;
  119. // 撤销之前左滑的操作
  120. if (direction == CardSwiperDirection.left) {
  121. debugPrint(
  122. 'photoGroups[groupIndex.value].id ${photoGroup.value.images[groupIndex.value].id}');
  123. if (photoGroup.value.selectedPhotosIds
  124. .contains(photoGroup.value.images[groupIndex.value].id)) {
  125. photoGroup.value.selectedPhotosIds
  126. .remove(photoGroup.value.images[groupIndex.value].id);
  127. }
  128. }
  129. updateSelectedFilesSize();
  130. return true;
  131. }
  132. onSwiperEnd() {
  133. isSwiperEnd.value = true;
  134. debugPrint('onSwiperEnd');
  135. // 延迟500ms
  136. Future.delayed(Duration(milliseconds: 200), () {
  137. animationController.forward(from: 0);
  138. });
  139. PhotoManager.clearFileCache();
  140. // PhotoSelectedPreviewPage.start(photosType, selectedPhotosIds);
  141. }
  142. void recoverSelectPhoto() {
  143. debugPrint('PhotoPreviewController recoverSelectPhoto');
  144. cardSwiperController.undo();
  145. }
  146. void clickSelect() {
  147. cardSwiperController.swipe(CardSwiperDirection.left);
  148. }
  149. void clickBack() {
  150. Get.back();
  151. }
  152. clickDelete() async {
  153. debugPrint('CalendarPreviewController clickDelete');
  154. if (userRepository.isVip()) {
  155. if (photoGroup.value.selectedPhotosIds.isNotEmpty) {
  156. photoDeletingDialog();
  157. // 获取要删除的资产
  158. final assetsToDelete = photoGroup.value.images
  159. .where(
  160. (asset) => photoGroup.value.selectedPhotosIds.contains(asset.id),
  161. )
  162. .toList();
  163. // 调用方法会返回被删除的资源,如果全部失败会返回空列表。
  164. final List<String> result = await PhotoManager.editor.deleteWithIds(
  165. assetsToDelete.map((e) => e.id).toList(),
  166. );
  167. // 比较result和selectedPhotosIds,如果result和selectedPhotosIds相等,说明删除成功,走下面的逻辑
  168. // 如果不相等,说明有删除失败的,走else逻辑
  169. if (result.length == photoGroup.value.selectedPhotosIds.length) {
  170. ImagePickerUtil.updatePhotoData(photoGroup.value.selectedPhotosIds);
  171. cleanSelections();
  172. ToastUtil.show('Delete success');
  173. Future.delayed(Duration(seconds: 2), () {
  174. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  175. photoDeleteFinishDialog();
  176. });
  177. } else {
  178. SmartDialog.dismiss(tag: 'photoDeletingDialog');
  179. // 删除失败
  180. ToastUtil.show("Delete failed");
  181. }
  182. }
  183. } else {
  184. StorePage.start();
  185. }
  186. }
  187. //删除成功清除选中的图片
  188. void cleanSelections() async {
  189. photoGroup.value.images.removeWhere(
  190. (element) => photoGroup.value.selectedPhotosIds.contains(element.id));
  191. photoGroup.value.selectedPhotosIds.clear();
  192. photoGroup.refresh();
  193. if (photoGroup.value.images.isEmpty) {
  194. return;
  195. }
  196. isSwiperEnd.value = false;
  197. if (photoGroup.value.images.isNotEmpty) {
  198. WidgetsBinding.instance.addPostFrameCallback((_) {
  199. groupIndex.value = 0;
  200. cardSwiperController.moveTo(0);
  201. });
  202. }
  203. updateSelectedFilesSize();
  204. }
  205. // 将selectedFilesSize转成String类型,然后单位转换,如果超过1MB,则转成MB,超过1GB,则转成GB,否则KB
  206. String get selectedFilesSizeString {
  207. final double sizeInKB = photoGroup.value.selectedTotalSize.value;
  208. if (sizeInKB >= 1024 * 1024) {
  209. // 先检查最大单位(GB)
  210. return "${(sizeInKB / (1024 * 1024)).toStringAsFixed(2)}GB";
  211. } else if (sizeInKB >= 1024) {
  212. // 然后检查MB
  213. return "${(sizeInKB / 1024).toStringAsFixed(2)}MB";
  214. } else {
  215. // 最后是KB
  216. return "${sizeInKB.toStringAsFixed(2)}KB";
  217. }
  218. }
  219. Future<void> updateSelectedFilesSize() async {
  220. // 如果没有选中的文件,直接返回
  221. if (photoGroup.value.selectedCount == 0) {
  222. photoGroup.value.selectedTotalSize.value = 0;
  223. return;
  224. }
  225. FileSizeCalculatorUtil.calculateTotalSize(
  226. assetIds: photoGroup.value.selectedPhotosIds,
  227. updateValue: (double totalSize) {
  228. photoGroup.value.selectedTotalSize.value = totalSize;
  229. },
  230. );
  231. }
  232. void restoreSelections() {
  233. if (photoGroup.value.images.isEmpty) {
  234. clickBack();
  235. return;
  236. }
  237. isSwiperEnd.value = false;
  238. if (photoGroup.value.images.isNotEmpty) {
  239. WidgetsBinding.instance.addPostFrameCallback((_) {
  240. groupIndex.value = 0;
  241. cardSwiperController.moveTo(0);
  242. });
  243. }
  244. updateSelectedFilesSize();
  245. }
  246. @override
  247. void onClose() {
  248. debugPrint('CalendarPreviewController onClose');
  249. animationController.dispose();
  250. super.onClose();
  251. isSwiperEnd.value = false;
  252. // 清理操作,释放资源
  253. cardSwiperController.dispose();
  254. // 清理缓存
  255. PhotoManager.clearFileCache();
  256. }
  257. }