photo_info_controller.dart 6.4 KB

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