Show correct total number for lazy loaded items

This commit is contained in:
ByteHamster 2022-05-08 10:55:05 +02:00
parent 7c0d084ffc
commit 03220d0774
9 changed files with 72 additions and 5 deletions

View File

@ -195,8 +195,8 @@ public class EpisodeItemListAdapter extends SelectableAdapter<EpisodeItemViewHol
setSelected(0, longPressedPosition, true);
return true;
} else if (item.getItemId() == R.id.select_all_below) {
setSelected(longPressedPosition + 1, getItemCount(), true);
shouldSelectLazyLoadedItems = true;
setSelected(longPressedPosition + 1, getItemCount(), true);
return true;
}
return false;

View File

@ -15,12 +15,14 @@ import java.util.HashSet;
/**
* Used by Recyclerviews that need to provide ability to select items.
*/
abstract class SelectableAdapter<T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> {
public abstract class SelectableAdapter<T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> {
public static final int COUNT_AUTOMATICALLY = -1;
private ActionMode actionMode;
private final HashSet<Long> selectedIds = new HashSet<>();
private final Activity activity;
private OnSelectModeListener onSelectModeListener;
boolean shouldSelectLazyLoadedItems = false;
private int totalNumberOfItems = COUNT_AUTOMATICALLY;
public SelectableAdapter(Activity activity) {
this.activity = activity;
@ -155,9 +157,17 @@ abstract class SelectableAdapter<T extends RecyclerView.ViewHolder> extends Recy
if (actionMode == null) {
return;
}
int totalCount = getItemCount();
int selectedCount = selectedIds.size();
if (totalNumberOfItems != COUNT_AUTOMATICALLY) {
totalCount = totalNumberOfItems;
if (shouldSelectLazyLoadedItems) {
selectedCount += (totalNumberOfItems - getItemCount());
}
}
actionMode.setTitle(activity.getResources()
.getQuantityString(R.plurals.num_selected_label, selectedIds.size(),
selectedIds.size(), getItemCount()));
selectedCount, totalCount));
}
public void setOnSelectModeListener(OnSelectModeListener onSelectModeListener) {
@ -174,6 +184,14 @@ abstract class SelectableAdapter<T extends RecyclerView.ViewHolder> extends Recy
return shouldSelectLazyLoadedItems;
}
/**
* Sets the total number of items that could be lazy-loaded.
* Can also be set to {@link #COUNT_AUTOMATICALLY} to simply use {@link #getItemCount}
*/
public void setTotalNumberOfItems(int totalNumberOfItems) {
this.totalNumberOfItems = totalNumberOfItems;
}
public interface OnSelectModeListener {
void onStartSelectMode();

View File

@ -105,4 +105,9 @@ public class AllEpisodesFragment extends EpisodesListFragment {
protected List<FeedItem> loadMoreData() {
return DBReader.getRecentlyPublishedEpisodes((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, feedItemFilter);
}
@Override
protected int loadTotalItemCount() {
return DBReader.getTotalEpisodeCount(feedItemFilter);
}
}

View File

@ -5,6 +5,7 @@ import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
@ -25,6 +26,7 @@ import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
import com.leinardi.android.speeddial.SpeedDialView;
import de.danoeh.antennapod.adapter.EpisodeItemListAdapter;
import de.danoeh.antennapod.adapter.SelectableAdapter;
import de.danoeh.antennapod.event.FeedListUpdateEvent;
import de.danoeh.antennapod.event.playback.PlaybackPositionEvent;
import de.danoeh.antennapod.event.PlayerStatusEvent;
@ -447,14 +449,15 @@ public abstract class EpisodesListFragment extends Fragment implements EpisodeIt
if (disposable != null) {
disposable.dispose();
}
disposable = Observable.fromCallable(this::loadData)
disposable = Observable.fromCallable(() -> new Pair<>(loadData(), loadTotalItemCount()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
progLoading.setVisibility(View.GONE);
loadingMoreView.setVisibility(View.GONE);
hasMoreItems = true;
episodes = data;
episodes = data.first;
listAdapter.setTotalNumberOfItems(data.second);
onFragmentLoaded(episodes);
if (getParentFragment() instanceof PagedToolbarFragment) {
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
@ -475,4 +478,11 @@ public abstract class EpisodesListFragment extends Fragment implements EpisodeIt
*/
@NonNull
protected abstract List<FeedItem> loadMoreData();
/**
* Returns the total number of items that would be returned if {@link #loadMoreData} was called often enough.
*/
protected int loadTotalItemCount() {
return SelectableAdapter.COUNT_AUTOMATICALLY;
}
}

View File

@ -12,6 +12,7 @@ import android.view.View;
import android.view.ViewGroup;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.model.feed.FeedItemFilter;
import de.danoeh.antennapod.view.viewholder.EpisodeItemViewHolder;
import org.greenrobot.eventbus.Subscribe;
@ -101,4 +102,9 @@ public class FavoriteEpisodesFragment extends EpisodesListFragment {
protected List<FeedItem> loadMoreData() {
return DBReader.getFavoriteItemsList((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE);
}
@Override
protected int loadTotalItemCount() {
return DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.IS_FAVORITE));
}
}

View File

@ -119,4 +119,9 @@ public class InboxFragment extends EpisodesListFragment implements Toolbar.OnMen
protected List<FeedItem> loadMoreData() {
return DBReader.getNewItemsList((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE);
}
@Override
protected int loadTotalItemCount() {
return DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.NEW));
}
}

View File

@ -402,6 +402,19 @@ public final class DBReader {
}
}
public static int getTotalEpisodeCount(FeedItemFilter filter) {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
try (Cursor cursor = adapter.getTotalEpisodeCountCursor(filter)) {
if (cursor.moveToFirst()) {
return cursor.getInt(0);
}
return -1;
} finally {
adapter.close();
}
}
/**
* Loads the playback history from the database. A FeedItem is in the playback history if playback of the correpsonding episode
* has been completed at least once.

View File

@ -1052,6 +1052,14 @@ public class PodDBAdapter {
return db.rawQuery(query, null);
}
public final Cursor getTotalEpisodeCountCursor(FeedItemFilter filter) {
String filterQuery = FeedItemFilterQuery.generateFrom(filter);
String whereClause = "".equals(filterQuery) ? "" : " WHERE " + filterQuery;
final String query = "SELECT count(" + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + ") FROM " + TABLE_NAME_FEED_ITEMS
+ JOIN_FEED_ITEM_AND_MEDIA + whereClause;
return db.rawQuery(query, null);
}
public Cursor getDownloadedItemsCursor() {
final String query = SELECT_FEED_ITEMS_AND_MEDIA
+ "WHERE " + TABLE_NAME_FEED_MEDIA + "." + KEY_DOWNLOADED + " > 0";

View File

@ -34,6 +34,8 @@ public class FeedItemFilterQuery {
statements.add(keyRead + " = 1 ");
} else if (filter.showUnplayed) {
statements.add(" NOT " + keyRead + " = 1 "); // Match "New" items (read = -1) as well
} else if (filter.showNew) {
statements.add(keyRead + " = -1 ");
}
if (filter.showPaused) {
statements.add(" (" + keyPosition + " NOT NULL AND " + keyPosition + " > 0 " + ") ");