Skip to content

Commit 79b4575

Browse files
committed
Fixes stats VM but some androidtests fail on prod
1 parent 0267e4a commit 79b4575

File tree

5 files changed

+27
-69
lines changed

5 files changed

+27
-69
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
compileSdkVersion rootProject.compileSdkVersion
88

99
defaultConfig {
10-
applicationId "com.example.android.architecture.blueprints.master"
10+
applicationId "com.example.android.architecture.blueprints.reactive"
1111
minSdkVersion rootProject.minSdkVersion
1212
targetSdkVersion rootProject.targetSdkVersion
1313
versionCode 1

app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsViewModel.kt

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package com.example.android.architecture.blueprints.todoapp.statistics
1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.ViewModel
22+
import androidx.lifecycle.map
2223
import androidx.lifecycle.viewModelScope
24+
import com.example.android.architecture.blueprints.todoapp.data.Result
25+
import com.example.android.architecture.blueprints.todoapp.data.Result.Error
2326
import com.example.android.architecture.blueprints.todoapp.data.Result.Success
2427
import com.example.android.architecture.blueprints.todoapp.data.Task
2528
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
@@ -33,68 +36,30 @@ class StatisticsViewModel(
3336
private val tasksRepository: TasksRepository
3437
) : ViewModel() {
3538

36-
private val _dataLoading = MutableLiveData<Boolean>()
37-
val dataLoading: LiveData<Boolean> = _dataLoading
38-
39-
private val _error = MutableLiveData<Boolean>()
40-
val error: LiveData<Boolean> = _error
41-
42-
/**
43-
* Controls whether the stats are shown or a "No data" message.
44-
*/
45-
private val _empty = MutableLiveData<Boolean>()
46-
val empty: LiveData<Boolean> = _empty
47-
48-
private val _activeTasksPercent = MutableLiveData<Float>()
49-
val activeTasksPercent: LiveData<Float> = _activeTasksPercent
50-
51-
private val _completedTasksPercent = MutableLiveData<Float>()
52-
val completedTasksPercent: LiveData<Float> = _completedTasksPercent
53-
54-
private var activeTasks = 0
55-
56-
private var completedTasks = 0
57-
58-
init {
59-
start()
39+
private val tasks: LiveData<Result<List<Task>>> = tasksRepository.observeTasks()
40+
private val _dataLoading = MutableLiveData<Boolean>(false)
41+
private val stats: LiveData<StatsResult?> = tasks.map {
42+
if (it is Success) {
43+
getActiveAndCompletedStats(it.data)
44+
} else {
45+
null
46+
}
6047
}
6148

62-
fun start() {
63-
if (_dataLoading.value == true) {
64-
return
65-
}
66-
_dataLoading.value = true
49+
val activeTasksPercent = stats.map {
50+
it?.activeTasksPercent ?: 0f }
51+
val completedTasksPercent: LiveData<Float> = stats.map { it?.completedTasksPercent ?: 0f }
52+
val dataLoading: LiveData<Boolean> = _dataLoading
53+
val error: LiveData<Boolean> = tasks.map { it is Error }
54+
val empty: LiveData<Boolean> = tasks.map { (it as? Success)?.data.isNullOrEmpty() }
6755

56+
fun refresh() {
57+
_dataLoading.value = true
6858
wrapEspressoIdlingResource {
6959
viewModelScope.launch {
70-
tasksRepository.getTasks().let { result ->
71-
if (result is Success) {
72-
_error.value = false
73-
computeStats(result.data)
74-
} else {
75-
_error.value = true
76-
activeTasks = 0
77-
completedTasks = 0
78-
computeStats(null)
79-
}
80-
}
60+
tasksRepository.refreshTasks()
61+
_dataLoading.value = false
8162
}
8263
}
8364
}
84-
85-
fun refresh() {
86-
start()
87-
}
88-
89-
/**
90-
* Called when new data is ready.
91-
*/
92-
private fun computeStats(tasks: List<Task>?) {
93-
getActiveAndCompletedStats(tasks).let {
94-
_activeTasksPercent.value = it.activeTasksPercent
95-
_completedTasksPercent.value = it.completedTasksPercent
96-
}
97-
_empty.value = tasks.isNullOrEmpty()
98-
_dataLoading.value = false
99-
}
10065
}

app/src/sharedTest/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsFragmentTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class StatisticsFragmentTest {
9797
.getString(R.string.statistics_active_tasks, 50.0f)
9898
val expectedCompletedTaskText = getApplicationContext<Context>()
9999
.getString(R.string.statistics_completed_tasks, 50.0f)
100+
Thread.sleep(4000)
100101
// check that both info boxes are displayed and contain the correct info
101102
onView(withId(R.id.stats_active_text)).check(matches(isDisplayed()))
102103
onView(withId(R.id.stats_active_text)).check(matches(withText(expectedActiveTaskText)))

app/src/test/java/com/example/android/architecture/blueprints/todoapp/FakeFailingTasksRemoteDataSource.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.android.architecture.blueprints.todoapp
1818

1919
import androidx.lifecycle.LiveData
20+
import androidx.lifecycle.liveData
2021
import com.example.android.architecture.blueprints.todoapp.data.Result
2122
import com.example.android.architecture.blueprints.todoapp.data.Task
2223
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
@@ -31,15 +32,15 @@ object FakeFailingTasksRemoteDataSource : TasksDataSource {
3132
}
3233

3334
override fun observeTasks(): LiveData<Result<List<Task>>> {
34-
TODO("not implemented")
35+
return liveData { emit(getTasks()) }
3536
}
3637

3738
override suspend fun refreshTasks() {
3839
TODO("not implemented")
3940
}
4041

4142
override fun observeTask(taskId: String): LiveData<Result<Task>> {
42-
TODO("not implemented")
43+
return liveData { emit(getTask(taskId)) }
4344
}
4445

4546
override suspend fun refreshTask(taskId: String) {

app/src/test/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsViewModelTest.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class StatisticsViewModelTest {
6060
fun loadEmptyTasksFromRepository_EmptyResults() = mainCoroutineRule.runBlockingTest {
6161
// Given an initialized StatisticsViewModel with no tasks
6262

63-
// When loading of Tasks is requested
64-
statisticsViewModel.start()
65-
6663
// Then the results are empty
6764
assertThat(statisticsViewModel.empty.awaitNextValue()).isTrue()
6865
}
@@ -76,9 +73,6 @@ class StatisticsViewModelTest {
7673
val task4 = Task("Title4", "Description4", true)
7774
tasksRepository.addTasks(task1, task2, task3, task4)
7875

79-
// When loading of Tasks is requested
80-
statisticsViewModel.start()
81-
8276
// Then the results are not empty
8377
assertThat(statisticsViewModel.empty.awaitNextValue())
8478
.isFalse()
@@ -99,9 +93,6 @@ class StatisticsViewModelTest {
9993
)
10094
)
10195

102-
// When statistics are loaded
103-
errorViewModel.start()
104-
10596
// Then an error message is shown
10697
assertThat(errorViewModel.empty.awaitNextValue()).isTrue()
10798
assertThat(errorViewModel.error.awaitNextValue()).isTrue()
@@ -113,7 +104,7 @@ class StatisticsViewModelTest {
113104
mainCoroutineRule.pauseDispatcher()
114105

115106
// Load the task in the viewmodel
116-
statisticsViewModel.start()
107+
statisticsViewModel.refresh()
117108

118109
// Then progress indicator is shown
119110
assertThat(statisticsViewModel.dataLoading.awaitNextValue()).isTrue()

0 commit comments

Comments
 (0)