sharee.bike-App/TINKLib/Services/Permissions/Essentials/Permissions.cs

81 lines
2.9 KiB
C#
Raw Permalink Normal View History

2021-11-07 19:42:59 +01:00
using System.Threading.Tasks;
using Xamarin.Essentials;
namespace TINK.Services.Permissions.Essentials
{
using XamPermission = Xamarin.Essentials.Permissions;
using XamPermissionStatus = PermissionStatus;
public class Permissions : 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;
}
}
}