Moved permission stuff from Utils to PermissionsUtils.kt and kept only the essential
This commit is contained in:
parent
e6015485f9
commit
43e1489dd4
@ -0,0 +1,48 @@
|
||||
package ch.mathieubroillet.jarvis.android.utils
|
||||
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import ch.mathieubroillet.jarvis.android.R
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.google.accompanist.permissions.PermissionRequired
|
||||
import com.google.accompanist.permissions.rememberPermissionState
|
||||
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
@Composable
|
||||
fun requestPermission(permission: String) {
|
||||
val cameraPermissionState = rememberPermissionState(permission = permission)
|
||||
val context = LocalContext.current
|
||||
|
||||
PermissionRequired(
|
||||
permissionState = cameraPermissionState,
|
||||
permissionNotGrantedContent = {
|
||||
Button(onClick = {
|
||||
cameraPermissionState.launchPermissionRequest()
|
||||
}) {
|
||||
Text(text = stringResource(id = R.string.permissions_page_grant_permission))
|
||||
}
|
||||
|
||||
},
|
||||
permissionNotAvailableContent = {
|
||||
Button(
|
||||
onClick = { openAppSettings(context) },
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.error)
|
||||
) {
|
||||
Text(text = stringResource(id = R.string.permissions_page_permission_denied))
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_outline_settings_24),
|
||||
contentDescription = "app settings icon"
|
||||
)
|
||||
}
|
||||
},
|
||||
content = {
|
||||
Button(onClick = {}, enabled = false) {
|
||||
Text(text = stringResource(id = R.string.permission_granted))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
@ -1,19 +1,13 @@
|
||||
package ch.mathieubroillet.jarvis.android.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.google.accompanist.permissions.PermissionRequired
|
||||
import com.google.accompanist.permissions.rememberPermissionState
|
||||
|
||||
|
||||
@Composable
|
||||
@ -32,75 +26,12 @@ fun DefaultBox(
|
||||
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
@Composable
|
||||
fun FeatureThatRequiresMicrophonePermission() {
|
||||
var doNotShowRationale by rememberSaveable {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
val cameraPermissionState =
|
||||
rememberPermissionState(permission = android.Manifest.permission.RECORD_AUDIO)
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
PermissionRequired(
|
||||
permissionState = cameraPermissionState,
|
||||
permissionNotGrantedContent = {
|
||||
if (doNotShowRationale) {
|
||||
Text("Feature not available")
|
||||
} else {
|
||||
PermissionNotGrantedUI(
|
||||
onYesClick = {
|
||||
cameraPermissionState.launchPermissionRequest()
|
||||
}, onCancelClick = {
|
||||
doNotShowRationale = true
|
||||
})
|
||||
}
|
||||
},
|
||||
permissionNotAvailableContent = {
|
||||
PermissionNotAvailableContent(
|
||||
onOpenSettingsClick = { /*context.openSettings()*/ })
|
||||
},
|
||||
content = {
|
||||
Text("Camera Permission Granted")
|
||||
}
|
||||
fun openAppSettings(context: Context) {
|
||||
context.startActivity(
|
||||
Intent(
|
||||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||
Uri
|
||||
.fromParts("package", context.applicationInfo.packageName, null)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun PermissionNotAvailableContent(onOpenSettingsClick: () -> Unit) {
|
||||
|
||||
Column {
|
||||
Text("Camera permission denied.")
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Button(onClick = { onOpenSettingsClick() }) {
|
||||
Text("Open settings")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun PermissionNotGrantedUI(onYesClick: () -> Unit, onCancelClick: () -> Unit) {
|
||||
Column {
|
||||
Text("Camera is important for this app. Please grant the permission.")
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Row {
|
||||
Button(onClick = {
|
||||
onYesClick()
|
||||
}) {
|
||||
Text("Yes")
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Button(onClick = {
|
||||
onCancelClick()
|
||||
}) {
|
||||
Text("Cancel")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user