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

Disable websites descriptor if no categories are enabled #329

Merged
merged 1 commit into from
Dec 3, 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 @@ -22,6 +22,7 @@ data class Descriptor(
val longRunningTests: List<NetTest> = emptyList(),
val source: Source,
val updateStatus: UpdateStatus,
val enabled: Boolean = true,
) {
sealed interface Source {
data class Default(val value: DefaultTestDescriptor) : Source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class Dependencies(
getDefaultTestDescriptors = getDefaultTestDescriptors::invoke,
listInstalledTestDescriptors = testDescriptorRepository::list,
descriptorUpdates = getDescriptorUpdate::observeAvailableUpdatesState,
getPreferenceValues = preferenceRepository::allSettings,
)
}
private val getTestDescriptorsBySpec by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GetAutoRunSpecification(
}

private suspend fun List<Descriptor>.filterForAutoRun() =
filter { it.isEnabledForAutoRun() }
filter { it.enabled && it.isEnabledForAutoRun() }
.map { descriptor ->
descriptor.copy(
netTests = descriptor.netTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@ import kotlinx.coroutines.flow.map
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.Settings_TestOptions_LongRunningTest
import org.jetbrains.compose.resources.stringResource
import org.ooni.engine.models.WebConnectivityCategory
import org.ooni.probe.data.models.DefaultTestDescriptor
import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.DescriptorUpdatesStatus
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.models.UpdateStatus
import org.ooni.probe.data.models.toDescriptor

class GetTestDescriptors(
private val getDefaultTestDescriptors: () -> List<DefaultTestDescriptor>,
private val listInstalledTestDescriptors: () -> Flow<List<InstalledTestDescriptorModel>>,
private val descriptorUpdates: () -> Flow<DescriptorUpdatesStatus>,
private val getPreferenceValues: (List<SettingsKey>) -> Flow<Map<SettingsKey, Any?>>,
) {
operator fun invoke(): Flow<List<Descriptor>> {
return combine(
listInstalledTestDescriptors(),
descriptorUpdates(),
flowOf(getDefaultTestDescriptors()),
) { installedDescriptors, descriptorUpdates, defaultDescriptors ->
isWebsitesDescriptorEnabled(),
) { installedDescriptors, descriptorUpdates, defaultDescriptors, isWebsitesEnabled ->
val updatedDescriptors = installedDescriptors.map { item ->
item.toDescriptor(updateStatus = descriptorUpdates.getStatusOf(item.id))
}
return@combine defaultDescriptors
.map { it.toDescriptor() } + updatedDescriptors
val allDescriptors = defaultDescriptors.map { it.toDescriptor() } + updatedDescriptors
return@combine allDescriptors.map {
it.copy(enabled = it.name != "websites" || isWebsitesEnabled)
}
}
}

private fun isWebsitesDescriptorEnabled() =
getPreferenceValues(WebConnectivityCategory.entries.mapNotNull { it.settingsKey })
.map { preferences -> preferences.any { it.value == true } }

private fun DefaultTestDescriptor.toDescriptor() =
Descriptor(
name = label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ private fun LazyListScope.regularTestItems(
items(testItems, key = { "${descriptor.key}_${it.item.test.name}" }) { testItem ->
TestItem(
testItem = testItem,
enabled = descriptor.enabled,
onChecked = {
onEvent(
RunViewModel.Event.NetTestChecked(
Expand Down Expand Up @@ -271,11 +272,13 @@ private fun DescriptorItem(
state = descriptorItem.state,
onClick = { onChecked(descriptorItem.state != ToggleableState.On) },
role = Role.Checkbox,
enabled = descriptor.enabled,
)
.padding(horizontal = 16.dp),
) {
TriStateCheckbox(
state = descriptorItem.state,
enabled = descriptor.enabled,
onClick = null,
modifier = Modifier.padding(end = 24.dp),
)
Expand Down Expand Up @@ -304,6 +307,7 @@ private fun DescriptorItem(
@Composable
fun TestItem(
testItem: SelectableItem<NetTest>,
enabled: Boolean = true,
onChecked: (Boolean) -> Unit,
) {
val test = testItem.item
Expand All @@ -315,11 +319,13 @@ fun TestItem(
value = testItem.isSelected,
onValueChange = { onChecked(it) },
role = Role.Checkbox,
enabled = enabled,
)
.padding(horizontal = 16.dp, vertical = 10.dp),
) {
Checkbox(
checked = testItem.isSelected,
enabled = enabled,
onCheckedChange = null,
modifier = Modifier.padding(end = 24.dp),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import org.ooni.probe.config.OrganizationConfig
import org.ooni.probe.config.TestDisplayMode
import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.DescriptorType
import org.ooni.probe.data.models.PlatformAction
import org.ooni.probe.data.models.NetTest
import org.ooni.probe.data.models.PlatformAction
import org.ooni.probe.data.models.RunSpecification
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.repositories.PreferenceRepository
Expand Down Expand Up @@ -62,9 +62,9 @@ class RunViewModel(

ParentSelectableItem(
item = descriptor,
state = when (selectedTestsCount) {
0 -> ToggleableState.Off
tests.size -> ToggleableState.On
state = when {
!descriptor.enabled || selectedTestsCount == 0 -> ToggleableState.Off
selectedTestsCount == tests.size -> ToggleableState.On
else -> ToggleableState.Indeterminate
},
isExpanded = when (OrganizationConfig.testDisplayMode) {
Expand All @@ -75,7 +75,7 @@ class RunViewModel(
) to tests.map { test ->
SelectableItem(
item = test,
isSelected = preferences[descriptor to test] == true,
isSelected = descriptor.enabled && preferences[descriptor to test] == true,
)
}
}
Expand Down