diff --git a/lib/utils/download.dart b/lib/utils/download.dart index ad008f6d8..a9c56ec0e 100644 --- a/lib/utils/download.dart +++ b/lib/utils/download.dart @@ -1,40 +1,94 @@ import 'dart:typed_data'; import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:saver_gallery/saver_gallery.dart'; class DownloadUtils { // 获取存储权限 - static requestStoragePer() async { - Map statuses = await [ - Permission.storage, - Permission.photos, - ].request(); - statuses[Permission.storage].toString(); + static Future requestStoragePer() async { + await Permission.storage.request(); + PermissionStatus status = await Permission.storage.status; + if (status == PermissionStatus.denied || + status == PermissionStatus.permanentlyDenied) { + SmartDialog.show( + useSystem: true, + animationType: SmartAnimationType.centerFade_otherSlide, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('提示'), + content: const Text('存储权限未授权'), + actions: [ + TextButton( + onPressed: () async { + openAppSettings(); + }, + child: const Text('去授权'), + ) + ], + ); + }, + ); + return false; + } else { + return true; + } + } + + // 获取相册权限 + static Future requestPhotoPer() async { + await Permission.photos.request(); + PermissionStatus status = await Permission.photos.status; + if (status == PermissionStatus.denied || + status == PermissionStatus.permanentlyDenied) { + SmartDialog.show( + useSystem: true, + animationType: SmartAnimationType.centerFade_otherSlide, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('提示'), + content: const Text('相册权限未授权'), + actions: [ + TextButton( + onPressed: () async { + openAppSettings(); + }, + child: const Text('去授权'), + ) + ], + ); + }, + ); + return false; + } else { + return true; + } } static Future downloadImg(String imgUrl, {String imgType = 'cover'}) async { try { - await requestStoragePer(); + if (!await requestPhotoPer()) { + return false; + } SmartDialog.showLoading(msg: '保存中'); var response = await Dio() .get(imgUrl, options: Options(responseType: ResponseType.bytes)); + final String imgSuffix = imgUrl.split('.').last; String picName = - "plpl_${imgType}_${DateTime.now().toString().split('-').join()}"; + "plpl_${imgType}_${DateTime.now().toString().replaceAll(RegExp(r'[- :]'), '').split('.').first}"; final SaveResult result = await SaverGallery.saveImage( Uint8List.fromList(response.data), - quality: 60, - name: picName, + name: '$picName.$imgSuffix', // 保存到 PiliPala文件夹 androidRelativePath: "Pictures/PiliPala", androidExistNotSave: false, ); SmartDialog.dismiss(); if (result.isSuccess) { - await SmartDialog.showToast('「$picName」已保存 '); + await SmartDialog.showToast('「${'$picName.$imgSuffix'}」已保存 '); } return true; } catch (err) {