From 3c3fe7bf836552b6d2529cc6d6463041c078125a Mon Sep 17 00:00:00 2001 From: John Zhen Mo Date: Wed, 31 Jan 2018 11:51:47 -0800 Subject: [PATCH] -Fixed database updates cause outdated record to overwrite reordered local playlist when fragment is active. -Fixed save on exit causes empty list being saved after orientation changes on older devices. -Fixed NPE on animating garbage collected views on local item fragments. -Reduced drag speed from 15 to 12 items per second. --- .../playlist/dao/PlaylistStreamDAO.java | 4 +- .../local/BaseLocalListFragment.java | 6 +- .../local/LocalPlaylistFragment.java | 156 +++++++++++------- 3 files changed, 100 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java index dd2994d29..8bf1ea696 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java @@ -47,8 +47,8 @@ public abstract class PlaylistStreamDAO implements BasicDAO saveImmediate()); + .subscribe(ignored -> saveImmediate(), this::onError); } private void saveImmediate() { + // List must be loaded and modified in order to save + if (isLoadingComplete == null || isModified == null || + !isLoadingComplete.get() || !isModified.get()) { + Log.w(TAG, "Attempting to save playlist when local playlist " + + "is not loaded or not modified: playlist id=[" + playlistId + "]"); + return; + } + final List items = itemListAdapter.getItemsList(); List streamIds = new ArrayList<>(items.size()); for (final LocalItem item : items) { @@ -386,12 +416,60 @@ public class LocalPlaylistFragment extends BaseLocalListFragment {/*Do nothing on success*/}, this::onError); + .subscribe( + () -> { if (isModified != null) isModified.set(false); }, + this::onError + ); + } + + + private ItemTouchHelper.SimpleCallback getItemTouchCallback() { + return new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, + ItemTouchHelper.ACTION_STATE_IDLE) { + @Override + public int interpolateOutOfBoundsScroll(RecyclerView recyclerView, int viewSize, + int viewSizeOutOfBounds, int totalSize, + long msSinceStartScroll) { + final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize, + viewSizeOutOfBounds, totalSize, msSinceStartScroll); + final int minimumAbsVelocity = Math.max(MINIMUM_INITIAL_DRAG_VELOCITY, + Math.abs(standardSpeed)); + return minimumAbsVelocity * (int) Math.signum(viewSizeOutOfBounds); + } + + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, + RecyclerView.ViewHolder target) { + if (source.getItemViewType() != target.getItemViewType() || + itemListAdapter == null) { + return false; + } + + final int sourceIndex = source.getAdapterPosition(); + final int targetIndex = target.getAdapterPosition(); + final boolean isSwapped = itemListAdapter.swapItems(sourceIndex, targetIndex); + if (isSwapped) saveChanges(); + return isSwapped; + } + + @Override + public boolean isLongPressDragEnabled() { + return false; + } + + @Override + public boolean isItemViewSwipeEnabled() { + return false; + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {} + }; } /*////////////////////////////////////////////////////////////////////////// @@ -449,50 +527,6 @@ public class LocalPlaylistFragment extends BaseLocalListFragment