Skip to content
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

Long-click on stream segment to share timestamped url #9203

Merged
merged 2 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ class StreamSegmentAdapter(

interface StreamSegmentListener {
fun onItemClick(item: StreamSegmentItem, seconds: Int)
fun onItemLongClick(item: StreamSegmentItem, seconds: Int)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class StreamSegmentItem(
viewHolder.root.findViewById<TextView>(R.id.textViewStartSeconds).text =
Localization.getDurationString(item.startTimeSeconds.toLong())
viewHolder.root.setOnClickListener { onClick.onItemClick(this, item.startTimeSeconds) }
viewHolder.root.setOnLongClickListener { onClick.onItemLongClick(this, item.startTimeSeconds); true }
viewHolder.root.isSelected = isSelected
}

Expand Down
34 changes: 29 additions & 5 deletions app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static org.schabi.newpipe.MainActivity.DEBUG;
import static org.schabi.newpipe.QueueItemMenuUtil.openPopupMenu;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.player.Player.STATE_COMPLETED;
import static org.schabi.newpipe.player.Player.STATE_PAUSED;
Expand Down Expand Up @@ -52,6 +53,7 @@
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.info_list.StreamSegmentAdapter;
import org.schabi.newpipe.info_list.StreamSegmentItem;
import org.schabi.newpipe.ktx.AnimationType;
import org.schabi.newpipe.local.dialog.PlaylistDialog;
import org.schabi.newpipe.player.Player;
Expand All @@ -60,6 +62,7 @@
import org.schabi.newpipe.player.gesture.MainPlayerGestureListener;
import org.schabi.newpipe.player.helper.PlaybackParameterDialog;
import org.schabi.newpipe.player.helper.PlayerHelper;
import org.schabi.newpipe.player.mediaitem.MediaItemTag;
import org.schabi.newpipe.player.playqueue.PlayQueue;
import org.schabi.newpipe.player.playqueue.PlayQueueAdapter;
import org.schabi.newpipe.player.playqueue.PlayQueueItem;
Expand All @@ -69,6 +72,7 @@
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.external_communication.KoreUtils;
import org.schabi.newpipe.util.external_communication.ShareUtils;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -644,7 +648,7 @@ private void onSegmentsClicked() {
private void buildSegments() {
binding.itemsList.setAdapter(segmentAdapter);
binding.itemsList.setClickable(true);
binding.itemsList.setLongClickable(false);
binding.itemsList.setLongClickable(true);

binding.itemsList.clearOnScrollListeners();
if (itemTouchHelper != null) {
Expand Down Expand Up @@ -696,10 +700,30 @@ public void onScrolledDown(final RecyclerView recyclerView) {
}

private StreamSegmentAdapter.StreamSegmentListener getStreamSegmentListener() {
return (item, seconds) -> {
segmentAdapter.selectSegment(item);
player.seekTo(seconds * 1000L);
player.triggerProgressUpdate();
return new StreamSegmentAdapter.StreamSegmentListener() {
@Override
public void onItemClick(@NonNull final StreamSegmentItem item, final int seconds) {
segmentAdapter.selectSegment(item);
player.seekTo(seconds * 1000L);
player.triggerProgressUpdate();
}

@Override
public void onItemLongClick(@NonNull final StreamSegmentItem item, final int seconds) {
@Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata();
if (currentMetadata == null
|| currentMetadata.getServiceId() != YouTube.getServiceId()) {
return;
}

final PlayQueueItem currentItem = player.getCurrentItem();
if (currentItem != null) {
String videoUrl = player.getVideoUrl();
videoUrl += ("&t=" + seconds);
ShareUtils.shareText(context, currentItem.getTitle(),
videoUrl, currentItem.getThumbnailUrl());
Comment on lines +721 to +724
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for services other than YouTube. You should use the same logic as in the player:

final long timeSeconds = simpleExoPlayer.getCurrentPosition() / 1000;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey sorry for clarification, do you mean that I should be surrounding line 714 with an if statement checking that the service ID is indeed YouTube? I did look at this part in the player but thought it might not be necessary since I thought that these click handlers on the segments would only ever be called from a YouTube service. Sorry if I am misunderstanding and thanks for the quick replies!
And yes I am an ANU student :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that these click handlers on the segments would only ever be called from a YouTube service

That's probably true, but who knows if a future service will support segments ;-)

Yes, sorround it with a "is youtube" check. If it's not YouTube, don't allow long-pressing the item at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I have been attempting to add this check to the buildSegements function with setLongClickable(false), however even with setOnLongClickListener(null), the long click event still occurs. Similarly, the existing onClick method used for seeking to different chapters does not get disabled by setting clickable to false. I have also tried adding the check to the beginning of the long click call, however since the call consumes the click event, this is not the desired result.

don't allow long-pressing the item at all.

Is there some better way of doing this that I am unaware of? Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then it's fine if you just don't do anything when a long-press is detected but the url is non-youtube

}
}
};
}

Expand Down