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

Collecting the StateFlow is sending the initial State #64

Closed
Kranthi94 opened this issue Jul 6, 2023 · 6 comments
Closed

Collecting the StateFlow is sending the initial State #64

Kranthi94 opened this issue Jul 6, 2023 · 6 comments

Comments

@Kranthi94
Copy link

When we call permissionFlow.getMultiplePermissionState(*permissions) and use collect on it it is creating a state flow with an initial value this initial value is getting collected first and then permission dialog is shown to the user based on user action collect is again triggered. In the initial state permission is not granted and if the user also didn't grant the permission then collect block is called with the same permission state as initial state here how to identify these two states inside the collect block the initial state and the user denied state.

@PatilShreyas
Copy link
Owner

@Kranthi94 It's the original nature of the StateFlow that it always emits the current state. If you're using it for some purpose and don't want first update, just ignore the first emission of the StateFlow.

@Kranthi94
Copy link
Author

Kranthi94 commented Jul 6, 2023

@PatilShreyas To ignore i need a way to know it but the initiate state and the state when the permission is denied is the same. There is no difference between those two states, so i am finding it difficult to differentiate for ignoring.

@PatilShreyas
Copy link
Owner

Understood, actually, PermissionFlow is there to solve domain-specific issues and here the problem is occurring because you're using it for UI operations. It's meant for core use case only which only has live knowledge of permission and its state.

For your particular use case, you can do something like

private val permissionLauncher = registerForPermissionFlowRequestsResult { permissionMap ->
    if (permissionMap[Manifest.permission.____] == true) {
        // Permission granted
    } else {
        // Permission NOT granted
    }
}

Since in this case, flow will not give updates since state hasn't changed.

@PatilShreyas
Copy link
Owner

Meanwhile, I'll also think from an API perspective if something can be done for this as well or not.

@Kranthi94
Copy link
Author

Thanks for the quick response

@PatilShreyas
Copy link
Owner

After thinking a lot from the API perspective, this use case doesn't actually fit for the library. Because the solution on this thread actually solves the issue and there's no need for special API.

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

No branches or pull requests

2 participants