123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import 'package:flutter/material.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:photo_manager/photo_manager.dart';
- import 'package:shared/utils/device_utils.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:widgets/dialog/app_default_dialog.dart';
- import 'package:widgets/dialog/permission_desc_dialog.dart';
- import '../dialog/dialog_engine.dart';
- class PermissionEngine {
-
- PermissionEngine._privateConstructor();
-
- static final PermissionEngine _instance = PermissionEngine._privateConstructor();
-
- factory PermissionEngine() {
- return _instance;
- }
-
- void requestPhotosPermission(void Function() success) async {
-
- if (DeviceUtils.isIOS) {
-
- final value = await PhotoManager.requestPermissionExtend();
- if (value.hasAccess) {
-
- Log.d("相册已授权");
- success();
- } else if (value == PermissionState.limited) {
- Log.d("相册访问受限,去设置受限");
- PhotoManager.presentLimited();
- } else {
- Log.d("相册无授权,去设置");
- DialogEngine.show(
- widget: AppDefaultDialog(
- title: "Alert",
- message: 'No album permission, go to settings?',
- confirmAction: () {
- PhotoManager.openSetting();
- },
- ),
- );
- }
- } else {
-
- var status = await Permission.storage.status;
- late PermissionState ps;
- if (status.isGranted) {
-
- success();
- } else {
-
- var permissionRequestFuture = PhotoManager.requestPermissionExtend();
-
- var delayFuture = Future.delayed(const Duration(milliseconds: 500), () => 'delay');
-
- var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
-
- if (firstCompleted == 'delay') {
- Log.d("判断响应结果:1");
-
- _showPermissionDialog("“YYHome” would like to access your multimedia album for functions such as image uploading and saving. Please allow me to obtain your permission");
-
- ps = await permissionRequestFuture;
- DialogEngine.dismiss(tag: "permission");
- } else {
- Log.d("判断响应结果:2");
-
- DialogEngine.dismiss(tag: "permission");
- ps = firstCompleted as PermissionState;
- }
- if (ps.isAuth) {
-
- success();
- } else {
-
- await DialogEngine.show(
- widget: AppDefaultDialog(
- message: "Please set the permission to open the photo album on your phone",
- title: "Alert",
- confirmAction: () {
- openAppSettings();
- }),
- );
- }
- }
- }
- }
-
- void requestCameraPermission(void Function() success) async {
-
- var status = await Permission.camera.status;
- if (status.isGranted) {
-
- success();
- } else {
-
- var permissionRequestFuture = Permission.camera.request();
-
- var delayFuture = Future.delayed(const Duration(milliseconds: 500), () => 'delay');
-
- var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
-
- if (firstCompleted == 'delay') {
-
- _showPermissionDialog("“YYHome” requests to use your camera permission for functions such as taking avatars, uploading and saving images. Please allow me to obtain your permission");
-
- status = await permissionRequestFuture;
- DialogEngine.dismiss(tag: "permission");
- } else {
-
- DialogEngine.dismiss(tag: "permission");
- status = firstCompleted as PermissionStatus;
- }
- if (status.isGranted) {
-
- success();
- } else {
-
- await DialogEngine.show(
- widget: AppDefaultDialog(
- message: "Please set the permission to open the camera on your phone",
- title: "Alert",
- confirmAction: () {
- openAppSettings();
- }),
- );
- }
- }
- }
-
- Future<bool> requestLocationPermission() async {
-
- var status = await Permission.location.status;
- if (status.isGranted) {
-
- return true;
- } else {
-
- var permissionRequestFuture = Permission.location.request();
-
- var delayFuture = Future.delayed(const Duration(milliseconds: 500), () => 'delay');
-
- var firstCompleted = await Future.any([permissionRequestFuture, delayFuture]);
-
- if (firstCompleted == 'delay') {
-
- _showPermissionDialog("“YYHome” want to access your location permission to obtain your location and recommend nearby information");
-
- status = await permissionRequestFuture;
- DialogEngine.dismiss(tag: "permission");
- } else {
- Log.d("权限请求已完成,立刻取消对话框展示");
-
- DialogEngine.dismiss(tag: "permission");
- status = firstCompleted as PermissionStatus;
- }
- if (status.isGranted) {
-
- return true;
- } else {
-
- await DialogEngine.show(
- widget: AppDefaultDialog(
- message: "Please go to your phone settings to enable location permission",
- title: "Alert",
- confirmAction: () {
- openAppSettings();
- }),
- );
- return false;
- }
- }
- }
-
- void _showPermissionDialog(String desc) {
- DialogEngine.show(
- clickMaskDismiss: false,
- backDismiss: true,
- tag: "permission",
- maskColor: Colors.transparent,
- widget: PermissionDescDialog(desc),
- );
- }
- }
|