Skip to content

Commit cad6069

Browse files
authored
add requested state to permissionStatus, fix requested bg location show up (#6)
* add requested state to permissionStatus, fix requested bg location shows up * update readme * update PR template
1 parent b8679a1 commit cad6069

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

.github/pull_request_template.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Describe your changes.
33

44
#### How do we test this change?
5-
- [ ] Describe how we can test this change.
5+
1. Describe how we can test this change.
66

77
#### Any screenshot for this change?
88
Post any screenshots for your feature, if any.
99

1010
#### Checklist
11-
- [ ] Run the `pre_pr.sh` script to ensure code quality.
11+
- [ ] Create suitable unit/widget tests for your change.
12+
- [ ] Run the `pre_pr.sh` script to ensure code quality.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Show permission disclosure page and allows required permissions and their associ
66
This package shows a prominent in-app disclosure page for getting permissions as required by [Google Play](https://support.google.com/googleplay/android-developer/answer/9799150?visit_id=638041800350153935-369621111&p=pd-m&rd=1#prominent_disclosure&zippy=%2Cstep-provide-prominent-in-app-disclosure%2Cstep-review-best-practices-for-accessing-location%2Cstep-consider-alternatives-to-accessing-location-in-the-background%2Cstep-make-access-to-location-in-the-background-clear-to-users%2Csee-an-example-of-prominent-in-app-disclosure).
77
Also support iOS to ensure a consistent experience.
88

9-
In addition, permissions can be set as "required". If this is set, those required permissions will be required and if users denied it,
10-
this package will show a custom dialog and redirect user to the appropriate settings page provided by the OS.
9+
In addition, permissions and their associated services (e.g. GPS) can be set as "required". If this is set, those required permissions will
10+
be required and if users denied it, this package will show a customizable dialog and redirect user to the appropriate settings page provided by the native OS.
1111

1212
## Setup
1313
1. Add the following to `pubspec.yaml`
@@ -65,7 +65,7 @@ final perm = FlutterForcePermission(
6565
2. Show the disclosure page as needed. This method will handle showing the disclosure page and requesting permissions.
6666
This function takes a [NavigatorState](https://api.flutter.dev/flutter/widgets/NavigatorState-class.html) which can be retrieved through `Navigator.of(context)` call.
6767
This is an async function. Wrap the function in an `async` block as needed.
68-
Returns a map of permission and their requested status (granted/denied/etc). Refer to [permission_handler](https://pub.dev/packages/permission_handler) for the interface.
68+
Returns a map of permission and their requested status (granted/denied/etc), service status and whether they are requested by this plugin.
6969
```dart
7070
final result = await perm.show(Navigator.of(context));
7171
```

lib/flutter_force_permission.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ class FlutterForcePermission {
4545
return permissionStatuses;
4646
}
4747

48-
final prefs = await _service.getSharedPreference();
4948
var needShow = false;
5049
for (final permConfig in config.permissionItemConfigs) {
5150
for (final perm in permConfig.permissions) {
52-
final requested = prefs.getBool(getRequestedPrefKey(perm));
53-
if (requested != true) {
51+
if (!(permissionStatuses[perm]?.requested ?? true)) {
5452
needShow = true;
5553
break;
5654
}
@@ -94,17 +92,20 @@ class FlutterForcePermission {
9492
/// Only permissions specified in the configuration will be queried and returned.
9593
Future<Map<Permission, PermissionServiceStatus>>
9694
getPermissionStatuses() async {
95+
final prefs = await _service.getSharedPreference();
9796
final Map<Permission, PermissionServiceStatus> result = {};
9897
for (final List<Permission> perms
9998
in config.permissionItemConfigs.map((e) => e.permissions)) {
10099
for (final Permission perm in perms) {
101100
final status = await _service.status(perm);
101+
final requested = prefs.getBool(getRequestedPrefKey(perm)) ?? false;
102102
ServiceStatus? serviceStatus;
103103
if (perm is PermissionWithService) {
104104
serviceStatus = await _service.serviceStatus(perm);
105105
}
106106
result[perm] = PermissionServiceStatus(
107107
status: status,
108+
requested: requested,
108109
serviceStatus: serviceStatus,
109110
);
110111
}

lib/permission_service_status.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ import 'package:permission_handler/permission_handler.dart';
44
class PermissionServiceStatus {
55
PermissionServiceStatus({
66
required this.status,
7+
required this.requested,
78
this.serviceStatus,
89
});
910

1011
/// Status of the permission.
1112
final PermissionStatus status;
1213

14+
/// Whether the permission is requested by this plugin.
15+
///
16+
/// Note that detecting whether a permission is requested by anywhere from the app is not possible due to
17+
/// a lack of an API to query such information without actually making the request in both Android and iOS.
18+
final bool requested;
19+
1320
/// Status of the service associated to the permission. Null if no associated service.
1421
final ServiceStatus? serviceStatus;
1522

1623
@override
1724
String toString() =>
18-
'PermissionServiceStatus{status: $status, serviceStatus: $serviceStatus}';
25+
'PermissionServiceStatus{status: $status, requested: $requested, serviceStatus: $serviceStatus}';
1926
}

lib/src/views/disclosure_page.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ class _DisclosurePageState extends State<DisclosurePage>
110110
final permissionItems =
111111
widget.permissionConfig.permissionItemConfigs.expand((e) {
112112
var denied = false;
113+
var requested = false;
113114
var serviceDisabled = false;
114115
for (final Permission p in e.permissions) {
115116
if (widget.permissionStatuses[p]?.status != PermissionStatus.granted) {
116117
denied = true;
117118
}
119+
if (widget.permissionStatuses[p]?.requested ?? true) {
120+
requested = true;
121+
}
118122
if (widget.permissionStatuses[p]?.serviceStatus ==
119123
ServiceStatus.disabled) {
120124
serviceDisabled = true;
@@ -128,7 +132,7 @@ class _DisclosurePageState extends State<DisclosurePage>
128132
if (serviceDisabled && serviceText != null) {
129133
result.add(_PermissionItem(permission, serviceText));
130134
}
131-
if (denied) {
135+
if (denied && !requested) {
132136
result.add(_PermissionItem(permission, itemText));
133137
}
134138

test/widget_test/disclosure_page_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void main() {
4949
final status = <Permission, PermissionServiceStatus>{
5050
Permission.location: PermissionServiceStatus(
5151
status: PermissionStatus.denied,
52+
requested: false,
5253
serviceStatus: ServiceStatus.enabled,
5354
),
5455
};
@@ -111,6 +112,7 @@ void main() {
111112
final status = <Permission, PermissionServiceStatus>{
112113
Permission.location: PermissionServiceStatus(
113114
status: PermissionStatus.denied,
115+
requested: false,
114116
serviceStatus: ServiceStatus.enabled,
115117
),
116118
};
@@ -175,6 +177,7 @@ void main() {
175177
final status = <Permission, PermissionServiceStatus>{
176178
Permission.location: PermissionServiceStatus(
177179
status: PermissionStatus.denied,
180+
requested: false,
178181
serviceStatus: ServiceStatus.enabled,
179182
),
180183
};
@@ -269,6 +272,7 @@ void main() {
269272
final status = <Permission, PermissionServiceStatus>{
270273
Permission.location: PermissionServiceStatus(
271274
status: PermissionStatus.granted,
275+
requested: false,
272276
serviceStatus: ServiceStatus.disabled,
273277
),
274278
};

0 commit comments

Comments
 (0)