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

View File

@ -83,8 +83,6 @@ public abstract class EpisodesListFragment extends Fragment {
@NonNull
List<FeedItem> episodes = new ArrayList<>();
@NonNull
private List<Downloader> downloaderList = new ArrayList<>();
private boolean isUpdatingFeeds;
boolean isMenuInvalidationAllowed = false;
@ -344,10 +342,10 @@ public abstract class EpisodesListFragment extends Fragment {
}
protected void onFragmentLoaded(List<FeedItem> episodes) {
listAdapter.notifyDataSetChanged();
if (episodes.size() == 0) {
createRecycleAdapter(recyclerView, emptyView);
} else {
listAdapter.updateItems(episodes);
}
restoreScrollPosition();
@ -360,40 +358,13 @@ public abstract class EpisodesListFragment extends Fragment {
*/
private void createRecycleAdapter(RecyclerView recyclerView, EmptyViewHandler emptyViewHandler) {
MainActivity mainActivity = (MainActivity) getActivity();
listAdapter = new AllEpisodesRecycleAdapter(mainActivity, itemAccess);
listAdapter = new AllEpisodesRecycleAdapter(mainActivity);
listAdapter.setHasStableIds(true);
listAdapter.updateItems(episodes);
recyclerView.setAdapter(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)
public void onEventMainThread(FeedItemEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
@ -432,7 +403,7 @@ public abstract class EpisodesListFragment extends Fragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update;
downloaderList = update.downloaders;
List<Downloader> downloaderList = update.downloaders;
if (isMenuInvalidationAllowed && event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
requireActivity().invalidateOptionsMenu();
}