home_controller.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'dart:io';
  2. import 'package:classify_photo/classify_photo.dart';
  3. import 'package:clean/base/base_controller.dart';
  4. import 'package:clean/module/image_picker/image_picker_util.dart';
  5. import 'package:clean/module/locations_photo/locations_photo_view.dart';
  6. import 'package:clean/module/people_photo/people_photo_view.dart';
  7. import 'package:clean/module/screenshots_blurry/screenshots_view.dart';
  8. import 'package:clean/module/similar_photo/similar_photo_view.dart';
  9. import 'package:clean/utils/toast_util.dart';
  10. import 'package:disk_space/disk_space.dart';
  11. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  12. import 'package:get/get.dart';
  13. import 'package:permission_handler/permission_handler.dart';
  14. import 'dart:typed_data';
  15. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  16. class HomeController extends BaseController {
  17. Rx<double> totalSpace = 500.0.obs;
  18. Rx<double> usedSpace = 300.0.obs;
  19. Rx<double> photoSpace = 100.0.obs;
  20. Rx<double> freeSpace = 200.0.obs;
  21. // 计算已用存储百分比
  22. double get usedSpacePercentage => (usedSpace.value / totalSpace.value) * 100;
  23. // 计算照片占用存储百分比
  24. double get photoSpacePercentage => (photoSpace.value / totalSpace.value) * 100;
  25. // 计算可用存储百分比
  26. double get freeSpacePercentage => (freeSpace.value / totalSpace.value) * 100;
  27. RxList<String> similarImages = List.generate(4, (index) => 'iconHomeNoPhoto').obs;
  28. RxInt imageCount = 0.obs;
  29. // 相似图片
  30. RxList<AssetEntity> similarPhotos = <AssetEntity>[].obs;
  31. // 人物图片
  32. RxList<AssetEntity> peoplePhotos = <AssetEntity>[].obs;
  33. // 地点图片
  34. Rx<AssetEntity?> locationPhoto = Rx<AssetEntity?>(null);
  35. // 截图照片
  36. Rx<AssetEntity?> screenshotPhoto = Rx<AssetEntity?>(null);
  37. // 模糊照片
  38. Rx<AssetEntity?> blurryPhoto = Rx<AssetEntity?>(null);
  39. // 是否扫描完成
  40. RxBool isScanned = false.obs;
  41. @override
  42. Future<void> onInit() async {
  43. // TODO: implement onInit
  44. super.onInit();
  45. // loadPhotosFromDirectory();
  46. if (await Permission.photos.request().isGranted) {
  47. PhotoManager.clearFileCache();
  48. getStorageInfo();
  49. handlePhotos();
  50. } else {
  51. ToastUtil.show("请先开启相册权限");
  52. }
  53. }
  54. Future<void> loadPhotosFromDirectory() async {
  55. if(ImagePickerUtil.peoplePhotos.isEmpty||ImagePickerUtil.similarPhotos.isEmpty||ImagePickerUtil.locationPhotos.isEmpty||ImagePickerUtil.screenshotPhotos.isEmpty){
  56. try {
  57. final List<AssetEntity>? result = await AssetPicker.pickAssets(
  58. Get.context!,
  59. );
  60. ImagePickerUtil.peoplePhotos.value = result ?? [];
  61. if (result == null) {
  62. }else {
  63. ImagePickerUtil.locationPhotos['location'] = result ?? [];
  64. }
  65. ImagePickerUtil.screenshotPhotos.value = result ?? [];
  66. ImagePickerUtil.similarPhotos.add(result ?? []);
  67. ImagePickerUtil.blurryPhotos.value = result ?? [];
  68. } catch (e) {
  69. print('Error loading photos: $e');
  70. }
  71. }
  72. }
  73. Future<void> getStorageInfo() async {
  74. final classifyPhoto = ClassifyPhoto();
  75. try {
  76. final storageInfo = await classifyPhoto.getStorageInfo();
  77. // 转换为 GB
  78. final totalSpaceGB = storageInfo['totalSpace']! / (1000 * 1000 * 1000);
  79. final freeSpaceGB = storageInfo['freeSpace']! / (1024 * 1024 * 1024);
  80. final usedSpaceGB = storageInfo['usedSpace']! / (1024 * 1024 * 1024);
  81. final photoSpaceGB = storageInfo['photoSpace']! / (1024 * 1024 * 1024);
  82. totalSpace.value = totalSpaceGB.round().toDouble();
  83. freeSpace.value = freeSpaceGB;
  84. usedSpace.value = usedSpaceGB;
  85. photoSpace.value = photoSpaceGB;
  86. print('总容量: ${totalSpaceGB.toStringAsFixed(0)} GB');
  87. print('可用空间: ${freeSpaceGB.toStringAsFixed(2)} GB');
  88. print('已用空间: ${usedSpaceGB.toStringAsFixed(2)} GB');
  89. print('照片占用: ${photoSpaceGB.toStringAsFixed(2)} GB');
  90. } catch (e) {
  91. print('获取存储信息失败: $e');
  92. }
  93. }
  94. Future<void> handlePhotos() async {
  95. final photoClassify = ClassifyPhoto();
  96. try {
  97. print('开始获取照片');
  98. final photos = await photoClassify.getPhoto();
  99. print('获取照片完成: ${photos?.length ?? 0} 组照片');
  100. // 已完成扫描
  101. isScanned.value = true;
  102. if (photos != null) {
  103. await ImagePickerUtil.updatePhotos(photos);
  104. similarPhotos.clear();
  105. if (ImagePickerUtil.similarPhotos.isNotEmpty) {
  106. for (var group in ImagePickerUtil.similarPhotos) {
  107. for (var asset in group) {
  108. similarPhotos.add(asset);
  109. if (similarPhotos.length == 4) {
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. // 处理地点照片
  116. locationPhoto.value = null;
  117. if (ImagePickerUtil.locationPhotos.isNotEmpty) {
  118. // 获取第一个地点的第一张照片
  119. final firstLocationPhotos = ImagePickerUtil.locationPhotos.values.first;
  120. if (firstLocationPhotos.isNotEmpty) {
  121. var asset = firstLocationPhotos.first;
  122. locationPhoto.value = asset;
  123. }
  124. }
  125. // 处理人物照片
  126. peoplePhotos.clear();
  127. if (ImagePickerUtil.peoplePhotos.isNotEmpty) {
  128. for (var personPhotos in ImagePickerUtil.peoplePhotos) {
  129. peoplePhotos.add(personPhotos);
  130. if (peoplePhotos.length == 2) {
  131. break;
  132. }
  133. }
  134. }
  135. if (ImagePickerUtil.screenshotPhotos.isNotEmpty) {
  136. var asset = ImagePickerUtil.screenshotPhotos.first;
  137. screenshotPhoto.value = asset;
  138. }
  139. if (ImagePickerUtil.blurryPhotos.isNotEmpty) {
  140. var asset = ImagePickerUtil.blurryPhotos.first;
  141. blurryPhoto.value = asset;
  142. }
  143. }
  144. } catch (e, stackTrace) {
  145. print('获取照片失败: $e');
  146. print('Stack trace: $stackTrace');
  147. }
  148. }
  149. similarCleanClick() {
  150. print('similarCleanClick');
  151. SimilarPhotoPage.start();
  152. }
  153. peopleCleanClick() {
  154. PeoplePhotoPage.start();
  155. print('peopleCleanClick');
  156. }
  157. locationCleanClick() {
  158. LocationsPhotoPage.start();
  159. print('locationCleanClick');
  160. }
  161. screenshotCleanClick() {
  162. ScreenshotsPage.start("Screenshots");
  163. print('screenshotCleanClick');
  164. }
  165. blurryCleanClick() {
  166. ScreenshotsPage.start("Blurry");
  167. print('blurCleanClick');
  168. }
  169. }