sharee.bike-App/TINKLib/Services/Permissions/Essentials/LocationPermissions.cs
2022-12-07 16:54:52 +01:00

80 lines
2.2 KiB
C#

using System.Threading.Tasks;
using Xamarin.Essentials;
namespace TINK.Services.Permissions.Essentials
{
using XamPermission = Xamarin.Essentials.Permissions;
using XamPermissionStatus = PermissionStatus;
public class LocationPermissions : ILocationPermission
{
/// <summary> Checks the permission status.</summary>
/// <returns>Current permission status.</returns>
public async Task<Status> CheckStatusAsync()
{
var status = await XamPermission.CheckStatusAsync<XamPermission.LocationWhenInUse>();
if (status == XamPermissionStatus.Granted)
{
return Status.Granted;
}
if (status == XamPermissionStatus.Denied
&& DeviceInfo.Platform == DevicePlatform.iOS)
{
// Prompt the user to turn on in settings
// On iOS once a permission has been denied it may not be requested again from the application
return Status.DeniedRequiresSettingsUI;
}
if (XamPermission.ShouldShowRationale<XamPermission.LocationWhenInUse>())
{
// Prompt the user with additional information as to why the permission is needed
return Status.Denied;
}
switch (status)
{
case XamPermissionStatus.Unknown:
case XamPermissionStatus.Denied: // Map Denied to unknown because "denied means user did not allow access to location".
return Status.Unknown;
default:
// Comprises:
// - XamPermissionStatus.Restricted:
// - XamPermissionStatus.Disabled
// Perission XamPermissionStatus.Granted is handled above.
return Status.DeniedRequiresSettingsUI;
}
}
/// <summary> Requests location permission.</summary>
/// <returns>Permission status after request.</returns>
public async Task<Status> RequestAsync()
{
switch (await XamPermission.RequestAsync<XamPermission.LocationWhenInUse>())
{
case XamPermissionStatus.Unknown:
return Status.Unknown;
case XamPermissionStatus.Denied:
return Status.Denied;
case XamPermissionStatus.Granted:
return Status.Granted;
default:
// Comprises:
// - XamPermissionStatus.Restricted:
// - XamPermissionStatus.Disabled
return Status.DeniedRequiresSettingsUI;
}
}
/// <summary> Opens app settings dialog.</summary>
public bool OpenAppSettings()
{
AppInfo.ShowSettingsUI();
return true;
}
}
}