Simplify AllCastActivity: the cast list is always passed by the calling activity/fragment (via Intent), so this activity looses the ability to fetch the list from the database.

Change calling activities to reflect that.
This commit is contained in:
Synced Synapse 2015-07-29 18:53:50 +01:00
parent a07a819898
commit 2eb2a0f494
4 changed files with 26 additions and 180 deletions

View File

@ -53,32 +53,19 @@ import butterknife.InjectView;
/**
* Activity that presents all cast of a movie or TV Show
* Accepts the title to be shown on the action bar and a ArrayList<Cast> to display
*/
public class AllCastActivity extends BaseActivity
implements LoaderManager.LoaderCallbacks<Cursor> {
public class AllCastActivity extends BaseActivity {
private static final String TAG = LogUtils.makeLogTag(AllCastActivity.class);
// Extras to be passed to this activity: type (0 - movie, 1 -tv_show) and the corresponding id
public static final String EXTRA_CAST_TYPE = "EXTRA_CAST_TYPE";
public static final String EXTRA_ID = "EXTRA_ID";
// Extras to be passed to this activity: title and the cast list
public static final String EXTRA_TITLE = "EXTRA_TITLE";
public static final String EXTRA_CAST_LIST = "EXTRA_CAST_LIST";
public static final int EXTRA_TYPE_MOVIE = 0;
public static final int EXTRA_TYPE_TVSHOW = 1;
public static final int EXTRA_TYPE_CAST_LIST = 2;
// Loader IDs
private static final int LOADER_CAST = 0;
// Passed arguments
private int cast_type;
private int movie_tvshow_id = -1;
private String movie_tvshow_title;
private ArrayList<VideoType.Cast> castArrayList;
private CursorAdapter cursorAdapter;
NavigationDrawerFragment navigationDrawerFragment;
@InjectView(R.id.cast_list) GridView castGridView;
@ -96,13 +83,11 @@ public class AllCastActivity extends BaseActivity
navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
if (savedInstanceState == null) {
cast_type = getIntent().getIntExtra(EXTRA_CAST_TYPE, EXTRA_TYPE_MOVIE);
movie_tvshow_id = getIntent().getIntExtra(EXTRA_ID, -1);
movie_tvshow_title = getIntent().getStringExtra(EXTRA_TITLE);
castArrayList = getIntent().getParcelableArrayListExtra(EXTRA_CAST_LIST);
} else {
cast_type = savedInstanceState.getInt(EXTRA_CAST_TYPE);
movie_tvshow_id = savedInstanceState.getInt(EXTRA_ID);
movie_tvshow_title = savedInstanceState.getString(EXTRA_TITLE);
castArrayList = savedInstanceState.getParcelableArrayList(EXTRA_CAST_LIST);
}
LogUtils.LOGD(TAG, "Showing cast for: " + movie_tvshow_title);
@ -117,19 +102,8 @@ public class AllCastActivity extends BaseActivity
}
});
if (cast_type == EXTRA_TYPE_CAST_LIST) {
if (savedInstanceState == null) {
castArrayList = getIntent().getParcelableArrayListExtra(EXTRA_CAST_LIST);
} else {
castArrayList = savedInstanceState.getParcelableArrayList(EXTRA_CAST_LIST);
}
CastArrayAdapter arrayAdapter = new CastArrayAdapter(this, castArrayList);
castGridView.setAdapter(arrayAdapter);
} else {
cursorAdapter = new CastCursorAdapter(this, cast_type);
castGridView.setAdapter(cursorAdapter);
getLoaderManager().initLoader(LOADER_CAST, null, this);
}
CastArrayAdapter arrayAdapter = new CastArrayAdapter(this, castArrayList);
castGridView.setAdapter(arrayAdapter);
setupActionBar(movie_tvshow_title);
}
@ -145,13 +119,10 @@ public class AllCastActivity extends BaseActivity
}
@Override
protected void onSaveInstanceState (Bundle outState) {
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(EXTRA_CAST_TYPE, cast_type);
outState.putInt(EXTRA_ID, movie_tvshow_id);
outState.putString(EXTRA_TITLE, movie_tvshow_title);
if (cast_type == EXTRA_TYPE_CAST_LIST)
outState.putParcelableArrayList(EXTRA_CAST_LIST, castArrayList);
outState.putParcelableArrayList(EXTRA_CAST_LIST, castArrayList);
}
private void setupActionBar(String title) {
@ -166,49 +137,6 @@ public class AllCastActivity extends BaseActivity
actionBar.setTitle(getResources().getString(R.string.cast) + " - " + title);
}
/**
* Loader callbacks
*/
/** {@inheritDoc} */
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Uri uri;
int hostId = HostManager.getInstance(this).getHostInfo().getId();
switch (cast_type) {
case EXTRA_TYPE_MOVIE:
uri = MediaContract.MovieCast.buildMovieCastListUri(hostId, movie_tvshow_id);
return new CursorLoader(this, uri,
MovieDetailsFragment.MovieCastListQuery.PROJECTION,
null, null,
MovieDetailsFragment.MovieCastListQuery.SORT);
case EXTRA_TYPE_TVSHOW:
uri = MediaContract.TVShowCast.buildTVShowCastListUri(hostId, movie_tvshow_id);
return new CursorLoader(this, uri,
TVShowOverviewFragment.TVShowCastListQuery.PROJECTION,
null, null,
TVShowOverviewFragment.TVShowCastListQuery.SORT);
default:
return null;
}
}
/** {@inheritDoc} */
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
LogUtils.LOGD(TAG, "cursor count: " + cursor.getCount());
cursorAdapter.swapCursor(cursor);
// To prevent the empty text from appearing on the first load, set it now
emptyView.setText(getString(R.string.no_cast_info));
}
/** {@inheritDoc} */
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
// Release loader's data
cursorAdapter.swapCursor(null);
}
public static class CastArrayAdapter extends ArrayAdapter<VideoType.Cast> {
private HostManager hostManager;
private int artWidth = -1, artHeight = -1;
@ -266,88 +194,6 @@ public class AllCastActivity extends BaseActivity
}
}
private static class CastCursorAdapter extends CursorAdapter {
Context context;
private HostManager hostManager;
private int artWidth = -1, artHeight = -1;
private int name_idx;
private int role_idx = 3;
private int thumbnail_idx = 4;
public CastCursorAdapter(Context context, int castType) {
super(context, null, false);
this.context = context;
this.hostManager = HostManager.getInstance(context);
// Get the art dimensions
// Resources resources = context.getResources();
// artWidth = (int)(resources.getDimension(R.dimen.movielist_art_width) /
// UIUtils.IMAGE_RESIZE_FACTOR);
// artHeight = (int)(resources.getDimension(R.dimen.movielist_art_heigth) /
// UIUtils.IMAGE_RESIZE_FACTOR);
name_idx = (castType == EXTRA_TYPE_MOVIE) ?
MovieDetailsFragment.MovieCastListQuery.NAME :
TVShowOverviewFragment.TVShowCastListQuery.NAME;
role_idx = (castType == EXTRA_TYPE_MOVIE) ?
MovieDetailsFragment.MovieCastListQuery.ROLE :
TVShowOverviewFragment.TVShowCastListQuery.ROLE;
thumbnail_idx = (castType == EXTRA_TYPE_MOVIE) ?
MovieDetailsFragment.MovieCastListQuery.THUMBNAIL :
TVShowOverviewFragment.TVShowCastListQuery.THUMBNAIL;
}
/** {@inheritDoc} */
@Override
public View newView(Context context, final Cursor cursor, ViewGroup parent) {
final View view = LayoutInflater.from(context)
.inflate(R.layout.grid_item_cast, parent, false);
if (artWidth == -1) {
Resources resources = context.getResources();
int imageMarginPx = resources.getDimensionPixelSize(R.dimen.small_padding);
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int numColumns = resources.getInteger(R.integer.cast_grid_view_columns);
artWidth = (displayMetrics.widthPixels - (2 + numColumns - 1) * imageMarginPx) / numColumns;
artHeight = (int) (artWidth * 1.5);
LogUtils.LOGD(TAG, "width: " + artWidth);
}
// Setup View holder pattern
ViewHolder viewHolder = new ViewHolder();
viewHolder.roleView = (TextView)view.findViewById(R.id.role);
viewHolder.nameView = (TextView)view.findViewById(R.id.name);
viewHolder.pictureView = (ImageView)view.findViewById(R.id.picture);
view.setTag(viewHolder);
view.getLayoutParams().width = artWidth;
view.getLayoutParams().height = artHeight;
return view;
}
/** {@inheritDoc} */
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder viewHolder = (ViewHolder)view.getTag();
String name = cursor.getString(name_idx);
viewHolder.roleView.setText(cursor.getString(role_idx));
viewHolder.nameView.setText(name);
UIUtils.loadImageWithCharacterAvatar(context, hostManager,
cursor.getString(thumbnail_idx), name,
viewHolder.pictureView, artWidth, artHeight);
viewHolder.castName = name;
}
}
/**
* View holder pattern
*/

View File

@ -102,6 +102,8 @@ public class MovieDetailsFragment extends Fragment
private int movieId = -1;
private String movieTitle;
private ArrayList<VideoType.Cast> castArrayList;
// Info for downloading the movie
private FileDownloadHelper.MovieInfo movieDownloadInfo = null;
@ -294,9 +296,8 @@ public class MovieDetailsFragment extends Fragment
@OnClick(R.id.see_all_cast)
public void onSeeAllCastClicked(View v) {
Intent launchIntent = new Intent(getActivity(), AllCastActivity.class)
.putExtra(AllCastActivity.EXTRA_CAST_TYPE, AllCastActivity.EXTRA_TYPE_MOVIE)
.putExtra(AllCastActivity.EXTRA_ID, movieId)
.putExtra(AllCastActivity.EXTRA_TITLE, movieTitle);
.putExtra(AllCastActivity.EXTRA_TITLE, movieTitle)
.putParcelableArrayListExtra(AllCastActivity.EXTRA_CAST_LIST, castArrayList);
startActivity(launchIntent);
getActivity().overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
}
@ -626,15 +627,15 @@ public class MovieDetailsFragment extends Fragment
// Transform the cursor into a List<VideoType.Cast>
if (cursor.moveToFirst()) {
List<VideoType.Cast> castList = new ArrayList<VideoType.Cast>(cursor.getCount());
castArrayList = new ArrayList<VideoType.Cast>(cursor.getCount());
do {
castList.add(new VideoType.Cast(cursor.getString(MovieCastListQuery.NAME),
cursor.getInt(MovieCastListQuery.ORDER),
cursor.getString(MovieCastListQuery.ROLE),
cursor.getString(MovieCastListQuery.THUMBNAIL)));
castArrayList.add(new VideoType.Cast(cursor.getString(MovieCastListQuery.NAME),
cursor.getInt(MovieCastListQuery.ORDER),
cursor.getString(MovieCastListQuery.ROLE),
cursor.getString(MovieCastListQuery.THUMBNAIL)));
} while (cursor.moveToNext());
UIUtils.setupCastInfo(getActivity(), castList, videoCastList);
UIUtils.setupCastInfo(getActivity(), castArrayList, videoCastList);
seeAllCast.setVisibility(
(cursor.getCount() <= Settings.DEFAULT_MAX_CAST_PICTURES) ?
View.GONE : View.VISIBLE);

View File

@ -860,8 +860,6 @@ public class NowPlayingFragment extends Fragment
@Override
public void onClick(View v) {
Intent launchIntent = new Intent(getActivity(), AllCastActivity.class)
.putExtra(AllCastActivity.EXTRA_CAST_TYPE, AllCastActivity.EXTRA_TYPE_CAST_LIST)
.putExtra(AllCastActivity.EXTRA_ID, 0)
.putExtra(AllCastActivity.EXTRA_TITLE, title)
.putParcelableArrayListExtra(AllCastActivity.EXTRA_CAST_LIST,
(ArrayList<VideoType.Cast>)getItemResult.cast);

View File

@ -82,6 +82,8 @@ public class TVShowOverviewFragment extends Fragment
private int tvshowId = -1;
private String tvshowTitle;
private ArrayList<VideoType.Cast> castArrayList;
// Controls whether a automatic sync refresh has been issued for this show
private static boolean hasIssuedOutdatedRefresh = false;
@ -259,9 +261,8 @@ public class TVShowOverviewFragment extends Fragment
@OnClick(R.id.see_all_cast)
public void onSeeAllCastClicked(View v) {
Intent launchIntent = new Intent(getActivity(), AllCastActivity.class)
.putExtra(AllCastActivity.EXTRA_CAST_TYPE, AllCastActivity.EXTRA_TYPE_TVSHOW)
.putExtra(AllCastActivity.EXTRA_ID, tvshowId)
.putExtra(AllCastActivity.EXTRA_TITLE, tvshowTitle);
.putExtra(AllCastActivity.EXTRA_TITLE, tvshowTitle)
.putParcelableArrayListExtra(AllCastActivity.EXTRA_CAST_LIST, castArrayList);
startActivity(launchIntent);
getActivity().overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
}
@ -383,15 +384,15 @@ public class TVShowOverviewFragment extends Fragment
// Transform the cursor into a List<VideoType.Cast>
if (cursor.moveToFirst()) {
List<VideoType.Cast> castList = new ArrayList<VideoType.Cast>(cursor.getCount());
castArrayList = new ArrayList<VideoType.Cast>(cursor.getCount());
do {
castList.add(new VideoType.Cast(cursor.getString(TVShowCastListQuery.NAME),
castArrayList.add(new VideoType.Cast(cursor.getString(TVShowCastListQuery.NAME),
cursor.getInt(TVShowCastListQuery.ORDER),
cursor.getString(TVShowCastListQuery.ROLE),
cursor.getString(TVShowCastListQuery.THUMBNAIL)));
} while (cursor.moveToNext());
UIUtils.setupCastInfo(getActivity(), castList, videoCastList);
UIUtils.setupCastInfo(getActivity(), castArrayList, videoCastList);
seeAllCast.setVisibility(
(cursor.getCount() <= Settings.DEFAULT_MAX_CAST_PICTURES) ?
View.GONE : View.VISIBLE);