Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/GeolocatorService.Reactive/GeolocatorServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ public static IObservable<bool> ObserveIsPermissionGranted(this IGeolocatorServi
/// <summary>
/// Gets and observes the location permission. Requests the permission if this was not already done.
/// </summary>
public static async Task<IObservable<bool>> GetAndObserveIsPermissionGranted(this IGeolocatorService service, CancellationToken ct)
public static IObservable<bool> GetAndObserveIsPermissionGranted(this IGeolocatorService service)
{
var isPermissionGranted = await service.RequestPermission(ct);
var initialPermission = Observable.Create<bool>(service.GetInitialPermission);

return ObserveIsPermissionGranted(service).Merge(initialPermission);
}

return ObserveIsPermissionGranted(service).StartWith(isPermissionGranted);
private static async Task GetInitialPermission(this IGeolocatorService service, IObserver<bool> observer, CancellationToken ct)
{
var isPermissionGranted = await service.RequestPermission(ct);
observer.OnNext(isPermissionGranted);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Margin="0,40,0,0">
<StackPanel Margin="0,40,0,0"
Spacing="20">

<Button Content="Request permission"
Command="{Binding RequestPermissionCommand}" />

<TextBlock Text="{Binding PageContent}" />

<Button Content="Get location"
Command="{Binding GetLocation}" />

<TextBlock Text="{Binding PermissionStatus}" />
</StackPanel>
</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class MainPageViewModel : INotifyPropertyChanged
{
private CoreDispatcher _dispatcher;
private IGeolocatorService _geolocatorService;
private IDisposable _subscription = null;
private IDisposable _locationOrNullSubscription = null;
private IDisposable _permissionSubscription = null;

public event PropertyChangedEventHandler PropertyChanged;

Expand Down Expand Up @@ -48,10 +49,36 @@ private void SetPageContent(string content)
}
}

private string _permissionStatus = "";
public string PermissionStatus
{
get => _permissionStatus;
set
{
_permissionStatus = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PermissionStatus)));
}
}

private void SetPermissionStatus(string content)
{
if (_dispatcher.HasThreadAccess)
{
PermissionStatus = content;
}
else
{
_ = _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
SetPermissionStatus(content);
});
}
}

public DelegateCommand RequestPermissionCommand => new DelegateCommand(() =>
{
_subscription = _geolocatorService.
GetAndObserveLocationOrDefault()
_locationOrNullSubscription = _geolocatorService
.GetAndObserveLocationOrDefault()
.Subscribe(
location =>
{
Expand All @@ -69,6 +96,39 @@ private void SetPageContent(string content)
SetPageContent($"An error has occurred: {error}");
}
);

_permissionSubscription = _geolocatorService
.GetAndObserveIsPermissionGranted()
.Subscribe(
isGranted =>
{
if (isGranted)
{
SetPermissionStatus("Permission: Granted");
}
else
{
SetPermissionStatus($"Permission: Denied");
}
},
error =>
{
SetPermissionStatus($"An error has occurred: {error}");
}
);
});

public DelegateCommand GetLocationCommand => new DelegateCommand(async () =>
{
try
{
var location = await _geolocatorService.GetLocation(CancellationToken.None);
SetPageContent($"GetLocation- ({location.Point.Position.Longitude}, {location.Point.Position.Latitude})");
}
catch(Exception ex)
{
SetPageContent($"GetLocation- An error has occurred : {ex}");
}
});

public class DelegateCommand : ICommand
Expand Down