Created
September 14, 2017 12:34
-
-
Save abdallaadelessa/cebdd1f550976bd4022bb3c9de83d485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Android; | |
using Android.Content.PM; | |
using Android.OS; | |
using Android.Support.V4.Content; | |
using MvvmCross.Core.ViewModels; | |
using NMFoundation.MvvmCross.Core; | |
using NMFoundation.MvvmCross.Droid; | |
namespace Permissions | |
{ | |
public abstract class BaseActivity : NMAppCompatActivity | |
{ | |
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) | |
{ | |
base.OnRequestPermissionsResult(requestCode, permissions, grantResults); | |
Mvx.Resolve<IPermissionsService>().OnRequestPermissionsResult(requestCode,permissions,grantResults); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Android.Content.PM; | |
namespace Permissions | |
{ | |
public interface IPermissionsService | |
{ | |
void RequestPermissions(int requestCode, String[] permissions, Action<PermissionResponse> action); | |
void OnRequestPermissionsResult(int requestCode, String[] permission, Permission[] grantResults); | |
} | |
public class PermissionResponse | |
{ | |
public List<PermissionResult> PermissionResults { get; private set; } | |
public PermissionResponse() | |
{ | |
PermissionResults = new List<PermissionResult>(); | |
} | |
public List<string> DeniedPermissions | |
{ | |
get | |
{ | |
return PermissionResults != null ? PermissionResults.Where((x) => !x.IsGranted).Select((x) => x.PermissionName).ToList() : new List<string>(); | |
} | |
} | |
public List<string> GrantedPermissions | |
{ | |
get | |
{ | |
return PermissionResults != null ? PermissionResults.Where((x) => x.IsGranted).Select((x) => x.PermissionName).ToList() : new List<string>(); | |
} | |
} | |
public bool IsAllGranted | |
{ | |
get | |
{ | |
return PermissionResults != null && PermissionResults.All((p) => p.IsGranted); | |
} | |
} | |
} | |
public class PermissionResult | |
{ | |
public String PermissionName { get; private set; } | |
public bool IsGranted { get; set; } | |
public PermissionResult(string permissionName, bool isGranted) | |
{ | |
PermissionName = permissionName; | |
IsGranted = isGranted; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Android.App; | |
using Android.Content; | |
using Android.Content.PM; | |
using Android.OS; | |
using Android.Support.V4.App; | |
using Android.Support.V4.Content; | |
using MvvmCross.Platform; | |
using MvvmCross.Platform.Droid.Platform; | |
using System.Linq; | |
namespace Permissions | |
{ | |
public class PermissionService : IPermissionsService | |
{ | |
private IDictionary<int, PermissionInfo> _requests; | |
public PermissionService() | |
{ | |
_requests = new Dictionary<int, PermissionInfo>(); | |
} | |
private Activity CurrentActivity | |
{ | |
get | |
{ | |
return Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity; | |
} | |
} | |
public void RequestPermissions(int requestCode, string[] permissions, Action<PermissionResponse> action) | |
{ | |
if (CurrentActivity == null || permissions == null) return; | |
// Wrap string permission to PermissionResponse | |
PermissionResponse response = new PermissionResponse(); | |
foreach (String permission in permissions) | |
{ | |
bool isGranted = ContextCompat.CheckSelfPermission(CurrentActivity, permission) == Permission.Granted; | |
response.PermissionResults.Add(new PermissionResult(permission, isGranted)); | |
} | |
// Should Request Permission | |
if ((int)Build.VERSION.SdkInt >= 23) | |
{ | |
// Check is granted | |
if (response.IsAllGranted) | |
{ | |
// All Granted | |
action?.Invoke(response); | |
} | |
else | |
{ | |
// Should request permission | |
_requests.Add(requestCode, new PermissionInfo(response, action)); | |
List<string> deniedPermissions = new List<string>(response.DeniedPermissions); | |
ActivityCompat.RequestPermissions(CurrentActivity, deniedPermissions.ToArray(), requestCode); | |
} | |
} | |
else | |
{ | |
// All Granted Below Marshmallow | |
action?.Invoke(response); | |
} | |
} | |
public void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) | |
{ | |
if (_requests.ContainsKey(requestCode)) | |
{ | |
PermissionInfo permissionInfo; | |
_requests.TryGetValue(requestCode, out permissionInfo); | |
// Update permissions | |
if (permissionInfo != null) | |
{ | |
for (int i = 0; i < permissions.Count(); i++) | |
{ | |
String permission = permissions[i]; | |
bool isGranted = grantResults[i] == Permission.Granted; | |
PermissionResult permissionResult = permissionInfo.Response.PermissionResults.Where((x) => x.PermissionName.Equals(permission)).FirstOrDefault(); | |
if (permissionResult != null) | |
{ | |
permissionResult.IsGranted = isGranted; | |
} | |
} | |
permissionInfo.ResponseAction?.Invoke(permissionInfo.Response); | |
} | |
_requests.Remove(requestCode); | |
} | |
} | |
public void CancelByRequestCode(int requestCode) | |
{ | |
_requests?.Remove(requestCode); | |
} | |
public void CancelAll() | |
{ | |
_requests?.Clear(); | |
} | |
#region | |
private class PermissionInfo | |
{ | |
public PermissionResponse Response { get; private set; } | |
public Action<PermissionResponse> ResponseAction { get; private set; } | |
public PermissionInfo(PermissionResponse response, Action<PermissionResponse> action) | |
{ | |
Response = response; | |
ResponseAction = action; | |
} | |
} | |
#endregion | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Android.Content; | |
using Android.Views; | |
using Android.Widget; | |
using BSSConsumer.Core; | |
using BSSConsumer.Shared; | |
using MvvmCross.Binding.Bindings.Target.Construction; | |
using MvvmCross.Core.ViewModels; | |
using MvvmCross.Droid.Support.V7.RecyclerView; | |
using MvvmCross.Droid.Views; | |
using MvvmCross.Platform; | |
using MvvmCross.Platform.IoC; | |
using NMFoundation.LazyLoading.Core; | |
using NMFoundation.LazyLoading.Droid.Widgets; | |
using NMFoundation.MvvmCross.Droid; | |
using NMFoundation.Notifications.Droid; | |
using NMFoundation.Notifications.Droid.PushNotifications; | |
namespace Permissions | |
{ | |
public class Setup : NMAndroidSetup | |
{ | |
public Setup(Context applicationContext) | |
: base(applicationContext) | |
{ | |
} | |
protected override void InitializePlatformServices() | |
{ | |
base.InitializePlatformServices(); | |
Mvx.LazyConstructAndRegisterSingleton<IPermissionsService, PermissionService>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment