generated from nventive/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeolocatorServiceExtensions.cs
More file actions
74 lines (66 loc) · 2.79 KB
/
GeolocatorServiceExtensions.cs
File metadata and controls
74 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
namespace GeolocatorService
{
public static class GeolocatorServiceExtensions
{
/// <summary>
/// Observes the user's location.
/// </summary>
public static IObservable<Geocoordinate> ObserveLocation(this IGeolocatorService service)
{
return Observable.FromEventPattern<LocationChangedEventHandler, LocationChangedEventArgs> (
h => service.LocationChanged += h,
h => service.LocationChanged -= h
)
.Select(p => p.EventArgs.Coordinate);
}
/// <summary>
/// Gets and observes the user's location (or null if it's unavailable). Requests the permission if this was not already done.
/// The location becomes null as soon as the location permission is denied.
/// </summary>
public static IObservable<Geocoordinate> GetAndObserveLocationOrDefault(this IGeolocatorService service)
{
var initialLocationOrNull = Observable.Create<Geocoordinate>(service.GetInitialLocationOrDefault);
var locationObservable = ObserveLocation(service);
var locationUnavailableObservable = ObserveIsPermissionGranted(service)
.Where(isGranted => !isGranted)
.Select(_ => default(Geocoordinate));
return Observable.Merge(initialLocationOrNull, locationObservable, locationUnavailableObservable);
}
private static async Task GetInitialLocationOrDefault(this IGeolocatorService service, IObserver<Geocoordinate> observer, CancellationToken ct)
{
var isPermissionGranted = await service.RequestPermission(ct);
var currentLocationOrNull = isPermissionGranted ? await service.GetLocationOrDefault(ct) : null;
observer.OnNext(currentLocationOrNull);
}
/// <summary>
/// Observes the location permission.
/// </summary>
public static IObservable<bool> ObserveIsPermissionGranted(this IGeolocatorService service)
{
return Observable.FromEventPattern<LocationPermissionChangedEventHandler, LocationPermissionChangedEventArgs>(
h => service.LocationPermissionChanged += h,
h => service.LocationPermissionChanged -= h
)
.Select(p => p.EventArgs.IsLocationPermissionGranted);
}
/// <summary>
/// Gets and observes the location permission. Requests the permission if this was not already done.
/// </summary>
public static IObservable<bool> GetAndObserveIsPermissionGranted(this IGeolocatorService service)
{
var initialPermission = Observable.Create<bool>(service.GetInitialPermission);
return ObserveIsPermissionGranted(service).Merge(initialPermission);
}
private static async Task GetInitialPermission(this IGeolocatorService service, IObserver<bool> observer, CancellationToken ct)
{
var isPermissionGranted = await service.RequestPermission(ct);
observer.OnNext(isPermissionGranted);
}
}
}