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

feat: added proxy label to running screen #202

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -9,6 +9,7 @@
<string name="Dashboard_Running_EstimatedTimeLeft">Estimated time left:</string>
<string name="Dashboard_Running_Stopping_Title">Stopping test…</string>
<string name="Dashboard_Running_Stopping_Notice">Finishing the currently pending tests, please wait…</string>
<string name="Dashboard_Running_ProxyInUse">Proxy in use</string>
<string name="Dashboard_RunV2_Ooni_Title">OONI Tests</string>
<string name="Dashboard_RunV2_Title">OONI Run Links</string>
<string name="Dashboard_RunV2_RunFinished">Run finished. Tap to view results.</string>
Expand Down Expand Up @@ -231,7 +232,7 @@
<string name="Modal_Autorun_BatteryOptimization">OONI Probe cannot run automatically without battery optimization. Do you want to try again?</string>

<!-- New Strings -->
<string name="Dashboard_Runv2_Overview_LastUpdatd">Last updated %1$s</string>
<string name="Dashboard_Runv2_Overview_LastUpdatd">Last updated %1$s</string>
<string name="back">Back</string>
<string name="refresh">refresh</string>
<string name="measurement">Measurement</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class Dependencies(
observeTestRunState = testStateManager.observeState(),
observeTestRunErrors = testStateManager.observeErrors(),
cancelTestRun = testStateManager::cancelTestRun,
preferencesRepository = preferenceRepository,
)

fun runViewModel(onBack: () -> Unit) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.AssistChip
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand All @@ -33,13 +35,15 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import ooniprobe.composeapp.generated.resources.Dashboard_Running_EstimatedTimeLeft
import ooniprobe.composeapp.generated.resources.Dashboard_Running_ProxyInUse
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Running
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Stopping_Notice
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Stopping_Title
import ooniprobe.composeapp.generated.resources.Notification_StopTest
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.back
import ooniprobe.composeapp.generated.resources.ooni_empty_state
import ooniprobe.composeapp.generated.resources.test_circumvention
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.ooni.probe.data.models.TestRunState
Expand Down Expand Up @@ -80,7 +84,7 @@ fun RunningScreen(
)

when (state.testRunState) {
is TestRunState.Running -> TestRunning(state.testRunState, onEvent)
is TestRunState.Running -> TestRunning(state.hasProxy, state.testRunState, onEvent)
TestRunState.Stopping -> TestStopping()
else -> Unit
}
Expand All @@ -95,6 +99,7 @@ fun RunningScreen(

@Composable
private fun TestRunning(
hasProxy: Boolean,
state: TestRunState.Running,
onEvent: (RunningViewModel.Event) -> Unit,
) {
Expand All @@ -120,6 +125,32 @@ private fun TestRunning(
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
)
if (hasProxy) {
AssistChip(
onClick = { },
label = {
Text(
text = stringResource(Res.string.Dashboard_Running_ProxyInUse),
style = MaterialTheme.typography.bodyLarge,
)
},
leadingIcon = {
Icon(
painter = painterResource(Res.drawable.test_circumvention),
contentDescription = null,
modifier = Modifier.size(AssistChipDefaults.IconSize),
tint = MaterialTheme.customColors.onDescriptor,
)
},
colors = AssistChipDefaults.assistChipColors(
labelColor = MaterialTheme.customColors.onDescriptor,
),
border = AssistChipDefaults.assistChipBorder(
enabled = true,
borderColor = MaterialTheme.customColors.onDescriptor,
),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,40 @@ import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import org.ooni.probe.data.models.ProxySettings
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.models.TestRunError
import org.ooni.probe.data.models.TestRunState
import org.ooni.probe.ui.dashboard.DashboardViewModel.Event
import org.ooni.probe.data.repositories.PreferenceRepository

class RunningViewModel(
onBack: () -> Unit,
goToResults: () -> Unit,
observeTestRunState: Flow<TestRunState>,
observeTestRunErrors: Flow<TestRunError>,
cancelTestRun: () -> Unit,
preferencesRepository: PreferenceRepository,
) : ViewModel() {
private val events = MutableSharedFlow<Event>(extraBufferCapacity = 1)

private val _state = MutableStateFlow(State())
val state = _state.asStateFlow()

init {
preferencesRepository.allSettings(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory this doesn't tell us if the actual test is running with a VPN or not. The user could have changed the settings while a test is running. But I think it's good enough for the use case.

listOf(
SettingsKey.PROXY_PROTOCOL,
SettingsKey.PROXY_HOSTNAME,
SettingsKey.PROXY_PORT,
),
).onEach { proxySettings ->
val proxy = ProxySettings.newProxySettings(
protocol = proxySettings[SettingsKey.PROXY_PROTOCOL] as? String,
hostname = proxySettings[SettingsKey.PROXY_HOSTNAME] as? String,
port = proxySettings[SettingsKey.PROXY_PORT] as? String,
).getProxyString()
_state.update { it.copy(hasProxy = proxy.isNotEmpty()) }
}.launchIn(viewModelScope)
observeTestRunState
.onEach { testRunState ->
if (testRunState is TestRunState.Idle) {
Expand Down Expand Up @@ -72,6 +89,7 @@ class RunningViewModel(
data class State(
val testRunState: TestRunState? = null,
val testRunErrors: List<TestRunError> = emptyList(),
val hasProxy: Boolean = false,
)

sealed interface Event {
Expand Down