similar_photo_controller.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'dart:io';
  2. import 'package:clean/module/image_picker/image_picker_util.dart';
  3. import 'package:clean/module/people_photo/photo_group.dart';
  4. import 'package:get/get.dart';
  5. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  6. class SimilarPhotoController extends GetxController {
  7. final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
  8. static final Map<String, List<bool>> _savedSelections = {};
  9. static bool _hasInitializedSelections = false;
  10. int get selectedFileCount =>
  11. photoGroups.fold(0, (sum, group) => sum + group.selectedCount);
  12. Future<double> getSelectedFilesSize() async {
  13. double totalSize = 0;
  14. for (var group in photoGroups) {
  15. for (int i = 0; i < group.images.length; i++) {
  16. if (group.selectedImages[i]) {
  17. final file = await group.images[i].file;
  18. if (file != null) {
  19. totalSize += file.lengthSync();
  20. }
  21. }
  22. }
  23. }
  24. return totalSize / 1024; // Convert to KB
  25. }
  26. void toggleImageSelection(String groupTitle, int imageIndex) {
  27. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  28. group.selectedImages[imageIndex] = !group.selectedImages[imageIndex];
  29. group.isSelected.value = group.selectedImages.every((selected) => selected);
  30. _saveSelections();
  31. }
  32. void toggleGroupSelection(String groupTitle) {
  33. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  34. final newValue = !group.isSelected.value;
  35. group.toggleSelectAll(newValue);
  36. _saveSelections();
  37. }
  38. void _saveSelections() {
  39. for (var group in photoGroups) {
  40. _savedSelections[group.title] = group.selectedImages.toList();
  41. }
  42. _hasInitializedSelections = true;
  43. }
  44. void _restoreSelections() {
  45. for (var group in photoGroups) {
  46. // 这里假设每次页面加载时,已选择的状态会保存在 `RxList<bool>` 中。
  47. group.isSelected.value =
  48. group.selectedImages.every((selected) => selected);
  49. }
  50. }
  51. @override
  52. void onInit() {
  53. super.onInit();
  54. loadSimilarPhotos();
  55. if (_hasInitializedSelections) {
  56. _restoreSelections();
  57. }
  58. // loadPhotosFromDirectory().then((_) {
  59. //
  60. // });
  61. }
  62. Future<void> loadPhotosFromDirectory() async {
  63. try {
  64. final List<AssetEntity>? result = await AssetPicker.pickAssets(
  65. Get.context!,
  66. );
  67. if (result != null && result.isNotEmpty) {
  68. photoGroups.add(PhotoGroup(
  69. title: 'photo: ${result.length}',
  70. imageCount: result.length,
  71. isSelected: false,
  72. images: result,
  73. ));
  74. }
  75. } catch (e) {
  76. print('Error loading photos: $e');
  77. }
  78. }
  79. void loadSimilarPhotos() {
  80. // 清空现有数据
  81. photoGroups.clear();
  82. // 将 ImagePickerUtil 中的相似照片转换为 PhotoGroup
  83. for (int i = 0; i < ImagePickerUtil.similarPhotos.length; i++) {
  84. final photoGroup = ImagePickerUtil.similarPhotos[i];
  85. if (photoGroup.isNotEmpty) {
  86. photoGroups.add(PhotoGroup(
  87. title: 'photo : ${photoGroup.length}',
  88. imageCount: photoGroup.length,
  89. isSelected: false,
  90. images: photoGroup,
  91. ));
  92. }
  93. }
  94. }
  95. // 修改视图构建方法,使用 AssetEntity 而不是文件路径
  96. AssetEntity? getAssetById(String id) {
  97. for (var group in ImagePickerUtil.similarPhotos) {
  98. for (var asset in group) {
  99. if (asset.id == id) {
  100. return asset;
  101. }
  102. }
  103. }
  104. return null;
  105. }
  106. }