using System.Threading.Tasks;
using Xamarin.Essentials;
namespace TINK.Services.Permissions.Essentials
{
using XamPermission = Xamarin.Essentials.Permissions;
using XamPermissionStatus = PermissionStatus;
public class LocationPermissions : ILocationPermission
{
/// Checks the permission status.
/// Current permission status.
public async Task CheckStatusAsync()
{
var status = await XamPermission.CheckStatusAsync();
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())
{
// 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;
}
}
/// Requests location permission.
/// Permission status after request.
public async Task RequestAsync()
{
switch (await XamPermission.RequestAsync())
{
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;
}
}
/// Opens app settings dialog.
public bool OpenAppSettings()
{
AppInfo.ShowSettingsUI();
return true;
}
}
}