home_controller.dart 6.8 KB

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