Skip to content

Commit

Permalink
Merge pull request #2207 from yausername/timestampClickFix
Browse files Browse the repository at this point in the history
seek on clicking timestamp links in comments
  • Loading branch information
TobiGr authored Apr 3, 2019
2 parents e98f68e + bf845be commit 09eeaa9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String transformUrl(Matcher match, String url) {
if(hours != null) timestamp += (Integer.parseInt(hours.replace(":", ""))*3600);
if(minutes != null) timestamp += (Integer.parseInt(minutes.replace(":", ""))*60);
if(seconds != null) timestamp += (Integer.parseInt(seconds));
return streamUrl + url.replace(match.group(0), "&t=" + String.valueOf(timestamp));
return streamUrl + url.replace(match.group(0), "#timestamp=" + String.valueOf(timestamp));
}
};

Expand Down Expand Up @@ -93,15 +93,14 @@ public void onClick(View view) {

streamUrl = item.getUrl();

itemContentView.setMaxLines(commentDefaultLines);
itemContentView.setLines(commentDefaultLines);
commentText = item.getCommentText();
itemContentView.setText(commentText);
linkify();
itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE);

if(itemContentView.getLineCount() == 0){
if (itemContentView.getLineCount() == 0) {
itemContentView.post(() -> ellipsize());
}else{
} else {
ellipsize();
}

Expand All @@ -121,15 +120,17 @@ public void onClick(View view) {
private void ellipsize() {
if (itemContentView.getLineCount() > commentDefaultLines){
int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1);
String newVal = itemContentView.getText().subSequence(0, endOfLastLine - 3) + "...";
int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine -2);
if(end == -1) end = Math.max(endOfLastLine -2, 0);
String newVal = itemContentView.getText().subSequence(0, end) + " …";
itemContentView.setText(newVal);
linkify();
}
linkify();
}

private void toggleEllipsize() {
if (itemContentView.getText().toString().equals(commentText)) {
ellipsize();
if (itemContentView.getLineCount() > commentDefaultLines) ellipsize();
} else {
expand();
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/org/schabi/newpipe/player/BasePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ public void handleIntent(Intent intent) {
final boolean playbackSkipSilence = intent.getBooleanExtra(PLAYBACK_SKIP_SILENCE,
getPlaybackSkipSilence());

// seek to timestamp if stream is already playing
if (simpleExoPlayer != null
&& queue.size() == 1
&& playQueue != null
&& playQueue.getItem() != null
&& queue.getItem().getUrl().equals(playQueue.getItem().getUrl())
&& queue.getItem().getRecoveryPosition() != PlayQueueItem.RECOVERY_UNSET
) {
simpleExoPlayer.seekTo(playQueue.getIndex(), queue.getItem().getRecoveryPosition());
return;
}

// Good to go...
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence,
/*playOnInit=*/true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public SinglePlayQueue(final StreamInfo info) {
super(0, Collections.singletonList(new PlayQueueItem(info)));
}

public SinglePlayQueue(final StreamInfo info, final long startPosition) {
super(0, Collections.singletonList(new PlayQueueItem(info)));
getItem().setRecoveryPosition(startPosition);
}

public SinglePlayQueue(final List<StreamInfoItem> items, final int index) {
super(index, playQueueItemsOf(items));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CommentTextOnTouchListener implements View.OnTouchListener {

public static final CommentTextOnTouchListener INSTANCE = new CommentTextOnTouchListener();

private static final Pattern timestampPattern = Pattern.compile(".*&t=(\\d+)");
private static final Pattern timestampPattern = Pattern.compile("(.*)#timestamp=(\\d+)");

@Override
public boolean onTouch(View v, MotionEvent event) {
Expand Down Expand Up @@ -86,6 +86,12 @@ public boolean onTouch(View v, MotionEvent event) {

private boolean handleUrl(Context context, URLSpan urlSpan) {
String url = urlSpan.getURL();
int seconds = -1;
Matcher matcher = timestampPattern.matcher(url);
if(matcher.matches()){
url = matcher.group(1);
seconds = Integer.parseInt(matcher.group(2));
}
StreamingService service;
StreamingService.LinkType linkType;
try {
Expand All @@ -97,9 +103,7 @@ private boolean handleUrl(Context context, URLSpan urlSpan) {
if(linkType == StreamingService.LinkType.NONE){
return false;
}
Matcher matcher = timestampPattern.matcher(url);
if(linkType == StreamingService.LinkType.STREAM && matcher.matches()){
int seconds = Integer.parseInt(matcher.group(1));
if(linkType == StreamingService.LinkType.STREAM && seconds != -1){
return playOnPopup(context, url, service, seconds);
}else{
NavigationHelper.openRouterActivity(context, url);
Expand All @@ -119,9 +123,8 @@ private boolean playOnPopup(Context context, String url, StreamingService servic
single.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(info -> {
PlayQueue playQueue = new SinglePlayQueue((StreamInfo) info);
((StreamInfo) info).setStartPosition(seconds);
NavigationHelper.enqueueOnPopupPlayer(context, playQueue, true);
PlayQueue playQueue = new SinglePlayQueue((StreamInfo) info, seconds*1000);
NavigationHelper.playOnPopupPlayer(context, playQueue);
});
return true;
}
Expand Down

0 comments on commit 09eeaa9

Please sign in to comment.