Skip to content

Commit b658672

Browse files
authored
[Jetsurvey] Improve permissions UX (#577)
1 parent 35660c6 commit b658672

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyQuestions.kt

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import androidx.compose.runtime.LaunchedEffect
5959
import androidx.compose.runtime.getValue
6060
import androidx.compose.runtime.mutableStateOf
6161
import androidx.compose.runtime.remember
62-
import androidx.compose.runtime.saveable.rememberSaveable
6362
import androidx.compose.runtime.setValue
6463
import androidx.compose.ui.Alignment
6564
import androidx.compose.ui.Modifier
@@ -96,9 +95,6 @@ fun Question(
9695
} else {
9796
val permissionsContentModifier = modifier.padding(horizontal = 20.dp)
9897

99-
// When true, the permissions request must be presented to the user.
100-
var launchPermissionsRequest by rememberSaveable { mutableStateOf(false) }
101-
10298
val multiplePermissionsState =
10399
rememberMultiplePermissionsState(question.permissionsRequired)
104100

@@ -107,28 +103,27 @@ fun Question(
107103
multiplePermissionsState.allPermissionsGranted -> {
108104
QuestionContent(question, answer, onAnswer, onAction, modifier)
109105
}
110-
// The user denied some permissions but a rationale should be shown
111-
multiplePermissionsState.shouldShowRationale -> {
112-
if (!shouldAskPermissions) {
113-
PermissionsDenied(
114-
question.questionText,
115-
openSettings,
116-
permissionsContentModifier
117-
)
118-
} else {
119-
PermissionsRationale(
120-
question,
121-
multiplePermissionsState,
122-
onDoNotAskForPermissions,
123-
permissionsContentModifier
124-
)
106+
// If user denied some permissions but a rationale should be shown or the user
107+
// is going to be presented with the permission for the first time. Let's explain
108+
// why we need the permission
109+
multiplePermissionsState.shouldShowRationale ||
110+
!multiplePermissionsState.permissionRequested ->
111+
{
112+
if (!shouldAskPermissions) {
113+
PermissionsDenied(
114+
question.questionText,
115+
openSettings,
116+
permissionsContentModifier
117+
)
118+
} else {
119+
PermissionsRationale(
120+
question,
121+
multiplePermissionsState,
122+
onDoNotAskForPermissions,
123+
permissionsContentModifier
124+
)
125+
}
125126
}
126-
}
127-
// The permissions are not granted, the rationale shouldn't be shown to the user,
128-
// and the permissions haven't been requested previously. Request permission!
129-
!multiplePermissionsState.permissionRequested -> {
130-
launchPermissionsRequest = true
131-
}
132127
// If the criteria above hasn't been met, the user denied some permission.
133128
else -> {
134129
PermissionsDenied(question.questionText, openSettings, permissionsContentModifier)
@@ -139,14 +134,6 @@ fun Question(
139134
}
140135
}
141136

142-
// Trigger a side-effect to request the permissions if they need to be presented to the user
143-
if (launchPermissionsRequest) {
144-
LaunchedEffect(multiplePermissionsState) {
145-
multiplePermissionsState.launchMultiplePermissionRequest()
146-
launchPermissionsRequest = false
147-
}
148-
}
149-
150137
// If permissions are denied, inform the caller that can move to the next question
151138
if (!shouldAskPermissions) {
152139
LaunchedEffect(true) {

0 commit comments

Comments
 (0)