-
Notifications
You must be signed in to change notification settings - Fork 565
Extension for running blocks with permissions in an Activity #158
Comments
If it's going to be |
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. |
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. |
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) |
@scottmeschke I agree with the sentiment, not a good fit for this library. |
I'd love to be able to do the following in an Activities code:
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?
The text was updated successfully, but these errors were encountered: