Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Extension for running blocks with permissions in an Activity #158

Closed
davethomas11 opened this issue Feb 5, 2018 · 5 comments
Closed

Extension for running blocks with permissions in an Activity #158

davethomas11 opened this issue Feb 5, 2018 · 5 comments

Comments

@davethomas11
Copy link
Contributor

davethomas11 commented Feb 5, 2018

I'd love to be able to do the following in an Activities code:

withPermissions(permission.CAMERA) { hasPermissions ->
    if (hasPermissions) {
        openCameraActivity();
    } else {
        warnUserNoPermssions();
    }
}

Where the extension function signature is something like:
inline fun Activity.withPermissions(vararg permissions: String, crossinline block:Activity.(hasPermissions: Boolean) -> Unit)

Also, the withPermissions would ask the user for permission before executing the block if some or all of the permissions were missing.


The flow of the function in order to do this may be too hacky. Let me know. I am envisioning the following steps to pull this off.

Step 1 - ContextCompat.checkSelfPermission for each requested permission
Step 2 - If all permissions allowed call block
Step 3 - Else register lifecycle callback for activity resumed on application context
Step 4 - Request needed permissions
Step 5 - In activity on resumed callback recheck permissions if permissions granted call block()

The activity lifecycle callback is the hacky part because the permission request will trigger transition to another activity to make the request.

Also is it possible to know if the user has clicked do not show permission request in which case always call block(false) when a request is required?

I know this is a bit of hacky request due to the nature of how permission requests are returned in onActivityResult. I'd love to see some sort of callback registering feature added to onActivityResult one day.

What other ways could this be implemented?
What other partial solutions around extensions with permissions could we settle for?

@kevinmost
Copy link

If it's going to be vararg, wouldn't it be useful to know which permissions are granted or not? If there's going to be an extension to handle this, as well, would it be useful to know if a rationale should be shown (ie, if the user declined the permission just once or permanently)?

@davethomas11
Copy link
Contributor Author

davethomas11 commented Feb 5, 2018

Yes perhaps being returned the boolean plus information would be helpful. I'd ignore the permissions which are being granted in most cases, and just care that they were all present for the action I want to execute.

I'm ok with handling rationale outside of the extension beforehand if needed.

@JakeWharton
Copy link
Contributor

I have a hard time seeing how this could work. It seems like a much better target for coroutines where the suspending nature of a function could be made apparent. The suggested approach does not convey the asynchronous nature of the block nor provides a mechanism for stopping the operation.

@scottroemeschke
Copy link

scottroemeschke commented Feb 6, 2018

Things like permissions, and other complex APIs (Fingerprint is another good example) may not be right for this library.

You could redesign permissions API essentially in Kotlin in 10 different ways, (for instance a sealed class PermissionsResult type or a bunch of callback-lamdas you pass in) all of them Kotlin-y and simpler than existing API.

And it might even be a "better" API for the average dev.

But it would be rather opinionated, and heavy. And lots of methods/classes etc wrapping the functionality.

Not sure if those sorts of things are the goal for android-ktx or should be separate libraries. I would think the goal would be relatively non-opinionated straightforward extensions/functions (not API redesigns)

@davethomas11
Copy link
Contributor Author

@scottmeschke I agree with the sentiment, not a good fit for this library.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants