Skip to content

Commit

Permalink
Add getPermissions and checkPermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Almoullim committed Sep 2, 2019
1 parent d89f92f commit 1417f4f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.7

Add `Future getPermissions(onGranted:onDenied:)`
Add `Future<PermissionStatus> checkPermissions()`

## 0.0.6

Allow Significant-change for longer location updates (iOS)
Expand Down
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
9 changes: 8 additions & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.6"
version: "0.0.7"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions lib/background_location.dart
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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<PermissionStatus> 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"],
Expand All @@ -49,6 +88,8 @@ class BackgroundLocation {
}
}

/// An object containing infromation
/// about the user current location
class _Location {
_Location(
{this.longitude,
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <ali.almoullim@gmail.com>
- Shahzad Akram <shahxadakram@gmail.com>
Expand All @@ -13,6 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
permission_handler: ^3.2.1+1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 1417f4f

Please sign in to comment.