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 {
|
||||
|
||||
private AppDatabase m_db;
|
||||
private MonsterRepository m_monsterLibraryRepository;
|
||||
|
||||
public MonsterRepository getMonsterRepository() {
|
||||
return m_monsterLibraryRepository;
|
||||
}
|
||||
|
||||
public static MonsterCardsApplication getInstance(Context context) {
|
||||
return (MonsterCardsApplication) context.getApplicationContext();
|
||||
}
|
||||
@@ -25,6 +32,8 @@ public class MonsterCardsApplication extends Application {
|
||||
super.onCreate();
|
||||
// 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.
|
||||
|
||||
@@ -3,10 +3,8 @@ package com.majinnaibu.monstercards.data;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.majinnaibu.monstercards.AppDatabase;
|
||||
import com.majinnaibu.monstercards.helpers.StringHelper;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -15,39 +13,23 @@ import io.reactivex.rxjava3.core.Completable;
|
||||
import io.reactivex.rxjava3.core.Flowable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class MonsterRepository {
|
||||
|
||||
private final AppDatabase m_db;
|
||||
private AppDatabase m_db;
|
||||
|
||||
public MonsterRepository(@NonNull AppDatabase db) {
|
||||
m_db = db;
|
||||
}
|
||||
|
||||
public Flowable<List<Monster>> getMonsters() {
|
||||
|
||||
return m_db.monsterDAO()
|
||||
.getAll()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread());
|
||||
}
|
||||
|
||||
public Flowable<List<Monster>> searchMonsters(String searchText) {
|
||||
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) {
|
||||
public Flowable<Monster> getMonster(UUID monsterId) {
|
||||
return m_db.monsterDAO()
|
||||
.loadAllByIds(new String[]{monsterId.toString()})
|
||||
.map(
|
||||
@@ -74,39 +56,4 @@ public class MonsterRepository {
|
||||
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