kopia lustrzana https://github.com/vitorpamplona/amethyst
96 wiersze
3.2 KiB
Kotlin
96 wiersze
3.2 KiB
Kotlin
package com.vitorpamplona.amethyst.ui.navigation
|
|
|
|
import android.net.Uri
|
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.material.Button
|
|
import androidx.compose.material.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.SideEffect
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.unit.dp
|
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
|
import com.google.accompanist.permissions.isGranted
|
|
import com.google.accompanist.permissions.rememberPermissionState
|
|
import com.google.accompanist.permissions.shouldShowRationale
|
|
|
|
@OptIn(ExperimentalPermissionsApi::class)
|
|
@Composable
|
|
fun UploadFromGallery(onImageChosen: (Uri) -> Unit) {
|
|
val cameraPermissionState = rememberPermissionState(
|
|
android.Manifest.permission.READ_MEDIA_IMAGES
|
|
)
|
|
|
|
if (cameraPermissionState.status.isGranted) {
|
|
var showGallerySelect by remember { mutableStateOf(false) }
|
|
if (showGallerySelect) {
|
|
GallerySelect(
|
|
onImageUri = { uri ->
|
|
showGallerySelect = false
|
|
if (uri != null)
|
|
onImageChosen(uri)
|
|
}
|
|
)
|
|
} else {
|
|
Box() {
|
|
Button(
|
|
modifier = Modifier
|
|
.align(Alignment.TopCenter)
|
|
.padding(4.dp),
|
|
onClick = {
|
|
showGallerySelect = true
|
|
}
|
|
) {
|
|
Text("Add Image from Gallery")
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
Column {
|
|
val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
|
|
// If the user has denied the permission but the rationale can be shown,
|
|
// then gently explain why the app requires this permission
|
|
"Grant access to quickly upload pictures before posting"
|
|
} else {
|
|
// If it's the first time the user lands on this feature, or the user
|
|
// doesn't want to be asked again for this permission, explain that the
|
|
// permission is required
|
|
"Grant access to quickly upload pictures before posting"
|
|
}
|
|
Text(textToShow)
|
|
Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
|
|
Text("Request permission")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Composable
|
|
fun GallerySelect(
|
|
onImageUri: (Uri?) -> Unit = { }
|
|
) {
|
|
val launcher = rememberLauncherForActivityResult(
|
|
contract = ActivityResultContracts.GetContent(),
|
|
onResult = { uri: Uri? ->
|
|
onImageUri(uri)
|
|
}
|
|
)
|
|
|
|
@Composable
|
|
fun LaunchGallery() {
|
|
SideEffect {
|
|
launcher.launch("image/*")
|
|
}
|
|
}
|
|
|
|
LaunchGallery()
|
|
} |