aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2015-09-16 07:49:10 -0700
committerScott Jackson <daneren2005@gmail.com>2015-09-16 07:49:10 -0700
commitfa285723cd21e4799990bef0726d71ef96001e5c (patch)
tree5dc8c64d7720163609cf6d9f9115930e103a3566 /app
parent7df08f3d1ac3d098260a99871b1b03b273111bc0 (diff)
downloaddsub-fa285723cd21e4799990bef0726d71ef96001e5c.tar.gz
dsub-fa285723cd21e4799990bef0726d71ef96001e5c.tar.bz2
dsub-fa285723cd21e4799990bef0726d71ef96001e5c.zip
Fix for issues with drag/drop
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/util/DownloadFileItemHelperCallback.java112
1 files changed, 112 insertions, 0 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/util/DownloadFileItemHelperCallback.java b/app/src/main/java/github/daneren2005/dsub/util/DownloadFileItemHelperCallback.java
new file mode 100644
index 00000000..4074fece
--- /dev/null
+++ b/app/src/main/java/github/daneren2005/dsub/util/DownloadFileItemHelperCallback.java
@@ -0,0 +1,112 @@
+package github.daneren2005.dsub.util;
+
+import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.helper.ItemTouchHelper;
+
+import org.eclipse.jetty.util.ArrayQueue;
+
+import java.util.Queue;
+
+import github.daneren2005.dsub.adapter.SectionAdapter;
+import github.daneren2005.dsub.fragments.SubsonicFragment;
+import github.daneren2005.dsub.service.DownloadFile;
+import github.daneren2005.dsub.service.DownloadService;
+import github.daneren2005.dsub.view.SongView;
+import github.daneren2005.dsub.view.UpdateView;
+
+public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallback {
+ private SubsonicFragment fragment;
+ private boolean mainList;
+
+ private BackgroundTask pendingTask = null;
+ private Queue pendingOperations = new ArrayQueue();
+
+ public DownloadFileItemHelperCallback(SubsonicFragment fragment, boolean mainList) {
+ super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
+ this.fragment = fragment;
+ this.mainList = mainList;
+ }
+
+ @Override
+ public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder fromHolder, RecyclerView.ViewHolder toHolder) {
+ int from = fromHolder.getAdapterPosition();
+ int to = toHolder.getAdapterPosition();
+ getSectionAdapter().notifyItemMoved(from, to);
+
+ synchronized (pendingOperations) {
+ pendingOperations.add(new Pair<>(from, to));
+ updateDownloadService();
+ }
+ return true;
+ }
+
+ @Override
+ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
+ SongView songView = (SongView) ((UpdateView.UpdateViewHolder) viewHolder).getUpdateView();
+ DownloadFile downloadFile = songView.getDownloadFile();
+
+ getSectionAdapter().removeItem(downloadFile);
+ synchronized (pendingOperations) {
+ pendingOperations.add(downloadFile);
+ updateDownloadService();
+ }
+ }
+
+ public DownloadService getDownloadService() {
+ return fragment.getDownloadService();
+ }
+ public SectionAdapter getSectionAdapter() {
+ return fragment.getCurrentAdapter();
+ }
+
+ private void updateDownloadService() {
+ if(pendingTask == null) {
+ final DownloadService downloadService = getDownloadService();
+ if(downloadService == null) {
+ return;
+ }
+
+ pendingTask = new SilentBackgroundTask<Void>(downloadService) {
+ @Override
+ protected Void doInBackground() throws Throwable {
+ boolean running = true;
+ while(running) {
+ Object nextOperation = null;
+ synchronized (pendingOperations) {
+ if(!pendingOperations.isEmpty()) {
+ nextOperation = pendingOperations.remove();
+ }
+ }
+
+ if(nextOperation != null) {
+ if(nextOperation instanceof Pair) {
+ Pair<Integer, Integer> swap = (Pair) nextOperation;
+ downloadService.swap(mainList, swap.getFirst(), swap.getSecond());
+ } else if(nextOperation instanceof DownloadFile) {
+ DownloadFile downloadFile = (DownloadFile) nextOperation;
+ if(mainList) {
+ downloadService.remove(downloadFile);
+ } else {
+ downloadService.removeBackground(downloadFile);
+ }
+ }
+ } else {
+ running = false;
+ }
+ }
+
+ synchronized (pendingOperations) {
+ pendingTask = null;
+
+ // Start a task if this is non-empty. Means someone added while we were running operations
+ if(!pendingOperations.isEmpty()) {
+ updateDownloadService();
+ }
+ }
+ return null;
+ }
+ };
+ pendingTask.execute();
+ }
+ }
+}