From a9f566f3f104e37eb160a0dcb9dcb11f8879e110 Mon Sep 17 00:00:00 2001 From: Matthew Gaffen Date: Fri, 16 Mar 2018 11:01:23 +0000 Subject: [PATCH] Only write queue order to database on drag release for the podcast queue. This prevents large numbers of db writes in quick succession. This is a particular problem for large queue lengths. Fixes #2562 and potentially fixed #2573 --- .../antennapod/fragment/QueueFragment.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index bf22e71f5..7ec96dbbe 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -389,17 +389,29 @@ public class QueueFragment extends Fragment { itemTouchHelper = new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT) { + // Position tracking whilst dragging + int dragFrom = -1; + int dragTo = -1; + @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { + int fromPosition = viewHolder.getAdapterPosition(); + int toPosition = target.getAdapterPosition(); + + // Update tracked position + if(dragFrom == -1) { + dragFrom = fromPosition; + } + dragTo = toPosition; + int from = viewHolder.getAdapterPosition(); int to = target.getAdapterPosition(); - Log.d(TAG, "move(" + from + ", " + to + ")"); + Log.d(TAG, "move(" + from + ", " + to + ") in memory"); if(from >= queue.size() || to >= queue.size()) { return false; } queue.add(to, queue.remove(from)); recyclerAdapter.notifyItemMoved(from, to); - DBWriter.moveQueueItem(from, to, true); return true; } @@ -453,12 +465,25 @@ public class QueueFragment extends Fragment { RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); + // Check if drag finished + if(dragFrom != -1 && dragTo != -1 && dragFrom != dragTo) { + reallyMoved(dragFrom, dragTo); + } + + dragFrom = dragTo = -1; + if (viewHolder instanceof QueueRecyclerAdapter.ItemTouchHelperViewHolder) { QueueRecyclerAdapter.ItemTouchHelperViewHolder itemViewHolder = (QueueRecyclerAdapter.ItemTouchHelperViewHolder) viewHolder; itemViewHolder.onItemClear(); } } + + private void reallyMoved(int from, int to) { + // Write drag operation to database + Log.d(TAG, "Write to database move(" + dragFrom + ", " + dragTo + ")"); + DBWriter.moveQueueItem(dragFrom, dragTo, true); + } } ); itemTouchHelper.attachToRecyclerView(recyclerView);