home_controller.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import 'package:classify_photo/classify_photo.dart';
  2. import 'package:clean/base/base_controller.dart';
  3. import 'package:clean/data/consts/constants.dart';
  4. import 'package:clean/data/repositories/user_repository.dart';
  5. import 'package:clean/module/image_picker/image_picker_util.dart';
  6. import 'package:clean/module/locations_photo/locations_photo_view.dart';
  7. import 'package:clean/module/people_photo/people_photo_view.dart';
  8. import 'package:clean/module/screenshots_blurry/screenshots_view.dart';
  9. import 'package:clean/module/similar_photo/similar_photo_view.dart';
  10. import 'package:clean/router/app_pages.dart';
  11. import 'package:clean/utils/toast_util.dart';
  12. import 'package:get/get.dart';
  13. import 'package:permission_handler/permission_handler.dart';
  14. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  15. import '../../data/api/response/user_info_response.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 =>
  29. (photoSpace.value / totalSpace.value) * 100;
  30. // 计算可用存储百分比
  31. double get freeSpacePercentage => (freeSpace.value / totalSpace.value) * 100;
  32. RxList<String> similarImages =
  33. List.generate(4, (index) => 'iconHomeNoPhoto').obs;
  34. RxInt imageCount = 0.obs;
  35. // 相似图片
  36. RxList<AssetEntity> similarPhotos = <AssetEntity>[].obs;
  37. // 人物图片
  38. RxList<AssetEntity> peoplePhotos = <AssetEntity>[].obs;
  39. // 地点图片
  40. Rx<AssetEntity?> locationPhoto = Rx<AssetEntity?>(null);
  41. // 截图照片
  42. Rx<AssetEntity?> screenshotPhoto = Rx<AssetEntity?>(null);
  43. // 模糊照片
  44. Rx<AssetEntity?> blurryPhoto = Rx<AssetEntity?>(null);
  45. // 是否扫描完成
  46. RxBool isScanned = false.obs;
  47. UserInfoResponse? get userInfo => userRepository.userInfo.value;
  48. @override
  49. Future<void> onInit() async {
  50. // TODO: implement onInit
  51. super.onInit();
  52. loadPhotosFromDirectory();
  53. if (await Permission.photos.request().isGranted) {
  54. PhotoManager.clearFileCache();
  55. getStorageInfo();
  56. handlePhotos();
  57. } else {
  58. ToastUtil.show("请先开启相册权限");
  59. }
  60. }
  61. Future<void> loadPhotosFromDirectory() async {
  62. if (ImagePickerUtil.peoplePhotos.isEmpty ||
  63. ImagePickerUtil.similarPhotos.isEmpty ||
  64. ImagePickerUtil.locationPhotos.isEmpty ||
  65. ImagePickerUtil.screenshotPhotos.isEmpty) {
  66. try {
  67. final List<AssetEntity>? result = await AssetPicker.pickAssets(
  68. Get.context!,
  69. );
  70. ImagePickerUtil.peoplePhotos.value = result ?? [];
  71. if (result == null) {
  72. } else {
  73. ImagePickerUtil.locationPhotos['location'] = result ?? [];
  74. }
  75. ImagePickerUtil.screenshotPhotos.value = result ?? [];
  76. ImagePickerUtil.similarPhotos.add(result ?? []);
  77. ImagePickerUtil.blurryPhotos.value = result ?? [];
  78. } catch (e) {
  79. print('Error loading photos: $e');
  80. }
  81. }
  82. }
  83. Future<void> getStorageInfo() async {
  84. final classifyPhoto = ClassifyPhoto();
  85. try {
  86. final storageInfo = await classifyPhoto.getStorageInfo();
  87. // 转换为 GB
  88. final totalSpaceGB = storageInfo['totalSpace']! / (1000 * 1000 * 1000);
  89. final freeSpaceGB = storageInfo['freeSpace']! / (1024 * 1024 * 1024);
  90. final usedSpaceGB = storageInfo['usedSpace']! / (1024 * 1024 * 1024);
  91. final photoSpaceGB = storageInfo['photoSpace']! / (1024 * 1024 * 1024);
  92. totalSpaceStr.value = ImagePickerUtil.formatFileSize(
  93. storageInfo['totalSpace']!,
  94. decimals: 1);
  95. freeSpaceStr.value = ImagePickerUtil.formatFileSize(
  96. storageInfo['freeSpace']!,
  97. decimals: 1);
  98. usedSpaceStr.value = ImagePickerUtil.formatFileSize(
  99. storageInfo['usedSpace']!,
  100. decimals: 1);
  101. photoSpaceStr.value = ImagePickerUtil.formatFileSize(
  102. storageInfo['photoSpace']!,
  103. decimals: 1);
  104. totalSpace.value = totalSpaceGB.round().toDouble();
  105. freeSpace.value = freeSpaceGB;
  106. usedSpace.value = usedSpaceGB;
  107. photoSpace.value = photoSpaceGB;
  108. print('总容量: $totalSpaceStr');
  109. print('可用空间: $freeSpaceStr');
  110. print('已用空间: $usedSpaceStr');
  111. print('照片占用: $photoSpaceStr');
  112. } catch (e) {
  113. print('获取存储信息失败: $e');
  114. }
  115. }
  116. Future<void> handlePhotos() async {
  117. final photoClassify = ClassifyPhoto();
  118. try {
  119. print('开始获取照片');
  120. final photos = await photoClassify.getPhoto();
  121. print('获取照片完成: ${photos?.length ?? 0} 组照片');
  122. // 已完成扫描
  123. isScanned.value = true;
  124. if (photos != null) {
  125. await ImagePickerUtil.updatePhotos(photos);
  126. similarPhotos.clear();
  127. if (ImagePickerUtil.similarPhotos.isNotEmpty) {
  128. for (var group in ImagePickerUtil.similarPhotos) {
  129. for (var asset in group) {
  130. similarPhotos.add(asset);
  131. if (similarPhotos.length == 4) {
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. // 处理地点照片
  138. locationPhoto.value = null;
  139. if (ImagePickerUtil.locationPhotos.isNotEmpty) {
  140. // 获取第一个地点的第一张照片
  141. final firstLocationPhotos =
  142. ImagePickerUtil.locationPhotos.values.first;
  143. if (firstLocationPhotos.isNotEmpty) {
  144. var asset = firstLocationPhotos.first;
  145. locationPhoto.value = asset;
  146. }
  147. }
  148. // 处理人物照片
  149. peoplePhotos.clear();
  150. if (ImagePickerUtil.peoplePhotos.isNotEmpty) {
  151. for (var personPhotos in ImagePickerUtil.peoplePhotos) {
  152. peoplePhotos.add(personPhotos);
  153. if (peoplePhotos.length == 2) {
  154. break;
  155. }
  156. }
  157. }
  158. if (ImagePickerUtil.screenshotPhotos.isNotEmpty) {
  159. var asset = ImagePickerUtil.screenshotPhotos.first;
  160. screenshotPhoto.value = asset;
  161. }
  162. if (ImagePickerUtil.blurryPhotos.isNotEmpty) {
  163. var asset = ImagePickerUtil.blurryPhotos.first;
  164. blurryPhoto.value = asset;
  165. }
  166. }
  167. } catch (e, stackTrace) {
  168. print('获取照片失败: $e');
  169. print('Stack trace: $stackTrace');
  170. }
  171. }
  172. void _navigateAndStartPage(Function pageStartFunction) {
  173. // if (isFirstClickHomeClean()) {
  174. // setFirstClickHomeClean(false);
  175. // Get.toNamed(RoutePath.discount)?.then((value) {
  176. // pageStartFunction();
  177. // });
  178. // } else {
  179. pageStartFunction();
  180. // }
  181. }
  182. similarCleanClick() {
  183. print('similarCleanClick');
  184. _navigateAndStartPage(SimilarPhotoPage.start);
  185. }
  186. peopleCleanClick() {
  187. print('peopleCleanClick');
  188. _navigateAndStartPage(PeoplePhotoPage.start);
  189. }
  190. locationCleanClick() {
  191. print('locationCleanClick');
  192. _navigateAndStartPage(LocationsPhotoPage.start);
  193. }
  194. screenshotCleanClick() {
  195. print('screenshotCleanClick');
  196. _navigateAndStartPage(() => ScreenshotsPage.start("Screenshots"));
  197. }
  198. blurryCleanClick() {
  199. print('blurCleanClick');
  200. _navigateAndStartPage(() => ScreenshotsPage.start("Blurry"));
  201. }
  202. }