@@ -7,6 +7,8 @@ import 'package:permission_handler/permission_handler.dart';
7
7
abstract interface class PermissionClient {
8
8
Future <bool > hasCameraPermission ();
9
9
Future <bool > hasGalleryPermission ();
10
+ Future <void > requestCameraPermission ();
11
+ Future <void > requestGalleryPermission ();
10
12
}
11
13
12
14
@Injectable (as : PermissionClient )
@@ -17,88 +19,79 @@ final class PermissionClientImpl implements PermissionClient {
17
19
18
20
@override
19
21
Future <bool > hasCameraPermission () async {
20
- final hasPermission = await _isCameraPermissionGranted ();
21
-
22
- if (hasPermission) {
23
- return true ;
24
- } else {
25
- await _requestCameraPermission ();
26
- return false ;
27
- }
22
+ final hasCameraPermission = await _isCameraPermissionGranted ();
23
+ return hasCameraPermission;
28
24
}
29
25
30
26
@override
31
27
Future <bool > hasGalleryPermission () async {
32
- final hasPermission = await _isGalleryPermissionGranted ();
33
- if (hasPermission) {
34
- return true ;
35
- } else {
36
- await _requestGalleryPermission ();
37
- return false ;
38
- }
28
+ final hasGalleryPermission = await _isGalleryPermissionGranted ();
29
+ return hasGalleryPermission;
39
30
}
40
31
41
- Future <bool > _isGalleryPermissionGranted () async {
42
- late final PermissionStatus status;
43
- if (Platform .isAndroid) {
44
- final permission = await _getAndroidGalleryPermissionType ();
45
- status = await permission.status;
46
- } else if (Platform .isIOS) {
47
- status = await Permission .photos.status;
48
- }
49
- return status.isGranted;
50
- }
51
-
52
- Future <bool > _isCameraPermissionGranted () async {
53
- final status = await Permission .camera.status;
54
- return status.isGranted;
55
- }
56
-
57
- Future <void > _requestGalleryPermission () async {
32
+ @override
33
+ Future <void > requestCameraPermission () async {
58
34
if (Platform .isAndroid) {
59
- final permission = await _getAndroidGalleryPermissionType () ;
35
+ const permission = Permission .camera ;
60
36
61
37
final before = await permission.shouldShowRequestRationale;
62
38
final rs = await permission.request ();
63
39
final after = await permission.shouldShowRequestRationale;
40
+
64
41
// If the user denies the permission twice, openAppSettings will be called
65
42
if (! rs.isGranted && ! before && ! after) {
66
43
await openAppSettings ();
67
44
}
68
45
} else if (Platform .isIOS) {
69
- final result = await Permission .photos .status;
46
+ final result = await Permission .camera .status;
70
47
71
48
if (result.isDenied) {
72
- await Permission .photos .request ();
49
+ await Permission .camera .request ();
73
50
} else if (result.isPermanentlyDenied) {
74
51
await openAppSettings ();
75
52
}
76
53
}
77
54
}
78
55
79
- Future <void > _requestCameraPermission () async {
56
+ @override
57
+ Future <void > requestGalleryPermission () async {
80
58
if (Platform .isAndroid) {
81
- const permission = Permission .camera ;
59
+ final permission = await _getAndroidGalleryPermissionType () ;
82
60
83
61
final before = await permission.shouldShowRequestRationale;
84
62
final rs = await permission.request ();
85
63
final after = await permission.shouldShowRequestRationale;
86
-
87
64
// If the user denies the permission twice, openAppSettings will be called
88
65
if (! rs.isGranted && ! before && ! after) {
89
66
await openAppSettings ();
90
67
}
91
68
} else if (Platform .isIOS) {
92
- final result = await Permission .camera .status;
69
+ final result = await Permission .photos .status;
93
70
94
71
if (result.isDenied) {
95
- await Permission .camera .request ();
72
+ await Permission .photos .request ();
96
73
} else if (result.isPermanentlyDenied) {
97
74
await openAppSettings ();
98
75
}
99
76
}
100
77
}
101
78
79
+ Future <bool > _isGalleryPermissionGranted () async {
80
+ late final PermissionStatus status;
81
+ if (Platform .isAndroid) {
82
+ final permission = await _getAndroidGalleryPermissionType ();
83
+ status = await permission.status;
84
+ } else if (Platform .isIOS) {
85
+ status = await Permission .photos.status;
86
+ }
87
+ return status.isGranted;
88
+ }
89
+
90
+ Future <bool > _isCameraPermissionGranted () async {
91
+ final status = await Permission .camera.status;
92
+ return status.isGranted;
93
+ }
94
+
102
95
/// Returns the type of the permission depending on the sdk version
103
96
///* Returns [Permission.storage] if device sdk version is <=32
104
97
///* Returns [Permission.photos] if device sdk version is >32
0 commit comments