Skip to content

Commit

Permalink
feat: added proxy label to running screen (#202)
Browse files Browse the repository at this point in the history
* chore: added proxy label to running screen

* chore: update based on review and create `GetProxySettings`
  • Loading branch information
aanorbel authored Oct 23, 2024
1 parent 9ddfc8d commit 84abd54
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
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 @@ -57,6 +57,7 @@ import org.ooni.probe.domain.GetBootstrapTestDescriptors
import org.ooni.probe.domain.GetDefaultTestDescriptors
import org.ooni.probe.domain.GetEnginePreferences
import org.ooni.probe.domain.GetFirstRun
import org.ooni.probe.domain.GetProxySettings
import org.ooni.probe.domain.GetResult
import org.ooni.probe.domain.GetResults
import org.ooni.probe.domain.GetSettings
Expand Down Expand Up @@ -238,7 +239,10 @@ class Dependencies(
}
val getCurrentTestState get() = testStateManager::observeState
private val getDefaultTestDescriptors by lazy { GetDefaultTestDescriptors() }
private val getEnginePreferences by lazy { GetEnginePreferences(preferenceRepository) }
private val getProxySettings by lazy { GetProxySettings(preferenceRepository) }
private val getEnginePreferences by lazy {
GetEnginePreferences(preferencesRepository = preferenceRepository, getProxySettings = getProxySettings::invoke)
}
private val getFirstRun by lazy { GetFirstRun(preferenceRepository) }
private val getResults by lazy {
GetResults(
Expand Down Expand Up @@ -453,6 +457,7 @@ class Dependencies(
observeTestRunState = testStateManager.observeState(),
observeTestRunErrors = testStateManager.observeErrors(),
cancelTestRun = testStateManager::cancelTestRun,
getProxySettings = getProxySettings::invoke,
)

fun runViewModel(onBack: () -> Unit) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.time.Duration.Companion.seconds

class GetEnginePreferences(
private val preferencesRepository: PreferenceRepository,
private val getProxySettings: suspend () -> ProxySettings,
) {
suspend operator fun invoke() =
EnginePreferences(
Expand All @@ -26,11 +27,7 @@ class GetEnginePreferences(
} else {
null
},
proxy = ProxySettings.newProxySettings(
protocol = getValueForKey(SettingsKey.PROXY_PROTOCOL) as? String,
hostname = getValueForKey(SettingsKey.PROXY_HOSTNAME) as? String,
port = getValueForKey(SettingsKey.PROXY_PORT) as? String,
).getProxyString(),
proxy = getProxySettings().getProxyString(),
)

private suspend fun getEnabledCategories(): List<String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.ooni.probe.domain

import kotlinx.coroutines.flow.first
import org.ooni.probe.data.models.ProxySettings
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.repositories.PreferenceRepository

class GetProxySettings(
private val preferencesRepository: PreferenceRepository,
) {
suspend operator fun invoke() =
ProxySettings.newProxySettings(
protocol = getValueForKey(SettingsKey.PROXY_PROTOCOL) as? String,
hostname = getValueForKey(SettingsKey.PROXY_HOSTNAME) as? String,
port = getValueForKey(SettingsKey.PROXY_PORT) as? String,
)

private suspend fun getValueForKey(settingsKey: SettingsKey) = preferencesRepository.getValueByKey(settingsKey).first()
}
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,29 @@ import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.ooni.probe.data.models.ProxySettings
import org.ooni.probe.data.models.TestRunError
import org.ooni.probe.data.models.TestRunState
import org.ooni.probe.ui.dashboard.DashboardViewModel.Event

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

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

init {
viewModelScope.launch {
val proxy = getProxySettings().getProxyString()
_state.update { it.copy(hasProxy = proxy.isNotEmpty()) }
}
observeTestRunState
.onEach { testRunState ->
if (testRunState is TestRunState.Idle) {
Expand Down Expand Up @@ -72,6 +78,7 @@ class RunningViewModel(
data class State(
val testRunState: TestRunState? = null,
val testRunErrors: List<TestRunError> = emptyList(),
val hasProxy: Boolean = false,
)

sealed interface Event {
Expand Down

0 comments on commit 84abd54

Please sign in to comment.