| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import 'dart:io';
- import 'package:get/get.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:path/path.dart' as p;
- class PhotoGroup {
- final String title;
- final int imageCount;
- final RxBool isSelected;
- final List<String> images;
- final RxList<bool> selectedImages;
- int get selectedCount => selectedImages.where((selected) => selected).length;
- PhotoGroup({
- required this.title,
- required this.imageCount,
- required bool isSelected,
- required this.images,
- }) : isSelected = isSelected.obs,
- selectedImages = List.generate(imageCount, (_) => isSelected).obs;
- void toggleSelectAll(bool value) {
- isSelected.value = value;
- selectedImages.assignAll(List.generate(images.length, (_) => value));
- }
- }
- class SimilarPhotoController 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();
- }
- 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);
- }
- }
- @override
- void onInit() {
- super.onInit();
- loadPhotosFromDirectory().then((_) {
- if (_hasInitializedSelections) {
- _restoreSelections();
- }
- });
- }
- 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;
- }
- 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) {
- photoGroups.add(PhotoGroup(
- title: 'photo: ${files.length}',
- imageCount: files.length,
- isSelected: false,
- images: files.map((file) => file.path).toList(),
- ));
- }
- }
- } catch (e) {
- print('Error loading photos: $e');
- }
- }
- }
|