diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-04-13 19:23:56 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-04-13 19:23:56 +0200 |
commit | b22c8be8459467cde8ac4e18fb9a06f8cd15878a (patch) | |
tree | 669a6fb2d27e893bc2e7bec6a1973456c1b4aa2a /src/de/danoeh/antennapod/fragment | |
parent | 3f1307c4d9d028ba058fb696153a470f8b9f4e51 (diff) | |
download | AntennaPod-b22c8be8459467cde8ac4e18fb9a06f8cd15878a.zip |
Added playback history fragment
Diffstat (limited to 'src/de/danoeh/antennapod/fragment')
-rw-r--r-- | src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java | 215 |
1 files changed, 135 insertions, 80 deletions
diff --git a/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java b/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java index d6524853f..1e4140b7b 100644 --- a/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java +++ b/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java @@ -1,112 +1,167 @@ package de.danoeh.antennapod.fragment; +import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; +import android.support.v4.app.ListFragment; +import android.view.View; +import de.danoeh.antennapod.adapter.ActionButtonCallback; import de.danoeh.antennapod.adapter.InternalFeedItemlistAdapter; import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.storage.DBReader; +import de.danoeh.antennapod.util.QueueAccess; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; -public class PlaybackHistoryFragment extends ItemlistFragment { - private static final String TAG = "PlaybackHistoryFragment"; +public class PlaybackHistoryFragment extends ListFragment { + private static final String TAG = "PlaybackHistoryFragment"; private List<FeedItem> playbackHistory; + private QueueAccess queue; + private InternalFeedItemlistAdapter adapter; - public PlaybackHistoryFragment() { - super(true); - } + private boolean itemsLoaded = false; + private boolean viewsCreated = false; + + private AtomicReference<Activity> activity = new AtomicReference<Activity>(); - InternalFeedItemlistAdapter.ItemAccess itemAccessRef; @Override - protected InternalFeedItemlistAdapter.ItemAccess itemAccess() { - if (itemAccessRef == null) { - itemAccessRef = new InternalFeedItemlistAdapter.ItemAccess() { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setRetainInstance(true); - @Override - public FeedItem getItem(int position) { - return (playbackHistory != null) ? playbackHistory.get(position) : null; - } + startItemLoader(); + } - @Override - public int getCount() { - return (playbackHistory != null) ? playbackHistory.size() : 0; - } + @Override + public void onStart() { + super.onStart(); + EventDistributor.getInstance().register(contentUpdate); + } - @Override - public boolean isInQueue(FeedItem item) { - return (queue != null) ? queue.contains(item.getId()) : false; - } - }; - } - return itemAccessRef; + @Override + public void onStop() { + super.onStop(); + EventDistributor.getInstance().unregister(contentUpdate); + stopItemLoader(); } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - EventDistributor.getInstance().register(historyUpdate); - } - - @Override - public void onDestroy() { - super.onDestroy(); - EventDistributor.getInstance().unregister(historyUpdate); - } - - private EventDistributor.EventListener historyUpdate = new EventDistributor.EventListener() { - - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((EventDistributor.PLAYBACK_HISTORY_UPDATE & arg) != 0) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Received content update"); - loadData(); - } - - } - }; + public void onDetach() { + super.onDetach(); + stopItemLoader(); + adapter = null; + viewsCreated = false; + activity.set(null); + } @Override - protected void loadData() { - AsyncTask<Void, Void, Void> loadTask = new AsyncTask<Void, Void, Void>() { - private volatile List<FeedItem> phRef; - private volatile List<Long> queueRef; - - @Override - protected Void doInBackground(Void... voids) { - Context context = PlaybackHistoryFragment.this.getActivity(); - if (context != null) { - queueRef = DBReader.getQueueIDList(context); - phRef = DBReader.getPlaybackHistory(context); - } + public void onAttach(Activity activity) { + super.onAttach(activity); + this.activity.set(activity); + if (viewsCreated && itemsLoaded) { + onFragmentLoaded(); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + viewsCreated = true; + if (itemsLoaded) { + onFragmentLoaded(); + } + } + + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { + + @Override + public void update(EventDistributor eventDistributor, Integer arg) { + if ((arg & EventDistributor.PLAYBACK_HISTORY_UPDATE) != 0) { + startItemLoader(); + } + } + }; + + private void onFragmentLoaded() { + if (adapter == null) { + adapter = new InternalFeedItemlistAdapter(getActivity(), itemAccess, actionButtonCallback, true); + setListAdapter(adapter); + } + setListShown(true); + adapter.notifyDataSetChanged(); + + } + + private InternalFeedItemlistAdapter.ItemAccess itemAccess = new InternalFeedItemlistAdapter.ItemAccess() { + @Override + public boolean isInQueue(FeedItem item) { + return (queue != null) ? queue.contains(item.getId()) : false; + } + + @Override + public int getCount() { + return (playbackHistory != null) ? playbackHistory.size() : 0; + } + + @Override + public FeedItem getItem(int position) { + return (playbackHistory != null) ? playbackHistory.get(position) : null; + } + }; + + private ActionButtonCallback actionButtonCallback = new ActionButtonCallback() { + @Override + public void onActionButtonPressed(FeedItem item) { + + } + }; + + private ItemLoader itemLoader; + + private void startItemLoader() { + if (itemLoader != null) { + itemLoader.cancel(true); + } + itemLoader = new ItemLoader(); + itemLoader.execute(); + } + + private void stopItemLoader() { + if (itemLoader != null) { + itemLoader.cancel(true); + } + } + + private class ItemLoader extends AsyncTask<Void, Void, Object[]> { + + @Override + protected Object[] doInBackground(Void... params) { + Context context = activity.get(); + if (context != null) { + List<FeedItem> ph = DBReader.getPlaybackHistory(context); + DBReader.loadFeedDataOfFeedItemlist(context, ph); + return new Object[]{ph, + QueueAccess.IDListAccess(DBReader.getQueueIDList(context))}; + } else { return null; } + } - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if (queueRef != null && phRef != null) { - queue = queueRef; - playbackHistory = phRef; - Log.i(TAG, "Number of items in playback history: " + playbackHistory.size()); - if (fila != null) { - fila.notifyDataSetChanged(); - } - } else { - if (queueRef == null) { - Log.e(TAG, "Could not load queue"); - } - if (phRef == null) { - Log.e(TAG, "Could not load playback history"); - } + @Override + protected void onPostExecute(Object[] res) { + super.onPostExecute(res); + if (res != null) { + playbackHistory = (List<FeedItem>) res[0]; + queue = (QueueAccess) res[1]; + itemsLoaded = true; + if (viewsCreated) { + onFragmentLoaded(); } } - }; - loadTask.execute(); + } } } |