Skip to content

Commit

Permalink
Merge pull request TeamNewPipe#3689 from wb9688/next-stream
Browse files Browse the repository at this point in the history
Remove calls to getNextStream()
  • Loading branch information
TobiGr authored Jul 18, 2020
2 parents 2898bea + 4274827 commit 19e152a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ dependencies {
exclude module: 'support-annotations'
}

implementation 'com.github.TeamNewPipe:NewPipeExtractor:a70cb0283ffc3bba2709815673a5a7940aab0a3a'
implementation 'com.github.wb9688:NewPipeExtractor:fc3a63fec56c110d7f434ef4377b3eeb2b8bbefe'

implementation "com.github.TeamNewPipe:nanojson:1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751"
implementation "org.jsoup:jsoup:1.13.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,6 @@ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);

// Check if the next video label and video is visible,
// if it is, include the two elements in the next check
int nextCount = currentInfo != null && currentInfo.getNextVideo() != null ? 2 : 0;

if (!isLoading.get() && currentInfo != null && isVisible()) {
outState.putSerializable(INFO_KEY, currentInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -40,22 +39,14 @@ public class RelatedVideosFragment extends BaseListInfoFragment<RelatedStreamInf
//////////////////////////////////////////////////////////////////////////*/

private View headerRootLayout;
private Switch aSwitch;

private boolean mIsVisibleToUser = false;
private Switch autoplaySwitch;

public static RelatedVideosFragment getInstance(final StreamInfo info) {
RelatedVideosFragment instance = new RelatedVideosFragment();
instance.setInitialData(info);
return instance;
}

@Override
public void setUserVisibleHint(final boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
mIsVisibleToUser = isVisibleToUser;
}

/*//////////////////////////////////////////////////////////////////////////
// LifeCycle
//////////////////////////////////////////////////////////////////////////*/
Expand All @@ -81,22 +72,18 @@ public void onDestroy() {
}

protected View getListHeader() {
if (relatedStreamInfo != null && relatedStreamInfo.getNextStream() != null) {
if (relatedStreamInfo != null && relatedStreamInfo.getRelatedItems() != null) {
headerRootLayout = activity.getLayoutInflater()
.inflate(R.layout.related_streams_header, itemsList, false);
aSwitch = headerRootLayout.findViewById(R.id.autoplay_switch);

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
Boolean autoplay = pref.getBoolean(getString(R.string.auto_queue_key), false);
aSwitch.setChecked(autoplay);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton,
final boolean b) {
autoplaySwitch = headerRootLayout.findViewById(R.id.autoplay_switch);

final SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getContext());
final boolean autoplay = pref.getBoolean(getString(R.string.auto_queue_key), false);
autoplaySwitch.setChecked(autoplay);
autoplaySwitch.setOnCheckedChangeListener((compoundButton, b) ->
PreferenceManager.getDefaultSharedPreferences(getContext()).edit()
.putBoolean(getString(R.string.auto_queue_key), b).apply();
}
});
.putBoolean(getString(R.string.auto_queue_key), b).apply());
return headerRootLayout;
} else {
return null;
Expand All @@ -105,7 +92,7 @@ public void onCheckedChanged(final CompoundButton compoundButton,

@Override
protected Single<ListExtractor.InfoItemsPage> loadMoreItemsLogic() {
return Single.fromCallable(() -> ListExtractor.InfoItemsPage.emptyPage());
return Single.fromCallable(ListExtractor.InfoItemsPage::emptyPage);
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -216,8 +203,8 @@ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
final String s) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean autoplay = pref.getBoolean(getString(R.string.auto_queue_key), false);
if (null != aSwitch) {
aSwitch.setChecked(autoplay);
if (autoplaySwitch != null) {
autoplaySwitch.setChecked(autoplay);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ public static String cacheKeyOf(@NonNull final StreamInfo info,
}

/**
* Given a {@link StreamInfo} and the existing queue items, provide the
* {@link SinglePlayQueue} consisting of the next video for auto queuing.
* Given a {@link StreamInfo} and the existing queue items,
* provide the {@link SinglePlayQueue} consisting of the next video for auto queueing.
* <p>
* This method detects and prevents cycle by naively checking if a
* candidate next video's url already exists in the existing items.
* This method detects and prevents cycles by naively checking
* if a candidate next video's url already exists in the existing items.
* </p>
* <p>
* To select the next video, {@link StreamInfo#getNextVideo()} is first
* checked. If it is nonnull and is not part of the existing items, then
* it will be used as the next video. Otherwise, an random item with
* non-repeating url will be selected from the {@link StreamInfo#getRelatedStreams()}.
* The first item in {@link StreamInfo#getRelatedStreams()} is checked first.
* If it is non-null and is not part of the existing items, it will be used as the next stream.
* Otherwise, a random item with non-repeating url will be selected
* from the {@link StreamInfo#getRelatedStreams()}.
* </p>
*
* @param info currently playing stream
Expand All @@ -152,27 +152,28 @@ public static String cacheKeyOf(@NonNull final StreamInfo info,
@Nullable
public static PlayQueue autoQueueOf(@NonNull final StreamInfo info,
@NonNull final List<PlayQueueItem> existingItems) {
Set<String> urls = new HashSet<>(existingItems.size());
final Set<String> urls = new HashSet<>(existingItems.size());
for (final PlayQueueItem item : existingItems) {
urls.add(item.getUrl());
}

final StreamInfoItem nextVideo = info.getNextVideo();
if (nextVideo != null && !urls.contains(nextVideo.getUrl())) {
return getAutoQueuedSinglePlayQueue(nextVideo);
}

final List<InfoItem> relatedItems = info.getRelatedStreams();
if (relatedItems == null) {
return null;
}

List<StreamInfoItem> autoQueueItems = new ArrayList<>();
for (final InfoItem item : info.getRelatedStreams()) {
if (relatedItems.get(0) != null && relatedItems.get(0) instanceof StreamInfoItem
&& !urls.contains(relatedItems.get(0).getUrl())) {
return getAutoQueuedSinglePlayQueue((StreamInfoItem) relatedItems.get(0));
}

final List<StreamInfoItem> autoQueueItems = new ArrayList<>();
for (final InfoItem item : relatedItems) {
if (item instanceof StreamInfoItem && !urls.contains(item.getUrl())) {
autoQueueItems.add((StreamInfoItem) item);
}
}

Collections.shuffle(autoQueueItems);
return autoQueueItems.isEmpty()
? null : getAutoQueuedSinglePlayQueue(autoQueueItems.get(0));
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/java/org/schabi/newpipe/util/RelatedStreamInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
import org.schabi.newpipe.extractor.ListInfo;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RelatedStreamInfo extends ListInfo<InfoItem> {

private StreamInfoItem nextStream;

public RelatedStreamInfo(final int serviceId, final ListLinkHandler listUrlIdHandler,
final String name) {
super(serviceId, listUrlIdHandler, name);
Expand All @@ -25,20 +21,8 @@ public static RelatedStreamInfo getInfo(final StreamInfo info) {
RelatedStreamInfo relatedStreamInfo = new RelatedStreamInfo(
info.getServiceId(), handler, info.getName());
List<InfoItem> streams = new ArrayList<>();
if (info.getNextVideo() != null) {
streams.add(info.getNextVideo());
}
streams.addAll(info.getRelatedStreams());
relatedStreamInfo.setRelatedItems(streams);
relatedStreamInfo.setNextStream(info.getNextVideo());
return relatedStreamInfo;
}

public StreamInfoItem getNextStream() {
return nextStream;
}

public void setNextStream(final StreamInfoItem nextStream) {
this.nextStream = nextStream;
}
}

0 comments on commit 19e152a

Please sign in to comment.