Skip to content

Commit 22c964f

Browse files
Improve UseCase#8
1 parent bd92a73 commit 22c964f

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase8/AndroidVersionEntity.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import com.lukaslechner.coroutineusecasesonandroid.mock.AndroidVersion
77
@Entity(tableName = "androidversions")
88
data class AndroidVersionEntity(@PrimaryKey val apiLevel: Int, val name: String)
99

10-
fun AndroidVersionEntity.mapToUiModel() = AndroidVersion(this.apiLevel, this.name)
11-
1210
fun List<AndroidVersionEntity>.mapToUiModelList() = map {
13-
it.mapToUiModel()
11+
AndroidVersion(it.apiLevel, it.name)
1412
}
1513

1614
fun AndroidVersion.mapToEntity() = AndroidVersionEntity(this.apiLevel, this.name)

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase8/RoomAndCoroutinesActivity.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ class RoomAndCoroutinesActivity : BaseActivity() {
1717
override fun getToolbarTitle() = useCase8Description
1818

1919
private val binding by lazy { ActivityQueryfromroomdatabaseBinding.inflate(layoutInflater) }
20-
private val viewModel: RoomAndCoroutinesViewModel by viewModels()
20+
21+
private val viewModel: RoomAndCoroutinesViewModel by viewModels {
22+
ViewModelFactory(
23+
mockApi(),
24+
AndroidVersionDatabase.getInstance(applicationContext).androidVersionDao()
25+
)
26+
}
2127

2228
override fun onCreate(savedInstanceState: Bundle?) {
2329
super.onCreate(savedInstanceState)
2430
setContentView(binding.root)
2531

2632
// ugly setter injection of the database
2733
// could be improved to use constructor injection by using ViewModelFactory
28-
viewModel.database =
29-
AndroidVersionDatabase.getInstance(applicationContext).androidVersionDao()
34+
/*viewModel.database =
35+
AndroidVersionDatabase.getInstance(applicationContext).androidVersionDao()*/
3036
viewModel.uiState().observe(this, Observer { uiState ->
3137
if (uiState != null) {
3238
render(uiState)
@@ -71,12 +77,12 @@ class RoomAndCoroutinesActivity : BaseActivity() {
7177

7278
private fun onSuccess(uiState: UiState.Success) = with(binding) {
7379
when (uiState.dataSource) {
74-
DataSource.Network -> {
80+
DataSource.NETWORK -> {
7581
progressBarLoadFromNetwork.setGone()
7682
imageViewNetworkLoadSuccessOrError.setImageDrawable(getDrawable(R.drawable.ic_check_green_24dp))
7783
imageViewNetworkLoadSuccessOrError.setVisible()
7884
}
79-
DataSource.Database -> {
85+
DataSource.DATABASE -> {
8086
progressBarLoadFromDb.setGone()
8187
imageViewDatabaseLoadSuccessOrError.setImageDrawable(getDrawable(R.drawable.ic_check_green_24dp))
8288
imageViewDatabaseLoadSuccessOrError.setVisible()
@@ -93,12 +99,12 @@ class RoomAndCoroutinesActivity : BaseActivity() {
9399

94100
private fun onError(uiState: UiState.Error) = with(binding) {
95101
when (uiState.dataSource) {
96-
is DataSource.Network -> {
102+
DataSource.NETWORK -> {
97103
progressBarLoadFromNetwork.setGone()
98104
imageViewNetworkLoadSuccessOrError.setImageDrawable(getDrawable(R.drawable.ic_clear_red_24dp))
99105
imageViewNetworkLoadSuccessOrError.setVisible()
100106
}
101-
is DataSource.Database -> {
107+
DataSource.DATABASE -> {
102108
progressBarLoadFromDb.setGone()
103109
imageViewDatabaseLoadSuccessOrError.setImageDrawable(getDrawable(R.drawable.ic_clear_red_24dp))
104110
imageViewDatabaseLoadSuccessOrError.setVisible()

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase8/RoomAndCoroutinesViewModel.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,45 @@ import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
66
import kotlinx.coroutines.launch
77

88
class RoomAndCoroutinesViewModel(
9-
private val api: MockApi = mockApi(),
10-
var database: AndroidVersionDao? = null
9+
private val api: MockApi,
10+
var database: AndroidVersionDao
1111
) : BaseViewModel<UiState>() {
1212

1313
fun loadData() {
1414
uiState.value = UiState.Loading.LoadFromDb
15+
1516
viewModelScope.launch {
1617

17-
val localVersions = database!!.getAndroidVersions()
18+
val localVersions = database.getAndroidVersions()
1819
if (localVersions.isNotEmpty()) {
1920
uiState.value =
20-
UiState.Success(DataSource.Database, localVersions.mapToUiModelList())
21+
UiState.Success(DataSource.DATABASE, localVersions.mapToUiModelList())
2122
} else {
2223
uiState.value =
23-
UiState.Error(DataSource.Database, "Database empty!")
24+
UiState.Error(DataSource.DATABASE, "Database empty!")
2425
}
2526

2627
uiState.value = UiState.Loading.LoadFromNetwork
2728
try {
2829
val recentVersions = api.getRecentAndroidVersions()
2930
for (recentVersion in recentVersions) {
30-
database!!.insert(recentVersion.mapToEntity())
31+
database.insert(recentVersion.mapToEntity())
3132
}
32-
uiState.value = UiState.Success(DataSource.Network, recentVersions)
33+
uiState.value = UiState.Success(DataSource.NETWORK, recentVersions)
3334
} catch (exception: Exception) {
34-
uiState.value = UiState.Error(DataSource.Network, "Network Request failed")
35+
uiState.value = UiState.Error(DataSource.NETWORK, "Network Request failed")
3536
}
3637
}
3738
}
3839

3940
fun clearDatabase() {
4041
viewModelScope.launch {
41-
database!!.clear()
42+
database.clear()
4243
}
4344
}
4445
}
4546

46-
sealed class DataSource(val name: String) {
47-
object Database : DataSource("Database")
48-
object Network : DataSource("Network")
47+
enum class DataSource(val dataSourceName: String) {
48+
DATABASE("Database"),
49+
NETWORK("Network")
4950
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase8
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.ViewModelProvider
5+
import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
6+
7+
class ViewModelFactory(private val api: MockApi, private val database: AndroidVersionDao) :
8+
ViewModelProvider.Factory {
9+
10+
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
11+
return modelClass.getConstructor(MockApi::class.java, AndroidVersionDao::class.java)
12+
.newInstance(api, database)
13+
}
14+
}

app/src/main/res/layout/activity_queryfromroomdatabase.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
android:id="@+id/textViewResult"
9292
android:layout_width="match_parent"
9393
android:layout_height="match_parent"
94+
android:padding="8dp"
9495
android:textColor="@color/primary_text"
9596
android:textSize="16sp"
9697
tools:text="Results" />

0 commit comments

Comments
 (0)