Skip to content

Commit

Permalink
Merge pull request #7659 from litetex/load-enough-initial-data
Browse files Browse the repository at this point in the history
Load enough initial items (into BaseListFragment and descendants)
  • Loading branch information
Stypox authored Feb 19, 2022
2 parents 750490c + 2acaefd commit af80d96
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.PignateFooterBinding;
import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
Expand All @@ -44,6 +42,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.function.Supplier;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
Expand Down Expand Up @@ -79,11 +78,6 @@ public void onAttach(@NonNull final Context context) {
}
}

@Override
public void onDetach() {
super.onDetach();
}

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -220,14 +214,10 @@ public void onStart() {
//////////////////////////////////////////////////////////////////////////*/

@Nullable
protected ViewBinding getListHeader() {
protected Supplier<View> getListHeaderSupplier() {
return null;
}

protected ViewBinding getListFooter() {
return PignateFooterBinding.inflate(activity.getLayoutInflater(), itemsList, false);
}

protected RecyclerView.LayoutManager getListLayoutManager() {
return new SuperScrollLayoutManager(activity);
}
Expand All @@ -252,11 +242,10 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
itemsList.setLayoutManager(useGrid ? getGridLayoutManager() : getListLayoutManager());

infoListAdapter.setUseGridVariant(useGrid);
infoListAdapter.setFooter(getListFooter().getRoot());

final ViewBinding listHeader = getListHeader();
if (listHeader != null) {
infoListAdapter.setHeader(listHeader.getRoot());
final Supplier<View> listHeaderSupplier = getListHeaderSupplier();
if (listHeaderSupplier != null) {
infoListAdapter.setHeaderSupplier(listHeaderSupplier);
}

itemsList.setAdapter(infoListAdapter);
Expand All @@ -271,7 +260,7 @@ protected void onItemSelected(final InfoItem selectedItem) {
@Override
protected void initListeners() {
super.initListeners();
infoListAdapter.setOnStreamSelectedListener(new OnClickGesture<StreamInfoItem>() {
infoListAdapter.setOnStreamSelectedListener(new OnClickGesture<>() {
@Override
public void selected(final StreamInfoItem selectedItem) {
onStreamSelected(selectedItem);
Expand Down Expand Up @@ -315,22 +304,98 @@ public void selected(final PlaylistInfoItem selectedItem) {
}
});

infoListAdapter.setOnCommentsSelectedListener(new OnClickGesture<CommentsInfoItem>() {
infoListAdapter.setOnCommentsSelectedListener(new OnClickGesture<>() {
@Override
public void selected(final CommentsInfoItem selectedItem) {
onItemSelected(selectedItem);
}
});

// Ensure that there is always a scroll listener (e.g. when rotating the device)
useNormalItemListScrollListener();
}

/**
* Removes all listeners and adds the normal scroll listener to the {@link #itemsList}.
*/
protected void useNormalItemListScrollListener() {
if (DEBUG) {
Log.d(TAG, "useNormalItemListScrollListener called");
}
itemsList.clearOnScrollListeners();
itemsList.addOnScrollListener(new DefaultItemListOnScrolledDownListener());
}

/**
* Removes all listeners and adds the initial scroll listener to the {@link #itemsList}.
* <br/>
* Which tries to load more items when not enough are in the view (not scrollable)
* and more are available.
* <br/>
* Note: This method only works because "This callback will also be called if visible
* item range changes after a layout calculation. In that case, dx and dy will be 0."
* - which might be unexpected because no actual scrolling occurs...
* <br/>
* This listener will be replaced by DefaultItemListOnScrolledDownListener when
* <ul>
* <li>the view was actually scrolled</li>
* <li>the view is scrollable</li>
* <li>no more items can be loaded</li>
* </ul>
*/
protected void useInitialItemListLoadScrollListener() {
if (DEBUG) {
Log.d(TAG, "useInitialItemListLoadScrollListener called");
}
itemsList.clearOnScrollListeners();
itemsList.addOnScrollListener(new OnScrollBelowItemsListener() {
itemsList.addOnScrollListener(new DefaultItemListOnScrolledDownListener() {
@Override
public void onScrolledDown(final RecyclerView recyclerView) {
onScrollToBottom();
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);

if (dy != 0) {
log("Vertical scroll occurred");

useNormalItemListScrollListener();
return;
}
if (isLoading.get()) {
log("Still loading data -> Skipping");
return;
}
if (!hasMoreItems()) {
log("No more items to load");

useNormalItemListScrollListener();
return;
}
if (itemsList.canScrollVertically(1)
|| itemsList.canScrollVertically(-1)) {
log("View is scrollable");

useNormalItemListScrollListener();
return;
}

log("Loading more data");
loadMoreItems();
}

private void log(final String msg) {
if (DEBUG) {
Log.d(TAG, "initItemListLoadScrollListener - " + msg);
}
}
});
}

class DefaultItemListOnScrolledDownListener extends OnScrollBelowItemsListener {
@Override
public void onScrolledDown(final RecyclerView recyclerView) {
onScrollToBottom();
}
}

private void onStreamSelected(final StreamInfoItem selectedItem) {
onItemSelected(selectedItem);
NavigationHelper.openVideoDetailFragment(requireContext(), getFM(),
Expand Down Expand Up @@ -418,6 +483,12 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
// Load and handle
//////////////////////////////////////////////////////////////////////////*/

@Override
protected void startLoading(final boolean forceLoad) {
useInitialItemListLoadScrollListener();
super.startLoading(forceLoad);
}

protected abstract void loadMoreItems();

protected abstract boolean hasMoreItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void onResume() {
super.onResume();
// Check if it was loading when the fragment was stopped/paused,
if (wasLoading.getAndSet(false)) {
if (hasMoreItems() && infoListAdapter.getItemsList().size() > 0) {
if (hasMoreItems() && !infoListAdapter.getItemsList().isEmpty()) {
loadMoreItems();
} else {
doInitialLoadLogic();
Expand Down Expand Up @@ -105,6 +105,7 @@ public void readFrom(@NonNull final Queue<Object> savedObjects) throws Exception
// Load and handle
//////////////////////////////////////////////////////////////////////////*/

@Override
protected void doInitialLoadLogic() {
if (DEBUG) {
Log.d(TAG, "doInitialLoadLogic() called");
Expand Down Expand Up @@ -158,6 +159,7 @@ public void startLoading(final boolean forceLoad) {
*/
protected abstract Single<ListExtractor.InfoItemsPage> loadMoreItemsLogic();

@Override
protected void loadMoreItems() {
isLoading.set(true);

Expand All @@ -171,9 +173,9 @@ protected void loadMoreItems() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doFinally(this::allowDownwardFocusScroll)
.subscribe((@NonNull ListExtractor.InfoItemsPage InfoItemsPage) -> {
.subscribe(infoItemsPage -> {
isLoading.set(false);
handleNextItems(InfoItemsPage);
handleNextItems(infoItemsPage);
}, (@NonNull Throwable throwable) ->
dynamicallyShowErrorPanelOrSnackbar(new ErrorInfo(throwable,
errorUserAction, "Loading more items: " + url, serviceId)));
Expand Down Expand Up @@ -223,7 +225,7 @@ public void handleResult(@NonNull final I result) {
setTitle(name);

if (infoListAdapter.getItemsList().isEmpty()) {
if (result.getRelatedItems().size() > 0) {
if (!result.getRelatedItems().isEmpty()) {
infoListAdapter.addInfoItemList(result.getRelatedItems());
showListFooter(hasMoreItems());
} else {
Expand All @@ -240,7 +242,7 @@ public void handleResult(@NonNull final I result) {
final List<Throwable> errors = new ArrayList<>(result.getErrors());
// handling ContentNotSupportedException not to show the error but an appropriate string
// so that crashes won't be sent uselessly and the user will understand what happened
errors.removeIf(throwable -> throwable instanceof ContentNotSupportedException);
errors.removeIf(ContentNotSupportedException.class::isInstance);

if (!errors.isEmpty()) {
dynamicallyShowErrorPanelOrSnackbar(new ErrorInfo(result.getErrors(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.schabi.newpipe.fragments.list.channel;

import static org.schabi.newpipe.ktx.TextViewUtils.animateTextColor;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.ktx.ViewUtils.animateBackgroundColor;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
Expand All @@ -17,7 +21,6 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.core.content.ContextCompat;
import androidx.viewbinding.ViewBinding;

import com.jakewharton.rxbinding4.view.RxView;

Expand All @@ -29,7 +32,6 @@
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.channel.ChannelInfo;
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
Expand All @@ -43,13 +45,14 @@
import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.external_communication.ShareUtils;
import org.schabi.newpipe.util.PicassoHelper;
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.util.external_communication.ShareUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
Expand All @@ -61,10 +64,6 @@
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

import static org.schabi.newpipe.ktx.TextViewUtils.animateTextColor;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.ktx.ViewUtils.animateBackgroundColor;

public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
implements View.OnClickListener {

Expand Down Expand Up @@ -145,12 +144,12 @@ public void onDestroy() {
//////////////////////////////////////////////////////////////////////////*/

@Override
protected ViewBinding getListHeader() {
protected Supplier<View> getListHeaderSupplier() {
headerBinding = ChannelHeaderBinding
.inflate(activity.getLayoutInflater(), itemsList, false);
playlistControlBinding = headerBinding.playlistControl;

return headerBinding;
return headerBinding::getRoot;
}

@Override
Expand Down Expand Up @@ -183,21 +182,17 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
}
}

private void openRssFeed() {
final ChannelInfo info = currentInfo;
if (info != null) {
ShareUtils.openUrlInBrowser(requireContext(), info.getFeedUrl(), false);
}
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
NavigationHelper.openSettings(requireContext());
break;
case R.id.menu_item_rss:
openRssFeed();
if (currentInfo != null) {
ShareUtils.openUrlInBrowser(
requireContext(), currentInfo.getFeedUrl(), false);
}
break;
case R.id.menu_item_openInBrowser:
if (currentInfo != null) {
Expand Down Expand Up @@ -516,12 +511,11 @@ private PlayQueue getPlayQueue() {
}

private PlayQueue getPlayQueue(final int index) {
final List<StreamInfoItem> streamItems = new ArrayList<>();
for (final InfoItem i : infoListAdapter.getItemsList()) {
if (i instanceof StreamInfoItem) {
streamItems.add((StreamInfoItem) i);
}
}
final List<StreamInfoItem> streamItems = infoListAdapter.getItemsList().stream()
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast)
.collect(Collectors.toList());

return new ChannelPlayQueue(currentInfo.getServiceId(), currentInfo.getUrl(),
currentInfo.getNextPage(), streamItems, index);
}
Expand Down
Loading

0 comments on commit af80d96

Please sign in to comment.