From fc961718e3eb00a8c9697415c9dc629d4e2c84de Mon Sep 17 00:00:00 2001 From: Rudem Date: Mon, 21 Oct 2019 21:40:31 +0300 Subject: [PATCH 1/2] Add IntArray extension --- PermissionsHelper.kt | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/PermissionsHelper.kt b/PermissionsHelper.kt index abe9367..734736c 100644 --- a/PermissionsHelper.kt +++ b/PermissionsHelper.kt @@ -18,7 +18,12 @@ object PermissionsHelper { } } - fun checkPermission(fragment: Fragment, context: Context, permission: Permissions, action: PermissionAction) { + fun checkPermission( + fragment: Fragment, + context: Context, + permission: Permissions, + action: PermissionAction + ) { val hasGranted = hasGranted(context.checkSelfPermission(permission.permission)) if (hasGranted) { action.onPermissionGranted(permission) @@ -27,7 +32,11 @@ object PermissionsHelper { } } - fun checkPermissions(context: Activity, permissions: Set, action: PermissionActions) { + fun checkPermissions( + context: Activity, + permissions: Set, + action: PermissionActions + ) { var hasGranted = false val permissionsList = ArrayList() @@ -49,7 +58,12 @@ object PermissionsHelper { } } - fun checkPermissions(fragment: Fragment, context: Context, permissions: Set, action: PermissionActions) { + fun checkPermissions( + fragment: Fragment, + context: Context, + permissions: Set, + action: PermissionActions + ) { var hasGranted = false val permissionsList = ArrayList() @@ -116,7 +130,7 @@ object PermissionsHelper { } fun hasGranted(grantResults: IntArray): Boolean { - for (result in grantResults) { + grantResults.foreach { result -> if (!hasGranted(result)) { return false } @@ -124,6 +138,10 @@ object PermissionsHelper { return true } + private fun IntArray.foreach(block: (item: Int) -> Unit) { + for (item in this) block.invoke(item) + } + interface PermissionAction { fun onPermissionGranted(permission: Permissions) fun onPermissionDenied(permission: Permissions) From 4891162dc168b9b0d4bcfced60bef2f51293a4c9 Mon Sep 17 00:00:00 2001 From: Rudem Date: Mon, 21 Oct 2019 22:29:24 +0300 Subject: [PATCH 2/2] Change for loop with foreach --- PermissionsHelper.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/PermissionsHelper.kt b/PermissionsHelper.kt index 734736c..3ad3fd7 100644 --- a/PermissionsHelper.kt +++ b/PermissionsHelper.kt @@ -42,11 +42,11 @@ object PermissionsHelper { if (permissions.isEmpty()) return - for (permission in permissions) { + permissions.forEach { permission -> permissionsList.add(permission.permission) } - for (permission in permissions) { + permissions.forEach { permission -> hasGranted = hasGranted(context.checkSelfPermission(permission.permission)) if (!hasGranted) break } @@ -69,11 +69,11 @@ object PermissionsHelper { if (permissions.isEmpty()) return - for (permission in permissions) { + permissions.forEach { permission -> permissionsList.add(permission.permission) } - for (permission in permissions) { + permissions.forEach { permission -> hasGranted = hasGranted(context.checkSelfPermission(permission.permission)) if (!hasGranted) break } @@ -87,7 +87,7 @@ object PermissionsHelper { fun grandPermissions(fragment: Fragment, permissions: Set) { val permissionsList = ArrayList() - for (permission in permissions) { + permissions.forEach { permission -> permissionsList.add(permission.permission) } val permissionsArray = permissionsList.toTypedArray() @@ -100,7 +100,7 @@ object PermissionsHelper { fun grandPermissions(context: Activity, permissions: Set) { val permissionsList = ArrayList() - for (permission in permissions) { + permissions.forEach { permission -> permissionsList.add(permission.permission) } val permissionsArray = permissionsList.toTypedArray() @@ -114,10 +114,10 @@ object PermissionsHelper { fun showPermissionReasonDialog(context: Activity, permissions: Set): Boolean { var hasGranted = false var permissionNotGranted: Permissions = Permissions.PERMISSION_MISSING - for (permission in permissions) { + permissions.forEach { permission -> permissionNotGranted = permission hasGranted = hasGranted(context.checkSelfPermission(permission.permission)) - if (!hasGranted) break + if (!hasGranted) return@forEach } if (hasGranted) return true