| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import 'dart:io';
- import 'dart:math';
- import 'package:classify_photo/classify_photo.dart';
- import 'package:clean/module/analysis/analysis_state.dart';
- import 'package:clean/module/image_picker/image_picker_util.dart';
- import 'package:clean/module/privacy/privacy_state.dart';
- import 'package:clean/utils/expand.dart';
- import 'package:clean/utils/file_utils.dart';
- import 'package:clean/utils/image_util.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:get/get.dart';
- import 'package:wechat_assets_picker/wechat_assets_picker.dart';
- import '../../base/base_controller.dart';
- import '../../data/consts/event_report_id.dart';
- import '../../handler/event_handler.dart';
- import '../../model/asset_info.dart';
- class PhotoInfoController extends BaseController {
- Rx<FileType> type = FileType.analysis.obs;
- // 存储所有图片,按月份分组
- RxList<AssetInfo> imageList = <AssetInfo>[].obs;
- // 当前显示的图片索引
- final RxInt currentImageIndex = 0.obs;
- final RxMap<String, dynamic> photoDetails = <String, dynamic>{}.obs;
- RxString createTime = "".obs;
- RxString fileName = "".obs;
- RxString model = "".obs;
- RxString focalLength = "".obs;
- RxString size = "".obs;
- RxString iso = "".obs;
- RxString address = "".obs;
- @override
- void onInit() {
- // TODO: implement onInit
- super.onInit();
- if (Get.arguments != null) {
- final args = Get.arguments as Map<String, dynamic>;
- imageList.value = Get.arguments["list"] as List<AssetInfo>;
- var argType = Get.arguments["type"] as String;
- if (argType == "privacy") {
- type.value = FileType.privacy;
- } else {
- type.value = FileType.analysis;
- }
- // 设置初始索引
- if (args.containsKey('index')) {
- currentImageIndex.value = args['index'] as int;
- }
- loadPhotoInfo();
- }
- }
- Future<void> loadPhotoInfo() async {
- createTime.value = "";
- fileName.value = "";
- model.value = "";
- focalLength.value = "";
- size.value = "";
- iso.value = "";
- var assetInfo = imageList[currentImageIndex.value];
- photoDetails.value = await ImageUtil.getPhotoDetails(assetInfo);
- if (photoDetails.isNotEmpty) {
- createTime.value = photoDetails["createDate"] as String;
- fileName.value = photoDetails["fileName"] as String;
- if (photoDetails["model"] != null) {
- model.value = (photoDetails["model"] as String);
- }
- if (photoDetails["focalLength"] != null && photoDetails["aperture"] != null) {
- focalLength.value =
- "${photoDetails["focalLength"] as String}mm f/${photoDetails["aperture"] as String}";
- }
- if (photoDetails["size"] != null && photoDetails["width"] != null && photoDetails["height"] != null) {
- var imageSize = ImagePickerUtil.formatFileSize(photoDetails["size"], decimals: 1);
- size.value = "${photoDetails["width"] as int} X ${photoDetails["height"] as int} $imageSize";
- }
- if (photoDetails["iso"] != null && photoDetails["exposureTime"] != null) {
- iso.value = "ISO${photoDetails["iso"]} ${photoDetails["exposureTime"]}s";
- }
- }
- }
- @override
- void onReady() {
- super.onReady();
- if(type.value == FileType.analysis) {
- EventHandler.report(EventId.event_09002);
- }
- if(type.value == FileType.privacy) {
- EventHandler.report(EventId.event_05003);
- }
- }
- // 切换到下一张图片
- void nextImage() {
- if (currentImageIndex.value < imageList.length - 1) {
- currentImageIndex.value++;
- }
- loadPhotoInfo();
- }
- // 切换到上一张图片
- void previousImage() {
- if (currentImageIndex.value > 0) {
- currentImageIndex.value--;
- }
- loadPhotoInfo();
- }
- void deleteBtnClick(AssetInfo asset, int index) {
- if (type.value == FileType.analysis) {
- EventHandler.report(EventId.event_09003);
- }
- if (type.value == FileType.privacy) {
- EventHandler.report(EventId.event_05004);
- }
- showCupertinoModalPopup(
- context: Get.context!,
- builder: (context) {
- return CupertinoActionSheet(
- title: Text(
- "The file will be deleted from the privacy space",
- style: TextStyle(
- color: "#6E6E6E".color,
- fontSize: 12.sp,
- fontWeight: FontWeight.w300,
- ),
- ),
- actions: <Widget>[
- //操作按钮集合
- CupertinoActionSheetAction(
- onPressed: () {
- Navigator.pop(context);
- FileUtils.deleteAsset(type.value, Platform.isIOS ? asset.id.substring(0, 36) : asset.id);
- if (type.value == FileType.privacy) {
- PrivacyState.removeAsset(asset.id);
- // 从列表中移除
- imageList = PrivacyState.imageList;
- }
- if (type.value == FileType.analysis) {
- AnalysisState.removeAsset(asset.id);
- // 从列表中移除
- imageList = AnalysisState.imageList;
- }
- // 如果没有图片了,返回上一页
- if (imageList.isEmpty) {
- Get.back();
- } else {
- if (currentImageIndex.value != 0) {
- currentImageIndex.value = imageList.length - 1;
- }
- }
- },
- child: Text(
- 'Delete',
- style: TextStyle(
- color: "#FC4C4F".color,
- fontWeight: FontWeight.w500,
- fontSize: 16.sp,
- ),
- ),
- ),
- ],
- cancelButton: CupertinoActionSheetAction(
- //取消按钮
- onPressed: () {
- Navigator.pop(context);
- },
- child: Text(
- 'Cancel',
- style: TextStyle(
- color: "#007AFF".color,
- fontWeight: FontWeight.w500,
- fontSize: 16.sp,
- ),
- ),
- ),
- );
- },
- );
- }
- // 格式化文件大小显示
- String formatFileSize(int bytes) {
- if (bytes <= 0) return "Delete";
- final units = ['B', 'KB', 'MB', 'GB'];
- int digitGroups = (log(bytes) / log(1024)).floor();
- if (bytes == 0) {
- return "Delete";
- } else {
- return "Delete(${(bytes / pow(1024, digitGroups)).toStringAsFixed(1)} ${units[digitGroups]})";
- }
- }
- }
|