privacy_controller.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import 'dart:ffi';
  2. import 'dart:math';
  3. import 'package:clean/base/base_controller.dart';
  4. import 'package:clean/model/asset_info.dart';
  5. import 'package:clean/utils/expand.dart';
  6. import 'package:clean/utils/file_utils.dart';
  7. import 'package:clean/utils/image_util.dart';
  8. import 'package:clean/utils/mmkv_util.dart';
  9. import 'package:flutter/Material.dart';
  10. import 'package:flutter/cupertino.dart';
  11. import 'package:flutter_screenutil/flutter_screenutil.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 'package:wechat_camera_picker/wechat_camera_picker.dart';
  16. import '../../utils/toast_util.dart';
  17. import '../image_picker/image_picker_assets.dart';
  18. import 'package:intl/intl.dart';
  19. import 'dart:typed_data';
  20. import 'dart:io';
  21. class PrivacyController extends BaseController {
  22. final String isExistPasswd = "PRIVACY_EXIST_PASSWD";
  23. final String isPublic = "PRIVACY_PUBLIC";
  24. final String privacyPasswd = "PRIVACY_PASSWD";
  25. // 是否存在密码
  26. RxBool isPrivacyExistPasswd = false.obs;
  27. // 是否公开
  28. RxBool isPrivacyPublic = false.obs;
  29. RxBool isConfirm = false.obs;
  30. // 是否为编辑状态
  31. RxBool isEdit = false.obs;
  32. // 是否在重置密码
  33. bool isReset = false;
  34. // 设置密码标题
  35. RxString passwordTitle = "Input password".obs;
  36. // 密码
  37. late var passwordStr = "".obs;
  38. late var newPasswordStr = "";
  39. // 是否为解锁状态
  40. late var isUnlock = false.obs;
  41. RxList<AssetInfo> imageList = <AssetInfo>[].obs;
  42. // 存储所有图片,按月份分组
  43. final assetsByMonth = <String, List<AssetInfo>>{}.obs;
  44. // 获取月份数量
  45. int get monthCount => assetsByMonth.length;
  46. // 获取总图片数量
  47. int get totalAssetCount =>
  48. assetsByMonth.values.fold(0, (sum, list) => sum + list.length);
  49. // 存储选中的图片ID
  50. final RxSet<String> selectedAssets = <String>{}.obs;
  51. // 是否全选
  52. RxBool isAllSelected = false.obs;
  53. // 选中图片的总容量(字节)
  54. final RxInt selectedTotalSize = 0.obs;
  55. @override
  56. void onInit() {
  57. // TODO: implement onInit
  58. super.onInit();
  59. isPrivacyExistPasswd.value = KVUtil.getBool(isExistPasswd, false);
  60. isPrivacyPublic.value = KVUtil.getBool(isPublic, false);
  61. isUnlock.value = isPrivacyPublic.value;
  62. if (isPrivacyExistPasswd.value) {
  63. passwordTitle.value = "Input password";
  64. } else {
  65. passwordTitle.value = "Create New Password";
  66. }
  67. loadAssets();
  68. }
  69. // 加载并分组图片
  70. Future<void> loadAssets() async {
  71. imageList.value = await FileUtils.getAllAssets();
  72. if (imageList.isEmpty) {
  73. isEdit.value = false;
  74. return;
  75. }
  76. // 清空现有数据
  77. assetsByMonth.clear();
  78. // 按月份分组
  79. for (var asset in imageList) {
  80. final monthKey = ImageUtil.getMonthKey(asset.createDateTime);
  81. if (!assetsByMonth.containsKey(monthKey)) {
  82. assetsByMonth[monthKey] = [];
  83. }
  84. assetsByMonth[monthKey]!.add(asset);
  85. }
  86. // 对每个月份内的图片按时间排序(新的在前)
  87. assetsByMonth.forEach((key, list) {
  88. list.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
  89. });
  90. // 打印分组结果
  91. assetsByMonth.forEach((key, assets) {
  92. print('${ImageUtil.formatMonthKey(key)}: ${assets.length} photos');
  93. });
  94. }
  95. // 处理输入密码逻辑
  96. void inputPassword(String num) {
  97. passwordStr.value = passwordStr.value + num;
  98. if (passwordStr.value.length == 4) {
  99. if (isReset) {
  100. // 二次输入密码的情况
  101. if (isConfirm.value) {
  102. // 输入错误
  103. if (passwordStr.value != newPasswordStr) {
  104. ToastUtil.show("Input Error");
  105. Future.delayed(const Duration(milliseconds: 100), () {
  106. passwordStr.value = "";
  107. });
  108. } else {
  109. isUnlock.value = true;
  110. KVUtil.putString(privacyPasswd, passwordStr.value);
  111. KVUtil.putBool(isExistPasswd, true);
  112. KVUtil.putBool(isPublic, false);
  113. isPrivacyExistPasswd.value = KVUtil.getBool(isExistPasswd, false);
  114. isPrivacyPublic.value = KVUtil.getBool(isPublic, false);
  115. isReset = false;
  116. passwordStr.value = "";
  117. }
  118. } else {
  119. // 第一次输入密码
  120. isConfirm.value = true;
  121. newPasswordStr = passwordStr.value;
  122. passwordTitle.value = "Confirm Password";
  123. Future.delayed(const Duration(milliseconds: 100), () {
  124. passwordStr.value = "";
  125. });
  126. }
  127. return;
  128. }
  129. }
  130. if (passwordStr.value.length == 4) {
  131. // 存在密码的情况
  132. if (isPrivacyExistPasswd.value) {
  133. String? password = KVUtil.getString(privacyPasswd, "");
  134. if (passwordStr.value == password) {
  135. isUnlock.value = true;
  136. passwordStr.value = "";
  137. } else {
  138. ToastUtil.show("Input Error");
  139. Future.delayed(const Duration(milliseconds: 100), () {
  140. passwordStr.value = "";
  141. });
  142. }
  143. } else {
  144. // 二次输入密码的情况
  145. if (isConfirm.value) {
  146. // 输入错误
  147. if (passwordStr.value != newPasswordStr) {
  148. ToastUtil.show("Input Error");
  149. Future.delayed(const Duration(milliseconds: 100), () {
  150. passwordStr.value = "";
  151. });
  152. } else {
  153. isUnlock.value = true;
  154. KVUtil.putString(privacyPasswd, passwordStr.value);
  155. KVUtil.putBool(isExistPasswd, true);
  156. KVUtil.putBool(isPublic, false);
  157. isPrivacyExistPasswd.value = KVUtil.getBool(isExistPasswd, false);
  158. isPrivacyPublic.value = KVUtil.getBool(isPublic, false);
  159. isReset = false;
  160. passwordStr.value = "";
  161. }
  162. } else {
  163. // 第一次输入密码
  164. isConfirm.value = true;
  165. newPasswordStr = passwordStr.value;
  166. passwordTitle.value = "Confirm Password";
  167. Future.delayed(const Duration(milliseconds: 100), () {
  168. passwordStr.value = "";
  169. });
  170. }
  171. return;
  172. }
  173. }
  174. }
  175. void dialogSetPassword() {
  176. // 存在密码情况下
  177. if (isPrivacyExistPasswd.value) {
  178. isConfirm.value = false;
  179. isUnlock.value = false;
  180. passwordTitle.value = "Input password";
  181. isReset = true;
  182. } else {
  183. isUnlock.value = false;
  184. }
  185. }
  186. // 设置公开状态
  187. void setPublic() {
  188. KVUtil.putString("", passwordStr.value);
  189. KVUtil.putBool(isExistPasswd, false);
  190. KVUtil.putBool(isPublic, true);
  191. isPrivacyExistPasswd.value = KVUtil.getBool(isExistPasswd, false);
  192. isPrivacyPublic.value = KVUtil.getBool(isPublic, false);
  193. }
  194. // 上传按钮点击
  195. void uploadBtnClick() {
  196. showCupertinoModalPopup(
  197. context: Get.context!,
  198. builder: (context) {
  199. return CupertinoActionSheet(
  200. actions: <Widget>[
  201. //操作按钮集合
  202. CupertinoActionSheetAction(
  203. onPressed: () {
  204. Navigator.pop(context);
  205. openGallery();
  206. },
  207. child: Text(
  208. 'Upload from Gallery',
  209. style: TextStyle(
  210. color: "#007AFF".color,
  211. fontWeight: FontWeight.w500,
  212. fontSize: 16.sp,
  213. ),
  214. ),
  215. ),
  216. CupertinoActionSheetAction(
  217. onPressed: () {
  218. Navigator.pop(context);
  219. openCamera();
  220. },
  221. child: Text(
  222. 'Take and Upload',
  223. style: TextStyle(
  224. color: "#007AFF".color,
  225. fontWeight: FontWeight.w500,
  226. fontSize: 16.sp,
  227. ),
  228. ),
  229. ),
  230. ],
  231. cancelButton: CupertinoActionSheetAction(
  232. //取消按钮
  233. onPressed: () {
  234. Navigator.pop(context);
  235. },
  236. child: Text(
  237. 'Cancel',
  238. style: TextStyle(
  239. color: "#007AFF".color,
  240. fontWeight: FontWeight.w500,
  241. fontSize: 16.sp,
  242. ),
  243. ),
  244. ),
  245. );
  246. },
  247. );
  248. }
  249. // 保存并刷新图片列表
  250. Future<void> saveAndRefreshAssets(List<AssetEntity> assets) async {
  251. for (var asset in assets) {
  252. await FileUtils.saveAsset(asset);
  253. }
  254. // 重新加载图片列表
  255. loadAssets();
  256. }
  257. // 选择/取消选择图片
  258. void toggleSelectAsset(String assetId) {
  259. final asset = imageList.firstWhere((asset) => asset.id == assetId);
  260. if (selectedAssets.contains(assetId)) {
  261. selectedAssets.remove(assetId);
  262. if (asset.size != null) {
  263. selectedTotalSize.value -= asset.size!;
  264. }
  265. } else {
  266. selectedAssets.add(assetId);
  267. if (asset.size != null) {
  268. selectedTotalSize.value += asset.size!;
  269. }
  270. }
  271. // 更新全选状态
  272. isAllSelected.value = selectedAssets.length == imageList.length;
  273. }
  274. // 全选/取消全选
  275. void toggleSelectAll() {
  276. if (isAllSelected.value) {
  277. selectedAssets.clear();
  278. selectedTotalSize.value = 0;
  279. } else {
  280. selectedAssets.addAll(imageList.map((asset) => asset.id));
  281. selectedTotalSize.value = imageList.fold(
  282. 0, (sum, asset) => sum + (asset.size != null ? asset.size! : 0));
  283. }
  284. isAllSelected.value = !isAllSelected.value;
  285. }
  286. // 退出编辑模式时清空选择
  287. void exitEditMode() {
  288. isEdit.value = false;
  289. selectedAssets.clear();
  290. isAllSelected.value = false;
  291. selectedTotalSize.value = 0;
  292. }
  293. // 删除文件
  294. void deleteBtnClick() {
  295. // 获取要删除的资产
  296. final assetsToDelete = imageList.where(
  297. (asset) => selectedAssets.contains(asset.id)
  298. ).toList();
  299. for (var asset in assetsToDelete) {
  300. FileUtils.deleteAsset(asset.id.substring(0, 36));
  301. }
  302. selectedTotalSize.value = 0;
  303. loadAssets();
  304. }
  305. // 格式化文件大小显示
  306. String formatFileSize(int bytes) {
  307. if (bytes <= 0) return "Delete";
  308. final units = ['B', 'KB', 'MB', 'GB'];
  309. int digitGroups = (log(bytes) / log(1024)).floor();
  310. if (bytes == 0) {
  311. return "Delete";
  312. } else {
  313. return "Delete(${(bytes / pow(1024, digitGroups)).toStringAsFixed(
  314. 1)} ${units[digitGroups]})";
  315. }
  316. }
  317. // 开启图库
  318. Future<void> openGallery() async {
  319. var status = await Permission.photos.status;
  320. if (status == PermissionStatus.granted) {
  321. List<AssetEntity> assets = <AssetEntity>[];
  322. for (var asset in imageList) {
  323. var newAsset = await asset.toAssetEntity();
  324. if (newAsset != null) {
  325. assets.add(newAsset);
  326. }
  327. }
  328. List<AssetEntity>? pickList = await ImagePickAssets.pick();
  329. if (pickList != null && pickList.isNotEmpty) {
  330. await saveAndRefreshAssets(pickList);
  331. }
  332. } else {
  333. ToastUtil.show("请先开启权限");
  334. }
  335. }
  336. // 开启相机
  337. Future<void> openCamera() async {
  338. final entity = await CameraPicker.pickFromCamera(
  339. Get.context!,
  340. pickerConfig: const CameraPickerConfig(),
  341. );
  342. if (entity != null) {
  343. await saveAndRefreshAssets([entity]);
  344. }
  345. }
  346. }