Skip to content

Instantly share code, notes, and snippets.

View MainViewModel.java
public class MainViewModel extends AndroidViewModel {
private LiveData<List<StarWarsMovie>> moviesLiveData;
@Inject AppDatabase mDb;
public MainViewModel(Application application) {
super(application);
((StarWarsApplication) getApplication()).getAppComponent().inject(this);
requestDataUpdates();
View AppModule.java
@Module public class AppModule {
private Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides @Singleton Context providesAppContext() {
return application.getApplicationContext();
View AppDatabase.java
@Database(entities = { StarWarsMovie.class }, version = 1) public abstract class AppDatabase
extends RoomDatabase {
public abstract StarWarsMovieDao starWarsMovieModel();
}
View StarWarsMovieDao.java
@Dao
public interface StarWarsMovieDao {
@Query("select * from StarWarsMovie") LiveData<List<StarWarsMovie>> loadMovies();
@Query("DELETE FROM StarWarsMovie") void deleteAll();
@Insert(onConflict = REPLACE) void insertMovie(StarWarsMovie starWarsMovie);
}
View StarWarsService.java
public class StarWarsService extends IntentService {
@Inject AppDatabase db;
public StarWarsService() {
super("StarWarsService");
}
@Override public void onCreate() {
super.onCreate();
View MainActivity.java
public class MainActivity extends AppCompatActivity implements MainActivityView {
@BindView(R.id.lv_am_movie_list) ListView listView;
@BindView(R.id.pb_am_loading) ProgressBar progressBar;
public static MainPresenter mainPresenter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View MainActivityView.java
public interface MainActivityView extends MvpView {
ListView getListView();
ProgressBar getProgressBar();
}
View BasePresenter.java
public abstract class BasePresenter<T extends MvpView> {
HandlerThread thread;
Looper looper;
Handler handler;
private T mView;
public BasePresenter() {
}
View MainPresenter.java
public class MainPresenter extends BasePresenter<MainActivityView> {
public static final String LOAD_COMPLETE_ACTION =
"com.academy.android.starwarsmovies.load_complete";
public static final String DATA_KEY = "data_key";
private StarWarsMovieDataReceiver movieDataReceiver;
private ArrayList<StarWarsMovie> movieList;
@Override public void attachView(MainActivityView mainActivityView) {
super.attachView(mainActivityView);
registerLoadDataReceiver();
View StarWarsService.java
public class StarWarsService extends IntentService {
public StarWarsService() {
super("StarWarsService");
}
@Override protected void onHandleIntent(Intent intent) {
if (intent != null) {
String json = ResourcesUtil.loadJson(this);
Gson gson = new GsonBuilder().setDateFormat("MMM d, yyyy").create();
ArrayList<StarWarsMovie> moviesList =