1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Create utilities to copy to clipboard in Compose code

This commit is contained in:
Stypox 2024-11-11 14:52:44 +01:00
parent cea149f852
commit 37d1c784fa
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 31 additions and 12 deletions

View File

@ -1,8 +1,6 @@
package org.schabi.newpipe.ui.components.video.comment package org.schabi.newpipe.ui.components.video.comment
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build
import android.widget.Toast
import androidx.compose.animation.animateContentSize import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
@ -34,7 +32,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.pluralStringResource
@ -53,12 +50,12 @@ import org.schabi.newpipe.ui.components.common.rememberParsedDescription
import org.schabi.newpipe.ui.theme.AppTheme import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.Localization import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.external_communication.copyToClipboardCallback
import org.schabi.newpipe.util.image.ImageStrategy import org.schabi.newpipe.util.image.ImageStrategy
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun Comment(comment: CommentsInfoItem, onCommentAuthorOpened: () -> Unit) { fun Comment(comment: CommentsInfoItem, onCommentAuthorOpened: () -> Unit) {
val clipboardManager = LocalClipboardManager.current
val context = LocalContext.current val context = LocalContext.current
var isExpanded by rememberSaveable { mutableStateOf(false) } var isExpanded by rememberSaveable { mutableStateOf(false) }
var showReplies by rememberSaveable { mutableStateOf(false) } var showReplies by rememberSaveable { mutableStateOf(false) }
@ -68,14 +65,8 @@ fun Comment(comment: CommentsInfoItem, onCommentAuthorOpened: () -> Unit) {
modifier = Modifier modifier = Modifier
.animateContentSize() .animateContentSize()
.combinedClickable( .combinedClickable(
onLongClick = { onLongClick = copyToClipboardCallback { parsedDescription },
clipboardManager.setText(parsedDescription) onClick = { isExpanded = !isExpanded },
if (Build.VERSION.SDK_INT < 33) {
// Android 13 has its own "copied to clipboard" dialog
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show()
}
},
onClick = { isExpanded = !isExpanded }
) )
.padding(start = 8.dp, top = 10.dp, end = 8.dp, bottom = 4.dp), .padding(start = 8.dp, top = 10.dp, end = 8.dp, bottom = 4.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp) horizontalArrangement = Arrangement.spacedBy(8.dp)

View File

@ -0,0 +1,28 @@
package org.schabi.newpipe.util.external_communication
import android.content.Context
import android.os.Build
import android.widget.Toast
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.AnnotatedString
import org.schabi.newpipe.R
fun ClipboardManager.setTextAndShowToast(context: Context, annotatedString: AnnotatedString) {
setText(annotatedString)
if (Build.VERSION.SDK_INT < 33) {
// Android 13 has its own "copied to clipboard" dialog
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show()
}
}
@Composable
fun copyToClipboardCallback(annotatedString: () -> AnnotatedString): (() -> Unit) {
val clipboardManager = LocalClipboardManager.current
val context = LocalContext.current
return {
clipboardManager.setTextAndShowToast(context, annotatedString())
}
}