| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import 'dart:io';
- import 'package:clean/module/people_photo/photo_group.dart';
- import 'package:get/get.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:path/path.dart' as p;
- class PeoplePhotoController extends GetxController {
- final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
- static final Map<String, List<bool>> _savedSelections = {};
- static bool _hasInitializedSelections = false;
- int get selectedFileCount =>
- photoGroups.fold(0, (sum, group) => sum + group.selectedCount);
- double get selectedFilesSize {
- double totalSize = 0;
- for (var group in photoGroups) {
- for (int i = 0; i < group.images.length; i++) {
- if (group.selectedImages[i]) {
- totalSize += File(group.images[i]).lengthSync();
- }
- }
- }
- return totalSize / 1024; // Convert to KB
- }
- void toggleImageSelection(String groupTitle, int imageIndex) {
- final group = photoGroups.firstWhere((g) => g.title == groupTitle);
- group.selectedImages[imageIndex] = !group.selectedImages[imageIndex];
- group.isSelected.value = group.selectedImages.every((selected) => selected);
- _saveSelections(); // 保存选择状态
- }
- void toggleGroupSelection(String groupTitle) {
- final group = photoGroups.firstWhere((g) => g.title == groupTitle);
- final newValue = !group.isSelected.value;
- group.toggleSelectAll(newValue);
- _saveSelections(); // 保存选择状态
- }
- @override
- void onInit() {
- super.onInit();
- loadPhotosFromDirectory().then((_) {
- // 恢复保存的选择状态
- if (_hasInitializedSelections) {
- _restoreSelections();
- }
- });
- }
- void _saveSelections() {
- for (var group in photoGroups) {
- _savedSelections[group.title] = group.selectedImages.toList();
- }
- _hasInitializedSelections = true;
- }
- void _restoreSelections() {
- for (var group in photoGroups) {
- // 这里假设每次页面加载时,已选择的状态会保存在 `RxList<bool>` 中。
- group.isSelected.value = group.selectedImages.every((selected) => selected);
- }
- }
- Future<void> loadPhotosFromDirectory() async {
- try {
- final Directory appDir = await getApplicationDocumentsDirectory();
- final String photosPath = '${appDir.path}/photos';
- final Directory photosDir = Directory(photosPath);
- if (!await photosDir.exists()) {
- return;
- }
- // 获取photos目录下的所有文件夹
- final List<Directory> subDirs = await photosDir
- .list()
- .where((entity) => entity is Directory)
- .map((e) => e as Directory)
- .toList();
- // 遍历每个文件夹
- for (final dir in subDirs) {
- // 获取文件夹中的所有图片
- final List<FileSystemEntity> files = await dir
- .list()
- .where((entity) =>
- entity is File &&
- ['.jpg', '.jpeg', '.png']
- .contains(p.extension(entity.path).toLowerCase()))
- .toList();
- if (files.isNotEmpty) {
- // 为每个文件夹创建一个PhotoGroup
- photoGroups.add(PhotoGroup(
- // title: '${p.basename(dir.path)}: ${files.length}', // 使用文件夹名作为标题
- title: 'people : ${files.length}', // 使用文件夹名作为标题
- imageCount: files.length,
- isSelected: false,
- images: files.map((file) => file.path).toList(),
- ));
- }
- }
- } catch (e) {
- print('Error loading photos: $e');
- }
- }
- }
|