From eac75e057a9a911dd28153738bdd4a49b9a10d6c Mon Sep 17 00:00:00 2001 From: Maary <24504742+Steve-Mr@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:07:32 +0800 Subject: [PATCH] [fix]onClick on MediaPlayer --- .../maary/oblivionis/ui/ActionComponents.kt | 33 +++++-------------- .../oblivionis/ui/screen/ActionScreen.kt | 7 ++-- .../oblivionis/ui/screen/RecycleScreen.kt | 14 +------- 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/top/maary/oblivionis/ui/ActionComponents.kt b/app/src/main/java/top/maary/oblivionis/ui/ActionComponents.kt index bc0fd4b..a3df162 100644 --- a/app/src/main/java/top/maary/oblivionis/ui/ActionComponents.kt +++ b/app/src/main/java/top/maary/oblivionis/ui/ActionComponents.kt @@ -2,7 +2,9 @@ package top.maary.oblivionis.ui import android.net.Uri import android.provider.MediaStore +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.clickable +import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.PressInteraction @@ -61,6 +63,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collectLatest import top.maary.oblivionis.R +@OptIn(ExperimentalFoundationApi::class) @Composable fun MediaPlayer( modifier: Modifier, @@ -68,33 +71,16 @@ fun MediaPlayer( imageLoader: ImageLoader, isMultiSelectionState: Boolean = false, isSelected: Boolean = false, // 用于表示当前项是否被选中 - onImageClick: () -> Unit = {}, - onVideoClick: () -> Unit = {}, + onMediaClick: () -> Unit = {}, onLongPress: () -> Unit = {} // 长按事件 ) { Box( modifier = Modifier .wrapContentSize() - .pointerInput(Unit) { - detectTapGestures( - onLongPress = { - onLongPress() // 处理长按事件 - }, - onTap = { - if (uri - .toString() - .startsWith(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()) - ) { - onImageClick() - } else if (uri - .toString() - .startsWith(MediaStore.Video.Media.EXTERNAL_CONTENT_URI.toString()) - ) { - onVideoClick() - } - } - ) - } + .combinedClickable ( + onLongClick = { onLongPress() }, + onClick = { onMediaClick() } + ) ) { when { // 处理图片显示 @@ -326,14 +312,13 @@ fun VideoViewAlt( ) { Box( modifier = modifier - .clickable { onClick() }, contentAlignment = Alignment.BottomEnd + , contentAlignment = Alignment.BottomEnd ) { AsyncImage( model = uri, contentDescription = "", imageLoader = imageLoader, modifier = modifier - .clickable { onClick() } .fillMaxWidth() .clip(RoundedCornerShape(8.dp)) ) diff --git a/app/src/main/java/top/maary/oblivionis/ui/screen/ActionScreen.kt b/app/src/main/java/top/maary/oblivionis/ui/screen/ActionScreen.kt index 32f9e18..e3ce7c8 100644 --- a/app/src/main/java/top/maary/oblivionis/ui/screen/ActionScreen.kt +++ b/app/src/main/java/top/maary/oblivionis/ui/screen/ActionScreen.kt @@ -2,6 +2,7 @@ package top.maary.oblivionis.ui.screen import android.content.Intent import android.provider.MediaStore +import android.util.Log import androidx.compose.animation.core.animate import androidx.compose.animation.core.tween import androidx.compose.foundation.gestures.Orientation @@ -321,13 +322,15 @@ fun ActionScreen( MediaPlayer(modifier = Modifier.fillMaxWidth(), uri = uri, imageLoader = imageLoader, - onVideoClick = { + onMediaClick = { + Log.v("OBLIVIONIS", "IMAGE CLICK") context.startActivity( Intent.createChooser( intent, context.getString(R.string.choose_app) ) ) - }) + } + ) if (images.value[page].isExcluded) { IconButton( onClick = {}, colors = IconButtonColors( diff --git a/app/src/main/java/top/maary/oblivionis/ui/screen/RecycleScreen.kt b/app/src/main/java/top/maary/oblivionis/ui/screen/RecycleScreen.kt index e2e374f..adc043e 100644 --- a/app/src/main/java/top/maary/oblivionis/ui/screen/RecycleScreen.kt +++ b/app/src/main/java/top/maary/oblivionis/ui/screen/RecycleScreen.kt @@ -241,19 +241,7 @@ fun RecycleScreen( isMultiSelectionState = selectedItems.value.isNotEmpty(), isSelected = selectedItems.value.contains(index), imageLoader = imageLoader, - onImageClick = { - if (selectedItems.value.isNotEmpty()) { - val newSet = selectedItems.value.toMutableSet() // 创建一个新集合 - if (!newSet.add(index)) { - newSet.remove(index) // 如果元素已存在,则移除 - } - selectedItems.value = newSet // 更新 `selectedItems.value`,触发重组 - return@MediaPlayer - } - clickedIndex.intValue = index - openDialog.value = true - }, - onVideoClick = { + onMediaClick = { if (selectedItems.value.isNotEmpty()) { val newSet = selectedItems.value.toMutableSet() // 创建一个新集合 if (!newSet.add(index)) {