From 1417f4fb37f15e0ae5c1cdb3c8a53ef1207ac245 Mon Sep 17 00:00:00 2001 From: Ali Almoullim Date: Mon, 2 Sep 2019 11:28:13 +0300 Subject: [PATCH] Add getPermissions and checkPermissions --- CHANGELOG.md | 5 +++++ README.md | 28 +++++++++++++++++++++--- example/pubspec.lock | 9 +++++++- lib/background_location.dart | 41 ++++++++++++++++++++++++++++++++++++ pubspec.lock | 7 ++++++ pubspec.yaml | 3 ++- 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f318dbb..fe6b134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.0.7 + +Add `Future getPermissions(onGranted:onDenied:)` +Add `Future checkPermissions()` + ## 0.0.6 Allow Significant-change for longer location updates (iOS) diff --git a/README.md b/README.md index b47edd8..7f59d84 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ PS: This project was originaly created by [@shah-xad](https://github.com/shah-xa ```yaml dependencies: - background_location: ^0.0.6 + background_location: ^0.0.7 ``` **2:** Install packages from the command line: @@ -29,6 +29,28 @@ Import the package where you wanna use it. import 'package:background_location/background_location.dart'; ``` +Request permissions from the user. + +```dart +BackgroundLocation.getPermissions( + onGranted: () { + // Start location service here or do something else + }, + onDenied: () { + // Show a message asking the user to reconsider or do something else + }, +) +``` + +You can check if you have permissions at anytime with `checkPermissions()` + +```dart +BackgroundLocation.checkPermissions().then((status) { + // Check status here +}) + +``` + Start the location service. This will also ask the user for permission if not asked previously by another package. ```dart @@ -66,8 +88,8 @@ BackgroundLocation.stopLocationService(); ## Todo -- [ ] Add support for manually asking for permission. -- [ ] Add support for checking the permission status. +- [x] Add support for manually asking for permission. +- [x] Add support for checking the permission status. - [ ] Add support for getting the last location once without listening to location updates. - [ ] Add support for chosing the rate at the which the location is fetched based on time and distance. diff --git a/example/pubspec.lock b/example/pubspec.lock index e4789f5..1ac2a78 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -14,7 +14,7 @@ packages: path: ".." relative: true source: path - version: "0.0.6" + version: "0.0.7" boolean_selector: dependency: transitive description: @@ -81,6 +81,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.7.0" + permission_handler: + dependency: transitive + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.2" quiver: dependency: transitive description: diff --git a/lib/background_location.dart b/lib/background_location.dart index e1f0409..29999e6 100644 --- a/lib/background_location.dart +++ b/lib/background_location.dart @@ -1,18 +1,26 @@ import 'dart:async'; import 'package:flutter/services.dart'; +import 'package:permission_handler/permission_handler.dart'; +/// BackgroundLocation plugin to get background +/// lcoation updates in iOS and Android class BackgroundLocation { + // The channel to be used for communication. + // This channel is also refrenced inside both iOS and Abdroid classes static const MethodChannel _channel = const MethodChannel('almoullim.com/background_location'); + /// Stop receiving location updates static stopLocationService() { _channel.invokeMapMethod("stop_location_service"); } + /// Start receiving location updated static startLocationService() { _channel.invokeMapMethod("start_location_service"); } + /// Get the current location once. Future<_Location> getCurrentLocation() async { Completer<_Location> completer = Completer(); @@ -30,10 +38,41 @@ class BackgroundLocation { return completer.future; } + /// Ask the user for location permissions + static getPermissions({Function onGranted, Function onDenied}) async { + await PermissionHandler() + .requestPermissions([PermissionGroup.locationAlways]); + PermissionStatus permission = await PermissionHandler() + .checkPermissionStatus(PermissionGroup.locationAlways); + if (permission == PermissionStatus.granted) { + if (onGranted != null) { + onGranted(); + } + } else if (permission == PermissionStatus.denied || + permission == PermissionStatus.disabled || + permission == PermissionStatus.restricted || + permission == PermissionStatus.unknown) { + if (onDenied != null) { + onDenied(); + } + } + } + + /// Check what the current permissions status is + static Future checkPermissions() async { + PermissionStatus permission = await PermissionHandler() + .checkPermissionStatus(PermissionGroup.locationAlways); + return permission; + } + + /// Register a function to recive location updates as long as the location + /// service has started static getLocationUpdates(Function(_Location) location) { + // add a handler on the channel to recive updates from the native classes _channel.setMethodCallHandler((MethodCall methodCall) async { if (methodCall.method == "location") { Map locationData = Map.from(methodCall.arguments); + // Call the user passed function location( _Location( latitude: locationData["latitude"], @@ -49,6 +88,8 @@ class BackgroundLocation { } } +/// An object containing infromation +/// about the user current location class _Location { _Location( {this.longitude, diff --git a/pubspec.lock b/pubspec.lock index 7642229..f7cf4bf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -67,6 +67,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.7.0" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.2" quiver: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4cc43b7..bf80c7f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: background_location description: A Flutter plugin to get location updates in the background for both Android and iOS. Uses CoreLocation for iOS and FusedLocationProvider for Android. -version: 0.0.6 +version: 0.0.7 authors: - Ali Almoullim - Shahzad Akram @@ -13,6 +13,7 @@ environment: dependencies: flutter: sdk: flutter + permission_handler: ^3.2.1+1 dev_dependencies: flutter_test: