Removed unnecessary ItemAccess

This commit is contained in:
ByteHamster 2020-02-05 11:48:49 +01:00
parent 3ca6be8b04
commit 5a26207fdb
2 changed files with 19 additions and 52 deletions

View File

@ -11,13 +11,15 @@ import androidx.recyclerview.widget.RecyclerView;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.fragment.ItemPagerFragment; import de.danoeh.antennapod.fragment.ItemPagerFragment;
import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler; import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler;
import de.danoeh.antennapod.view.EpisodeItemViewHolder; import de.danoeh.antennapod.view.EpisodeItemViewHolder;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
/** /**
* List adapter for the list of new episodes. * List adapter for the list of new episodes.
@ -26,14 +28,18 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<EpisodeItemV
implements View.OnCreateContextMenuListener { implements View.OnCreateContextMenuListener {
private final WeakReference<MainActivity> mainActivityRef; private final WeakReference<MainActivity> mainActivityRef;
private final ItemAccess itemAccess; private List<FeedItem> episodes = new ArrayList<>();
private FeedItem selectedItem; private FeedItem selectedItem;
public AllEpisodesRecycleAdapter(MainActivity mainActivity, ItemAccess itemAccess) { public AllEpisodesRecycleAdapter(MainActivity mainActivity) {
super(); super();
this.mainActivityRef = new WeakReference<>(mainActivity); this.mainActivityRef = new WeakReference<>(mainActivity);
this.itemAccess = itemAccess; }
public void updateItems(List<FeedItem> items) {
episodes = items;
notifyDataSetChanged();
} }
@NonNull @NonNull
@ -46,7 +52,7 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<EpisodeItemV
@Override @Override
public void onBindViewHolder(EpisodeItemViewHolder holder, int pos) { public void onBindViewHolder(EpisodeItemViewHolder holder, int pos) {
FeedItem item = itemAccess.getItem(pos); FeedItem item = episodes.get(pos);
holder.bind(item); holder.bind(item);
holder.itemView.setOnLongClickListener(v -> { holder.itemView.setOnLongClickListener(v -> {
selectedItem = item; selectedItem = item;
@ -55,7 +61,7 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<EpisodeItemV
holder.itemView.setOnClickListener(v -> { holder.itemView.setOnClickListener(v -> {
MainActivity activity = mainActivityRef.get(); MainActivity activity = mainActivityRef.get();
if (activity != null) { if (activity != null) {
long[] ids = itemAccess.getQueueIds().toArray(); long[] ids = FeedItemUtil.getIds(episodes);
int position = ArrayUtils.indexOf(ids, item.getId()); int position = ArrayUtils.indexOf(ids, item.getId());
activity.loadChildFragment(ItemPagerFragment.newInstance(ids, position)); activity.loadChildFragment(ItemPagerFragment.newInstance(ids, position));
} }
@ -71,13 +77,13 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<EpisodeItemV
@Override @Override
public long getItemId(int position) { public long getItemId(int position) {
FeedItem item = itemAccess.getItem(position); FeedItem item = episodes.get(position);
return item != null ? item.getId() : RecyclerView.NO_POSITION; return item != null ? item.getId() : RecyclerView.NO_POSITION;
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return itemAccess.getCount(); return episodes.size();
} }
@Override @Override
@ -88,16 +94,6 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<EpisodeItemV
FeedItemMenuHandler.onPrepareMenu(menu, selectedItem, R.id.skip_episode_item); FeedItemMenuHandler.onPrepareMenu(menu, selectedItem, R.id.skip_episode_item);
} }
public interface ItemAccess {
int getCount();
FeedItem getItem(int position);
LongList getQueueIds();
}
/** /**
* Notifies a View Holder of relevant callbacks from * Notifies a View Holder of relevant callbacks from
* {@link ItemTouchHelper.Callback}. * {@link ItemTouchHelper.Callback}.

View File

@ -83,8 +83,6 @@ public abstract class EpisodesListFragment extends Fragment {
@NonNull @NonNull
List<FeedItem> episodes = new ArrayList<>(); List<FeedItem> episodes = new ArrayList<>();
@NonNull
private List<Downloader> downloaderList = new ArrayList<>();
private boolean isUpdatingFeeds; private boolean isUpdatingFeeds;
boolean isMenuInvalidationAllowed = false; boolean isMenuInvalidationAllowed = false;
@ -344,10 +342,10 @@ public abstract class EpisodesListFragment extends Fragment {
} }
protected void onFragmentLoaded(List<FeedItem> episodes) { protected void onFragmentLoaded(List<FeedItem> episodes) {
listAdapter.notifyDataSetChanged();
if (episodes.size() == 0) { if (episodes.size() == 0) {
createRecycleAdapter(recyclerView, emptyView); createRecycleAdapter(recyclerView, emptyView);
} else {
listAdapter.updateItems(episodes);
} }
restoreScrollPosition(); restoreScrollPosition();
@ -360,40 +358,13 @@ public abstract class EpisodesListFragment extends Fragment {
*/ */
private void createRecycleAdapter(RecyclerView recyclerView, EmptyViewHandler emptyViewHandler) { private void createRecycleAdapter(RecyclerView recyclerView, EmptyViewHandler emptyViewHandler) {
MainActivity mainActivity = (MainActivity) getActivity(); MainActivity mainActivity = (MainActivity) getActivity();
listAdapter = new AllEpisodesRecycleAdapter(mainActivity, itemAccess); listAdapter = new AllEpisodesRecycleAdapter(mainActivity);
listAdapter.setHasStableIds(true); listAdapter.setHasStableIds(true);
listAdapter.updateItems(episodes);
recyclerView.setAdapter(listAdapter); recyclerView.setAdapter(listAdapter);
emptyViewHandler.updateAdapter(listAdapter); emptyViewHandler.updateAdapter(listAdapter);
} }
private final AllEpisodesRecycleAdapter.ItemAccess itemAccess = new AllEpisodesRecycleAdapter.ItemAccess() {
@Override
public int getCount() {
return episodes.size();
}
@Override
public FeedItem getItem(int position) {
if (0 <= position && position < episodes.size()) {
return episodes.get(position);
}
return null;
}
@Override
public LongList getQueueIds() {
LongList queueIds = new LongList();
for (FeedItem item : episodes) {
if (item.isTagged(FeedItem.TAG_QUEUE)) {
queueIds.add(item.getId());
}
}
return queueIds;
}
};
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(FeedItemEvent event) { public void onEventMainThread(FeedItemEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
@ -432,7 +403,7 @@ public abstract class EpisodesListFragment extends Fragment {
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update; DownloaderUpdate update = event.update;
downloaderList = update.downloaders; List<Downloader> downloaderList = update.downloaders;
if (isMenuInvalidationAllowed && event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) { if (isMenuInvalidationAllowed && event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
requireActivity().invalidateOptionsMenu(); requireActivity().invalidateOptionsMenu();
} }