photo_info_controller.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'package:classify_photo/classify_photo.dart';
  4. import 'package:clean/module/analysis/analysis_state.dart';
  5. import 'package:clean/module/image_picker/image_picker_util.dart';
  6. import 'package:clean/module/privacy/privacy_state.dart';
  7. import 'package:clean/utils/expand.dart';
  8. import 'package:clean/utils/file_utils.dart';
  9. import 'package:clean/utils/image_util.dart';
  10. import 'package:flutter/cupertino.dart';
  11. import 'package:flutter_screenutil/flutter_screenutil.dart';
  12. import 'package:get/get.dart';
  13. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  14. import '../../base/base_controller.dart';
  15. import '../../data/consts/event_report_id.dart';
  16. import '../../handler/event_handler.dart';
  17. import '../../model/asset_info.dart';
  18. class PhotoInfoController extends BaseController {
  19. Rx<FileType> type = FileType.analysis.obs;
  20. // 存储所有图片,按月份分组
  21. RxList<AssetInfo> imageList = <AssetInfo>[].obs;
  22. // 当前显示的图片索引
  23. final RxInt currentImageIndex = 0.obs;
  24. final RxMap<String, dynamic> photoDetails = <String, dynamic>{}.obs;
  25. RxString createTime = "".obs;
  26. RxString fileName = "".obs;
  27. RxString model = "".obs;
  28. RxString focalLength = "".obs;
  29. RxString size = "".obs;
  30. RxString iso = "".obs;
  31. RxString address = "".obs;
  32. @override
  33. void onInit() {
  34. // TODO: implement onInit
  35. super.onInit();
  36. if (Get.arguments != null) {
  37. final args = Get.arguments as Map<String, dynamic>;
  38. imageList.value = Get.arguments["list"] as List<AssetInfo>;
  39. var argType = Get.arguments["type"] as String;
  40. if (argType == "privacy") {
  41. type.value = FileType.privacy;
  42. } else {
  43. type.value = FileType.analysis;
  44. }
  45. // 设置初始索引
  46. if (args.containsKey('index')) {
  47. currentImageIndex.value = args['index'] as int;
  48. }
  49. loadPhotoInfo();
  50. }
  51. }
  52. Future<void> loadPhotoInfo() async {
  53. createTime.value = "";
  54. fileName.value = "";
  55. model.value = "";
  56. focalLength.value = "";
  57. size.value = "";
  58. iso.value = "";
  59. var assetInfo = imageList[currentImageIndex.value];
  60. photoDetails.value = await ImageUtil.getPhotoDetails(assetInfo);
  61. if (photoDetails.isNotEmpty) {
  62. createTime.value = photoDetails["createDate"] as String;
  63. fileName.value = photoDetails["fileName"] as String;
  64. if (photoDetails["model"] != null) {
  65. model.value = (photoDetails["model"] as String);
  66. }
  67. if (photoDetails["focalLength"] != null && photoDetails["aperture"] != null) {
  68. focalLength.value =
  69. "${photoDetails["focalLength"] as String}mm f/${photoDetails["aperture"] as String}";
  70. }
  71. if (photoDetails["size"] != null && photoDetails["width"] != null && photoDetails["height"] != null) {
  72. var imageSize = ImagePickerUtil.formatFileSize(photoDetails["size"], decimals: 1);
  73. size.value = "${photoDetails["width"] as int} X ${photoDetails["height"] as int} $imageSize";
  74. }
  75. if (photoDetails["iso"] != null && photoDetails["exposureTime"] != null) {
  76. iso.value = "ISO${photoDetails["iso"]} ${photoDetails["exposureTime"]}s";
  77. }
  78. }
  79. }
  80. @override
  81. void onReady() {
  82. super.onReady();
  83. if(type.value == FileType.analysis) {
  84. EventHandler.report(EventId.event_09002);
  85. }
  86. if(type.value == FileType.privacy) {
  87. EventHandler.report(EventId.event_05003);
  88. }
  89. }
  90. // 切换到下一张图片
  91. void nextImage() {
  92. if (currentImageIndex.value < imageList.length - 1) {
  93. currentImageIndex.value++;
  94. }
  95. loadPhotoInfo();
  96. }
  97. // 切换到上一张图片
  98. void previousImage() {
  99. if (currentImageIndex.value > 0) {
  100. currentImageIndex.value--;
  101. }
  102. loadPhotoInfo();
  103. }
  104. void deleteBtnClick(AssetInfo asset, int index) {
  105. if (type.value == FileType.analysis) {
  106. EventHandler.report(EventId.event_09003);
  107. }
  108. if (type.value == FileType.privacy) {
  109. EventHandler.report(EventId.event_05004);
  110. }
  111. showCupertinoModalPopup(
  112. context: Get.context!,
  113. builder: (context) {
  114. return CupertinoActionSheet(
  115. title: Text(
  116. "The file will be deleted from the privacy space",
  117. style: TextStyle(
  118. color: "#6E6E6E".color,
  119. fontSize: 12.sp,
  120. fontWeight: FontWeight.w300,
  121. ),
  122. ),
  123. actions: <Widget>[
  124. //操作按钮集合
  125. CupertinoActionSheetAction(
  126. onPressed: () {
  127. Navigator.pop(context);
  128. FileUtils.deleteAsset(type.value, Platform.isIOS ? asset.id.substring(0, 36) : asset.id);
  129. if (type.value == FileType.privacy) {
  130. PrivacyState.removeAsset(asset.id);
  131. // 从列表中移除
  132. imageList = PrivacyState.imageList;
  133. }
  134. if (type.value == FileType.analysis) {
  135. AnalysisState.removeAsset(asset.id);
  136. // 从列表中移除
  137. imageList = AnalysisState.imageList;
  138. }
  139. // 如果没有图片了,返回上一页
  140. if (imageList.isEmpty) {
  141. Get.back();
  142. } else {
  143. if (currentImageIndex.value != 0) {
  144. currentImageIndex.value = imageList.length - 1;
  145. }
  146. }
  147. },
  148. child: Text(
  149. 'Delete',
  150. style: TextStyle(
  151. color: "#FC4C4F".color,
  152. fontWeight: FontWeight.w500,
  153. fontSize: 16.sp,
  154. ),
  155. ),
  156. ),
  157. ],
  158. cancelButton: CupertinoActionSheetAction(
  159. //取消按钮
  160. onPressed: () {
  161. Navigator.pop(context);
  162. },
  163. child: Text(
  164. 'Cancel',
  165. style: TextStyle(
  166. color: "#007AFF".color,
  167. fontWeight: FontWeight.w500,
  168. fontSize: 16.sp,
  169. ),
  170. ),
  171. ),
  172. );
  173. },
  174. );
  175. }
  176. // 格式化文件大小显示
  177. String formatFileSize(int bytes) {
  178. if (bytes <= 0) return "Delete";
  179. final units = ['B', 'KB', 'MB', 'GB'];
  180. int digitGroups = (log(bytes) / log(1024)).floor();
  181. if (bytes == 0) {
  182. return "Delete";
  183. } else {
  184. return "Delete(${(bytes / pow(1024, digitGroups)).toStringAsFixed(1)} ${units[digitGroups]})";
  185. }
  186. }
  187. }