diff --git a/app/src/main/java/dev/marcosfarias/pokedex/App.kt b/app/src/main/java/dev/marcosfarias/pokedex/App.kt index 5370eb4..c41ec01 100644 --- a/app/src/main/java/dev/marcosfarias/pokedex/App.kt +++ b/app/src/main/java/dev/marcosfarias/pokedex/App.kt @@ -8,7 +8,7 @@ import dev.marcosfarias.pokedex.database.AppDatabase class App : Application() { companion object { var context: Context? = null - var database: AppDatabase? = null + lateinit var database: AppDatabase } override fun onCreate() { diff --git a/app/src/main/java/dev/marcosfarias/pokedex/ui/dashboard/DashboardViewModel.kt b/app/src/main/java/dev/marcosfarias/pokedex/ui/dashboard/DashboardViewModel.kt index 1b665cd..4efb7c2 100644 --- a/app/src/main/java/dev/marcosfarias/pokedex/ui/dashboard/DashboardViewModel.kt +++ b/app/src/main/java/dev/marcosfarias/pokedex/ui/dashboard/DashboardViewModel.kt @@ -1,19 +1,14 @@ package dev.marcosfarias.pokedex.ui.dashboard import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import dev.marcosfarias.pokedex.App import dev.marcosfarias.pokedex.database.dao.PokemonDAO import dev.marcosfarias.pokedex.model.Pokemon -import dev.marcosfarias.pokedex.repository.APIService -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response class DashboardViewModel : ViewModel() { - private val pokemonDAO : PokemonDAO = App.database!!.pokemonDAO() + private val pokemonDAO: PokemonDAO = App.database.pokemonDAO() fun getPokemonById(id: String?): LiveData?> { return pokemonDAO.getById(id) diff --git a/app/src/main/java/dev/marcosfarias/pokedex/ui/pokedex/PokedexViewModel.kt b/app/src/main/java/dev/marcosfarias/pokedex/ui/pokedex/PokedexViewModel.kt index c466349..eeb60b0 100644 --- a/app/src/main/java/dev/marcosfarias/pokedex/ui/pokedex/PokedexViewModel.kt +++ b/app/src/main/java/dev/marcosfarias/pokedex/ui/pokedex/PokedexViewModel.kt @@ -14,7 +14,7 @@ import retrofit2.Response class PokedexViewModel : ViewModel() { private var listPokemon = MutableLiveData>() - private val pokemonDAO : PokemonDAO = App.database!!.pokemonDAO() + private val pokemonDAO: PokemonDAO = App.database.pokemonDAO() init { initNetworkRequest() @@ -26,11 +26,11 @@ class PokedexViewModel : ViewModel() { override fun onResponse(call: Call?>?, response: Response?>?) { response?.body()?.let { val pokemons: List = it - Thread(Runnable { - for (pokemon in pokemons){ - pokemonDAO.add(pokemon) + App.database.apply { + queryExecutor.execute { + runInTransaction { pokemonDAO.add(*pokemons.toTypedArray()) } } - }).start() + } listPokemon.value = pokemons } }