screenshots_controller.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. PhotoPreviewPage.start(PhotosType.screenshots, image.id);
  43. } else if (titleName == "Blurry") {
  44. PhotoPreviewPage.start(PhotosType.blurryPhotos, image.id);
  45. }
  46. }
  47. void toggleGroupSelection(String groupTitle) {
  48. final group = _getGroupByTitle(groupTitle);
  49. final newValue = !group.isSelected.value;
  50. group.toggleSelectAll(newValue);
  51. for (var image in group.images) {
  52. _updateSelectedScreenshotsPhotosIds(image.id, newValue);
  53. }
  54. updateSelectedFilesSize();
  55. selectedFileCount.value = selectedPhotosIds.length;
  56. }
  57. // 通过标题获取照片组
  58. PhotoGroup _getGroupByTitle(String groupTitle) {
  59. return photoGroups.firstWhere((g) => g.title == groupTitle);
  60. }
  61. // // 通过photoGroup的images来获取图片组
  62. // PhotoGroup _getGroupByImages(List<AssetEntity> images) {
  63. // return photoGroups.firstWhere((g) => g.images == images);
  64. // }
  65. void _updateSelectedScreenshotsPhotosIds(String photoId, bool isSelected) {
  66. if (isSelected) {
  67. selectedPhotosIds.add(photoId);
  68. } else {
  69. selectedPhotosIds.remove(photoId);
  70. }
  71. }
  72. void restoreSelections() {
  73. if (titleName == "Screenshots") {
  74. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  75. } else if (titleName == "Blurry") {
  76. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  77. }
  78. // print('screenshots restoreSelections ${selectedPhotosIds.length}');
  79. for (var group in photoGroups) {
  80. for (int i = 0; i < group.images.length; i++) {
  81. if (selectedPhotosIds.contains(group.images[i].id)) {
  82. group.selectedImages[i] = true;
  83. }else {
  84. group.selectedImages[i] = false;
  85. }
  86. }
  87. group.isSelected.value =
  88. group.selectedImages.every((selected) => selected);
  89. }
  90. updateSelectedFilesSize();
  91. selectedFileCount.value = selectedPhotosIds.length;
  92. }
  93. @override
  94. void onInit() async {
  95. // TODO: implement onInit
  96. super.onInit();
  97. _getArgs();
  98. loadScreenshots();
  99. restoreSelections();
  100. }
  101. void _getArgs() {
  102. titleName = parameters?['titleName'];
  103. }
  104. void loadScreenshots() {
  105. // 清空现有数据
  106. photoGroups.clear();
  107. // 将 ImagePickerUtil 中的相似照片转换为 PhotoGroup
  108. if (titleName == "Screenshots") {
  109. final photoGroup = ImagePickerUtil.screenshotPhotos;
  110. if (photoGroup.isNotEmpty) {
  111. photoGroups.add(PhotoGroup(
  112. title: 'photo : ${photoGroup.length}',
  113. imageCount: photoGroup.length,
  114. isSelected: false,
  115. images: photoGroup,
  116. ));
  117. }
  118. selectedPhotosIds.assignAll(ImagePickerUtil.selectedScreenshotPhotosIds);
  119. } else if (titleName == "Blurry") {
  120. final photoGroup = ImagePickerUtil.blurryPhotos;
  121. if (photoGroup.isNotEmpty) {
  122. photoGroups.add(PhotoGroup(
  123. title: 'photo : ${photoGroup.length}',
  124. imageCount: photoGroup.length,
  125. isSelected: false,
  126. images: photoGroup,
  127. ));
  128. }
  129. selectedPhotosIds.assignAll(ImagePickerUtil.selectedBlurryPhotosIds);
  130. }
  131. }
  132. @override
  133. void onClose() {
  134. // TODO: implement onClose
  135. super.onClose();
  136. if (titleName == "Screenshots") {
  137. ImagePickerUtil.selectedScreenshotPhotosIds.assignAll(selectedPhotosIds);
  138. } else if (titleName == "Blurry") {
  139. ImagePickerUtil.selectedBlurryPhotosIds.assignAll(selectedPhotosIds);
  140. }
  141. }
  142. }