Skip to content

Instantly share code, notes, and snippets.

@mttkay
Created July 21, 2013 21:25
Show Gist options
  • Save mttkay/6050054 to your computer and use it in GitHub Desktop.
Save mttkay/6050054 to your computer and use it in GitHub Desktop.
Note that you must setRetainInstance(true); otherwise the Observable and all associated state will get destroyed of course. So make sure you don't create strong references to views/context in onCreate or onActivityCreated, or they will leak. The nice thing about this is that there apart from splitting up the code for creating the observable from…
package com.github.super8.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import com.github.super8.R;
import com.github.super8.activities.MovieDetailsActivity;
import com.github.super8.adapters.ImageGridAdapter;
import com.github.super8.apis.tmdb.v3.TmdbApi;
import com.github.super8.model.Movie;
import com.github.super8.model.Person;
import com.github.super8.model.PersonCredits;
import com.github.super8.support.SuperToast;
import com.google.inject.Inject;
import java.util.List;
import roboguice.fragment.RoboFragment;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import rx.Observable;
import rx.Subscription;
import rx.android.RxFragmentObserver;
import rx.android.schedulers.MainThreadScheduler;
public class PersonMoviesFragment extends RoboFragment implements AdapterView.OnItemClickListener {
@InjectExtra(Person.EXTRA_KEY)
private Person person;
@InjectView(R.id.person_movies_grid)
private GridView gridView;
@Inject
private ImageGridAdapter<Movie> adapter;
@Inject
private TmdbApi tmdb;
private Observable<PersonCredits> getCredits;
private Subscription getCreditsSubscription;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
getCredits = tmdb.personCredits(person.getTmdbId())
.observeOn(new MainThreadScheduler())
.cache();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.person_movies_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adapter.setActionItemLabelResId(R.string.person_movies_see_all_label);
adapter.setRequestedItemHeight((int) getResources().getDimension(R.dimen.movie_grid_item_height));
adapter.setNumCells(getResources().getInteger(R.integer.person_movies_grid_cells));
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
getCreditsSubscription = getCredits.subscribe(new GetCreditsObserver(this));
}
@Override
public void onDestroy() {
super.onDestroy();
getCreditsSubscription.unsubscribe();
}
private static final class GetCreditsObserver extends RxFragmentObserver<PersonMoviesFragment, PersonCredits> {
public GetCreditsObserver(PersonMoviesFragment fragment) {
super(fragment);
}
@Override
public void onNext(PersonMoviesFragment fragment, PersonCredits credits) {
final int numCells = fragment.getResources().getInteger(R.integer.person_movies_grid_cells);
final List<Movie> knownForMovies = credits.getKnownForMovies(numCells);
for (Movie movie : knownForMovies) {
fragment.adapter.addItem(movie);
}
fragment.adapter.notifyDataSetChanged();
}
@Override
public void onError(PersonMoviesFragment fragment, Exception error) {
SuperToast.error(fragment.getActivity(), error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment