calendar_preview_controller.dart 10 KB

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