mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 15:46:29 +01:00
72 lines
3.1 KiB
C#
72 lines
3.1 KiB
C#
using Serilog;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Device;
|
|
using Xamarin.Essentials;
|
|
|
|
namespace TINK.Model.Services.Geolocation
|
|
{
|
|
public class LastKnownGeolocationService : IGeolocation
|
|
{
|
|
private IGeolodationDependent Dependent { get; }
|
|
|
|
public LastKnownGeolocationService(IGeolodationDependent dependent)
|
|
{
|
|
Dependent = dependent;
|
|
}
|
|
|
|
/// <summary> Gets the current location.</summary>
|
|
/// <param name="cancelationToken">Token to cancel request for geolocation.</param>
|
|
/// <param name="timeStamp">Time when geolocation is of interest. Is used to determine whether cached geoloation can be used or not.</param>
|
|
public async Task<Location> GetAsync(CancellationToken? cancelationToken = null, DateTime? timeStamp = null)
|
|
{
|
|
Location location;
|
|
try
|
|
{
|
|
location = await Xamarin.Essentials.Geolocation.GetLastKnownLocationAsync();
|
|
}
|
|
catch (FeatureNotSupportedException fnsEx)
|
|
{
|
|
// Handle not supported on device exception
|
|
Log.ForContext<LastKnownGeolocationService>().Error("Retrieving Geolocation not supported on device. {Exception}", fnsEx);
|
|
throw new Exception("Abfrage Standort nicht möglich auf Gerät.", fnsEx);
|
|
}
|
|
catch (FeatureNotEnabledException fneEx)
|
|
{
|
|
// Handle not enabled on device exception
|
|
Log.ForContext<LastKnownGeolocationService>().Error("Retrieving Geolocation not enabled on device. {Exception}", fneEx);
|
|
throw new Exception("Abfrage Standort nicht aktiviert auf Gerät.", fneEx);
|
|
}
|
|
catch (PermissionException pEx)
|
|
{
|
|
// Handle permission exception
|
|
Log.ForContext<LastKnownGeolocationService>().Error("Retrieving Geolocation not permitted on device. {Exception}", pEx);
|
|
throw new Exception("Berechtiung für Abfrage Standort nicht erteilt für App.", pEx);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Unable to get location
|
|
Log.ForContext<LastKnownGeolocationService>().Error("Retrieving Geolocation failed. {Exception}", ex);
|
|
throw new Exception("Abfrage Standort fehlgeschlagen.", ex);
|
|
}
|
|
|
|
if (location != null // Cached location is available.
|
|
&& (timeStamp == null || timeStamp.Value.Subtract(location.Timestamp.DateTime) < MaxAge))
|
|
{
|
|
// No time stamp available or location not too old.
|
|
return location;
|
|
}
|
|
|
|
return await new GeolocationService(Dependent).GetAsync(cancelationToken, timeStamp);
|
|
}
|
|
|
|
/// <summary> If true location data returned is simulated.</summary>
|
|
public bool IsSimulation { get => false; }
|
|
|
|
/// <summary> Maximum age allowed for location info. </summary>
|
|
public TimeSpan MaxAge => new TimeSpan(0, 3 /*minutes*/, 0);
|
|
|
|
public bool IsGeolcationEnabled => Dependent.IsGeolcationEnabled;
|
|
}
|
|
}
|