Skip to content

Use generic return type for runWithPermissions functions. #19

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

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ private const val TAG = "runWithPermissions"
* Injects code to ask for permissions before executing any code that requires permissions
* defined in the annotation
*/
fun Context?.runWithPermissions(
fun <T> Context?.runWithPermissions(
vararg permissions: String,
options: QuickPermissionsOptions = QuickPermissionsOptions(),
callback: () -> Unit
): Any? {
callback: () -> T
): T {
return runWithPermissionsHandler(this, permissions, callback, options)
}

/**
* Injects code to ask for permissions before executing any code that requires permissions
* defined in the annotation
*/
fun Fragment?.runWithPermissions(
fun <T> Fragment?.runWithPermissions(
vararg permissions: String,
options: QuickPermissionsOptions = QuickPermissionsOptions(),
callback: () -> Unit
): Any? {
callback: () -> T
): T {
return runWithPermissionsHandler(this, permissions, callback, options)
}

private fun runWithPermissionsHandler(target: Any?, permissions: Array<out String>, callback: () -> Unit, options: QuickPermissionsOptions): Nothing? {
private fun <T> runWithPermissionsHandler(target: Any?, permissions: Array<out String>, callback: () -> T, options: QuickPermissionsOptions): T {
Log.d(TAG, "runWithPermissions: start")

// get the permissions defined in annotation
Expand All @@ -54,7 +54,7 @@ private fun runWithPermissionsHandler(target: Any?, permissions: Array<out Strin
// check if we have the permissions
if (PermissionsUtil.hasSelfPermission(context, arrayOf(*permissions))) {
Log.d(TAG, "runWithPermissions: already has required permissions. Proceed with the execution.")
callback()
return callback()
} else {
// we don't have required permissions
// begin the permission request flow
Expand Down Expand Up @@ -100,11 +100,12 @@ private fun runWithPermissionsHandler(target: Any?, permissions: Array<out Strin
}

// set callback to permission checker fragment
var result: T? = null
permissionCheckerFragment.setListener(object : PermissionCheckerFragment.QuickPermissionsCallback {
override fun onPermissionsGranted(quickPermissionsRequest: QuickPermissionsRequest?) {
Log.d(TAG, "runWithPermissions: got permissions")
try {
callback()
result = callback()
} catch (throwable: Throwable) {
throwable.printStackTrace()
}
Expand Down Expand Up @@ -144,12 +145,12 @@ private fun runWithPermissionsHandler(target: Any?, permissions: Array<out Strin

// start requesting permissions for the first time
permissionCheckerFragment.requestPermissionsFromUser()
return result as T
}
} else {
// context is null
// cannot handle the permission checking from the any class other than AppCompatActivity/Fragment
// crash the app RIGHT NOW!
throw IllegalStateException("Found " + target!!::class.java.canonicalName + " : No support from any classes other than AppCompatActivity/Fragment")
}
return null
}