screenshots_controller.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. for (var group in photoGroups) {
  82. for (int i = 0; i < group.images.length; i++) {
  83. if (selectedPhotosIds.contains(group.images[i].id)) {
  84. group.selectedImages[i] = true;
  85. }else {
  86. group.selectedImages[i] = false;
  87. }
  88. }
  89. group.isSelected.value =
  90. group.selectedImages.every((selected) => selected);
  91. }
  92. updateSelectedFilesSize();
  93. selectedFileCount.value = selectedPhotosIds.length;
  94. }
  95. @override
  96. void onInit() async {
  97. // TODO: implement onInit
  98. super.onInit();
  99. _getArgs();
  100. loadScreenshots();
  101. restoreSelections();
  102. }
  103. void _getArgs() {
  104. titleName = parameters?['titleName'];
  105. }
  106. void loadScreenshots() {
  107. // 清空现有数据
  108. photoGroups.clear();
  109. // 将 ImagePickerUtil 中的相似照片转换为 PhotoGroup
  110. if (titleName == "Screenshots") {
  111. final photoGroup = ImagePickerUtil.screenshotPhotos;
  112. if (photoGroup.isNotEmpty) {
  113. photoGroups.add(PhotoGroup(
  114. title: 'photo : ${photoGroup.length}',
  115. imageCount: photoGroup.length,
  116. isSelected: false,
  117. images: photoGroup,
  118. ));
  119. }
  120. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  121. } else if (titleName == "Blurry") {
  122. final photoGroup = ImagePickerUtil.blurryPhotos;
  123. if (photoGroup.isNotEmpty) {
  124. photoGroups.add(PhotoGroup(
  125. title: 'photo : ${photoGroup.length}',
  126. imageCount: photoGroup.length,
  127. isSelected: false,
  128. images: photoGroup,
  129. ));
  130. }
  131. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  132. }
  133. }
  134. @override
  135. void onClose() {
  136. // TODO: implement onClose
  137. super.onClose();
  138. if (titleName == "Screenshots") {
  139. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  140. } else if (titleName == "Blurry") {
  141. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  142. }
  143. }
  144. }