2021-11-07 19:42:59 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Xamarin.Essentials;
|
|
|
|
|
|
|
|
|
|
namespace TINK.Services.Permissions.Essentials
|
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
|
using XamPermission = Xamarin.Essentials.Permissions;
|
|
|
|
|
using XamPermissionStatus = PermissionStatus;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
if (XamPermission.ShouldShowRationale<XamPermission.LocationWhenInUse>())
|
|
|
|
|
{
|
|
|
|
|
// Prompt the user with additional information as to why the permission is needed
|
|
|
|
|
return Status.Denied;
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
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;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
default:
|
|
|
|
|
// Comprises:
|
|
|
|
|
// - XamPermissionStatus.Restricted:
|
|
|
|
|
// - XamPermissionStatus.Disabled
|
|
|
|
|
// Perission XamPermissionStatus.Granted is handled above.
|
|
|
|
|
return Status.DeniedRequiresSettingsUI;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <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;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
case XamPermissionStatus.Denied:
|
|
|
|
|
return Status.Denied;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
case XamPermissionStatus.Granted:
|
|
|
|
|
return Status.Granted;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
default:
|
|
|
|
|
// Comprises:
|
|
|
|
|
// - XamPermissionStatus.Restricted:
|
|
|
|
|
// - XamPermissionStatus.Disabled
|
|
|
|
|
return Status.DeniedRequiresSettingsUI;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
|
/// <summary> Opens app settings dialog.</summary>
|
|
|
|
|
public bool OpenAppSettings()
|
|
|
|
|
{
|
|
|
|
|
AppInfo.ShowSettingsUI();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-07 19:42:59 +01:00
|
|
|
|
}
|