screenshots_controller.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:clean/base/base_controller.dart';
  2. import 'package:clean/data/bean/photos_type.dart';
  3. import 'package:clean/module/image_picker/image_picker_util.dart';
  4. import 'package:clean/module/people_photo/photo_group.dart';
  5. import 'package:clean/module/photo_preview/photo_preview_view.dart';
  6. import 'package:get/get.dart';
  7. class ScreenShotsController extends BaseController {
  8. late String titleName;
  9. final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
  10. final RxDouble selectedFilesSize = 0.0.obs;
  11. RxInt selectedFileCount = 0.obs;
  12. final RxList<String> selectedPhotosIds = <String>[].obs;
  13. Future<void> updateSelectedFilesSize() async {
  14. double totalSize = 0;
  15. for (var group in photoGroups) {
  16. for (int i = 0; i < group.images.length; i++) {
  17. if (group.selectedImages[i]) {
  18. final file = await group.images[i].file;
  19. if (file != null) {
  20. totalSize += file.lengthSync();
  21. }
  22. }
  23. }
  24. }
  25. selectedFilesSize.value = totalSize / 1024; // Convert to KB
  26. }
  27. void toggleImageSelection(String groupTitle, int imageIndex) {
  28. final group = _getGroupByTitle(groupTitle);
  29. final image = group.images[imageIndex];
  30. final selected = !group.selectedImages[imageIndex];
  31. group.selectedImages[imageIndex] = selected;
  32. _updateSelectedScreenshotsPhotosIds(image.id, selected);
  33. group.isSelected.value = group.selectedImages.every((selected) => selected);
  34. updateSelectedFilesSize();
  35. selectedFileCount.value = selectedPhotosIds.length;
  36. }
  37. void clickImage(String groupTitle, int imageIndex) {
  38. final group = _getGroupByTitle(groupTitle);
  39. final image = group.images[imageIndex];
  40. print('clickImage $groupTitle $imageIndex');
  41. if (titleName == "Screenshots") {
  42. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  43. PhotoPreviewPage.start(PhotosType.screenshots, image.id);
  44. } else if (titleName == "Blurry") {
  45. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  46. PhotoPreviewPage.start(PhotosType.blurryPhotos, image.id);
  47. }
  48. }
  49. void toggleGroupSelection(String groupTitle) {
  50. final group = _getGroupByTitle(groupTitle);
  51. final newValue = !group.isSelected.value;
  52. group.toggleSelectAll(newValue);
  53. for (var image in group.images) {
  54. _updateSelectedScreenshotsPhotosIds(image.id, newValue);
  55. }
  56. updateSelectedFilesSize();
  57. selectedFileCount.value = selectedPhotosIds.length;
  58. }
  59. // 通过标题获取照片组
  60. PhotoGroup _getGroupByTitle(String groupTitle) {
  61. return photoGroups.firstWhere((g) => g.title == groupTitle);
  62. }
  63. // // 通过photoGroup的images来获取图片组
  64. // PhotoGroup _getGroupByImages(List<AssetEntity> images) {
  65. // return photoGroups.firstWhere((g) => g.images == images);
  66. // }
  67. void _updateSelectedScreenshotsPhotosIds(String photoId, bool isSelected) {
  68. if (isSelected) {
  69. selectedPhotosIds.add(photoId);
  70. } else {
  71. selectedPhotosIds.remove(photoId);
  72. }
  73. }
  74. void restoreSelections() {
  75. if (titleName == "Screenshots") {
  76. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  77. } else if (titleName == "Blurry") {
  78. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  79. }
  80. // print('screenshots restoreSelections ${selectedPhotosIds.length}');
  81. if (selectedPhotosIds.isEmpty) {
  82. return;
  83. }
  84. for (var group in photoGroups) {
  85. for (int i = 0; i < group.images.length; i++) {
  86. if (selectedPhotosIds.contains(group.images[i].id)) {
  87. group.selectedImages[i] = true;
  88. }else {
  89. group.selectedImages[i] = false;
  90. }
  91. }
  92. group.isSelected.value =
  93. group.selectedImages.every((selected) => selected);
  94. }
  95. updateSelectedFilesSize();
  96. selectedFileCount.value = selectedPhotosIds.length;
  97. }
  98. @override
  99. void onInit() async {
  100. // TODO: implement onInit
  101. super.onInit();
  102. _getArgs();
  103. loadScreenshots();
  104. restoreSelections();
  105. }
  106. void _getArgs() {
  107. titleName = parameters?['titleName'];
  108. }
  109. void loadScreenshots() {
  110. // 清空现有数据
  111. photoGroups.clear();
  112. // 将 ImagePickerUtil 中的相似照片转换为 PhotoGroup
  113. if (titleName == "Screenshots") {
  114. final photoGroup = ImagePickerUtil.screenshotPhotos;
  115. if (photoGroup.isNotEmpty) {
  116. photoGroups.add(PhotoGroup(
  117. title: 'photo : ${photoGroup.length}',
  118. imageCount: photoGroup.length,
  119. isSelected: false,
  120. images: photoGroup,
  121. ));
  122. }
  123. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  124. } else if (titleName == "Blurry") {
  125. final photoGroup = ImagePickerUtil.blurryPhotos;
  126. if (photoGroup.isNotEmpty) {
  127. photoGroups.add(PhotoGroup(
  128. title: 'photo : ${photoGroup.length}',
  129. imageCount: photoGroup.length,
  130. isSelected: false,
  131. images: photoGroup,
  132. ));
  133. }
  134. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  135. }
  136. }
  137. @override
  138. void onClose() {
  139. // TODO: implement onClose
  140. super.onClose();
  141. if (titleName == "Screenshots") {
  142. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  143. } else if (titleName == "Blurry") {
  144. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  145. }
  146. }
  147. }