Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide tap results after tap #287

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@ class Dependencies(
private val getTestDescriptorsBySpec by lazy {
GetTestDescriptorsBySpec(getTestDescriptors = getTestDescriptors::invoke)
}
private val markJustFinishedTestAsSeen by lazy {
MarkJustFinishedTestAsSeen(runBackgroundStateManager::updateState)
}
val observeAndConfigureAutoRun by lazy {
ObserveAndConfigureAutoRun(
backgroundContext = backgroundContext,
Expand Down Expand Up @@ -475,6 +479,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 org.ooni.probe.data.models.RunBackgroundState

class MarkJustFinishedTestAsSeen(
private val setRunBackgroundState: ((RunBackgroundState) -> RunBackgroundState) -> Unit,
) {
operator fun invoke() {
setRunBackgroundState { state ->
if (state is RunBackgroundState.Idle && state.justFinishedTest) {
state.copy(justFinishedTest = false)
} else {
state
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.DescriptorType
import org.ooni.probe.data.models.DescriptorUpdatesStatus
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.TestRunError
import org.ooni.probe.data.models.RunBackgroundState
import org.ooni.probe.data.models.TestRunError
import org.ooni.probe.data.models.UpdateStatusType

class DashboardViewModel(
Expand Down Expand Up @@ -47,14 +47,17 @@ class DashboardViewModel(
.onEach { firstRun -> if (firstRun) goToOnboarding() }
.launchIn(viewModelScope)

observeAvailableUpdatesState().onEach { updates ->
_state.update {
it.copy(
availableUpdates = updates.availableUpdates.toList(),
refreshType = updates.refreshType,
)
observeAvailableUpdatesState()
.onEach { updates ->
_state.update {
it.copy(
availableUpdates = updates.availableUpdates.toList(),
refreshType = updates.refreshType,
)
}
}
}.launchIn(viewModelScope)
.launchIn(viewModelScope)

getTestDescriptors()
.onEach { tests ->
_state.update { it.copy(descriptors = tests.groupByType()) }
Expand Down Expand Up @@ -97,9 +100,7 @@ class DashboardViewModel(

events
.filterIsInstance<Event.DescriptorClicked>()
.onEach { event ->
goToDescriptor(event.descriptor.key)
}
.onEach { event -> goToDescriptor(event.descriptor.key) }
.launchIn(viewModelScope)

events
Expand All @@ -109,30 +110,40 @@ class DashboardViewModel(
}
.launchIn(viewModelScope)

events.filterIsInstance<Event.FetchUpdatedDescriptors>().onEach {
state.value.descriptors[DescriptorType.Installed]
?.map { (it.source as Descriptor.Source.Installed).value }
?.let { descriptors ->
fetchDescriptorUpdate(descriptors)
events
.filterIsInstance<Event.FetchUpdatedDescriptors>()
.onEach {
state.value.descriptors[DescriptorType.Installed]
?.map { (it.source as Descriptor.Source.Installed).value }
?.let { descriptors ->
fetchDescriptorUpdate(descriptors)
}
}.launchIn(viewModelScope)

events
.filterIsInstance<Event.ReviewUpdatesClicked>()
.onEach {
_state.update {
it.copy(
refreshType = UpdateStatusType.None,
)
}
}.launchIn(viewModelScope)
events.filterIsInstance<Event.ReviewUpdatesClicked>().onEach {
_state.update {
it.copy(
refreshType = UpdateStatusType.None,
)
reviewUpdates(state.value.availableUpdates)
goToReviewDescriptorUpdates()
}
reviewUpdates(state.value.availableUpdates)
goToReviewDescriptorUpdates()
}.launchIn(viewModelScope)
events.filterIsInstance<Event.CancelUpdatesClicked>().onEach {
cancelUpdates(state.value.availableUpdates)
_state.update {
it.copy(
refreshType = UpdateStatusType.None,
)
.launchIn(viewModelScope)

events
.filterIsInstance<Event.CancelUpdatesClicked>()
.onEach {
cancelUpdates(state.value.availableUpdates)
_state.update {
it.copy(
refreshType = UpdateStatusType.None,
)
}
}
}.launchIn(viewModelScope)
.launchIn(viewModelScope)
}

fun onEvent(event: Event) {
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: () -> 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