people_photo_controller.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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:path_provider/path_provider.dart';
  6. import 'package:path/path.dart' as p;
  7. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  8. class PeoplePhotoController extends GetxController {
  9. final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
  10. static final Map<String, List<bool>> _savedSelections = {};
  11. static bool _hasInitializedSelections = false;
  12. int get selectedFileCount =>
  13. photoGroups.fold(0, (sum, group) => sum + group.selectedCount);
  14. Future<double> getSelectedFilesSize() async {
  15. double totalSize = 0;
  16. for (var group in photoGroups) {
  17. for (int i = 0; i < group.images.length; i++) {
  18. if (group.selectedImages[i]) {
  19. final file = await group.images[i].file;
  20. if (file != null) {
  21. totalSize += file.lengthSync();
  22. }
  23. }
  24. }
  25. }
  26. return totalSize / 1024; // Convert to KB
  27. }
  28. void toggleImageSelection(String groupTitle, int imageIndex) {
  29. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  30. group.selectedImages[imageIndex] = !group.selectedImages[imageIndex];
  31. group.isSelected.value = group.selectedImages.every((selected) => selected);
  32. _saveSelections(); // 保存选择状态
  33. }
  34. void toggleGroupSelection(String groupTitle) {
  35. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  36. final newValue = !group.isSelected.value;
  37. group.toggleSelectAll(newValue);
  38. _saveSelections(); // 保存选择状态
  39. }
  40. @override
  41. void onInit() {
  42. super.onInit();
  43. loadPeoplePhoto();
  44. if (_hasInitializedSelections) {
  45. _restoreSelections();
  46. }
  47. // loadPhotosFromDirectory().then((_) {
  48. // // 恢复保存的选择状态
  49. // if (_hasInitializedSelections) {
  50. // _restoreSelections();
  51. // }
  52. // });
  53. }
  54. void _saveSelections() {
  55. for (var group in photoGroups) {
  56. _savedSelections[group.title] = group.selectedImages.toList();
  57. }
  58. _hasInitializedSelections = true;
  59. }
  60. void _restoreSelections() {
  61. for (var group in photoGroups) {
  62. // 这里假设每次页面加载时,已选择的状态会保存在 `RxList<bool>` 中。
  63. group.isSelected.value = group.selectedImages.every((selected) => selected);
  64. }
  65. }
  66. Future<void> loadPhotosFromDirectory() async {
  67. try {
  68. final List<AssetEntity>? result = await AssetPicker.pickAssets(
  69. Get.context!,
  70. );
  71. if (result != null && result.isNotEmpty) {
  72. photoGroups.add(PhotoGroup(
  73. title: 'photo: ${result.length}',
  74. imageCount: result.length,
  75. isSelected: false,
  76. images: result,
  77. ));
  78. }
  79. } catch (e) {
  80. print('Error loading photos: $e');
  81. }
  82. }
  83. void loadPeoplePhoto() {
  84. photoGroups.clear();
  85. final photoGroup = ImagePickerUtil.peoplePhotos;
  86. if (photoGroup.isNotEmpty) {
  87. photoGroups.add(PhotoGroup(
  88. title: 'photo : ${photoGroup.length}',
  89. imageCount: photoGroup.length,
  90. isSelected: false,
  91. images: photoGroup,
  92. ));
  93. }
  94. }
  95. }