Skip to content

Instantly share code, notes, and snippets.

View husaynhakeem's full-sized avatar

Husayn Hakeem husaynhakeem

View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rv_pokemons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context="io.husayn.paging_library_sample.listing.MainActivity"/>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:cardCornerRadius="@dimen/cardview_default_radius"
app:cardElevation="@dimen/cardview_default_elevation"
app:cardUseCompatPadding="true">
public static final DiffCallback<Pokemon> DIFF_CALLBACK = new DiffCallback<Pokemon>() {
@Override
public boolean areItemsTheSame(@NonNull Pokemon oldPokemon, @NonNull Pokemon newPokemon) {
return oldPokemon.id == newPokemon.id;
}
@Override
public boolean areContentsTheSame(@NonNull Pokemon oldPokemon, @NonNull Pokemon newPokemon) {
return oldPokemon.name.equals(newPokemon.name);
}
@Query("SELECT * FROM pokemon ORDER BY id ASC")
LivePagedListProvider<Integer, Pokemon> pokemons();
@Insert
void insert(Pokemon... pokemons);
@Database(entities = {Pokemon.class}, version = 1)
public abstract class PokemonDataBase extends RoomDatabase {
private static final String POKEMON_DB = "pokemon.db";
public abstract PokemonDao pokemonDao();
private static PokemonDataBase pokemonDB;
public static PokemonDataBase getInstance(Context context) {
@Database(entities = {Pokemon.class}, version = 1)
public abstract class PokemonDataBase extends RoomDatabase {
private static final String POKEMON_DB = "pokemon.db";
public abstract PokemonDao pokemonDao();
private static PokemonDataBase pokemonDB;
public static PokemonDataBase getInstance(Context context) {
if (pokemonDB == null)
pokemonDB = Room.databaseBuilder(context.getApplicationContext(), PokemonDataBase.class, POKEMON_DB).build();
public void populateDB() {
Completable.fromAction(() -> PokemonDataBase.getInstance(context).pokemonDao().insert(pokemonList()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onDBPopulationSuccess, this::onDBPopulationFailure);
}
public PokemonAdapter() {
super(Pokemon.DIFF_CALLBACK);
}
public MainViewModel() {
PokemonDao pokemonDao = PokemonDataBase.getInstance(PokemonApplication.getContext()).pokemonDao();
pokemonList = pokemonDao.pokemons().create(INITIAL_LOAD_KEY, new PagedList.Config.Builder()
.setPageSize(PAGE_SIZE)
.setPrefetchDistance(PREFETCH_DISTANCE)
.setEnablePlaceholders(true)
.build()
);
}
MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
final PokemonAdapter adapter = new PokemonAdapter();
viewModel.pokemonList.observe(this, adapter::setList);