From ef50c412c94bcff92ba88149d94e3e2449725cc4 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 18 Feb 2013 19:11:33 +0100 Subject: [PATCH] Implemented drag & drop reordering --- res/layout/organize_queue.xml | 35 +++++++++++ res/layout/organize_queue_listitem.xml | 6 +- res/values/ids.xml | 2 +- .../activity/OrganizeQueueActivity.java | 61 ++++++++++++++++++- .../danoeh/antennapod/feed/FeedManager.java | 34 ++++++++--- .../antennapod/fragment/EpisodesFragment.java | 3 + 6 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 res/layout/organize_queue.xml diff --git a/res/layout/organize_queue.xml b/res/layout/organize_queue.xml new file mode 100644 index 000000000..06da395d9 --- /dev/null +++ b/res/layout/organize_queue.xml @@ -0,0 +1,35 @@ + + + + + + + \ No newline at end of file diff --git a/res/layout/organize_queue_listitem.xml b/res/layout/organize_queue_listitem.xml index e74fffbe0..a109b5784 100644 --- a/res/layout/organize_queue_listitem.xml +++ b/res/layout/organize_queue_listitem.xml @@ -11,7 +11,7 @@ android:scaleType="centerCrop" /> - + \ No newline at end of file diff --git a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java index 8c194f138..a7017d2fb 100644 --- a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java +++ b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java @@ -2,7 +2,10 @@ package de.danoeh.antennapod.activity; import java.util.List; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.res.TypedArray; import android.os.Bundle; import android.view.LayoutInflater; @@ -15,6 +18,7 @@ import android.widget.TextView; import com.actionbarsherlock.app.SherlockListActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; +import com.mobeta.android.dslv.DragSortListView; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; @@ -26,19 +30,73 @@ public class OrganizeQueueActivity extends SherlockListActivity { private static final String TAG = "OrganizeQueueActivity"; private static final int MENU_ID_ACCEPT = 2; - + private OrganizeAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { setTheme(PodcastApp.getThemeResourceId()); super.onCreate(savedInstanceState); + setContentView(R.layout.organize_queue); + + DragSortListView listView = (DragSortListView) getListView(); + listView.setDropListener(dropListener); + listView.setRemoveListener(removeListener); adapter = new OrganizeAdapter(this, 0, FeedManager.getInstance() .getQueue()); setListAdapter(adapter); } + @Override + protected void onPause() { + super.onPause(); + try { + unregisterReceiver(contentUpdate); + } catch (IllegalArgumentException e) { + + } + } + + @Override + protected void onResume() { + super.onResume(); + IntentFilter filter = new IntentFilter(FeedManager.ACTION_QUEUE_UPDATE); + filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE); + registerReceiver(contentUpdate, filter); + } + + private BroadcastReceiver contentUpdate = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + if (adapter != null) { + adapter.notifyDataSetChanged(); + } + } + + }; + + private DragSortListView.DropListener dropListener = new DragSortListView.DropListener() { + + @Override + public void drop(int from, int to) { + FeedManager manager = FeedManager.getInstance(); + manager.moveQueueItem(OrganizeQueueActivity.this, from, to, false); + adapter.notifyDataSetChanged(); + } + }; + + private DragSortListView.RemoveListener removeListener = new DragSortListView.RemoveListener() { + + @Override + public void remove(int which) { + FeedManager manager = FeedManager.getInstance(); + manager.removeQueueItem(OrganizeQueueActivity.this, + (FeedItem) getListAdapter().getItem(which)); + } + }; + @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); @@ -112,7 +170,6 @@ public class OrganizeQueueActivity extends SherlockListActivity { TextView title; TextView feedTitle; ImageView feedImage; - View dragHandle; } } diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index 3e5e838b0..6c3471e5c 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -765,16 +765,29 @@ public class FeedManager { } } - public void moveQueueItem(final Context context, FeedItem item, int delta) { + /** + * Moves the queue item at the specified index to another position. If the + * indices are out of range, no operation will be performed. + * + * @param from + * index of the item that is going to be moved + * @param to + * destination index of item + * @param broadcastUpdate + * true if the method should send a queue update broadcast after + * the operation has been performed. This should be set to false + * if the order of the queue is changed through drag & drop + * reordering to avoid visual glitches. + */ + public void moveQueueItem(final Context context, int from, int to, + boolean broadcastUpdate) { if (AppConfig.DEBUG) - Log.d(TAG, "Moving queue item"); - int itemIndex = queue.indexOf(item); - int newIndex = itemIndex + delta; - if (newIndex >= 0 && newIndex < queue.size()) { - FeedItem oldItem = queue.set(newIndex, item); - queue.set(itemIndex, oldItem); + Log.d(TAG, "Moving queue item from index " + from + " to index " + + to); + if (from >= 0 && from < queue.size() && to >= 0 && to < queue.size()) { + FeedItem item = queue.remove(from); + queue.add(to, item); dbExec.execute(new Runnable() { - @Override public void run() { PodDBAdapter adapter = new PodDBAdapter(context); @@ -783,9 +796,10 @@ public class FeedManager { adapter.close(); } }); - + if (broadcastUpdate) { + sendQueueUpdateBroadcast(context, item); + } } - sendQueueUpdateBroadcast(context, item); } public boolean isInQueue(FeedItem item) { diff --git a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java index 967cfd2bc..843cf5af0 100644 --- a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java +++ b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java @@ -63,6 +63,9 @@ public class EpisodesFragment extends SherlockFragment { filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE); getActivity().registerReceiver(contentUpdate, filter); + if (adapter != null) { + adapter.notifyDataSetChanged(); + } } @Override