Skip to content

Commit ff7cfe4

Browse files
committed
Reverted to loading behavior of #7638 and improved it
The previous/reverted behavior caused unwanted data transmission: * Removed loading via handleResults/loadMoreItems-callback because the RecyclerView is apparently not immediately updated in the UI when the data is set which causes one load of data to much.
1 parent d3cd3d6 commit ff7cfe4

3 files changed

Lines changed: 68 additions & 80 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,74 @@ public void selected(final CommentsInfoItem selectedItem) {
312312
});
313313

314314
itemsList.clearOnScrollListeners();
315-
itemsList.addOnScrollListener(new OnScrollBelowItemsListener() {
315+
316+
/*
317+
* Add initial scroll listener - which tries to load more items when not enough
318+
* are in the view (not scrollable) and more are available.
319+
*
320+
* Note: This method only works because "This callback will also be called if visible
321+
* item range changes after a layout calculation. In that case, dx and dy will be 0."
322+
* - which might be unexpected because no actual scrolling occurs...
323+
*
324+
* This listener will be replaced by DefaultItemListOnScrolledDownListener when
325+
* * the view was actually scrolled
326+
* * the view is scrollable
327+
* * No more items can be loaded
328+
*/
329+
itemsList.addOnScrollListener(new DefaultItemListOnScrolledDownListener() {
316330
@Override
317-
public void onScrolledDown(final RecyclerView recyclerView) {
318-
onScrollToBottom();
331+
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
332+
super.onScrolled(recyclerView, dx, dy);
333+
334+
if (dy != 0) {
335+
log("Vertical scroll occurred");
336+
337+
useNormalScrollListener();
338+
return;
339+
}
340+
if (isLoading.get()) {
341+
log("Still loading data -> Skipping");
342+
return;
343+
}
344+
if (!hasMoreItems()) {
345+
log("No more items to load");
346+
347+
useNormalScrollListener();
348+
return;
349+
}
350+
if (itemsList.canScrollVertically(1)
351+
|| itemsList.canScrollVertically(-1)) {
352+
log("View is scrollable");
353+
354+
useNormalScrollListener();
355+
return;
356+
}
357+
358+
log("Loading more data");
359+
loadMoreItems();
360+
}
361+
362+
private void useNormalScrollListener() {
363+
log("Unregistering and using normal listener");
364+
itemsList.removeOnScrollListener(this);
365+
itemsList.addOnScrollListener(new DefaultItemListOnScrolledDownListener());
366+
}
367+
368+
private void log(final String msg) {
369+
if (DEBUG) {
370+
Log.d(TAG, "itemListInitScrollListener - " + msg);
371+
}
319372
}
320373
});
321374
}
322375

376+
class DefaultItemListOnScrolledDownListener extends OnScrollBelowItemsListener {
377+
@Override
378+
public void onScrolledDown(final RecyclerView recyclerView) {
379+
onScrollToBottom();
380+
}
381+
}
382+
323383
private void onStreamSelected(final StreamInfoItem selectedItem) {
324384
onItemSelected(selectedItem);
325385
NavigationHelper.openVideoDetailFragment(requireContext(), getFM(),
@@ -407,66 +467,7 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
407467
// Load and handle
408468
//////////////////////////////////////////////////////////////////////////*/
409469

410-
/**
411-
* If more items are loadable and the itemList is not scrollable -> load more data.
412-
* <br/>
413-
* Should be called once the initial items inside {@link #startLoading(boolean)}
414-
* has been loaded and added to the {@link #itemsList}.
415-
* <br/>
416-
* Otherwise the loading indicator is always shown but no data can be loaded
417-
* because the view is not scrollable; see also #1974.
418-
*/
419-
protected void ifMoreItemsLoadableLoadUntilScrollable() {
420-
ifMoreItemsLoadableLoadUntilScrollable(0);
421-
}
422-
423-
/**
424-
* If more items are loadable and the itemList is not scrollable -> load more data.
425-
*
426-
* @param recursiveCallCount Amount of recursive calls that occurred
427-
* @see #ifMoreItemsLoadableLoadUntilScrollable()
428-
*/
429-
protected void ifMoreItemsLoadableLoadUntilScrollable(final int recursiveCallCount) {
430-
// Try to prevent malfunction / stackoverflow
431-
if (recursiveCallCount > 100) {
432-
Log.w(TAG, "loadEnoughInitialData - Too many recursive calls - Aborting");
433-
return;
434-
}
435-
if (!hasMoreItems()) {
436-
if (DEBUG) {
437-
Log.d(TAG, "loadEnoughInitialData - OK: No more items to load");
438-
}
439-
return;
440-
}
441-
if (itemsList.canScrollVertically(1)
442-
|| itemsList.canScrollVertically(-1)) {
443-
if (DEBUG) {
444-
Log.d(TAG, "loadEnoughInitialData - OK: itemList is scrollable");
445-
}
446-
return;
447-
}
448-
if (DEBUG) {
449-
Log.d(TAG, "loadEnoughInitialData - View is not scrollable "
450-
+ "but it could load more items -> Loading more");
451-
}
452-
loadMoreItems(() ->
453-
ifMoreItemsLoadableLoadUntilScrollable(recursiveCallCount + 1));
454-
}
455-
456-
/**
457-
* Loads more items.
458-
* @param initialDataLoadCallback
459-
* Callback used in {@link #ifMoreItemsLoadableLoadUntilScrollable()}.
460-
* <br/>
461-
* Execute it once the data was loaded and added to the {@link #itemsList}.
462-
* <br/>
463-
* Might be <code>null</code>.
464-
*/
465-
protected abstract void loadMoreItems(@Nullable Runnable initialDataLoadCallback);
466-
467-
protected void loadMoreItems() {
468-
loadMoreItems(null);
469-
}
470+
protected abstract void loadMoreItems();
470471

471472
protected abstract boolean hasMoreItems();
472473

app/src/main/java/org/schabi/newpipe/fragments/list/BaseListInfoFragment.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.view.View;
77

88
import androidx.annotation.NonNull;
9-
import androidx.annotation.Nullable;
109

1110
import org.schabi.newpipe.error.ErrorInfo;
1211
import org.schabi.newpipe.error.UserAction;
@@ -146,7 +145,6 @@ public void startLoading(final boolean forceLoad) {
146145
currentInfo = result;
147146
currentNextPage = result.getNextPage();
148147
handleResult(result);
149-
ifMoreItemsLoadableLoadUntilScrollable();
150148
}, throwable ->
151149
showError(new ErrorInfo(throwable, errorUserAction,
152150
"Start loading: " + url, serviceId)));
@@ -162,7 +160,7 @@ public void startLoading(final boolean forceLoad) {
162160
protected abstract Single<ListExtractor.InfoItemsPage> loadMoreItemsLogic();
163161

164162
@Override
165-
protected void loadMoreItems(@Nullable final Runnable initialDataLoadCallback) {
163+
protected void loadMoreItems() {
166164
isLoading.set(true);
167165

168166
if (currentWorker != null) {
@@ -178,9 +176,6 @@ protected void loadMoreItems(@Nullable final Runnable initialDataLoadCallback) {
178176
.subscribe(infoItemsPage -> {
179177
isLoading.set(false);
180178
handleNextItems(infoItemsPage);
181-
if (initialDataLoadCallback != null) {
182-
initialDataLoadCallback.run();
183-
}
184179
}, (@NonNull Throwable throwable) ->
185180
dynamicallyShowErrorPanelOrSnackbar(new ErrorInfo(throwable,
186181
errorUserAction, "Loading more items: " + url, serviceId)));

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,12 @@ public void startLoading(final boolean forceLoad) {
868868
.subscribeOn(Schedulers.io())
869869
.observeOn(AndroidSchedulers.mainThread())
870870
.doOnEvent((searchResult, throwable) -> isLoading.set(false))
871-
.subscribe(result -> {
872-
handleResult(result);
873-
ifMoreItemsLoadableLoadUntilScrollable();
874-
}, this::onItemError);
871+
.subscribe(this::handleResult, this::onItemError);
875872

876873
}
877874

878875
@Override
879-
protected void loadMoreItems(@Nullable final Runnable initialDataLoadCallback) {
876+
protected void loadMoreItems() {
880877
if (!Page.isValid(nextPage)) {
881878
return;
882879
}
@@ -894,12 +891,7 @@ protected void loadMoreItems(@Nullable final Runnable initialDataLoadCallback) {
894891
.subscribeOn(Schedulers.io())
895892
.observeOn(AndroidSchedulers.mainThread())
896893
.doOnEvent((nextItemsResult, throwable) -> isLoading.set(false))
897-
.subscribe(itemsPage -> {
898-
handleNextItems(itemsPage);
899-
if (initialDataLoadCallback != null) {
900-
initialDataLoadCallback.run();
901-
}
902-
}, this::onItemError);
894+
.subscribe(this::handleNextItems, this::onItemError);
903895
}
904896

905897
@Override

0 commit comments

Comments
 (0)