Skip to content

Commit

Permalink
Moves to the new Android Image/Video Picker
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Nov 25, 2024
1 parent 04e77da commit 7bd9b6a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 60 deletions.
6 changes: 1 addition & 5 deletions amethyst/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
<!-- To take pictures -->
<uses-permission android:name="android.permission.CAMERA" />

<!-- To Upload media (newer devices) -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

<!-- To Upload media (old devices) -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />

<!-- To read QRs that contain nostr:<NIP19> -->
<uses-permission android:name="android.permission.CAMERA" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ package com.vitorpamplona.amethyst.ui.actions

import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
Expand Down Expand Up @@ -55,11 +56,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.content.FileProvider
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.GetMediaActivityResultContract
import com.vitorpamplona.amethyst.ui.stringRes
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
Expand All @@ -69,40 +66,26 @@ import java.util.Date
import java.util.Locale
import java.util.concurrent.atomic.AtomicBoolean

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun UploadFromGallery(
isUploading: Boolean,
tint: Color,
modifier: Modifier,
onImageChosen: (Uri) -> Unit,
) {
val cameraPermissionState =
rememberPermissionState(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
android.Manifest.permission.READ_MEDIA_IMAGES
} else {
android.Manifest.permission.READ_EXTERNAL_STORAGE
var showGallerySelect by remember { mutableStateOf(false) }
if (showGallerySelect) {
GallerySelect(
onImageUri = { uri ->
showGallerySelect = false
if (uri != null) {
onImageChosen(uri)
}
},
)

if (cameraPermissionState.status.isGranted) {
var showGallerySelect by remember { mutableStateOf(false) }
if (showGallerySelect) {
GallerySelect(
onImageUri = { uri ->
showGallerySelect = false
if (uri != null) {
onImageChosen(uri)
}
},
)
}

UploadBoxButton(isUploading, tint, modifier) { showGallerySelect = true }
} else {
UploadBoxButton(isUploading, tint, modifier) { cameraPermissionState.launchPermissionRequest() }
}

UploadBoxButton(isUploading, tint, modifier) { showGallerySelect = true }
}

@Composable
Expand Down Expand Up @@ -183,6 +166,7 @@ fun LoadingAnimation(
easing = LinearEasing,
),
),
label = "UploadGalleryUploadingAnimation",
)

CircularProgressIndicator(
Expand All @@ -203,10 +187,10 @@ fun LoadingAnimation(

@Composable
fun GallerySelect(onImageUri: (Uri?) -> Unit = {}) {
var hasLaunched by remember { mutableStateOf(AtomicBoolean(false)) }
val hasLaunched by remember { mutableStateOf(AtomicBoolean(false)) }
val launcher =
rememberLauncherForActivityResult(
contract = GetMediaActivityResultContract(),
contract = ActivityResultContracts.PickVisualMedia(),
onResult = { uri: Uri? ->
onImageUri(uri)
hasLaunched.set(false)
Expand All @@ -217,7 +201,7 @@ fun GallerySelect(onImageUri: (Uri?) -> Unit = {}) {
fun LaunchGallery() {
SideEffect {
if (!hasLaunched.getAndSet(true)) {
launcher.launch("*/*")
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.video

import android.Manifest
import android.net.Uri
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -151,31 +150,18 @@ fun NewImageButton(
}

if (wantsToPostFromGallery) {
val cameraPermissionState =
rememberPermissionState(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_IMAGES
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
var showGallerySelect by remember { mutableStateOf(false) }
if (showGallerySelect) {
GallerySelect(
onImageUri = { uri ->
wantsToPostFromGallery = false
showGallerySelect = false
pickedURI = uri
},
)

if (cameraPermissionState.status.isGranted) {
var showGallerySelect by remember { mutableStateOf(false) }
if (showGallerySelect) {
GallerySelect(
onImageUri = { uri ->
wantsToPostFromGallery = false
showGallerySelect = false
pickedURI = uri
},
)
}

showGallerySelect = true
} else {
LaunchedEffect(key1 = accountViewModel) { cameraPermissionState.launchPermissionRequest() }
}

showGallerySelect = true
}

pickedURI?.let {
Expand Down

0 comments on commit 7bd9b6a

Please sign in to comment.