diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-05-28 18:15:00 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-05-28 18:15:00 -0700 |
commit | 1ba7ddefc46d502bd01f95527d8dd75c2952aaf5 (patch) | |
tree | cbdd83d7eaa2e675d95b9636151a04068c6a4327 | |
parent | 07665ccfbc9c9f72c6d384a00ff692dde141319a (diff) | |
download | dsub-1ba7ddefc46d502bd01f95527d8dd75c2952aaf5.tar.gz dsub-1ba7ddefc46d502bd01f95527d8dd75c2952aaf5.tar.bz2 dsub-1ba7ddefc46d502bd01f95527d8dd75c2952aaf5.zip |
Convert artist fragments over to RecyclerView
6 files changed, 212 insertions, 142 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/ArtistAdapter2.java b/app/src/main/java/github/daneren2005/dsub/adapter/ArtistAdapter2.java new file mode 100644 index 00000000..88b3ae28 --- /dev/null +++ b/app/src/main/java/github/daneren2005/dsub/adapter/ArtistAdapter2.java @@ -0,0 +1,128 @@ +/* + 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 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.adapter; + +import android.content.Context; +import android.support.v7.widget.PopupMenu; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.util.List; + +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.MusicFolder; +import github.daneren2005.dsub.util.Util; +import github.daneren2005.dsub.view.ArtistView; +import github.daneren2005.dsub.view.UpdateView; + +public class ArtistAdapter2 extends SectionAdapter<Artist> { + public static int VIEW_TYPE_ARTIST = 1; + + private List<MusicFolder> musicFolders; + private OnMusicFolderChanged onMusicFolderChanged; + + public ArtistAdapter2(Context context, List<Artist> artists, OnItemClickedListener listener) { + this(context, artists, null, listener, null); + } + + public ArtistAdapter2(Context context, List<Artist> artists, List<MusicFolder> musicFolders, OnItemClickedListener onItemClickedListener, OnMusicFolderChanged onMusicFolderChanged) { + super(context, artists); + this.musicFolders = musicFolders; + this.onItemClickedListener = onItemClickedListener; + this.onMusicFolderChanged = onMusicFolderChanged; + + if(musicFolders != null) { + this.singleSectionHeader = true; + } + } + + @Override + public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) { + final View header = LayoutInflater.from(context).inflate(R.layout.select_artist_header, parent, false); + header.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + PopupMenu popup = new PopupMenu(context, header); + + popup.getMenu().add(R.string.select_artist_all_folders); + for (MusicFolder musicFolder : musicFolders) { + popup.getMenu().add(musicFolder.getName()); + } + + popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + for (MusicFolder musicFolder : musicFolders) { + if(item.getTitle().equals(musicFolder.getName())) { + if(onMusicFolderChanged != null) { + onMusicFolderChanged.onMusicFolderChanged(musicFolder); + } + return true; + } + } + + if(onMusicFolderChanged != null) { + onMusicFolderChanged.onMusicFolderChanged(null); + } + return true; + } + }); + popup.show(); + } + }); + + return new UpdateView.UpdateViewHolder(header, false); + } + @Override + public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header) { + TextView folderName = (TextView) holder.getView().findViewById(R.id.select_artist_folder_2); + + String musicFolderId = Util.getSelectedMusicFolderId(context); + if(musicFolderId != null) { + for (MusicFolder musicFolder : musicFolders) { + if (musicFolder.getId().equals(musicFolderId)) { + folderName.setText(musicFolder.getName()); + break; + } + } + } else { + folderName.setText(R.string.select_artist_all_folders); + } + } + + @Override + public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) { + return new UpdateView.UpdateViewHolder(new ArtistView(context)); + } + + @Override + public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Artist item, int viewType) { + holder.getUpdateView().setObject(item); + } + + @Override + public int getItemViewType(Artist item) { + return VIEW_TYPE_ARTIST; + } + + public interface OnMusicFolderChanged { + void onMusicFolderChanged(MusicFolder musicFolder); + } +} diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java index 84b21e30..85a7f18f 100644 --- a/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java +++ b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java @@ -198,7 +198,9 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH } public void onBindHeaderHolder(UpdateViewHolder holder, String header) { UpdateView view = holder.getUpdateView(); - view.setObject(header); + if(view != null) { + view.setObject(header); + } } public T getItemForPosition(int position) { diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SelectArtistFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SelectArtistFragment.java index 5488c95b..b282cf6f 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SelectArtistFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SelectArtistFragment.java @@ -15,6 +15,8 @@ import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.adapter.ArtistAdapter2; +import github.daneren2005.dsub.adapter.SectionAdapter; import github.daneren2005.dsub.domain.Artist; import github.daneren2005.dsub.domain.Indexes; import github.daneren2005.dsub.domain.MusicDirectory; @@ -24,18 +26,16 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.adapter.ArtistAdapter; +import github.daneren2005.dsub.view.UpdateView; import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class SelectArtistFragment extends SelectListFragment<Artist> { +public class SelectArtistFragment extends SelectRecyclerFragment<Artist> implements ArtistAdapter2.OnMusicFolderChanged { private static final String TAG = SelectArtistFragment.class.getSimpleName(); private static final int MENU_GROUP_MUSIC_FOLDER = 10; - private View folderButtonParent; - private View folderButton; - private TextView folderName; private List<MusicFolder> musicFolders = null; private List<MusicDirectory.Entry> entries; private String groupId; @@ -75,19 +75,7 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { } } - folderButton = null; super.onCreateView(inflater, container, bundle); - - if("4.4.2".equals(Build.VERSION.RELEASE)) { - listView.setFastScrollAlwaysVisible(true); - } - - if(objects != null && currentTask == null) { - if (Util.isOffline(context) || Util.isTagBrowsing(context) || groupId != null) { - folderButton.setVisibility(View.GONE); - } - setMusicFolders(); - } return rootView; } @@ -95,30 +83,12 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { @Override public void onCreateContextMenu(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 entry = listView.getItemAtPosition(info.position); - - if (entry instanceof Artist) { - onCreateContextMenu(menu, view, menuInfo, entry); - } else if (info.position == 0) { - String musicFolderId = Util.getSelectedMusicFolderId(context); - MenuItem menuItem = menu.add(MENU_GROUP_MUSIC_FOLDER, -1, 0, R.string.select_artist_all_folders); - if (musicFolderId == null) { - menuItem.setChecked(true); - } - if (musicFolders != null) { - for (int i = 0; i < musicFolders.size(); i++) { - MusicFolder musicFolder = musicFolders.get(i); - menuItem = menu.add(MENU_GROUP_MUSIC_FOLDER, i, i + 1, musicFolder.getName()); - if (musicFolder.getId().equals(musicFolderId)) { - menuItem.setChecked(true); - } - } - } - menu.setGroupCheckable(MENU_GROUP_MUSIC_FOLDER, true, true); - } + Artist artist = adapter.getContextItem(); + onCreateContextMenu(menu, view, menuInfo, artist); recreateContextMenu(menu); } @@ -129,56 +99,33 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { } AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); - Artist artist = (Artist) listView.getItemAtPosition(info.position); - - if (artist != null) { - return onContextItemSelected(menuItem, artist); - } else if (info.position == 0) { - MusicFolder selectedFolder = menuItem.getItemId() == -1 ? null : musicFolders.get(menuItem.getItemId()); - String musicFolderId = selectedFolder == null ? null : selectedFolder.getId(); - String musicFolderName = selectedFolder == null ? context.getString(R.string.select_artist_all_folders) - : selectedFolder.getName(); - Util.setSelectedMusicFolderId(context, musicFolderId); - folderName.setText(musicFolderName); - context.invalidate(); - } + Artist artist = adapter.getContextItem(); - return true; + return onContextItemSelected(menuItem, artist); } @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - if (view == folderButtonParent) { - selectFolder(); - } else { - Artist artist = (Artist) parent.getItemAtPosition(position); - - SubsonicFragment fragment; - if((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || "root".equals(artist.getId()) || groupId != null) { - fragment = new SelectDirectoryFragment(); - Bundle args = new Bundle(); - args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); - args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName()); - if ("root".equals(artist.getId())) { - args.putSerializable(Constants.FRAGMENT_LIST, (Serializable) entries); - } - args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true); - fragment.setArguments(args); - } else { - fragment = new SelectArtistFragment(); - Bundle args = new Bundle(); - args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); - args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName()); - fragment.setArguments(args); + public void onItemClicked(Artist artist) { + SubsonicFragment fragment; + if((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || "root".equals(artist.getId()) || groupId != null) { + fragment = new SelectDirectoryFragment(); + Bundle args = new Bundle(); + args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); + args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName()); + if ("root".equals(artist.getId())) { + args.putSerializable(Constants.FRAGMENT_LIST, (Serializable) entries); } - - replaceFragment(fragment); + args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true); + fragment.setArguments(args); + } else { + fragment = new SelectArtistFragment(); + Bundle args = new Bundle(); + args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); + args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName()); + fragment.setArguments(args); } - } - @Override - public void onFinishRefresh() { - setMusicFolders(); + replaceFragment(fragment); } @Override @@ -215,9 +162,8 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { } @Override - public ArrayAdapter getAdapter(List<Artist> objects) { - createMusicFolderButton(); - return new ArtistAdapter(context, objects); + public SectionAdapter getAdapter(List<Artist> objects) { + return new ArtistAdapter2(context, objects, musicFolders, this, this); } @Override @@ -236,12 +182,12 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { String musicFolderId = Util.getSelectedMusicFolderId(context); Indexes indexes = musicService.getIndexes(musicFolderId, refresh, context, listener); - artists = new ArrayList<Artist>(indexes.getShortcuts().size() + indexes.getArtists().size()); + artists = new ArrayList<>(indexes.getShortcuts().size() + indexes.getArtists().size()); artists.addAll(indexes.getShortcuts()); artists.addAll(indexes.getArtists()); entries = indexes.getEntries(); } else { - artists = new ArrayList<Artist>(); + artists = new ArrayList<>(); MusicDirectory dir = musicService.getMusicDirectory(groupId, groupName, refresh, context, listener); for(MusicDirectory.Entry entry: dir.getChildren(true, false)) { Artist artist = new Artist(); @@ -251,7 +197,7 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { artists.add(artist); } - entries = new ArrayList<MusicDirectory.Entry>(); + entries = new ArrayList<>(); entries.addAll(dir.getChildren(false, true)); if(!entries.isEmpty()) { Artist root = new Artist(); @@ -270,32 +216,14 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { return groupId == null ? R.string.button_bar_browse : 0; } - private void createMusicFolderButton() { - if(folderButton == null) { - folderButtonParent = context.getLayoutInflater().inflate(R.layout.select_artist_header, listView, false); - folderName = (TextView) folderButtonParent.findViewById(R.id.select_artist_folder_2); - listView.addHeaderView(folderButtonParent); - folderButton = folderButtonParent.findViewById(R.id.select_artist_folder); - } - - if (Util.isOffline(context) || Util.isTagBrowsing(context) || musicFolders == null) { - folderButton.setVisibility(View.GONE); - } else { - folderButton.setVisibility(View.VISIBLE); - } - } - @Override public void setEmpty(boolean empty) { super.setEmpty(empty); if(empty && !Util.isOffline(context)) { - createMusicFolderButton(); - setMusicFolders(); - objects.clear(); - listView.setAdapter(new ArtistAdapter(context, objects)); - listView.setVisibility(View.VISIBLE); + recyclerView.setAdapter(new ArtistAdapter2(context, objects, this)); + recyclerView.setVisibility(View.VISIBLE); View view = rootView.findViewById(R.id.tab_progress); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); @@ -305,29 +233,19 @@ public class SelectArtistFragment extends SelectListFragment<Artist> { } } - private void setMusicFolders() { - // Display selected music folder - if (musicFolders != null) { - String musicFolderId = Util.getSelectedMusicFolderId(context); - if (musicFolderId == null) { - folderName.setText(R.string.select_artist_all_folders); - } else { - for (MusicFolder musicFolder : musicFolders) { - if (musicFolder.getId().equals(musicFolderId)) { - folderName.setText(musicFolder.getName()); - break; - } - } - } - } - } - - private void selectFolder() { - folderButton.showContextMenu(); - } - private void toggleFirstLevelArtist() { Util.toggleFirstLevelArtist(context); context.invalidateOptionsMenu(); } + + @Override + public void onMusicFolderChanged(MusicFolder selectedFolder) { + String startMusicFolderId = Util.getSelectedMusicFolderId(context); + String musicFolderId = selectedFolder == null ? null : selectedFolder.getId(); + + if(!Util.equals(startMusicFolderId, musicFolderId)) { + Util.setSelectedMusicFolderId(context, musicFolderId); + context.invalidate(); + } + } } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SimilarArtistFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SimilarArtistFragment.java index 79e759cc..6284dcb9 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SimilarArtistFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SimilarArtistFragment.java @@ -17,12 +17,15 @@ package github.daneren2005.dsub.fragments; import android.os.Bundle; import android.view.ContextMenu; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; -import android.widget.ArrayAdapter; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.adapter.ArtistAdapter2; +import github.daneren2005.dsub.adapter.SectionAdapter; import github.daneren2005.dsub.domain.Artist; import github.daneren2005.dsub.domain.ArtistInfo; import github.daneren2005.dsub.domain.MusicDirectory; @@ -32,13 +35,13 @@ import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.Util; -import github.daneren2005.dsub.adapter.ArtistAdapter; +import github.daneren2005.dsub.view.UpdateView; import java.net.URLEncoder; import java.util.LinkedList; import java.util.List; -public class SimilarArtistFragment extends SelectListFragment<Artist> { +public class SimilarArtistFragment extends SelectRecyclerFragment<Artist> { private static final String TAG = SimilarArtistFragment.class.getSimpleName(); private ArtistInfo info; private String artistId; @@ -52,6 +55,18 @@ public class SimilarArtistFragment extends SelectListFragment<Artist> { } @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + super.onCreateOptionsMenu(menu, menuInflater); + if(!primaryFragment) { + return; + } + + if(info.getMissingArtists().isEmpty()) { + menu.removeItem(R.id.menu_show_missing); + } + } + + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_play_now: @@ -71,10 +86,11 @@ public class SimilarArtistFragment extends SelectListFragment<Artist> { @Override public void onCreateContextMenu(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 entry = listView.getItemAtPosition(info.position); - onCreateContextMenu(menu, view, menuInfo, entry); + Artist artist = adapter.getContextItem(); + onCreateContextMenu(menu, view, menuInfo, artist); recreateContextMenu(menu); } @@ -85,14 +101,12 @@ public class SimilarArtistFragment extends SelectListFragment<Artist> { return false; } - AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); - Artist artist = (Artist) listView.getItemAtPosition(info.position); + Artist artist = adapter.getContextItem(); return onContextItemSelected(menuItem, artist); } @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - Artist artist = (Artist) parent.getItemAtPosition(position); + public void onItemClicked(Artist artist) { SubsonicFragment fragment = new SelectDirectoryFragment(); Bundle args = new Bundle(); args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); @@ -109,8 +123,8 @@ public class SimilarArtistFragment extends SelectListFragment<Artist> { } @Override - public ArrayAdapter getAdapter(List<Artist> objects) { - return new ArtistAdapter(context, objects); + public SectionAdapter getAdapter(List<Artist> objects) { + return new ArtistAdapter2(context, objects, this); } @Override diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java index 905fbb06..62d1672d 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java @@ -290,22 +290,28 @@ public class UpdateView extends LinearLayout { public static class UpdateViewHolder<T> extends RecyclerView.ViewHolder { private UpdateView updateView; + private View view; private T item; public UpdateViewHolder(UpdateView itemView) { super(itemView); this.updateView = itemView; + this.view = itemView; } // Different is so that call is not ambiguous public UpdateViewHolder(View view, boolean different) { super(view); + this.view = view; } public UpdateView getUpdateView() { return updateView; } + public View getView() { + return view; + } public void setItem(T item) { this.item = item; } diff --git a/app/src/main/res/layout/select_artist_header.xml b/app/src/main/res/layout/select_artist_header.xml index 2821ce43..0ac9e6a6 100644 --- a/app/src/main/res/layout/select_artist_header.xml +++ b/app/src/main/res/layout/select_artist_header.xml @@ -2,7 +2,9 @@ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" - android:layout_height="wrap_content"> + android:layout_height="wrap_content" + android:background="?attr/selectableItemBackground"> + <LinearLayout android:id="@+id/select_artist_folder" android:orientation="horizontal" |