Skip to content

Commit

Permalink
Mark as seen test on the results screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Nov 19, 2024
1 parent 69c9fc7 commit 1f6e7f2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import org.ooni.probe.domain.GetSettings
import org.ooni.probe.domain.GetStorageUsed
import org.ooni.probe.domain.GetTestDescriptors
import org.ooni.probe.domain.GetTestDescriptorsBySpec
import org.ooni.probe.domain.MarkJustFinishedTestAsSeen
import org.ooni.probe.domain.ObserveAndConfigureAutoRun
import org.ooni.probe.domain.RunBackgroundStateManager
import org.ooni.probe.domain.RunDescriptors
Expand Down Expand Up @@ -294,6 +295,12 @@ class Dependencies(
private val getTestDescriptorsBySpec by lazy {
GetTestDescriptorsBySpec(getTestDescriptors = getTestDescriptors::invoke)
}
private val markJustFinishedTestAsSeen by lazy {
MarkJustFinishedTestAsSeen(
getRunBackgroundState = runBackgroundStateManager::observeState,
setRunBackgroundState = runBackgroundStateManager::updateState,
)
}
val observeAndConfigureAutoRun by lazy {
ObserveAndConfigureAutoRun(
backgroundContext = backgroundContext,
Expand Down Expand Up @@ -414,7 +421,6 @@ class Dependencies(
goToReviewDescriptorUpdates = goToReviewDescriptorUpdates,
getTestDescriptors = getTestDescriptors::invoke,
observeRunBackgroundState = runBackgroundStateManager.observeState(),
setRunBackgroundState = runBackgroundStateManager::updateState,
observeTestRunErrors = runBackgroundStateManager.observeErrors(),
shouldShowVpnWarning = shouldShowVpnWarning::invoke,
fetchDescriptorUpdate = fetchDescriptorUpdate,
Expand Down Expand Up @@ -476,6 +482,7 @@ class Dependencies(
getResults = getResults::invoke,
getDescriptors = getTestDescriptors::invoke,
deleteAllResults = deleteAllResults::invoke,
markJustFinishedTestAsSeen = markJustFinishedTestAsSeen::invoke,
)

fun runningViewModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ooni.probe.domain

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import org.ooni.probe.data.models.RunBackgroundState

class MarkJustFinishedTestAsSeen(
private val getRunBackgroundState: () -> Flow<RunBackgroundState>,
private val setRunBackgroundState: ((RunBackgroundState) -> RunBackgroundState) -> Unit,
) {
suspend operator fun invoke() {
val state = getRunBackgroundState().first()
if (state is RunBackgroundState.Idle && state.justFinishedTest) {
setRunBackgroundState { state.copy(justFinishedTest = false) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.take
Expand All @@ -30,7 +29,6 @@ class DashboardViewModel(
goToReviewDescriptorUpdates: () -> Unit,
getTestDescriptors: () -> Flow<List<Descriptor>>,
observeRunBackgroundState: Flow<RunBackgroundState>,
setRunBackgroundState: ((RunBackgroundState) -> RunBackgroundState) -> Unit,
observeTestRunErrors: Flow<TestRunError>,
shouldShowVpnWarning: suspend () -> Boolean,
fetchDescriptorUpdate: suspend (List<InstalledTestDescriptorModel>) -> Unit,
Expand Down Expand Up @@ -90,13 +88,7 @@ class DashboardViewModel(

events
.filterIsInstance<Event.SeeResultsClick>()
.onEach {
val runState = observeRunBackgroundState.first()
if (runState is RunBackgroundState.Idle && runState.justFinishedTest) {
setRunBackgroundState { runState.copy(justFinishedTest = false) }
}
goToResults()
}
.onEach { goToResults() }
.launchIn(viewModelScope)

events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -175,6 +176,10 @@ fun ResultsScreen(
},
)
}

LaunchedEffect(Unit) {
onEvent(ResultsViewModel.Event.Start)
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ResultsViewModel(
getResults: (ResultFilter) -> Flow<List<ResultListItem>>,
getDescriptors: () -> Flow<List<Descriptor>>,
deleteAllResults: suspend () -> Unit,
markJustFinishedTestAsSeen: suspend () -> Unit,
) : ViewModel() {
private val events = MutableSharedFlow<Event>(extraBufferCapacity = 1)

Expand Down Expand Up @@ -61,6 +62,11 @@ class ResultsViewModel(
}
.launchIn(viewModelScope)

events
.filterIsInstance<Event.Start>()
.onEach { markJustFinishedTestAsSeen() }
.launchIn(viewModelScope)

events
.filterIsInstance<Event.ResultClick>()
.onEach { goToResult(it.result.idOrThrow) }
Expand Down Expand Up @@ -136,6 +142,8 @@ class ResultsViewModel(
)

sealed interface Event {
data object Start : Event

data class ResultClick(val result: ResultListItem) : Event

data object UploadClick : Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class ResultsScreenTest {
@Test
fun start() =
runComposeUiTest {
val events = mutableListOf<ResultsViewModel.Event>()
setContent {
ResultsScreen(
state = ResultsViewModel.State(results = emptyMap(), isLoading = true),
onEvent = events::add,
)
}

assertEquals(ResultsViewModel.Event.Start, events.last())
}

@Test
fun showResults() =
runComposeUiTest {
Expand Down Expand Up @@ -65,7 +79,7 @@ class ResultsScreenTest {
}

onNodeWithText(title!!).performClick()
assertEquals(ResultsViewModel.Event.ResultClick(item), events.first())
assertEquals(ResultsViewModel.Event.ResultClick(item), events.last())
}

@Test
Expand All @@ -88,7 +102,7 @@ class ResultsScreenTest {
runTest {
onNodeWithContentDescription(getString(Res.string.Modal_Delete)).performClick()
onNodeWithText(getString(Res.string.Modal_Delete)).performClick()
assertEquals(ResultsViewModel.Event.DeleteAllClick, events.first())
assertEquals(ResultsViewModel.Event.DeleteAllClick, events.last())
}
}

Expand Down

0 comments on commit 1f6e7f2

Please sign in to comment.