Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forEach, replace optional with lateinit #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/dev/marcosfarias/pokedex/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Pokemon?>?> {
return pokemonDAO.getById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import retrofit2.Response
class PokedexViewModel : ViewModel() {

private var listPokemon = MutableLiveData<List<Pokemon>>()
private val pokemonDAO : PokemonDAO = App.database!!.pokemonDAO()
private val pokemonDAO: PokemonDAO = App.database.pokemonDAO()

init {
initNetworkRequest()
Expand All @@ -26,11 +26,11 @@ class PokedexViewModel : ViewModel() {
override fun onResponse(call: Call<List<Pokemon>?>?, response: Response<List<Pokemon>?>?) {
response?.body()?.let {
val pokemons: List<Pokemon> = it
Thread(Runnable {
for (pokemon in pokemons){
pokemonDAO.add(pokemon)
App.database.apply {
queryExecutor.execute {
runInTransaction { pokemonDAO.add(*pokemons.toTypedArray()) }
}
}).start()
}
listPokemon.value = pokemons
}
}
Expand Down