Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/readability improvements #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions PermissionsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,17 +32,21 @@ object PermissionsHelper {
}
}

fun checkPermissions(context: Activity, permissions: Set<Permissions>, action: PermissionActions) {
fun checkPermissions(
context: Activity,
permissions: Set<Permissions>,
action: PermissionActions
) {
var hasGranted = false
val permissionsList = ArrayList<String>()

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
}
Expand All @@ -49,17 +58,22 @@ object PermissionsHelper {
}
}

fun checkPermissions(fragment: Fragment, context: Context, permissions: Set<Permissions>, action: PermissionActions) {
fun checkPermissions(
fragment: Fragment,
context: Context,
permissions: Set<Permissions>,
action: PermissionActions
) {
var hasGranted = false
val permissionsList = ArrayList<String>()

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
}
Expand All @@ -73,7 +87,7 @@ object PermissionsHelper {

fun grandPermissions(fragment: Fragment, permissions: Set<Permissions>) {
val permissionsList = ArrayList<String>()
for (permission in permissions) {
permissions.forEach { permission ->
permissionsList.add(permission.permission)
}
val permissionsArray = permissionsList.toTypedArray()
Expand All @@ -86,7 +100,7 @@ object PermissionsHelper {

fun grandPermissions(context: Activity, permissions: Set<Permissions>) {
val permissionsList = ArrayList<String>()
for (permission in permissions) {
permissions.forEach { permission ->
permissionsList.add(permission.permission)
}
val permissionsArray = permissionsList.toTypedArray()
Expand All @@ -100,10 +114,10 @@ object PermissionsHelper {
fun showPermissionReasonDialog(context: Activity, permissions: Set<Permissions>): 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
Expand All @@ -116,14 +130,18 @@ object PermissionsHelper {
}

fun hasGranted(grantResults: IntArray): Boolean {
for (result in grantResults) {
grantResults.foreach { result ->
if (!hasGranted(result)) {
return false
}
}
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)
Expand Down