Items in queue can now be reordered

This commit is contained in:
daniel oeh 2012-07-05 11:03:35 +02:00
parent 5920c96ae7
commit c0c9e2fed2
5 changed files with 90 additions and 37 deletions

View File

@ -1,6 +1,8 @@
<resources>
<item type="id" name="action_bar_refresh" />
<item type="id" name="action_bar_add" />
<item name="action_bar_refresh" type="id"/>
<item name="action_bar_add" type="id"/>
<item name="move_up_item" type="id"/>
<item name="move_down_item" type="id"/>
</resources>
</resources>

View File

@ -73,5 +73,7 @@
<string name="playback_error_unknown">Unknown Error</string>
<string name="author_label">Author</string>
<string name="language_label">Language</string>
<string name="move_up_label">Move up</string>
<string name="move_down_label">Move down</string>
</resources>

View File

@ -214,6 +214,20 @@ public class FeedManager {
adapter.close();
}
}
public void moveQueueItem(Context context, FeedItem item, int delta) {
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);
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
adapter.setQueue(queue);
adapter.close();
}
}
public boolean isInQueue(FeedItem item) {
return queue.contains(item);

View File

@ -28,7 +28,8 @@ import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.util.FeedItemMenuHandler;
/** Displays a list of FeedItems. */
public class ItemlistFragment extends SherlockListFragment {
public class ItemlistFragment extends SherlockListFragment implements
ActionMode.Callback {
private static final String TAG = "FeedItemlistFragment";
public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.podfetcher.activity.selected_feeditem";
@ -42,7 +43,7 @@ public class ItemlistFragment extends SherlockListFragment {
protected FeedItem selectedItem;
protected ActionMode mActionMode;
/** Argument for FeeditemlistAdapter */
protected boolean showFeedtitle;
@ -57,9 +58,12 @@ public class ItemlistFragment extends SherlockListFragment {
public ItemlistFragment() {
}
/** Creates new ItemlistFragment which shows the Feeditems of a specific feed.
* Sets 'showFeedtitle' to false
* @param feedId The id of the feed to show
/**
* Creates new ItemlistFragment which shows the Feeditems of a specific
* feed. Sets 'showFeedtitle' to false
*
* @param feedId
* The id of the feed to show
* @return the newly created instance of an ItemlistFragment
*/
public static ItemlistFragment newInstance(long feedId) {
@ -70,7 +74,7 @@ public class ItemlistFragment extends SherlockListFragment {
i.setArguments(b);
return i;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -91,7 +95,7 @@ public class ItemlistFragment extends SherlockListFragment {
mActionMode.finish();
}
}
@Override
public void onResume() {
super.onResume();
@ -103,7 +107,6 @@ public class ItemlistFragment extends SherlockListFragment {
getActivity().registerReceiver(contentUpdate, filter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FeedItem selection = fila.getItem(position);
@ -114,7 +117,7 @@ public class ItemlistFragment extends SherlockListFragment {
startActivity(showItem);
}
private BroadcastReceiver contentUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -136,7 +139,7 @@ public class ItemlistFragment extends SherlockListFragment {
selectedItem = newSelectedItem;
mActionMode = getSherlockActivity().startActionMode(
mActionModeCallback);
ItemlistFragment.this);
} else {
mActionMode.finish();
}
@ -149,39 +152,38 @@ public class ItemlistFragment extends SherlockListFragment {
public void onViewCreated(View view, Bundle savedInstanceState) {
this.getListView().setItemsCanFocus(true);
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return FeedItemMenuHandler.onPrepareMenu(menu, selectedItem);
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return FeedItemMenuHandler.onPrepareMenu(menu, selectedItem);
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
selectedItem = null;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
selectedItem = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(selectedItem.getTitle());
return FeedItemMenuHandler.onCreateMenu(mode.getMenuInflater(), menu);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(selectedItem.getTitle());
return FeedItemMenuHandler.onCreateMenu(mode.getMenuInflater(), menu);
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
FeedItemMenuHandler.onMenuItemClicked(getSherlockActivity(), item, selectedItem);
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean handled = FeedItemMenuHandler.onMenuItemClicked(
getSherlockActivity(), item, selectedItem);
if (handled) {
fila.notifyDataSetChanged();
mode.finish();
return true;
}
};
mode.finish();
return handled;
}
public FeedItemlistAdapter getListAdapter() {
return fila;
}
}

View File

@ -1,5 +1,10 @@
package de.podfetcher.fragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import de.podfetcher.R;
import de.podfetcher.feed.FeedManager;
public class QueueFragment extends ItemlistFragment {
@ -8,4 +13,32 @@ public class QueueFragment extends ItemlistFragment {
super(FeedManager.getInstance().getQueue(), true);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
super.onCreateActionMode(mode, menu);
menu.add(Menu.NONE, R.id.move_up_item, Menu.NONE, R.string.move_up_label);
menu.add(Menu.NONE, R.id.move_down_item, Menu.NONE, R.string.move_down_label);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean handled = false;
switch(item.getItemId()) {
case R.id.move_up_item:
manager.moveQueueItem(getActivity(), selectedItem, -1);
handled = true;
break;
case R.id.move_down_item:
manager.moveQueueItem(getActivity(), selectedItem, 1);
handled = true;
break;
default:
handled = super.onActionItemClicked(mode, item);
}
fila.notifyDataSetChanged();
mode.finish();
return handled;
}
}