Skip to content

Commit c32f53b

Browse files
Improve Readability of UseCase#8
1 parent 22c964f commit c32f53b

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@ import kotlinx.coroutines.launch
77

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

1313
fun loadData() {
1414
uiState.value = UiState.Loading.LoadFromDb
1515

1616
viewModelScope.launch {
17-
1817
val localVersions = database.getAndroidVersions()
19-
if (localVersions.isNotEmpty()) {
18+
if (localVersions.isEmpty()) {
2019
uiState.value =
21-
UiState.Success(DataSource.DATABASE, localVersions.mapToUiModelList())
20+
UiState.Error(DataSource.DATABASE, "Database empty!")
2221
} else {
2322
uiState.value =
24-
UiState.Error(DataSource.DATABASE, "Database empty!")
23+
UiState.Success(DataSource.DATABASE, localVersions.mapToUiModelList())
2524
}
2625

2726
uiState.value = UiState.Loading.LoadFromNetwork
2827
try {
2928
val recentVersions = api.getRecentAndroidVersions()
30-
for (recentVersion in recentVersions) {
31-
database.insert(recentVersion.mapToEntity())
29+
for (version in recentVersions) {
30+
database.insert(version.mapToEntity())
3231
}
3332
uiState.value = UiState.Success(DataSource.NETWORK, recentVersions)
3433
} catch (exception: Exception) {
35-
uiState.value = UiState.Error(DataSource.NETWORK, "Network Request failed")
34+
uiState.value = UiState.Error(DataSource.NETWORK, "Something went wrong!")
3635
}
3736
}
3837
}

0 commit comments

Comments
 (0)