permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.22k stars 1.44k forks source link

[ktx] onNeverAskAgain not called #767

Open SammyOdenhoven opened 2 years ago

SammyOdenhoven commented 2 years ago

Overview

onNeverAskAgain callback is not called when "Don't ask again" is selected.

Expected

onNeverAskAgain callback is called, so I can show the appropriate UI.

Actual

onNeverAskAgain callback is not called.

Environment

Investigation

After debugging it, I can see that onNeverAskAgain: WeakReference<Fun>? is null in PermissionRequestViewModel.invoke(). After reading through the closed issues, I came across https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/735. Wondering if the weakreference could be the issue? If I downgrade the ktx library to 1.0.4, onNeverAskAgain is successfully called.

Reproducible steps

https://user-images.githubusercontent.com/35034828/158357946-129980bf-6547-4228-82a4-a03399f88269.mp4

OKatrych commented 2 years ago

Agree, I have the same issue WeakReference is null. It is null for all three arguments:

requiresPermission: WeakReference<Fun>,
onPermissionDenied: WeakReference<Fun>?,
onNeverAskAgain: WeakReference<Fun>?
OKatrych commented 2 years ago

It can be reproduced on Android < 10. For example, in my case, I have used an Android 7.1.1 build on Emulator.

dpatel-mercari commented 2 years ago

I can also reproduce it on Android > 10. The references are null and so going back and forth the callbacks are not invoked.

francescopedronomnys commented 2 years ago

I'm able to reproduce the problem using ktx:1.1.4, I've found a workaround keeping a reference to the onNeverAskAgain lambda from the Fragment that is constructing the permission request using constructPermissionsRequest; see sample code:

class MyFragment : Fragment() {
    private val locationPermissionNeverAskAgain : () -> Unit = { /* never ask again code here */ }
    private val locationPermissionRequest = constructPermissionsRequest(
        /* other settings here */
        onNeverAskAgain = locationPermissionNeverAskAgain
    ) { /* permission granted code here */ }
}

Probably library should declare onNeverAskAgain as a val on PermissionsRequesterImpl.