Skip to content

Commit

Permalink
Refactor permissions library (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcoder authored Oct 25, 2023
1 parent 2505c34 commit 27f1b7a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
76 changes: 38 additions & 38 deletions Softeq.XToolkit.Permissions.Droid/PermissionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// http://www.softeq.com

using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Maui.Storage;
using BasePermission = Microsoft.Maui.ApplicationModel.Permissions.BasePermission;
using XToolkitPermissions = Softeq.XToolkit.Permissions.Permissions;
using XToolkitPermissionsDroid = Softeq.XToolkit.Permissions.Droid.Permissions;
using CustomPermissions = Softeq.XToolkit.Permissions.Permissions;
using PlatformCustomPermissions = Softeq.XToolkit.Permissions.Droid.Permissions;

namespace Softeq.XToolkit.Permissions.Droid
{
Expand All @@ -31,15 +31,15 @@ public virtual Task<PermissionStatus> CheckAsync<T>()
where T : BasePermission, new()
{
var permissionType = typeof(T);
if (permissionType == typeof(XToolkitPermissions.Notifications))
if (permissionType == typeof(CustomPermissions.Notifications))
{
return _permissionsService.CheckPermissionsAsync<XToolkitPermissionsDroid.Notifications>();
return _permissionsService.CheckPermissionsAsync<PlatformCustomPermissions.Notifications>();
}
else if (permissionType == typeof(XToolkitPermissions.Bluetooth))
else if (permissionType == typeof(CustomPermissions.Bluetooth))
{
return _permissionsService.CheckPermissionsAsync<XToolkitPermissionsDroid.Bluetooth>();
return _permissionsService.CheckPermissionsAsync<PlatformCustomPermissions.Bluetooth>();
}
else
else
{
return _permissionsService.CheckPermissionsAsync<T>();
}
Expand All @@ -50,15 +50,15 @@ public Task<PermissionStatus> CheckWithRequestAsync<T>()
where T : BasePermission, new()
{
var permissionType = typeof(T);
if (permissionType == typeof(XToolkitPermissions.Notifications))
if (permissionType == typeof(CustomPermissions.Notifications))
{
return CommonCheckWithRequestAsync<XToolkitPermissionsDroid.Notifications>();
return CommonCheckWithRequestAsync<PlatformCustomPermissions.Notifications>();
}
else if (permissionType == typeof(XToolkitPermissions.Bluetooth))
else if (permissionType == typeof(CustomPermissions.Bluetooth))
{
return CommonCheckWithRequestAsync<XToolkitPermissionsDroid.Bluetooth>();
return CommonCheckWithRequestAsync<PlatformCustomPermissions.Bluetooth>();
}
else
else
{
return CommonCheckWithRequestAsync<T>();
}
Expand All @@ -76,31 +76,31 @@ private void OpenSettings()
_permissionsService.OpenSettings();
}

private void RemoveOldKeys<T>()
where T : BasePermission, new()
{
var requestedKey = GetPermissionRequestedKey<T>();
private void RemoveOldKeys<T>()
where T : BasePermission, new()
{
var requestedKey = GetPermissionRequestedKey<T>();
if (Preferences.ContainsKey(requestedKey))
{
Preferences.Remove(requestedKey);
}

var deniedEverKey = GetPermissionDeniedEverKey<T>();
{
Preferences.Remove(requestedKey);
}

var deniedEverKey = GetPermissionDeniedEverKey<T>();
if (Preferences.ContainsKey(deniedEverKey))
{
Preferences.Remove(deniedEverKey);
}

string GetPermissionRequestedKey<TPermission>()
where TPermission : BasePermission
{
return $"{nameof(PermissionsManager)}_IsPermissionRequested_{typeof(T).Name}";
}

{
Preferences.Remove(deniedEverKey);
}

string GetPermissionRequestedKey<TPermission>()
where TPermission : BasePermission
{
return $"{nameof(PermissionsManager)}_IsPermissionRequested_{typeof(T).Name}";
}

string GetPermissionDeniedEverKey<T>()
where T : BasePermission
{
return $"{nameof(PermissionsManager)}_IsPermissionDeniedEver_{typeof(T).Name}";
where T : BasePermission
{
return $"{nameof(PermissionsManager)}_IsPermissionDeniedEver_{typeof(T).Name}";
}
}

Expand All @@ -115,7 +115,7 @@ private async Task<PermissionStatus> CommonCheckWithRequestAsync<T>()

RemoveOldKeys<T>();

// Timer are used for confirm a fact of showing a request of permission access popup
// Timer are used for confirm a fact of showing a request of permission access popup
// in another case user should see screen with settings for changing permission state
var timer = new Stopwatch();
timer.Start();
Expand All @@ -126,7 +126,7 @@ private async Task<PermissionStatus> CommonCheckWithRequestAsync<T>()
permissionStatus = await _permissionsService.RequestPermissionsAsync<T>().ConfigureAwait(false);
}

if (permissionStatus == PermissionStatus.Denied
if (permissionStatus == PermissionStatus.Denied
&& timer.Elapsed < _showPermissionDialogThreshold)
{
await OpenSettingsWithConfirmationAsync<T>().ConfigureAwait(false);
Expand All @@ -147,4 +147,4 @@ private async Task OpenSettingsWithConfirmationAsync<T>()
}
}
}
}
}
16 changes: 8 additions & 8 deletions Softeq.XToolkit.Permissions.iOS/Permissions/Bluetooth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using CoreBluetooth;
using BasePlatformPermission = Microsoft.Maui.ApplicationModel.Permissions.BasePlatformPermission;
Expand All @@ -15,21 +12,24 @@ namespace Softeq.XToolkit.Permissions.iOS.Permissions
{
public class Bluetooth : BasePlatformPermission
{
/// <inheritdoc />
protected override Func<IEnumerable<string>> RequiredInfoPlistKeys =>
() => new string[] { "NSBluetoothAlwaysUsageDescription" };

/// <inheritdoc />
public override Task<EssentialsPermissionStatus> CheckStatusAsync()
{
EnsureDeclared();

return Task.FromResult(ParseAuthorization(CBManager.Authorization));
}

/// <inheritdoc />
public override async Task<EssentialsPermissionStatus> RequestAsync()
{
EnsureDeclared();

var status = await CheckStatusAsync();
var status = await CheckStatusAsync().ConfigureAwait(false);

if (status == EssentialsPermissionStatus.Granted)
{
Expand All @@ -39,7 +39,7 @@ public override async Task<EssentialsPermissionStatus> RequestAsync()
if (CBManager.Authorization == CBManagerAuthorization.NotDetermined)
{
var centralManagerDelegate = new CentralManagerDelegate();
await centralManagerDelegate.RequestAccessAsync();
await centralManagerDelegate.RequestAccessAsync().ConfigureAwait(false);
}

return ParseAuthorization(CBManager.Authorization);
Expand All @@ -61,7 +61,7 @@ private class CentralManagerDelegate : CBCentralManagerDelegate
{
private readonly CBCentralManager _centralManager;

private TaskCompletionSource<CBManagerState> _statusRequest =
private readonly TaskCompletionSource<CBManagerState> _statusRequest =
new TaskCompletionSource<CBManagerState>();

public CentralManagerDelegate()
Expand All @@ -71,7 +71,7 @@ public CentralManagerDelegate()

public async Task<CBManagerState> RequestAccessAsync()
{
var state = await _statusRequest.Task;
var state = await _statusRequest.Task.ConfigureAwait(false);
return state;
}

Expand All @@ -84,4 +84,4 @@ public override void UpdatedState(CBCentralManager centralManager)
}
}
}
}
}
10 changes: 6 additions & 4 deletions Softeq.XToolkit.Permissions.iOS/Permissions/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// http://www.softeq.com

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CoreBluetooth;
using UserNotifications;
using BasePlatformPermission = Microsoft.Maui.ApplicationModel.Permissions.BasePlatformPermission;
using EssentialsPermissionStatus = Microsoft.Maui.ApplicationModel.PermissionStatus;
Expand All @@ -16,6 +14,7 @@ public class Notifications : BasePlatformPermission
public static UNAuthorizationOptions AuthorizationOptions =
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound;

/// <inheritdoc />
public override async Task<EssentialsPermissionStatus> CheckStatusAsync()
{
var notificationSettings = await UNUserNotificationCenter.Current
Expand All @@ -24,10 +23,13 @@ public override async Task<EssentialsPermissionStatus> CheckStatusAsync()
return ParseAuthorization(notificationSettings.AuthorizationStatus);
}

/// <inheritdoc />
public override async Task<EssentialsPermissionStatus> RequestAsync()
{
var notificationCenter = UNUserNotificationCenter.Current;
var (isGranted, _) = await notificationCenter.RequestAuthorizationAsync(AuthorizationOptions);
var (isGranted, _) = await notificationCenter
.RequestAuthorizationAsync(AuthorizationOptions)
.ConfigureAwait(false);
return isGranted
? EssentialsPermissionStatus.Granted
: EssentialsPermissionStatus.Denied;
Expand All @@ -45,4 +47,4 @@ private static EssentialsPermissionStatus ParseAuthorization(UNAuthorizationStat
};
}
}
}
}
26 changes: 13 additions & 13 deletions Softeq.XToolkit.Permissions.iOS/PermissionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Threading.Tasks;
using Microsoft.Maui.Storage;
using BasePermission = Microsoft.Maui.ApplicationModel.Permissions.BasePermission;
using XToolkitPermissions = Softeq.XToolkit.Permissions.Permissions;
using XToolkitPermissionsIos = Softeq.XToolkit.Permissions.iOS.Permissions;
using CustomPermissions = Softeq.XToolkit.Permissions.Permissions;
using PlatformCustomPermissions = Softeq.XToolkit.Permissions.iOS.Permissions;

namespace Softeq.XToolkit.Permissions.iOS
{
Expand Down Expand Up @@ -39,15 +39,15 @@ public virtual Task<PermissionStatus> CheckWithRequestAsync<T>()
{
var permissionType = typeof(T);

if (permissionType == typeof(XToolkitPermissions.Notifications))
if (permissionType == typeof(CustomPermissions.Notifications))
{
return CommonCheckWithRequestAsync<XToolkitPermissionsIos.Notifications>();
return CommonCheckWithRequestAsync<PlatformCustomPermissions.Notifications>();
}
else if (permissionType == typeof(XToolkitPermissions.Bluetooth))
else if (permissionType == typeof(CustomPermissions.Bluetooth))
{
return CommonCheckWithRequestAsync<XToolkitPermissionsIos.Bluetooth>();
return CommonCheckWithRequestAsync<PlatformCustomPermissions.Bluetooth>();
}
else
else
{
return CommonCheckWithRequestAsync<T>();
}
Expand All @@ -59,15 +59,15 @@ public Task<PermissionStatus> CheckAsync<T>()
{
var permissionType = typeof(T);

if (permissionType == typeof(XToolkitPermissions.Notifications))
if (permissionType == typeof(CustomPermissions.Notifications))
{
return _permissionsService.CheckPermissionsAsync<XToolkitPermissionsIos.Notifications>();
return _permissionsService.CheckPermissionsAsync<PlatformCustomPermissions.Notifications>();
}
else if (permissionType == typeof(XToolkitPermissions.Bluetooth))
else if (permissionType == typeof(CustomPermissions.Bluetooth))
{
return _permissionsService.CheckPermissionsAsync<XToolkitPermissionsIos.Bluetooth>();
return _permissionsService.CheckPermissionsAsync<PlatformCustomPermissions.Bluetooth>();
}
else
else
{
return _permissionsService.CheckPermissionsAsync<T>();
}
Expand Down Expand Up @@ -124,4 +124,4 @@ private async Task<PermissionStatus> OpenSettingsWithConfirmationAsync<T>()
return PermissionStatus.Unknown;
}
}
}
}

0 comments on commit 27f1b7a

Please sign in to comment.