diff options
author | Paul Ganssle <1377457+pganssle@users.noreply.github.com> | 2022-06-09 16:24:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 22:24:22 +0200 |
commit | df53c5bfe57e95211fd6e3f90b4c2480cf5f7bf9 (patch) | |
tree | 5c6ba253d4066cff98f369b46ee4528318c759fd /app | |
parent | fd066a648ba46762267acb9fd1042d1c2f851303 (diff) | |
download | AntennaPod-df53c5bfe57e95211fd6e3f90b4c2480cf5f7bf9.zip |
Convert playback history fragment to lazy loading (#5886)
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java | 4 | ||||
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java | 195 | ||||
-rw-r--r-- | app/src/main/res/layout/list_container_fragment.xml (renamed from app/src/main/res/layout/inbox_fragment.xml) | 4 |
3 files changed, 40 insertions, 163 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java index 067e7466c..9d5376c2a 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java @@ -49,9 +49,9 @@ public class InboxFragment extends EpisodesListFragment implements Toolbar.OnMen @NonNull @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View inboxContainer = View.inflate(getContext(), R.layout.inbox_fragment, null); + View inboxContainer = View.inflate(getContext(), R.layout.list_container_fragment, null); View root = super.onCreateView(inflater, container, savedInstanceState); - ((FrameLayout) inboxContainer.findViewById(R.id.inboxContent)).addView(root); + ((FrameLayout) inboxContainer.findViewById(R.id.listContent)).addView(root); emptyView.setTitle(R.string.no_inbox_head_label); emptyView.setMessage(R.string.no_inbox_label); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java index 857b59967..8bcef4181 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java @@ -1,57 +1,36 @@ package de.danoeh.antennapod.fragment; import android.os.Bundle; -import android.util.Log; import android.view.ContextMenu; -import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.ProgressBar; +import android.widget.FrameLayout; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; -import androidx.fragment.app.Fragment; + import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.adapter.EpisodeItemListAdapter; -import de.danoeh.antennapod.core.event.DownloadEvent; -import de.danoeh.antennapod.core.event.DownloaderUpdate; import de.danoeh.antennapod.core.menuhandler.MenuItemUtils; -import de.danoeh.antennapod.event.FeedItemEvent; import de.danoeh.antennapod.event.playback.PlaybackHistoryEvent; -import de.danoeh.antennapod.event.playback.PlaybackPositionEvent; import de.danoeh.antennapod.event.PlayerStatusEvent; import de.danoeh.antennapod.event.UnreadItemsUpdateEvent; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; -import de.danoeh.antennapod.core.util.FeedItemUtil; -import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler; -import de.danoeh.antennapod.view.EmptyViewHandler; -import de.danoeh.antennapod.view.EpisodeItemListRecyclerView; -import de.danoeh.antennapod.view.viewholder.EpisodeItemViewHolder; -import io.reactivex.Observable; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.Disposable; -import io.reactivex.schedulers.Schedulers; -import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.List; -public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuItemClickListener { +public class PlaybackHistoryFragment extends EpisodesListFragment implements Toolbar.OnMenuItemClickListener { public static final String TAG = "PlaybackHistoryFragment"; private static final String KEY_UP_ARROW = "up_arrow"; - private List<FeedItem> playbackHistory; - private PlaybackHistoryListAdapter adapter; - private Disposable disposable; - private EpisodeItemListRecyclerView recyclerView; - private EmptyViewHandler emptyView; - private ProgressBar progressBar; private Toolbar toolbar; private boolean displayUpArrow; @@ -64,8 +43,12 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View root = inflater.inflate(R.layout.simple_list_fragment, container, false); - toolbar = root.findViewById(R.id.toolbar); + View historyContainer = View.inflate(getContext(), R.layout.list_container_fragment, null); + View root = super.onCreateView(inflater, container, savedInstanceState); + + ((FrameLayout) historyContainer.findViewById(R.id.listContent)).addView(root); + + toolbar = historyContainer.findViewById(R.id.toolbar); toolbar.setTitle(R.string.playback_history_label); toolbar.setOnMenuItemClickListener(this); displayUpArrow = getParentFragmentManager().getBackStackEntryCount() != 0; @@ -76,34 +59,14 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI toolbar.inflateMenu(R.menu.playback_history); refreshToolbarState(); - recyclerView = root.findViewById(R.id.recyclerView); - recyclerView.setRecycledViewPool(((MainActivity) getActivity()).getRecycledViewPool()); - adapter = new PlaybackHistoryListAdapter((MainActivity) getActivity()); - recyclerView.setAdapter(adapter); - progressBar = root.findViewById(R.id.progLoading); + listAdapter = new PlaybackHistoryListAdapter((MainActivity) getActivity()); + recyclerView.setAdapter(listAdapter); - emptyView = new EmptyViewHandler(getActivity()); emptyView.setIcon(R.drawable.ic_history); emptyView.setTitle(R.string.no_history_head_label); emptyView.setMessage(R.string.no_history_label); - emptyView.attachToRecyclerView(recyclerView); - return root; - } - - @Override - public void onStart() { - super.onStart(); - EventBus.getDefault().register(this); - loadItems(); - } - @Override - public void onStop() { - super.onStop(); - EventBus.getDefault().unregister(this); - if (disposable != null) { - disposable.dispose(); - } + return historyContainer; } @Override @@ -112,55 +75,8 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI super.onSaveInstanceState(outState); } - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEventMainThread(FeedItemEvent event) { - Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); - if (playbackHistory == null) { - return; - } else if (adapter == null) { - loadItems(); - return; - } - for (int i = 0, size = event.items.size(); i < size; i++) { - FeedItem item = event.items.get(i); - int pos = FeedItemUtil.indexOfItemWithId(playbackHistory, item.getId()); - if (pos >= 0) { - playbackHistory.remove(pos); - playbackHistory.add(pos, item); - adapter.notifyItemChangedCompat(pos); - } - } - } - - @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) - public void onEventMainThread(DownloadEvent event) { - Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); - DownloaderUpdate update = event.update; - if (adapter != null && update.mediaIds.length > 0) { - for (long mediaId : update.mediaIds) { - int pos = FeedItemUtil.indexOfItemWithMediaId(playbackHistory, mediaId); - if (pos >= 0) { - adapter.notifyItemChangedCompat(pos); - } - } - } - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEventMainThread(PlaybackPositionEvent event) { - if (adapter != null) { - for (int i = 0; i < adapter.getItemCount(); i++) { - EpisodeItemViewHolder holder = (EpisodeItemViewHolder) recyclerView.findViewHolderForAdapterPosition(i); - if (holder != null && holder.isCurrentlyPlayingItem()) { - holder.notifyPlaybackPositionUpdated(event); - break; - } - } - } - } - public void refreshToolbarState() { - boolean hasHistory = playbackHistory != null && !playbackHistory.isEmpty(); + boolean hasHistory = episodes != null && !episodes.isEmpty(); toolbar.getMenu().findItem(R.id.clear_history_item).setVisible(hasHistory); } @@ -173,33 +89,6 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI return false; } - @Override - public boolean onContextItemSelected(@NonNull MenuItem item) { - FeedItem selectedItem = adapter.getLongPressedItem(); - if (selectedItem == null) { - Log.i(TAG, "Selected item at current position was null, ignoring selection"); - return super.onContextItemSelected(item); - } - return FeedItemMenuHandler.onMenuItemClicked(this, item.getItemId(), selectedItem); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onKeyUp(KeyEvent event) { - if (!isAdded() || !isVisible() || !isMenuVisible()) { - return; - } - switch (event.getKeyCode()) { - case KeyEvent.KEYCODE_T: - recyclerView.smoothScrollToPosition(0); - break; - case KeyEvent.KEYCODE_B: - recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1); - break; - default: - break; - } - } - @Subscribe(threadMode = ThreadMode.MAIN) public void onHistoryUpdated(PlaybackHistoryEvent event) { loadItems(); @@ -212,41 +101,20 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI refreshToolbarState(); } + @Override @Subscribe(threadMode = ThreadMode.MAIN) public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { loadItems(); refreshToolbarState(); } - private void onFragmentLoaded() { - adapter.notifyDataSetChanged(); + @Override + protected void onFragmentLoaded(List<FeedItem> episodes) { + super.onFragmentLoaded(episodes); + listAdapter.notifyDataSetChanged(); refreshToolbarState(); } - private void loadItems() { - if (disposable != null) { - disposable.dispose(); - } - progressBar.setVisibility(View.VISIBLE); - emptyView.hide(); - disposable = Observable.fromCallable(this::loadData) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(result -> { - progressBar.setVisibility(View.GONE); - playbackHistory = result; - adapter.updateItems(playbackHistory); - onFragmentLoaded(); - }, error -> Log.e(TAG, Log.getStackTraceString(error))); - } - - @NonNull - private List<FeedItem> loadData() { - List<FeedItem> history = DBReader.getPlaybackHistory(); - DBReader.loadAdditionalFeedItemListData(history); - return history; - } - private class PlaybackHistoryListAdapter extends EpisodeItemListAdapter { public PlaybackHistoryListAdapter(MainActivity mainActivity) { @@ -254,17 +122,26 @@ public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuI } @Override - protected void afterBindViewHolder(EpisodeItemViewHolder holder, int pos) { - // played items shouldn't be transparent for this fragment since, *all* items - // in this fragment will, by definition, be played. So it serves no purpose and can make - // it harder to read. - holder.itemView.setAlpha(1.0f); - } - - @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuItemUtils.setOnClickListeners(menu, PlaybackHistoryFragment.this::onContextItemSelected); } } + + @NonNull + @Override + protected List<FeedItem> loadData() { + return DBReader.getPlaybackHistory(0, page * EPISODES_PER_PAGE); + } + + @NonNull + @Override + protected List<FeedItem> loadMoreData(int page) { + return DBReader.getPlaybackHistory((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE); + } + + @Override + protected int loadTotalItemCount() { + return (int) DBReader.getPlaybackHistoryLength(); + } } diff --git a/app/src/main/res/layout/inbox_fragment.xml b/app/src/main/res/layout/list_container_fragment.xml index fbacf7827..1b6debb13 100644 --- a/app/src/main/res/layout/inbox_fragment.xml +++ b/app/src/main/res/layout/list_container_fragment.xml @@ -9,13 +9,13 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_alignParentTop="true" android:minHeight="?attr/actionBarSize" android:theme="?attr/actionBarTheme" - android:layout_alignParentTop="true" app:title="@string/inbox_label" /> <FrameLayout - android:id="@+id/inboxContent" + android:id="@+id/listContent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar" /> |