import 'dart:io'; import 'package:classify_photo/classify_photo.dart'; import 'package:clean/base/base_controller.dart'; import 'package:clean/module/image_picker/image_picker_util.dart'; import 'package:clean/module/locations_photo/locations_photo_view.dart'; import 'package:clean/module/people_photo/people_photo_view.dart'; import 'package:clean/module/similar_photo/similar_photo_view.dart'; import 'package:clean/utils/toast_util.dart'; import 'package:disk_space/disk_space.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:permission_handler/permission_handler.dart'; import 'dart:typed_data'; import 'package:wechat_assets_picker/wechat_assets_picker.dart'; class HomeController extends BaseController { Rx totalSpace = 500.0.obs; Rx usedSpace = 300.0.obs; Rx photoSpace = 100.0.obs; Rx freeSpace = 200.0.obs; // 计算已用存储百分比 double get usedSpacePercentage => (usedSpace.value / totalSpace.value) * 100; // 计算照片占用存储百分比 double get photoSpacePercentage => (photoSpace.value / totalSpace.value) * 100; // 计算可用存储百分比 double get freeSpacePercentage => (freeSpace.value / totalSpace.value) * 100; RxList similarImages = List.generate(4, (index) => 'iconHomeNoPhoto').obs; RxInt imageCount = 0.obs; // 相似图片 RxList similarPhotos = [].obs; // 人物图片 RxList peoplePhotos = [].obs; // 地点图片 Rx locationPhoto = Rx(null); // 截图照片 Rx screenshotPhoto = Rx(null); // 模糊照片 Rx blurryPhoto = Rx(null); // 是否扫描完成 RxBool isScanned = false.obs; @override Future onInit() async { // TODO: implement onInit super.onInit(); if (await Permission.photos.request().isGranted) { PhotoManager.clearFileCache(); getStorageInfo(); handlePhotos(); } else { ToastUtil.show("请先开启相册权限"); } } Future getStorageInfo() async { final classifyPhoto = ClassifyPhoto(); try { final storageInfo = await classifyPhoto.getStorageInfo(); // 转换为 GB final totalSpaceGB = storageInfo['totalSpace']! / (1000 * 1000 * 1000); final freeSpaceGB = storageInfo['freeSpace']! / (1024 * 1024 * 1024); final usedSpaceGB = storageInfo['usedSpace']! / (1024 * 1024 * 1024); final photoSpaceGB = storageInfo['photoSpace']! / (1024 * 1024 * 1024); totalSpace.value = totalSpaceGB.round().toDouble(); freeSpace.value = freeSpaceGB; usedSpace.value = usedSpaceGB; photoSpace.value = photoSpaceGB; print('总容量: ${totalSpaceGB.toStringAsFixed(0)} GB'); print('可用空间: ${freeSpaceGB.toStringAsFixed(2)} GB'); print('已用空间: ${usedSpaceGB.toStringAsFixed(2)} GB'); print('照片占用: ${photoSpaceGB.toStringAsFixed(2)} GB'); } catch (e) { print('获取存储信息失败: $e'); } } Future handlePhotos() async { final photoClassify = ClassifyPhoto(); try { print('开始获取照片'); final photos = await photoClassify.getPhoto(); print('获取照片完成: ${photos?.length ?? 0} 组照片'); // 已完成扫描 isScanned.value = true; if (photos != null) { await ImagePickerUtil.updatePhotos(photos); similarPhotos.clear(); if (ImagePickerUtil.similarPhotos.isNotEmpty) { for (var group in ImagePickerUtil.similarPhotos) { for (var asset in group) { similarPhotos.add(asset); if (similarPhotos.length == 4) { break; } } } } // 处理地点照片 locationPhoto.value = null; if (ImagePickerUtil.locationPhotos.isNotEmpty) { // 获取第一个地点的第一张照片 final firstLocationPhotos = ImagePickerUtil.locationPhotos.values.first; if (firstLocationPhotos.isNotEmpty) { var asset = firstLocationPhotos.first; locationPhoto.value = asset; } } // 处理人物照片 peoplePhotos.clear(); if (ImagePickerUtil.peoplePhotos.isNotEmpty) { for (var personPhotos in ImagePickerUtil.peoplePhotos) { peoplePhotos.add(personPhotos); if (peoplePhotos.length == 2) { break; } } } if (ImagePickerUtil.screenshotPhotos.isNotEmpty) { var asset = ImagePickerUtil.screenshotPhotos.first; screenshotPhoto.value = asset; } if (ImagePickerUtil.blurryPhotos.isNotEmpty) { var asset = ImagePickerUtil.blurryPhotos.first; blurryPhoto.value = asset; } } } catch (e, stackTrace) { print('获取照片失败: $e'); print('Stack trace: $stackTrace'); } } similarCleanClick() { print('similarCleanClick'); SimilarPhotoPage.start(); } peopleCleanClick() { PeoplePhotoPage.start(); print('peopleCleanClick'); } locationCleanClick() { LocationsPhotoPage.start(); print('locationCleanClick'); } screenshotCleanClick() { print('screenshotCleanClick'); } blurryCleanClick() { print('blurCleanClick'); } }