Skip to content

Commit 494d3e1

Browse files
committed
Rearrange properties in DetailViewModel
2 parents 19c0ecf + f7af05e commit 494d3e1

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

.github/workflows/android.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Android CI
33
on:
44
push:
55
branches:
6-
- master
6+
- main
77

88
jobs:
99
build:

app/src/main/java/com/skydoves/pokedex/di/PersistenceModule.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import androidx.room.Room
2121
import com.skydoves.pokedex.persistence.AppDatabase
2222
import com.skydoves.pokedex.persistence.PokemonDao
2323
import com.skydoves.pokedex.persistence.PokemonInfoDao
24+
import com.skydoves.pokedex.persistence.TypeResponseConverter
25+
import com.squareup.moshi.Moshi
2426
import dagger.Module
2527
import dagger.Provides
2628
import dagger.hilt.InstallIn
@@ -33,10 +35,20 @@ object PersistenceModule {
3335

3436
@Provides
3537
@Singleton
36-
fun provideAppDatabase(application: Application): AppDatabase {
38+
fun provideMoshi(): Moshi {
39+
return Moshi.Builder().build()
40+
}
41+
42+
@Provides
43+
@Singleton
44+
fun provideAppDatabase(
45+
application: Application,
46+
typeResponseConverter: TypeResponseConverter
47+
): AppDatabase {
3748
return Room
3849
.databaseBuilder(application, AppDatabase::class.java, "Pokedex.db")
3950
.fallbackToDestructiveMigration()
51+
.addTypeConverter(typeResponseConverter)
4052
.build()
4153
}
4254

@@ -51,4 +63,10 @@ object PersistenceModule {
5163
fun providePokemonInfoDao(appDatabase: AppDatabase): PokemonInfoDao {
5264
return appDatabase.pokemonInfoDao()
5365
}
66+
67+
@Provides
68+
@Singleton
69+
fun provideTypeResponseConverter(moshi: Moshi): TypeResponseConverter {
70+
return TypeResponseConverter(moshi)
71+
}
5472
}

app/src/main/java/com/skydoves/pokedex/persistence/TypeResponseConverter.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@
1616

1717
package com.skydoves.pokedex.persistence
1818

19+
import androidx.room.ProvidedTypeConverter
1920
import androidx.room.TypeConverter
2021
import com.skydoves.pokedex.model.PokemonInfo
2122
import com.squareup.moshi.JsonAdapter
2223
import com.squareup.moshi.Moshi
2324
import com.squareup.moshi.Types
25+
import javax.inject.Inject
2426

25-
object TypeResponseConverter {
27+
@ProvidedTypeConverter
28+
class TypeResponseConverter @Inject constructor(
29+
private val moshi: Moshi
30+
) {
2631

27-
private val moshi = Moshi.Builder().build()
28-
29-
@JvmStatic
3032
@TypeConverter
3133
fun fromString(value: String): List<PokemonInfo.TypeResponse>? {
3234
val listType = Types.newParameterizedType(List::class.java, PokemonInfo.TypeResponse::class.java)
3335
val adapter: JsonAdapter<List<PokemonInfo.TypeResponse>> = moshi.adapter(listType)
3436
return adapter.fromJson(value)
3537
}
3638

37-
@JvmStatic
3839
@TypeConverter
3940
fun fromInfoType(type: List<PokemonInfo.TypeResponse>?): String {
4041
val listType = Types.newParameterizedType(List::class.java, PokemonInfo.TypeResponse::class.java)

app/src/main/java/com/skydoves/pokedex/ui/details/DetailActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class DetailActivity : DataBindingActivity() {
6161
}
6262

6363
companion object {
64-
6564
@VisibleForTesting const val EXTRA_POKEMON = "EXTRA_POKEMON"
6665

6766
fun startActivity(transformationLayout: TransformationLayout, pokemon: Pokemon) {

app/src/main/java/com/skydoves/pokedex/ui/details/DetailViewModel.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,24 @@ class DetailViewModel @AssistedInject constructor(
3434
@Assisted private val pokemonName: String
3535
) : LiveCoroutinesViewModel() {
3636

37+
val pokemonInfoLiveData: LiveData<PokemonInfo?>
38+
39+
private val _toastLiveData: MutableLiveData<String> = MutableLiveData()
40+
val toastLiveData: LiveData<String> get() = _toastLiveData
41+
3742
val isLoading: ObservableBoolean = ObservableBoolean(false)
38-
val toastLiveData: MutableLiveData<String> = MutableLiveData()
39-
val pokemonInfoLiveData: LiveData<PokemonInfo?> = launchOnViewModelScope(
40-
block = {
43+
44+
init {
45+
Timber.d("init DetailViewModel")
46+
47+
pokemonInfoLiveData = launchOnViewModelScope {
4148
isLoading.set(true)
4249
detailRepository.fetchPokemonInfo(
4350
name = pokemonName,
4451
onSuccess = { isLoading.set(false) },
45-
onError = { toastLiveData.postValue(it) }
52+
onError = { _toastLiveData.postValue(it) }
4653
).asLiveData()
4754
}
48-
)
49-
50-
init {
51-
Timber.d("init DetailViewModel")
5255
}
5356

5457
@AssistedInject.Factory

app/src/test/java/com/skydoves/pokedex/persistence/LocalDatabase.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.skydoves.pokedex.persistence
1818

1919
import androidx.room.Room
2020
import androidx.test.core.app.ApplicationProvider.getApplicationContext
21+
import com.squareup.moshi.Moshi
2122
import org.junit.After
2223
import org.junit.Before
2324
import org.junit.runner.RunWith
@@ -31,8 +32,10 @@ abstract class LocalDatabase {
3132

3233
@Before
3334
fun initDB() {
35+
val moshi = Moshi.Builder().build()
3436
db = Room.inMemoryDatabaseBuilder(getApplicationContext(), AppDatabase::class.java)
3537
.allowMainThreadQueries()
38+
.addTypeConverter(TypeResponseConverter(moshi))
3639
.build()
3740
}
3841

dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ext.versions = [
3434
// architecture components
3535
fragmentVersion : '1.2.5',
3636
lifecycleVersion : '2.2.0',
37-
roomVersion : '2.2.5',
37+
roomVersion : '2.3.0-alpha03',
3838
archCompomentVersion : '2.1.0',
3939

4040
// startup

0 commit comments

Comments
 (0)