home_controller.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ImagePickerUtil.locationPhotos['location'] = result ?? [];
  62. ImagePickerUtil.screenshotPhotos.value = result ?? [];
  63. ImagePickerUtil.similarPhotos.add(result ?? []);
  64. ImagePickerUtil.blurryPhotos.value = result ?? [];
  65. } catch (e) {
  66. print('Error loading photos: $e');
  67. }
  68. }
  69. }
  70. Future<void> getStorageInfo() async {
  71. final classifyPhoto = ClassifyPhoto();
  72. try {
  73. final storageInfo = await classifyPhoto.getStorageInfo();
  74. // 转换为 GB
  75. final totalSpaceGB = storageInfo['totalSpace']! / (1000 * 1000 * 1000);
  76. final freeSpaceGB = storageInfo['freeSpace']! / (1024 * 1024 * 1024);
  77. final usedSpaceGB = storageInfo['usedSpace']! / (1024 * 1024 * 1024);
  78. final photoSpaceGB = storageInfo['photoSpace']! / (1024 * 1024 * 1024);
  79. totalSpace.value = totalSpaceGB.round().toDouble();
  80. freeSpace.value = freeSpaceGB;
  81. usedSpace.value = usedSpaceGB;
  82. photoSpace.value = photoSpaceGB;
  83. print('总容量: ${totalSpaceGB.toStringAsFixed(0)} GB');
  84. print('可用空间: ${freeSpaceGB.toStringAsFixed(2)} GB');
  85. print('已用空间: ${usedSpaceGB.toStringAsFixed(2)} GB');
  86. print('照片占用: ${photoSpaceGB.toStringAsFixed(2)} GB');
  87. } catch (e) {
  88. print('获取存储信息失败: $e');
  89. }
  90. }
  91. Future<void> handlePhotos() async {
  92. final photoClassify = ClassifyPhoto();
  93. try {
  94. print('开始获取照片');
  95. final photos = await photoClassify.getPhoto();
  96. print('获取照片完成: ${photos?.length ?? 0} 组照片');
  97. // 已完成扫描
  98. isScanned.value = true;
  99. if (photos != null) {
  100. await ImagePickerUtil.updatePhotos(photos);
  101. similarPhotos.clear();
  102. if (ImagePickerUtil.similarPhotos.isNotEmpty) {
  103. for (var group in ImagePickerUtil.similarPhotos) {
  104. for (var asset in group) {
  105. similarPhotos.add(asset);
  106. if (similarPhotos.length == 4) {
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. // 处理地点照片
  113. locationPhoto.value = null;
  114. if (ImagePickerUtil.locationPhotos.isNotEmpty) {
  115. // 获取第一个地点的第一张照片
  116. final firstLocationPhotos = ImagePickerUtil.locationPhotos.values.first;
  117. if (firstLocationPhotos.isNotEmpty) {
  118. var asset = firstLocationPhotos.first;
  119. locationPhoto.value = asset;
  120. }
  121. }
  122. // 处理人物照片
  123. peoplePhotos.clear();
  124. if (ImagePickerUtil.peoplePhotos.isNotEmpty) {
  125. for (var personPhotos in ImagePickerUtil.peoplePhotos) {
  126. peoplePhotos.add(personPhotos);
  127. if (peoplePhotos.length == 2) {
  128. break;
  129. }
  130. }
  131. }
  132. if (ImagePickerUtil.screenshotPhotos.isNotEmpty) {
  133. var asset = ImagePickerUtil.screenshotPhotos.first;
  134. screenshotPhoto.value = asset;
  135. }
  136. if (ImagePickerUtil.blurryPhotos.isNotEmpty) {
  137. var asset = ImagePickerUtil.blurryPhotos.first;
  138. blurryPhoto.value = asset;
  139. }
  140. }
  141. } catch (e, stackTrace) {
  142. print('获取照片失败: $e');
  143. print('Stack trace: $stackTrace');
  144. }
  145. }
  146. similarCleanClick() {
  147. print('similarCleanClick');
  148. SimilarPhotoPage.start();
  149. }
  150. peopleCleanClick() {
  151. PeoplePhotoPage.start();
  152. print('peopleCleanClick');
  153. }
  154. locationCleanClick() {
  155. LocationsPhotoPage.start();
  156. print('locationCleanClick');
  157. }
  158. screenshotCleanClick() {
  159. ScreenshotsPage.start("Screenshots");
  160. print('screenshotCleanClick');
  161. }
  162. blurryCleanClick() {
  163. ScreenshotsPage.start("Blurry");
  164. print('blurCleanClick');
  165. }
  166. }