Adds MonsterRepository to manage access to the RoomDB store.
This commit is contained in:
@@ -10,6 +10,13 @@ import com.majinnaibu.monstercards.data.MonsterRepository;
|
|||||||
|
|
||||||
public class MonsterCardsApplication extends Application {
|
public class MonsterCardsApplication extends Application {
|
||||||
|
|
||||||
|
private AppDatabase m_db;
|
||||||
|
private MonsterRepository m_monsterLibraryRepository;
|
||||||
|
|
||||||
|
public MonsterRepository getMonsterRepository() {
|
||||||
|
return m_monsterLibraryRepository;
|
||||||
|
}
|
||||||
|
|
||||||
public static MonsterCardsApplication getInstance(Context context) {
|
public static MonsterCardsApplication getInstance(Context context) {
|
||||||
return (MonsterCardsApplication) context.getApplicationContext();
|
return (MonsterCardsApplication) context.getApplicationContext();
|
||||||
}
|
}
|
||||||
@@ -25,6 +32,8 @@ public class MonsterCardsApplication extends Application {
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
// Required initialization logic here!
|
// Required initialization logic here!
|
||||||
|
|
||||||
|
m_db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "monsters").build();
|
||||||
|
m_monsterLibraryRepository = new MonsterRepository(m_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called by the system when the device configuration changes while your component is running.
|
// Called by the system when the device configuration changes while your component is running.
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package com.majinnaibu.monstercards.data;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import com.majinnaibu.monstercards.AppDatabase;
|
import com.majinnaibu.monstercards.AppDatabase;
|
||||||
import com.majinnaibu.monstercards.helpers.StringHelper;
|
|
||||||
import com.majinnaibu.monstercards.models.Monster;
|
import com.majinnaibu.monstercards.models.Monster;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -15,39 +13,23 @@ import io.reactivex.rxjava3.core.Completable;
|
|||||||
import io.reactivex.rxjava3.core.Flowable;
|
import io.reactivex.rxjava3.core.Flowable;
|
||||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
|
||||||
public class MonsterRepository {
|
public class MonsterRepository {
|
||||||
|
|
||||||
private final AppDatabase m_db;
|
private AppDatabase m_db;
|
||||||
|
|
||||||
public MonsterRepository(@NonNull AppDatabase db) {
|
public MonsterRepository(@NonNull AppDatabase db) {
|
||||||
m_db = db;
|
m_db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Flowable<List<Monster>> getMonsters() {
|
public Flowable<List<Monster>> getMonsters() {
|
||||||
|
|
||||||
return m_db.monsterDAO()
|
return m_db.monsterDAO()
|
||||||
.getAll()
|
.getAll()
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread());
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Flowable<List<Monster>> searchMonsters(String searchText) {
|
public Flowable<Monster> getMonster(UUID monsterId) {
|
||||||
return m_db.monsterDAO()
|
|
||||||
.getAll()
|
|
||||||
.map(monsters -> {
|
|
||||||
ArrayList<Monster> filteredMonsters = new ArrayList<>();
|
|
||||||
for (Monster monster : monsters) {
|
|
||||||
if (Helpers.monsterMatchesSearch(monster, searchText)) {
|
|
||||||
filteredMonsters.add(monster);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (List<Monster>) filteredMonsters;
|
|
||||||
})
|
|
||||||
.subscribeOn(Schedulers.io())
|
|
||||||
.observeOn(AndroidSchedulers.mainThread());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Flowable<Monster> getMonster(@NonNull UUID monsterId) {
|
|
||||||
return m_db.monsterDAO()
|
return m_db.monsterDAO()
|
||||||
.loadAllByIds(new String[]{monsterId.toString()})
|
.loadAllByIds(new String[]{monsterId.toString()})
|
||||||
.map(
|
.map(
|
||||||
@@ -74,39 +56,4 @@ public class MonsterRepository {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Completable saveMonster(Monster monster) {
|
|
||||||
Completable result = m_db.monsterDAO().save(monster);
|
|
||||||
result.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Helpers {
|
|
||||||
static boolean monsterMatchesSearch(Monster monster, String searchText) {
|
|
||||||
if (StringHelper.isNullOrEmpty(searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringHelper.containsCaseInsensitive(monster.name, searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringHelper.containsCaseInsensitive(monster.size, searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringHelper.containsCaseInsensitive(monster.type, searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringHelper.containsCaseInsensitive(monster.subtype, searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringHelper.containsCaseInsensitive(monster.alignment, searchText)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user