diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-06-08 17:55:06 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-06-08 17:55:06 -0700 |
commit | f47da3c25f2ee0d4f74392a0a7e4a570cc121516 (patch) | |
tree | b6087d0230dacff6d46e38d79019c81c30cf157a /app/src/main/java/github/daneren2005/dsub/fragments | |
parent | 6f218cf51b27e24012335a27e806e3a24cc52c75 (diff) | |
download | dsub-f47da3c25f2ee0d4f74392a0a7e4a570cc121516.tar.gz dsub-f47da3c25f2ee0d4f74392a0a7e4a570cc121516.tar.bz2 dsub-f47da3c25f2ee0d4f74392a0a7e4a570cc121516.zip |
#232 Switch NowPlayingFragment to RecyclerView, add swipe to remove and better drag + drop support
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/fragments')
3 files changed, 91 insertions, 237 deletions
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 59229c3f..5a3d63ad 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java @@ -30,6 +30,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.adapter.SectionAdapter; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.DownloadService; @@ -38,13 +39,15 @@ 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.UpdateView; -public class DownloadFragment extends SelectListFragment<DownloadFile> { +public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> { private long currentRevision; private ScheduledExecutorService executorService; public DownloadFragment() { serialize = false; + pullToRefresh = false; } @Override @@ -80,7 +83,7 @@ public class DownloadFragment extends SelectListFragment<DownloadFile> { } @Override - public ArrayAdapter getAdapter(List<DownloadFile> objs) { + public SectionAdapter getAdapter(List<DownloadFile> objs) { return new DownloadFileAdapter(context, objs); } @@ -91,9 +94,6 @@ public class DownloadFragment extends SelectListFragment<DownloadFile> { return new ArrayList<DownloadFile>(); } - listView.setOnScrollListener(null); - refreshLayout.setEnabled(false); - List<DownloadFile> songList = new ArrayList<DownloadFile>(); songList.addAll(downloadService.getBackgroundDownloads()); currentRevision = downloadService.getDownloadListUpdateRevision(); @@ -106,7 +106,7 @@ public class DownloadFragment extends SelectListFragment<DownloadFile> { } @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + public void onItemClicked(DownloadFile item) { } @@ -144,11 +144,13 @@ public class DownloadFragment extends SelectListFragment<DownloadFile> { @Override public void onCreateContextMenu(android.view.ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); + UpdateView targetView = adapter.getContextView(); + menuInfo = new AdapterView.AdapterContextMenuInfo(targetView, 0, 0); - AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; - Object selectedItem = ((DownloadFile) listView.getItemAtPosition(info.position)).getSong(); + DownloadFile downloadFile = adapter.getContextItem(); + MusicDirectory.Entry selectedItem = downloadFile.getSong(); onCreateContextMenu(menu, view, menuInfo, selectedItem); - if(selectedItem instanceof MusicDirectory.Entry && !((MusicDirectory.Entry) selectedItem).isVideo() && !Util.isOffline(context)) { + if(!selectedItem.isVideo() && !Util.isOffline(context)) { menu.removeItem(R.id.song_menu_remove_playlist); } @@ -161,9 +163,8 @@ public class DownloadFragment extends SelectListFragment<DownloadFile> { return false; } - AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); - Object selectedItem = ((DownloadFile) listView.getItemAtPosition(info.position)).getSong(); - + DownloadFile downloadFile = adapter.getContextItem(); + MusicDirectory.Entry selectedItem = downloadFile.getSong(); if(onContextItemSelected(menuItem, selectedItem)) { return true; } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java index 6f325a4e..cbceccbe 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java @@ -31,6 +31,9 @@ import android.os.Bundle; import android.os.Handler; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.MediaRouteButton; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.view.ContextMenu; import android.view.Display; @@ -55,6 +58,7 @@ import android.widget.TextView; import android.widget.ViewFlipper; import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicFragmentActivity; +import github.daneren2005.dsub.adapter.SectionAdapter; import github.daneren2005.dsub.audiofx.EqualizerController; import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.PlayerState; @@ -70,6 +74,7 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.adapter.DownloadFileAdapter; import github.daneren2005.dsub.view.FadeOutAnimation; +import github.daneren2005.dsub.view.SongView; import github.daneren2005.dsub.view.UpdateView; import github.daneren2005.dsub.util.Util; @@ -79,10 +84,9 @@ import github.daneren2005.dsub.util.*; import github.daneren2005.dsub.view.AutoRepeatButton; import java.util.ArrayList; import java.util.concurrent.ScheduledFuture; -import com.mobeta.android.dslv.*; import github.daneren2005.dsub.activity.SubsonicActivity; -public class NowPlayingFragment extends SubsonicFragment implements OnGestureListener { +public class NowPlayingFragment extends SubsonicFragment implements OnGestureListener, SectionAdapter.OnItemClickedListener<DownloadFile> { private static final String TAG = NowPlayingFragment.class.getSimpleName(); private static final int PERCENTAGE_OF_SCREEN_FOR_SWIPE = 10; private static final int INCREMENT_TIME = 5000; @@ -97,7 +101,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis private TextView emptyTextView; private TextView songTitleTextView; private ImageView albumArtImageView; - private DragSortListView playlistView; + private RecyclerView playlistView; private TextView positionTextView; private TextView durationTextView; private TextView statusTextView; @@ -172,7 +176,6 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis durationTextView = (TextView)rootView.findViewById(R.id.download_duration); statusTextView = (TextView)rootView.findViewById(R.id.download_status); progressBar = (SeekBar)rootView.findViewById(R.id.download_progress_bar); - playlistView = (DragSortListView)rootView.findViewById(R.id.download_list); previousButton = (AutoRepeatButton)rootView.findViewById(R.id.download_previous); nextButton = (AutoRepeatButton)rootView.findViewById(R.id.download_next); pauseButton =rootView.findViewById(R.id.download_pause); @@ -184,6 +187,45 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis rateGoodButton = (ImageButton) rootView.findViewById(R.id.download_rating_good); toggleListButton =rootView.findViewById(R.id.download_toggle_list); + playlistView = (RecyclerView)rootView.findViewById(R.id.download_list); + setupLayoutManager(playlistView, false); + 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<Void>(context) { + private int from; + private int to; + + @Override + protected Void doInBackground() throws Throwable { + from = fromHolder.getAdapterPosition(); + to = toHolder.getAdapterPosition(); + getDownloadService().swap(true, from, to); + return null; + } + + @Override + protected void done(Void result) { + songListAdapter.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.remove(downloadFile); + songListAdapter.removeItem(downloadFile); + currentRevision = downloadService.getDownloadListUpdateRevision(); + } + }); + touchHelper.attachToRecyclerView(playlistView); + starButton = (ImageButton)rootView.findViewById(R.id.download_star); if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_MENU_STAR, true)) { starButton.setOnClickListener(new OnClickListener() { @@ -499,47 +541,6 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis } } }); - playlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { - warnIfStorageUnavailable(); - new SilentBackgroundTask<Void>(context) { - @Override - protected Void doInBackground() throws Throwable { - getDownloadService().play(position); - return null; - } - - @Override - protected void done(Void result) { - onCurrentChanged(); - onProgressChanged(); - } - }.execute(); - } - }); - playlistView.setDropListener(new DragSortListView.DropListener() { - @Override - public void drop(final int from, final int to) { - new SilentBackgroundTask<Void>(context) { - @Override - protected Void doInBackground() throws Throwable { - getDownloadService().swap(true, from, to); - onDownloadListChanged(); - - return null; - } - }.execute(); - } - }); - playlistView.setRemoveListener(new DragSortListView.RemoveListener() { - @Override - public void remove(int which) { - getDownloadService().remove(which); - onDownloadListChanged(); - } - }); - registerForContextMenu(playlistView); if(Build.MODEL.equals("Nexus 4") || Build.MODEL.equals("GT-I9100")) { @@ -605,10 +606,11 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis if(!primaryFragment) { return; } + UpdateView targetView = songListAdapter.getContextView(); + menuInfo = new AdapterView.AdapterContextMenuInfo(targetView, 0, 0); if (view == playlistView) { - AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; - DownloadFile downloadFile = (DownloadFile) playlistView.getItemAtPosition(info.position); + DownloadFile downloadFile = songListAdapter.getContextItem(); android.view.MenuInflater inflater = context.getMenuInflater(); if(Util.isOffline(context)) { @@ -633,8 +635,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis return false; } - AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); - DownloadFile downloadFile = (DownloadFile) playlistView.getItemAtPosition(info.position); + DownloadFile downloadFile = songListAdapter.getContextItem(); return menuItemSelected(menuItem.getItemId(), downloadFile) || super.onContextItemSelected(menuItem); } @@ -996,18 +997,18 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis return; } - for (int i = 0; i < songListAdapter.getCount(); i++) { - if (currentPlaying == playlistView.getItemAtPosition(i)) { - playlistView.setSelectionFromTop(i, 40); - return; - } + // Try to get position of current playing/downloading + int position = songListAdapter.getItemPosition(currentPlaying); + if(position == -1) { + DownloadFile currentDownloading = getDownloadService().getCurrentDownloading(); + position = songListAdapter.getItemPosition(currentDownloading); } - DownloadFile currentDownloading = getDownloadService().getCurrentDownloading(); - for (int i = 0; i < songListAdapter.getCount(); i++) { - if (currentDownloading == playlistView.getItemAtPosition(i)) { - playlistView.setSelectionFromTop(i, 40); - return; - } + + // If found, scroll to it + if(position != -1) { + // RecyclerView.scrollToPosition just puts it on the screen (ie: bottom if scrolled below it) + LinearLayoutManager layoutManager = (LinearLayoutManager) playlistView.getLayoutManager(); + layoutManager.scrollToPositionWithOffset(position, 0); } } @@ -1161,9 +1162,9 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis } if(songListAdapter == null || refresh) { - songList = new ArrayList<DownloadFile>(); + songList = new ArrayList<>(); songList.addAll(list); - playlistView.setAdapter(songListAdapter = new DownloadFileAdapter(context, songList)); + playlistView.setAdapter(songListAdapter = new DownloadFileAdapter(context, songList, NowPlayingFragment.this)); } else { songList.clear(); songList.addAll(list); @@ -1589,4 +1590,19 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis public boolean onSingleTapUp(MotionEvent e) { return false; } + + @Override + public void onItemClicked(final DownloadFile item) { + warnIfStorageUnavailable(); + new SilentBackgroundTask<Void>(context) { + @Override + protected Void doInBackground() throws Throwable { + getDownloadService().play(item); + + onCurrentChanged(); + onProgressChanged(); + return null; + } + }.execute(); + } } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SelectListFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SelectListFragment.java deleted file mode 100644 index 6f73f6e8..00000000 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SelectListFragment.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see <http://www.gnu.org/licenses/>. - - Copyright 2010 (C) Sindre Mehus - */ -package github.daneren2005.dsub.fragments; - -import android.os.Bundle; -import android.support.v4.widget.SwipeRefreshLayout; -import android.util.Log; -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; -import android.widget.ListView; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import github.daneren2005.dsub.R; -import github.daneren2005.dsub.service.MusicService; -import github.daneren2005.dsub.service.MusicServiceFactory; -import github.daneren2005.dsub.util.BackgroundTask; -import github.daneren2005.dsub.util.Constants; -import github.daneren2005.dsub.util.ProgressListener; -import github.daneren2005.dsub.util.TabBackgroundTask; - -public abstract class SelectListFragment<T> extends SubsonicFragment implements AdapterView.OnItemClickListener { - private static final String TAG = SelectListFragment.class.getSimpleName(); - protected ListView listView; - protected ArrayAdapter adapter; - protected BackgroundTask<List<T>> currentTask; - protected List<T> objects; - protected boolean serialize = true; - - @Override - public void onCreate(Bundle bundle) { - super.onCreate(bundle); - - if(bundle != null && serialize) { - objects = (List<T>) bundle.getSerializable(Constants.FRAGMENT_LIST); - } - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - if(serialize) { - outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) objects); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { - rootView = inflater.inflate(R.layout.abstract_list_fragment, container, false); - - refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout); - refreshLayout.setOnRefreshListener(this); - - listView = (ListView)rootView.findViewById(R.id.fragment_list); - listView.setOnItemClickListener(this); - setupScrollList(listView); - registerForContextMenu(listView); - - if(objects == null) { - refresh(false); - } else { - listView.setAdapter(adapter = getAdapter(objects)); - } - - return rootView; - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { - if(!primaryFragment) { - return; - } - - menuInflater.inflate(getOptionsMenu(), menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); - } - - @Override - protected void refresh(final boolean refresh) { - int titleRes = getTitleResource(); - if(titleRes != 0) { - setTitle(getTitleResource()); - } - listView.setVisibility(View.GONE); - - // Cancel current running task before starting another one - if(currentTask != null) { - currentTask.cancel(); - } - - currentTask = new TabBackgroundTask<List<T>>(this) { - @Override - protected List<T> doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - - objects = new ArrayList<T>(); - - try { - objects = getObjects(musicService, refresh, this); - } catch (Exception x) { - Log.e(TAG, "Failed to load", x); - } - - return objects; - } - - @Override - protected void done(List<T> result) { - if (result != null && !result.isEmpty()) { - // Toggle fast scroll to get around issue when length of list changes - listView.setFastScrollEnabled(false); - listView.setAdapter(adapter = getAdapter(result)); - listView.setFastScrollEnabled(true); - - onFinishRefresh(); - listView.setVisibility(View.VISIBLE); - } else { - setEmpty(true); - } - - currentTask = null; - } - }; - currentTask.execute(); - } - - public abstract int getOptionsMenu(); - public abstract ArrayAdapter getAdapter(List<T> objs); - public abstract List<T> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception; - public abstract int getTitleResource(); - - public void onFinishRefresh() { - - } -} |