This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Query("SELECT * FROM pokemon ORDER BY id ASC") | |
LivePagedListProvider<Integer, Pokemon> pokemons(); | |
@Insert | |
void insert(Pokemon... pokemons); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void populateDB() { | |
Completable.fromAction(() -> PokemonDataBase.getInstance(context).pokemonDao().insert(pokemonList())) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(this::onDBPopulationSuccess, this::onDBPopulationFailure); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public PokemonAdapter() { | |
super(Pokemon.DIFF_CALLBACK); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class); | |
final PokemonAdapter adapter = new PokemonAdapter(); | |
viewModel.pokemonList.observe(this, adapter::setList); |
OlderNewer