calendar_preview_controller.dart 10 KB

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