83 lines
2.6 KiB
Dart
83 lines
2.6 KiB
Dart
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
|
|
/// Information about a permission that the application cares about.
|
|
///
|
|
/// Add new entries to [appPermissions] when the app requires additional
|
|
/// runtime permissions. The UI that drives the "permissions screen" reads this
|
|
/// list and will automatically include new permissions without any further
|
|
/// changes.
|
|
class PermissionInfo {
|
|
const PermissionInfo({
|
|
required this.permission,
|
|
required this.label,
|
|
this.description,
|
|
});
|
|
|
|
final Permission permission;
|
|
final String label;
|
|
final String? description;
|
|
}
|
|
|
|
/// The set of permissions the app will check/request from the user.
|
|
///
|
|
/// Currently contains the permissions actually referenced in the codebase, but
|
|
/// it should be expanded as new features are added. Declaring a permission
|
|
/// here ensures it appears on the permissions screen and is considered when
|
|
/// performing bulk checks/requests.
|
|
List<PermissionInfo> get appPermissions {
|
|
// On web, browser notifications are handled separately; avoid including
|
|
// the notification runtime permission in the app permissions list so the
|
|
// permissions screen and bulk request flow do not attempt to enforce it.
|
|
if (kIsWeb) {
|
|
return [
|
|
PermissionInfo(
|
|
permission: Permission.location,
|
|
label: 'Location',
|
|
description: 'Access your location for geofencing',
|
|
),
|
|
];
|
|
}
|
|
|
|
return [
|
|
PermissionInfo(
|
|
permission: Permission.notification,
|
|
label: 'Notifications',
|
|
description: 'Deliver alerts and sounds',
|
|
),
|
|
PermissionInfo(
|
|
permission: Permission.location,
|
|
label: 'Location',
|
|
description: 'Access your location for geofencing',
|
|
),
|
|
// add new permissions here as necessary
|
|
];
|
|
}
|
|
|
|
/// Returns the current status for every permission in [appPermissions].
|
|
Future<Map<Permission, PermissionStatus>> getAllStatuses() async {
|
|
final map = <Permission, PermissionStatus>{};
|
|
for (final info in appPermissions) {
|
|
map[info.permission] = await info.permission.status;
|
|
}
|
|
return map;
|
|
}
|
|
|
|
/// Requests [permission] and returns the resulting status.
|
|
Future<PermissionStatus> requestPermission(Permission permission) async {
|
|
return permission.request();
|
|
}
|
|
|
|
/// Convenience that requests every permission that is currently not granted.
|
|
///
|
|
/// Returns the full status map after requesting.
|
|
Future<Map<Permission, PermissionStatus>> requestAllNeeded() async {
|
|
final statuses = await getAllStatuses();
|
|
for (final entry in statuses.entries) {
|
|
if (!entry.value.isGranted) {
|
|
statuses[entry.key] = await entry.key.request();
|
|
}
|
|
}
|
|
return statuses;
|
|
}
|