people_photo_controller.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import 'dart:io';
  2. import 'package:clean/module/people_photo/photo_group.dart';
  3. import 'package:get/get.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:path/path.dart' as p;
  6. class PeoplePhotoController 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. double get selectedFilesSize {
  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. totalSize += File(group.images[i]).lengthSync();
  18. }
  19. }
  20. }
  21. return totalSize / 1024; // Convert to KB
  22. }
  23. void toggleImageSelection(String groupTitle, int imageIndex) {
  24. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  25. group.selectedImages[imageIndex] = !group.selectedImages[imageIndex];
  26. group.isSelected.value = group.selectedImages.every((selected) => selected);
  27. _saveSelections(); // 保存选择状态
  28. }
  29. void toggleGroupSelection(String groupTitle) {
  30. final group = photoGroups.firstWhere((g) => g.title == groupTitle);
  31. final newValue = !group.isSelected.value;
  32. group.toggleSelectAll(newValue);
  33. _saveSelections(); // 保存选择状态
  34. }
  35. @override
  36. void onInit() {
  37. super.onInit();
  38. loadPhotosFromDirectory().then((_) {
  39. // 恢复保存的选择状态
  40. if (_hasInitializedSelections) {
  41. _restoreSelections();
  42. }
  43. });
  44. }
  45. void _saveSelections() {
  46. for (var group in photoGroups) {
  47. _savedSelections[group.title] = group.selectedImages.toList();
  48. }
  49. _hasInitializedSelections = true;
  50. }
  51. void _restoreSelections() {
  52. for (var group in photoGroups) {
  53. // 这里假设每次页面加载时,已选择的状态会保存在 `RxList<bool>` 中。
  54. group.isSelected.value = group.selectedImages.every((selected) => selected);
  55. }
  56. }
  57. Future<void> loadPhotosFromDirectory() async {
  58. try {
  59. final Directory appDir = await getApplicationDocumentsDirectory();
  60. final String photosPath = '${appDir.path}/photos';
  61. final Directory photosDir = Directory(photosPath);
  62. if (!await photosDir.exists()) {
  63. return;
  64. }
  65. // 获取photos目录下的所有文件夹
  66. final List<Directory> subDirs = await photosDir
  67. .list()
  68. .where((entity) => entity is Directory)
  69. .map((e) => e as Directory)
  70. .toList();
  71. // 遍历每个文件夹
  72. for (final dir in subDirs) {
  73. // 获取文件夹中的所有图片
  74. final List<FileSystemEntity> files = await dir
  75. .list()
  76. .where((entity) =>
  77. entity is File &&
  78. ['.jpg', '.jpeg', '.png']
  79. .contains(p.extension(entity.path).toLowerCase()))
  80. .toList();
  81. if (files.isNotEmpty) {
  82. // 为每个文件夹创建一个PhotoGroup
  83. photoGroups.add(PhotoGroup(
  84. // title: '${p.basename(dir.path)}: ${files.length}', // 使用文件夹名作为标题
  85. title: 'people : ${files.length}', // 使用文件夹名作为标题
  86. imageCount: files.length,
  87. isSelected: false,
  88. images: files.map((file) => file.path).toList(),
  89. ));
  90. }
  91. }
  92. } catch (e) {
  93. print('Error loading photos: $e');
  94. }
  95. }
  96. }