home_controller.dart 5.4 KB

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