Kore/app/src/main/java/org/xbmc/kore/ui/sections/audio/ArtistListFragment.java

245 lines
9.1 KiB
Java
Raw Normal View History

2015-01-14 12:12:47 +01:00
/*
* Copyright 2015 Synced Synapse. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui.sections.audio;
2015-01-14 12:12:47 +01:00
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
2015-01-14 12:12:47 +01:00
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.loader.content.CursorLoader;
import org.xbmc.kore.R;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
import org.xbmc.kore.provider.MediaContract;
import org.xbmc.kore.provider.MediaDatabase;
import org.xbmc.kore.service.library.LibrarySyncService;
import org.xbmc.kore.ui.AbstractCursorListFragment;
Refactored AbstractDetailsFragment This introduces the MVC model to details fragments. It moves as much view and control code out of the concrete fragments into the abstract classes. * Added UML class and sequence diagrams under doc/diagrams to clarify the new setup * Introduces new abstract classes * AbstractFragment class to hold the DataHolder * AbstractInfoFragment class to display media information * AbstractAddtionalInfoFragment class to allow *InfoFragments to add additional UI elements and propagate refresh requests. See for an example TVShowInfoFragment which adds TVShowProgressFragment to display next episodes and season progression. * Introduces new RefreshItem class to encapsulate all refresh functionality from AbstractDetailsFragment * Introduces new SharedElementTransition utility class to encapsulate all shared element transition code * Introduces new CastFragment class to encapsulate all code for displaying casts reducing code duplication * Introduces DataHolder class to replace passing the ViewHolder from the *ListFragment to the *DetailsFragment or *InfoFragment * Refactored AbstractDetailsFragment into two classes: o AbstractDetailsFragment: for fragments requiring a tab bar o AbstractInfoFragment: for fragments showing media information We used to use <NAME>DetailsFragments for both fragments that show generic info about some media item and fragments that hold all details for some media item. For example, artist details showed artist info and used tabs to show artist albums and songs as well. Now Details fragments are used to show all details, Info fragments only show media item information like description, title, rating, etc. * Moved swiperefreshlayout code from AbstractCursorListFragment to AbstractListFragment
2016-12-30 09:27:24 +01:00
import org.xbmc.kore.ui.AbstractInfoFragment;
import org.xbmc.kore.ui.RecyclerViewCursorAdapter;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.MediaPlayerUtils;
import org.xbmc.kore.utils.UIUtils;
import org.xbmc.kore.utils.Utils;
2015-01-14 12:12:47 +01:00
/**
* Fragment that presents the artists list
*/
public class ArtistListFragment extends AbstractCursorListFragment {
2015-01-14 12:12:47 +01:00
private static final String TAG = LogUtils.makeLogTag(ArtistListFragment.class);
public interface OnArtistSelectedListener {
public void onArtistSelected(ViewHolder tag);
2015-01-14 12:12:47 +01:00
}
// Activity listener
private OnArtistSelectedListener listenerActivity;
@Override
protected String getListSyncType() { return LibrarySyncService.SYNC_ALL_MUSIC; }
2015-01-14 12:12:47 +01:00
@Override
protected void onListItemClicked(View view) {
// Get the artist id from the tag
ViewHolder tag = (ViewHolder) view.getTag();
// Notify the activity
listenerActivity.onArtistSelected(tag);
}
2015-01-14 12:12:47 +01:00
@Override
protected RecyclerViewCursorAdapter createCursorAdapter() {
return new ArtistsAdapter(this);
}
2015-01-14 12:12:47 +01:00
@Override
protected CursorLoader createCursorLoader() {
HostInfo hostInfo = HostManager.getInstance(getActivity()).getHostInfo();
Uri uri = MediaContract.Artists.buildArtistsListUri(hostInfo != null ? hostInfo.getId() : -1);
String selection = null;
String selectionArgs[] = null;
String searchFilter = getSearchFilter();
if (!TextUtils.isEmpty(searchFilter)) {
selection = MediaContract.ArtistsColumns.ARTIST + " LIKE ?";
selectionArgs = new String[] {"%" + searchFilter + "%"};
}
return new CursorLoader(getActivity(), uri,
ArtistListQuery.PROJECTION, selection, selectionArgs, ArtistListQuery.SORT);
2015-01-14 12:12:47 +01:00
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
2015-01-14 12:12:47 +01:00
try {
listenerActivity = (OnArtistSelectedListener) context;
2015-01-14 12:12:47 +01:00
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnArtistSelectedListener");
2015-01-14 12:12:47 +01:00
}
setSupportsSearch(true);
2015-01-14 12:12:47 +01:00
}
@Override
public void onDetach() {
super.onDetach();
listenerActivity = null;
}
/**
* Artist list query parameters.
*/
private interface ArtistListQuery {
String[] PROJECTION = {
BaseColumns._ID,
MediaContract.Artists.ARTISTID,
MediaContract.Artists.ARTIST,
MediaContract.Artists.GENRE,
MediaContract.Artists.THUMBNAIL,
MediaContract.Artists.DESCRIPTION,
MediaContract.Artists.FANART
2015-01-14 12:12:47 +01:00
};
String SORT = MediaDatabase.sortCommonTokens(MediaContract.Artists.ARTIST) + " COLLATE NOCASE ASC";
2015-01-14 12:12:47 +01:00
int ID = 0;
int ARTISTID = 1;
int ARTIST = 2;
int GENRE = 3;
int THUMBNAIL = 4;
int DESCRIPTION = 5;
int FANART = 6;
2015-01-14 12:12:47 +01:00
}
private static class ArtistsAdapter extends RecyclerViewCursorAdapter {
2015-01-14 12:12:47 +01:00
private HostManager hostManager;
private int artWidth, artHeight;
Fragment fragment;
2015-01-14 12:12:47 +01:00
public ArtistsAdapter(Fragment fragment) {
this.fragment = fragment;
this.hostManager = HostManager.getInstance(fragment.getContext());
2015-01-14 12:12:47 +01:00
// Get the art dimensions
Resources resources = fragment.getContext().getResources();
Refactored AbstractDetailsFragment This introduces the MVC model to details fragments. It moves as much view and control code out of the concrete fragments into the abstract classes. * Added UML class and sequence diagrams under doc/diagrams to clarify the new setup * Introduces new abstract classes * AbstractFragment class to hold the DataHolder * AbstractInfoFragment class to display media information * AbstractAddtionalInfoFragment class to allow *InfoFragments to add additional UI elements and propagate refresh requests. See for an example TVShowInfoFragment which adds TVShowProgressFragment to display next episodes and season progression. * Introduces new RefreshItem class to encapsulate all refresh functionality from AbstractDetailsFragment * Introduces new SharedElementTransition utility class to encapsulate all shared element transition code * Introduces new CastFragment class to encapsulate all code for displaying casts reducing code duplication * Introduces DataHolder class to replace passing the ViewHolder from the *ListFragment to the *DetailsFragment or *InfoFragment * Refactored AbstractDetailsFragment into two classes: o AbstractDetailsFragment: for fragments requiring a tab bar o AbstractInfoFragment: for fragments showing media information We used to use <NAME>DetailsFragments for both fragments that show generic info about some media item and fragments that hold all details for some media item. For example, artist details showed artist info and used tabs to show artist albums and songs as well. Now Details fragments are used to show all details, Info fragments only show media item information like description, title, rating, etc. * Moved swiperefreshlayout code from AbstractCursorListFragment to AbstractListFragment
2016-12-30 09:27:24 +01:00
artWidth = (int)(resources.getDimension(R.dimen.detail_poster_width_square));
artHeight = (int)(resources.getDimension(R.dimen.detail_poster_height_square));
2015-01-14 12:12:47 +01:00
}
@Override
public CursorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(fragment.getContext())
.inflate(R.layout.grid_item_artist, parent, false);
return new ViewHolder(view, fragment.getContext(), hostManager, artWidth, artHeight, artistlistItemMenuClickListener);
}
private View.OnClickListener artistlistItemMenuClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
final ViewHolder viewHolder = (ViewHolder)v.getTag();
final PlaylistType.Item playListItem = new PlaylistType.Item();
playListItem.artistid = viewHolder.dataHolder.getId();
final PopupMenu popupMenu = new PopupMenu(fragment.getContext(), v);
popupMenu.getMenuInflater().inflate(R.menu.musiclist_item, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_play:
MediaPlayerUtils.play(fragment, playListItem);
return true;
case R.id.action_queue:
MediaPlayerUtils.queue(fragment, playListItem, PlaylistType.GetPlaylistsReturnType.AUDIO);
return true;
}
return false;
}
});
popupMenu.show();
}
};
protected int getSectionColumnIdx() { return ArtistListQuery.ARTIST; }
2015-01-14 12:12:47 +01:00
}
/**
* View holder pattern
*/
public static class ViewHolder extends RecyclerViewCursorAdapter.CursorViewHolder {
2015-01-14 12:12:47 +01:00
TextView nameView;
TextView genresView;
ImageView artView;
HostManager hostManager;
int artWidth;
int artHeight;
Context context;
2015-01-14 12:12:47 +01:00
Refactored AbstractDetailsFragment This introduces the MVC model to details fragments. It moves as much view and control code out of the concrete fragments into the abstract classes. * Added UML class and sequence diagrams under doc/diagrams to clarify the new setup * Introduces new abstract classes * AbstractFragment class to hold the DataHolder * AbstractInfoFragment class to display media information * AbstractAddtionalInfoFragment class to allow *InfoFragments to add additional UI elements and propagate refresh requests. See for an example TVShowInfoFragment which adds TVShowProgressFragment to display next episodes and season progression. * Introduces new RefreshItem class to encapsulate all refresh functionality from AbstractDetailsFragment * Introduces new SharedElementTransition utility class to encapsulate all shared element transition code * Introduces new CastFragment class to encapsulate all code for displaying casts reducing code duplication * Introduces DataHolder class to replace passing the ViewHolder from the *ListFragment to the *DetailsFragment or *InfoFragment * Refactored AbstractDetailsFragment into two classes: o AbstractDetailsFragment: for fragments requiring a tab bar o AbstractInfoFragment: for fragments showing media information We used to use <NAME>DetailsFragments for both fragments that show generic info about some media item and fragments that hold all details for some media item. For example, artist details showed artist info and used tabs to show artist albums and songs as well. Now Details fragments are used to show all details, Info fragments only show media item information like description, title, rating, etc. * Moved swiperefreshlayout code from AbstractCursorListFragment to AbstractListFragment
2016-12-30 09:27:24 +01:00
AbstractInfoFragment.DataHolder dataHolder = new AbstractInfoFragment.DataHolder(0);
ViewHolder(View itemView, Context context, HostManager hostManager, int artWidth, int artHeight,
View.OnClickListener contextMenuClickListener) {
super(itemView);
this.context = context;
this.hostManager = hostManager;
this.artWidth = artWidth;
this.artHeight = artHeight;
nameView = itemView.findViewById(R.id.name);
genresView = itemView.findViewById(R.id.genres);
artView = itemView.findViewById(R.id.art);
ImageView contextMenu = itemView.findViewById(R.id.list_context_menu);
contextMenu.setTag(this);
contextMenu.setOnClickListener(contextMenuClickListener);
}
@Override
public void bindView(Cursor cursor) {
dataHolder.setId(cursor.getInt(ArtistListQuery.ARTISTID));
dataHolder.setTitle(cursor.getString(ArtistListQuery.ARTIST));
dataHolder.setUndertitle(cursor.getString(ArtistListQuery.GENRE));
dataHolder.setDescription(cursor.getString(ArtistListQuery.DESCRIPTION));
dataHolder.setFanArtUrl(cursor.getString(ArtistListQuery.FANART));
nameView.setText(cursor.getString(ArtistListQuery.ARTIST));
genresView.setText(cursor.getString(ArtistListQuery.GENRE));
dataHolder.setPosterUrl(cursor.getString(ArtistListQuery.THUMBNAIL));
UIUtils.loadImageWithCharacterAvatar(context, hostManager,
dataHolder.getPosterUrl(), dataHolder.getTitle(),
artView, artWidth, artHeight);
if (Utils.isLollipopOrLater()) {
artView.setTransitionName("ar"+dataHolder.getId());
}
}
}
2015-01-14 12:12:47 +01:00
}