Skip to content

Commit 941b8eb

Browse files
Implement copy on long click
1 parent b1add13 commit 941b8eb

2 files changed

Lines changed: 35 additions & 17 deletions

File tree

app/src/main/java/org/schabi/newpipe/ui/components/common/DescriptionText.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import androidx.compose.runtime.Composable
66
import androidx.compose.runtime.remember
77
import androidx.compose.ui.Modifier
88
import androidx.compose.ui.text.AnnotatedString
9-
import androidx.compose.ui.text.ParagraphStyle
109
import androidx.compose.ui.text.SpanStyle
1110
import androidx.compose.ui.text.TextLinkStyles
1211
import androidx.compose.ui.text.TextStyle
@@ -23,21 +22,24 @@ fun DescriptionText(
2322
maxLines: Int = Int.MAX_VALUE,
2423
style: TextStyle = LocalTextStyle.current
2524
) {
25+
Text(
26+
modifier = modifier,
27+
text = rememberParsedDescription(description),
28+
maxLines = maxLines,
29+
style = style,
30+
overflow = overflow
31+
)
32+
}
33+
34+
@Composable
35+
fun rememberParsedDescription(description: Description): AnnotatedString {
2636
// TODO: Handle links and hashtags, Markdown.
27-
val parsedDescription = remember(description) {
37+
return remember(description) {
2838
if (description.type == Description.HTML) {
2939
val styles = TextLinkStyles(SpanStyle(textDecoration = TextDecoration.Underline))
3040
AnnotatedString.fromHtml(description.content, styles)
3141
} else {
32-
AnnotatedString(description.content, ParagraphStyle())
42+
AnnotatedString(description.content)
3343
}
3444
}
35-
36-
Text(
37-
modifier = modifier,
38-
text = parsedDescription,
39-
maxLines = maxLines,
40-
style = style,
41-
overflow = overflow
42-
)
4345
}

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.schabi.newpipe.ui.components.video.comment
22

33
import android.content.res.Configuration
4+
import android.os.Build
5+
import android.widget.Toast
46
import androidx.compose.animation.animateContentSize
7+
import androidx.compose.foundation.ExperimentalFoundationApi
58
import androidx.compose.foundation.Image
69
import androidx.compose.foundation.clickable
10+
import androidx.compose.foundation.combinedClickable
711
import androidx.compose.foundation.layout.Arrangement
812
import androidx.compose.foundation.layout.Column
913
import androidx.compose.foundation.layout.Row
@@ -27,6 +31,7 @@ import androidx.compose.runtime.setValue
2731
import androidx.compose.ui.Alignment
2832
import androidx.compose.ui.Modifier
2933
import androidx.compose.ui.draw.clip
34+
import androidx.compose.ui.platform.LocalClipboardManager
3035
import androidx.compose.ui.platform.LocalContext
3136
import androidx.compose.ui.res.painterResource
3237
import androidx.compose.ui.res.pluralStringResource
@@ -46,23 +51,34 @@ import org.schabi.newpipe.extractor.Page
4651
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
4752
import org.schabi.newpipe.extractor.stream.Description
4853
import org.schabi.newpipe.paging.CommentsSource
49-
import org.schabi.newpipe.ui.components.common.DescriptionText
54+
import org.schabi.newpipe.ui.components.common.rememberParsedDescription
5055
import org.schabi.newpipe.ui.theme.AppTheme
5156
import org.schabi.newpipe.util.Localization
5257
import org.schabi.newpipe.util.NavigationHelper
5358
import org.schabi.newpipe.util.image.ImageStrategy
5459

55-
@OptIn(ExperimentalMaterial3Api::class)
60+
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
5661
@Composable
5762
fun Comment(comment: CommentsInfoItem) {
63+
val clipboardManager = LocalClipboardManager.current
5864
val context = LocalContext.current
5965
var isExpanded by rememberSaveable { mutableStateOf(false) }
6066
var showReplies by rememberSaveable { mutableStateOf(false) }
67+
val parsedDescription = rememberParsedDescription(comment.commentText)
6168

6269
Row(
6370
modifier = Modifier
6471
.animateContentSize()
65-
.clickable { isExpanded = !isExpanded }
72+
.combinedClickable(
73+
onLongClick = {
74+
clipboardManager.setText(parsedDescription)
75+
if (Build.VERSION.SDK_INT < 33) {
76+
// Android 13 has its own "copied to clipboard" dialog
77+
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show()
78+
}
79+
},
80+
onClick = { isExpanded = !isExpanded }
81+
)
6682
.padding(8.dp),
6783
horizontalArrangement = Arrangement.spacedBy(8.dp)
6884
) {
@@ -99,13 +115,13 @@ fun Comment(comment: CommentsInfoItem) {
99115
Text(text = nameAndDate, color = MaterialTheme.colorScheme.secondary)
100116
}
101117

102-
DescriptionText(
103-
description = comment.commentText,
118+
Text(
119+
text = parsedDescription,
104120
// If the comment is expanded, we display all its content
105121
// otherwise we only display the first two lines
106122
maxLines = if (isExpanded) Int.MAX_VALUE else 2,
107123
overflow = TextOverflow.Ellipsis,
108-
style = MaterialTheme.typography.bodyMedium,
124+
style = MaterialTheme.typography.bodyMedium
109125
)
110126

111127
Row(

0 commit comments

Comments
 (0)