From 03da89a7a0a3dd2c642bc8c5a7f12ed04d1ffc46 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Thu, 25 Jun 2015 17:46:04 -0700 Subject: #525 Implement swipe remove on DownloadFragment --- .../dsub/fragments/DownloadFragment.java | 52 ++++++++++++++++++++++ .../daneren2005/dsub/service/DownloadService.java | 10 +++++ 2 files changed, 62 insertions(+) (limited to 'app') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java index 5a3d63ad..b89a25c5 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java @@ -16,10 +16,17 @@ package github.daneren2005.dsub.fragments; import android.content.DialogInterface; +import android.os.Bundle; import android.os.Handler; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; import android.view.ContextMenu; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; @@ -39,6 +46,7 @@ import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.adapter.DownloadFileAdapter; +import github.daneren2005.dsub.view.SongView; import github.daneren2005.dsub.view.UpdateView; public class DownloadFragment extends SelectRecyclerFragment { @@ -50,6 +58,50 @@ public class DownloadFragment extends SelectRecyclerFragment { pullToRefresh = false; } + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { + super.onCreateView(inflater, container, bundle); + + ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT) { + @Override + public boolean onMove(RecyclerView recyclerView, final RecyclerView.ViewHolder fromHolder, final RecyclerView.ViewHolder toHolder) { + new SilentBackgroundTask(context) { + private int from; + private int to; + + @Override + protected Void doInBackground() throws Throwable { + from = fromHolder.getAdapterPosition(); + to = toHolder.getAdapterPosition(); + getDownloadService().swap(false, from, to); + return null; + } + + @Override + protected void done(Void result) { + adapter.notifyItemMoved(from, to); + } + }.execute(); + + return true; + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { + SongView songView = (SongView) ((UpdateView.UpdateViewHolder) viewHolder).getUpdateView(); + DownloadFile downloadFile = songView.getDownloadFile(); + + DownloadService downloadService = getDownloadService(); + downloadService.removeBackground(downloadFile); + adapter.removeItem(downloadFile); + currentRevision = downloadService.getDownloadListUpdateRevision(); + } + }); + touchHelper.attachToRecyclerView(recyclerView); + + return rootView; + } + @Override public void onResume() { super.onResume(); diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index bf68c450..2e3217d2 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -739,6 +739,16 @@ public class DownloadService extends Service { setNextPlaying(); } } + public synchronized void removeBackground(DownloadFile downloadFile) { + if (downloadFile == currentDownloading && downloadFile != currentPlaying && downloadFile != nextPlaying) { + currentDownloading.cancelDownload(); + currentDownloading = null; + } + + backgroundDownloadList.remove(downloadFile); + revision++; + checkDownloads(); + } public synchronized void delete(List songs) { for (MusicDirectory.Entry song : songs) { -- cgit v1.2.3