Kore/app/src/main/java/org/xbmc/kore/ui/sections/video/TVShowEpisodeListFragment.java

359 lines
14 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.video;
2015-01-14 12:12:47 +01:00
import android.annotation.TargetApi;
2015-01-14 12:12:47 +01:00
import android.content.Context;
import android.content.SharedPreferences;
2015-01-14 12:12:47 +01:00
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
2015-01-14 12:12:47 +01:00
import android.provider.BaseColumns;
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.ImageView;
import android.widget.PopupMenu;
2015-01-14 12:12:47 +01:00
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.loader.content.CursorLoader;
import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
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.service.library.LibrarySyncService;
import org.xbmc.kore.ui.AbstractCursorListFragment;
import org.xbmc.kore.ui.AbstractFragment;
import org.xbmc.kore.ui.RecyclerViewCursorAdapter;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.MediaPlayerUtils;
import org.xbmc.kore.utils.UIUtils;
2015-01-14 12:12:47 +01:00
/**
* Presents a list of episodes for a TV show season
2015-01-14 12:12:47 +01:00
*/
public class TVShowEpisodeListFragment extends AbstractCursorListFragment {
2015-01-14 12:12:47 +01:00
private static final String TAG = LogUtils.makeLogTag(TVShowEpisodeListFragment.class);
public interface OnEpisodeSelectedListener {
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
void onEpisodeSelected(int tvshowId, ViewHolder dataHolder);
2015-01-14 12:12:47 +01:00
}
public static final String TVSHOWID = "tvshow_id";
public static final String TVSHOWSEASON = "season";
2015-01-14 12:12:47 +01:00
// Displayed show id
private int tvshowId = -1;
private int tvshowSeason = -1;
2015-01-14 12:12:47 +01:00
// Activity listener
private OnEpisodeSelectedListener listenerActivity;
/**
* Create a new instance of this, initialized to show tvshowId
*/
@TargetApi(21)
public static TVShowEpisodeListFragment newInstance(int tvshowId, int season) {
TVShowEpisodeListFragment fragment = new TVShowEpisodeListFragment();
Bundle args = new Bundle();
args.putInt(TVSHOWID, tvshowId);
args.putInt(TVSHOWSEASON, season);
fragment.setArguments(args);
return fragment;
}
@Override
protected String getListSyncType() { return LibrarySyncService.SYNC_SINGLE_TVSHOW; }
2015-01-14 12:12:47 +01:00
@Override
protected String getSyncID() { return LibrarySyncService.SYNC_TVSHOWID; };
2015-01-14 12:12:47 +01:00
@Override
protected int getSyncItemID() { return tvshowId; };
@TargetApi(16)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
2015-01-14 12:12:47 +01:00
tvshowId = getArguments().getInt(TVSHOWID, -1);
tvshowSeason = getArguments().getInt(TVSHOWSEASON, -1);
if ((tvshowId == -1) || (tvshowSeason == -1)) {
// There's nothing to show
2015-01-14 12:12:47 +01:00
return null;
}
return root;
}
@Override
protected void onListItemClicked(View view) {
// Get the movie id from the tag
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
ViewHolder tag = (ViewHolder) view.getTag();
// Notify the activity
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
listenerActivity.onEpisodeSelected(tvshowId, tag);
}
@Override
protected RecyclerViewCursorAdapter createCursorAdapter() {
return new SeasonsEpisodesAdapter(getActivity());
}
2015-01-14 12:12:47 +01:00
@Override
protected CursorLoader createCursorLoader() {
HostInfo hostInfo = HostManager.getInstance(getActivity()).getHostInfo();
Uri uri = MediaContract.Episodes.buildTVShowSeasonEpisodesListUri(hostInfo.getId(), tvshowId, tvshowSeason);
// Filters
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
StringBuilder selection = new StringBuilder();
if (preferences.getBoolean(Settings.KEY_PREF_TVSHOW_EPISODES_FILTER_HIDE_WATCHED, Settings.DEFAULT_PREF_TVSHOW_EPISODES_FILTER_HIDE_WATCHED)) {
selection.append(MediaContract.EpisodesColumns.PLAYCOUNT)
.append("=0");
}
return new CursorLoader(getActivity(), uri,
EpisodesListQuery.PROJECTION, selection.toString(), null, EpisodesListQuery.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 = (OnEpisodeSelectedListener) context;
2015-01-14 12:12:47 +01:00
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnEpisodeSelectedListener");
}
}
2015-01-14 12:12:47 +01:00
@Override
public void onDetach() {
super.onDetach();
listenerActivity = null;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!isAdded()) {
// HACK: Fix crash reported on Play Store. Why does this is necessary is beyond me
super.onCreateOptionsMenu(menu, inflater);
return;
}
2015-01-14 12:12:47 +01:00
inflater.inflate(R.menu.tvshow_episode_list, menu);
// Setup filters
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
2015-01-14 12:12:47 +01:00
menu.findItem(R.id.action_hide_watched)
.setChecked(preferences.getBoolean(Settings.KEY_PREF_TVSHOW_EPISODES_FILTER_HIDE_WATCHED, Settings.DEFAULT_PREF_TVSHOW_EPISODES_FILTER_HIDE_WATCHED));
2015-01-14 12:12:47 +01:00
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_hide_watched:
item.setChecked(!item.isChecked());
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
preferences.edit()
.putBoolean(Settings.KEY_PREF_TVSHOW_EPISODES_FILTER_HIDE_WATCHED, item.isChecked())
.apply();
refreshList();
2015-01-14 12:12:47 +01:00
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* Episodes list query parameters.
*/
private interface EpisodesListQuery {
String[] PROJECTION = {
BaseColumns._ID,
MediaContract.Episodes.EPISODEID,
MediaContract.Episodes.EPISODE,
MediaContract.Episodes.THUMBNAIL,
MediaContract.Episodes.PLAYCOUNT,
MediaContract.Episodes.TITLE,
MediaContract.Episodes.RUNTIME,
MediaContract.Episodes.FIRSTAIRED,
};
String SORT = MediaContract.Episodes.EPISODE + " ASC";
int ID = 0;
int EPISODEID = 1;
int EPISODE = 2;
int THUMBNAIL = 3;
int PLAYCOUNT = 4;
int TITLE = 5;
int RUNTIME = 6;
int FIRSTAIRED = 7;
2015-01-14 12:12:47 +01:00
}
private class SeasonsEpisodesAdapter extends RecyclerViewCursorAdapter {
2015-01-14 12:12:47 +01:00
private int themeAccentColor;
private HostManager hostManager;
private int artWidth;
private int artHeight;
2015-01-14 12:12:47 +01:00
SeasonsEpisodesAdapter(Context context) {
2015-01-14 12:12:47 +01:00
// Get the default accent color
Resources.Theme theme = context.getTheme();
TypedArray styledAttributes = theme.obtainStyledAttributes(new int[] {
R.attr.colorAccent
2015-01-14 12:12:47 +01:00
});
themeAccentColor = styledAttributes.getColor(styledAttributes.getIndex(0), getResources().getColor(R.color.default_accent));
2015-01-14 12:12:47 +01:00
styledAttributes.recycle();
hostManager = HostManager.getInstance(context);
2015-01-14 12:12:47 +01:00
// Get the art dimensions
Resources resources = context.getResources();
artWidth = (int)(resources.getDimension(R.dimen.episodelist_art_width) /
UIUtils.IMAGE_RESIZE_FACTOR);
artHeight = (int)(resources.getDimension(R.dimen.episodelist_art_heigth) /
UIUtils.IMAGE_RESIZE_FACTOR);
2015-01-14 12:12:47 +01:00
}
@Override
public RecyclerViewCursorAdapter.CursorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getActivity())
.inflate(R.layout.list_item_episode, parent, false);
return new ViewHolder(view, getActivity(), themeAccentColor,
contextlistItemMenuClickListener, hostManager,
artWidth, artHeight);
2015-01-14 12:12:47 +01:00
}
protected int getSectionColumnIdx() { return EpisodesListQuery.TITLE; }
}
2015-01-14 12:12:47 +01:00
/**
* View holder pattern, only for episodes
*/
static class ViewHolder extends RecyclerViewCursorAdapter.CursorViewHolder {
TextView titleView;
TextView detailsView;
TextView episodenumberView;
ImageView contextMenuView;
ImageView checkmarkView;
ImageView artView;
HostManager hostManager;
int artWidth;
int artHeight;
Context context;
int themeAccentColor;
AbstractFragment.DataHolder dataHolder = new AbstractFragment.DataHolder(0);
ViewHolder(View itemView, Context context, int themeAccentColor,
View.OnClickListener contextlistItemMenuClickListener,
HostManager hostManager,
int artWidth, int artHeight) {
super(itemView);
this.context = context;
this.themeAccentColor = themeAccentColor;
this.hostManager = hostManager;
this.artWidth = artWidth;
this.artHeight = artHeight;
titleView = itemView.findViewById(R.id.title);
detailsView = itemView.findViewById(R.id.details);
episodenumberView = itemView.findViewById(R.id.episode_number);
contextMenuView = itemView.findViewById(R.id.list_context_menu);
checkmarkView = itemView.findViewById(R.id.checkmark);
artView = itemView.findViewById(R.id.art);
contextMenuView.setOnClickListener(contextlistItemMenuClickListener);
}
2015-01-14 12:12:47 +01:00
@Override
public void bindView(Cursor cursor) {
2015-01-14 12:12:47 +01:00
// Save the episode id
dataHolder.setId(cursor.getInt(EpisodesListQuery.EPISODEID));
dataHolder.setTitle(cursor.getString(EpisodesListQuery.TITLE));
2015-01-14 12:12:47 +01:00
episodenumberView.setText(
2015-01-14 12:12:47 +01:00
String.format(context.getString(R.string.episode_number),
cursor.getInt(EpisodesListQuery.EPISODE)));
2015-01-14 12:12:47 +01:00
int runtime = cursor.getInt(EpisodesListQuery.RUNTIME) / 60;
String duration = runtime > 0 ?
String.format(context.getString(R.string.minutes_abbrev), String.valueOf(runtime)) +
" | " + cursor.getString(EpisodesListQuery.FIRSTAIRED) :
cursor.getString(EpisodesListQuery.FIRSTAIRED);
titleView.setText(cursor.getString(EpisodesListQuery.TITLE));
detailsView.setText(duration);
2015-01-14 12:12:47 +01:00
if (cursor.getInt(EpisodesListQuery.PLAYCOUNT) > 0) {
checkmarkView.setVisibility(View.VISIBLE);
checkmarkView.setColorFilter(themeAccentColor);
2015-01-14 12:12:47 +01:00
} else {
checkmarkView.setVisibility(View.INVISIBLE);
2015-01-14 12:12:47 +01:00
}
UIUtils.loadImageWithCharacterAvatar(context, hostManager,
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
cursor.getString(EpisodesListQuery.THUMBNAIL),
dataHolder.getTitle(),
artView, artWidth, artHeight);
contextMenuView.setTag(this);
2015-01-14 12:12:47 +01:00
}
}
private View.OnClickListener contextlistItemMenuClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
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
final ViewHolder viewHolder = (ViewHolder)v.getTag();
final PlaylistType.Item playListItem = new PlaylistType.Item();
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
playListItem.episodeid = viewHolder.dataHolder.getId();
final PopupMenu popupMenu = new PopupMenu(getActivity(), 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(TVShowEpisodeListFragment.this, playListItem);
return true;
case R.id.action_queue:
MediaPlayerUtils.queue(TVShowEpisodeListFragment.this, playListItem, PlaylistType.GetPlaylistsReturnType.VIDEO);
return true;
}
return false;
}
});
popupMenu.show();
}
};
2015-01-14 12:12:47 +01:00
}