-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[Refactor] Timestamp links in comments open external browser instead of seeking video #13416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ecomont
wants to merge
5
commits into
TeamNewPipe:refactor
Choose a base branch
from
Ecomont:fix/comment-timestamp-seek
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0f5e1d
Parse timestamps in descriptions into clickable annotations
Ecomont aab0970
Seek video and scroll back to player when a comment timestamp is tapped
Ecomont 4ff5d17
Update Comment component to handle author click events
Ecomont 2e9528d
Address PR review comments for timestamp link handling
Ecomont 1d89ad9
Scroll to player when tapping a timestamp in the description
Ecomont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 136 additions & 8 deletions
144
app/src/main/java/org/schabi/newpipe/ui/components/common/DescriptionText.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,173 @@ | ||
| package org.schabi.newpipe.ui.components.common | ||
|
|
||
| import android.net.Uri | ||
| import androidx.compose.material3.LocalTextStyle | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.text.AnnotatedString | ||
| import androidx.compose.ui.text.LinkAnnotation | ||
| import androidx.compose.ui.text.SpanStyle | ||
| import androidx.compose.ui.text.TextLinkStyles | ||
| import androidx.compose.ui.text.TextStyle | ||
| import androidx.compose.ui.text.buildAnnotatedString | ||
| import androidx.compose.ui.text.fromHtml | ||
| import androidx.compose.ui.text.style.TextDecoration | ||
| import androidx.compose.ui.text.style.TextOverflow | ||
| import org.schabi.newpipe.extractor.stream.Description | ||
| import org.schabi.newpipe.util.external_communication.ShareUtils | ||
| import org.schabi.newpipe.util.text.TimestampExtractor | ||
|
|
||
| @Composable | ||
| fun DescriptionText( | ||
| description: Description, | ||
| modifier: Modifier = Modifier, | ||
| overflow: TextOverflow = TextOverflow.Clip, | ||
| maxLines: Int = Int.MAX_VALUE, | ||
| style: TextStyle = LocalTextStyle.current | ||
| style: TextStyle = LocalTextStyle.current, | ||
| onTimestampClick: ((Int) -> Unit)? = null | ||
| ) { | ||
| Text( | ||
| modifier = modifier, | ||
| text = rememberParsedDescription(description), | ||
| text = rememberParsedDescription(description, onTimestampClick), | ||
| maxLines = maxLines, | ||
| style = style, | ||
| overflow = overflow | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| fun rememberParsedDescription(description: Description): AnnotatedString { | ||
| // TODO: Handle links and hashtags, Markdown. | ||
| return remember(description) { | ||
| fun rememberParsedDescription( | ||
| description: Description, | ||
| onTimestampClick: ((Int) -> Unit)? = null | ||
| ): AnnotatedString { | ||
| // TODO: Handle hashtags, Markdown. | ||
| // rememberUpdatedState lets the click handlers inside the AnnotatedString always invoke | ||
| // the latest callback without rebuilding the string on every recomposition. | ||
| val onTimestampClickState = rememberUpdatedState(onTimestampClick) | ||
| val contextState = rememberUpdatedState(LocalContext.current) | ||
| // Use a boolean key so the string is only rebuilt when handler presence changes, | ||
| // not on every lambda identity change. | ||
| val hasTimestampHandler = onTimestampClick != null | ||
|
|
||
| return remember(description, hasTimestampHandler) { | ||
| val linkStyle = TextLinkStyles(SpanStyle(textDecoration = TextDecoration.Underline)) | ||
|
|
||
| if (description.type == Description.HTML) { | ||
| val styles = TextLinkStyles(SpanStyle(textDecoration = TextDecoration.Underline)) | ||
| AnnotatedString.fromHtml(description.content, styles) | ||
| val baseString = AnnotatedString.fromHtml(description.content, linkStyle) | ||
| if (!hasTimestampHandler) return@remember baseString | ||
|
|
||
| // Rebuild the AnnotatedString, replacing timestamp URL annotations | ||
| // with Clickable ones so they seek the player instead of opening the browser. | ||
| buildAnnotatedString { | ||
| append(baseString.text) | ||
| baseString.spanStyles.forEach { span -> | ||
| addStyle(span.item, span.start, span.end) | ||
| } | ||
| baseString.paragraphStyles.forEach { para -> | ||
| addStyle(para.item, para.start, para.end) | ||
| } | ||
|
|
||
| val handledRanges = mutableListOf<IntRange>() | ||
|
|
||
| baseString.getLinkAnnotations(0, baseString.length).forEach { link -> | ||
| val ann = link.item | ||
| when (ann) { | ||
| is LinkAnnotation.Url -> { | ||
| val secs = getTimestampSecondsFromUrl(ann.url) | ||
| if (secs != null) { | ||
| addLink( | ||
| clickable = LinkAnnotation.Clickable( | ||
| tag = "timestamp", | ||
| styles = linkStyle, | ||
| linkInteractionListener = { | ||
| onTimestampClickState.value?.invoke(secs) | ||
| } | ||
| ), | ||
| start = link.start, | ||
| end = link.end | ||
| ) | ||
| handledRanges += link.start..link.end | ||
| } else { | ||
| val url = ann.url | ||
| addLink( | ||
| clickable = LinkAnnotation.Clickable( | ||
| tag = "url", | ||
| styles = ann.styles, | ||
| linkInteractionListener = { | ||
| ShareUtils.openUrlInApp(contextState.value, url) | ||
| } | ||
| ), | ||
| start = link.start, | ||
| end = link.end | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| is LinkAnnotation.Clickable -> addLink(ann, link.start, link.end) | ||
| } | ||
| } | ||
|
|
||
| // Also add Clickables for plain-text timestamp patterns not already covered | ||
| val text = baseString.text | ||
| val matcher = TimestampExtractor.TIMESTAMPS_PATTERN.matcher(text) | ||
| while (matcher.find()) { | ||
| val dto = TimestampExtractor.getTimestampFromMatcher(matcher, text) ?: continue | ||
| if (handledRanges.none { dto.timestampStart() in it }) { | ||
| val secs = dto.seconds() | ||
| addLink( | ||
| clickable = LinkAnnotation.Clickable( | ||
| tag = "timestamp", | ||
| styles = linkStyle, | ||
| linkInteractionListener = { | ||
| onTimestampClickState.value?.invoke(secs) | ||
| } | ||
| ), | ||
| start = dto.timestampStart(), | ||
| end = dto.timestampEnd() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| AnnotatedString(description.content) | ||
| // Plain text: scan for timestamp patterns | ||
| val baseString = AnnotatedString(description.content) | ||
| if (!hasTimestampHandler) return@remember baseString | ||
|
|
||
| val text = baseString.text | ||
| val matcher = TimestampExtractor.TIMESTAMPS_PATTERN.matcher(text) | ||
| if (!matcher.find()) return@remember baseString | ||
|
|
||
| buildAnnotatedString { | ||
| append(baseString) | ||
| matcher.reset() | ||
| while (matcher.find()) { | ||
| val dto = TimestampExtractor.getTimestampFromMatcher(matcher, text) ?: continue | ||
| val secs = dto.seconds() | ||
| addLink( | ||
| clickable = LinkAnnotation.Clickable( | ||
| tag = "timestamp", | ||
| styles = linkStyle, | ||
| linkInteractionListener = { | ||
| onTimestampClickState.value?.invoke(secs) | ||
| } | ||
| ), | ||
| start = dto.timestampStart(), | ||
| end = dto.timestampEnd() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun getTimestampSecondsFromUrl(url: String): Int? { | ||
| return try { | ||
| Uri.parse(url).getQueryParameter("t")?.toIntOrNull() | ||
| } catch (_: Exception) { | ||
| null | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.