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

Add NotGranted to PermissionState for Android #105

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
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 @@ -91,10 +91,10 @@ class PermissionsControllerImpl(
val resolverFragment: ResolverFragment = getOrCreateResolverFragment(fragmentManager)

val isAllRequestRationale: Boolean = permissions.all {
!resolverFragment.shouldShowRequestPermissionRationale(it)
resolverFragment.shouldShowRequestPermissionRationale(it)
}
return if (isAllRequestRationale) PermissionState.NotDetermined
else PermissionState.Denied
return if (isAllRequestRationale) PermissionState.Denied
else PermissionState.NotGranted
}

override fun openAppSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@
package dev.icerock.moko.permissions

enum class PermissionState {

/**
* Starting state for each permission.
*/
NotDetermined,

/**
* Android-only. This could mean [NotDetermined] or [DeniedAlways], but the OS doesn't
* expose which of the two it is in all scenarios.
*/
NotGranted,

Granted,

/**
* Android-only.
*/
Denied,

/**
* On Android only applicable to Push Notifications.
*/
DeniedAlways
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ expect interface PermissionsController {

/**
* Returns current state of permission. Can be suspended because on
* android detection of Denied/NotDetermined case require binded FragmentManager.
* Android detection of `Denied`/`NotDetermined` requires a bound FragmentManager.
*
* @param permission state of what permission we want
*
* @return current state. On Android can't be DeniedAlways (except push notifications).
* On iOS can't be Denied.
* @return current state. On Android can't be `DeniedAlways` (except push notifications).
* On iOS can't be `Denied`.
* @see PermissionState for a detailed description.
*/
suspend fun getPermissionState(permission: Permission): PermissionState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
Expand All @@ -17,6 +19,9 @@ import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
Expand All @@ -29,6 +34,7 @@ import dev.icerock.moko.mvvm.getViewModel
import dev.icerock.moko.permissions.DeniedAlwaysException
import dev.icerock.moko.permissions.DeniedException
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionState
import dev.icerock.moko.permissions.PermissionsController
import dev.icerock.moko.permissions.compose.BindEffect
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -100,16 +106,20 @@ fun TestScreen(viewModel: SampleViewModel) {
LaunchedEffect(true) {
viewModel.eventsDispatcher.bind(lifecycleOwner, eventsListener)
}
val permissionState by viewModel.permissionState.collectAsState()

BindEffect(viewModel.permissionsController)

Scaffold(scaffoldState = scaffoldState) {
Box(
Column(
modifier = Modifier
.fillMaxSize()
.padding(it),
contentAlignment = Alignment.Center
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Permission state: $permissionState")

Button(
onClick = viewModel::onRequestPermissionButtonPressed,
content = { Text("Request permission") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import dev.icerock.moko.mvvm.viewmodel.ViewModel
import dev.icerock.moko.permissions.DeniedAlwaysException
import dev.icerock.moko.permissions.DeniedException
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionState
import dev.icerock.moko.permissions.PermissionsController
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class SampleViewModel(
Expand All @@ -15,10 +18,12 @@ class SampleViewModel(
private val permissionType: Permission
) : ViewModel(), EventsDispatcherOwner<SampleViewModel.EventListener> {

val permissionState = MutableStateFlow(PermissionState.NotDetermined)

init {
viewModelScope.launch {
val startState = permissionsController.getPermissionState(permissionType)
println(startState)
permissionState.update { permissionsController.getPermissionState(permissionType) }
println(permissionState)
}
}

Expand All @@ -45,8 +50,10 @@ class SampleViewModel(
} catch (deniedException: DeniedException) {
eventsDispatcher.dispatchEvent { onDenied(deniedException) }
} finally {
permissionsController.getPermissionState(permission)
.also { println("post provide $it") }
permissionState.update {
permissionsController.getPermissionState(permission)
.also { println("post provide $it") }
}
}
}
}
Expand Down
Loading