Skip to content
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
1 change: 1 addition & 0 deletions composeApp/src/commonMain/kotlin/org/ooni/probe/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fun App(
}
LaunchedEffect(Unit) {
dependencies.bootstrapTestDescriptors()
dependencies.bootstrapPreferences()
}
LaunchedEffect(Unit) {
dependencies.observeAndConfigureAutoRun()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,32 @@ class PreferenceRepository(
}
}

suspend fun <T> setValueByKey(
suspend fun setValueByKey(
key: SettingsKey,
value: T,
value: Any?,
) {
setValuesByKey(listOf(key to value))
}

suspend fun setValuesByKey(pairs: List<Pair<SettingsKey, Any?>>) {
dataStore.edit {
when (val preferenceKey = preferenceKeyFromSettingsKey(key)) {
is PreferenceKey.IntKey -> it[preferenceKey.preferenceKey] = value as Int
is PreferenceKey.StringKey -> it[preferenceKey.preferenceKey] = value as String
is PreferenceKey.BooleanKey -> it[preferenceKey.preferenceKey] = value as Boolean
is PreferenceKey.FloatKey -> it[preferenceKey.preferenceKey] = value as Float
is PreferenceKey.LongKey -> it[preferenceKey.preferenceKey] = value as Long
pairs.forEach { (key, value) ->
when (val preferenceKey = preferenceKeyFromSettingsKey(key)) {
is PreferenceKey.IntKey ->
it[preferenceKey.preferenceKey] = value as Int

is PreferenceKey.StringKey ->
it[preferenceKey.preferenceKey] = value as String

is PreferenceKey.BooleanKey ->
it[preferenceKey.preferenceKey] = value as Boolean

is PreferenceKey.FloatKey ->
it[preferenceKey.preferenceKey] = value as Float

is PreferenceKey.LongKey ->
it[preferenceKey.preferenceKey] = value as Long
}
}
}
}
Expand Down Expand Up @@ -146,7 +161,17 @@ class PreferenceRepository(
dataStore.data.map {
list.associate { (descriptor, netTest) ->
Pair(descriptor, netTest) to
(it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true)
(
it[
booleanPreferencesKey(
getNetTestKey(
descriptor,
netTest,
isAutoRun,
),
),
] == true
)
}
}

Expand Down Expand Up @@ -176,8 +201,24 @@ class PreferenceRepository(
suspend fun removeDescriptorPreferences(descriptor: Descriptor) {
descriptor.netTests.forEach { nettest ->
dataStore.edit {
it.remove(booleanPreferencesKey(getNetTestKey(descriptor, nettest, isAutoRun = true)))
it.remove(booleanPreferencesKey(getNetTestKey(descriptor, nettest, isAutoRun = false)))
it.remove(
booleanPreferencesKey(
getNetTestKey(
descriptor,
nettest,
isAutoRun = true,
),
),
)
it.remove(
booleanPreferencesKey(
getNetTestKey(
descriptor,
nettest,
isAutoRun = false,
),
),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.ooni.probe.data.repositories.PreferenceRepository
import org.ooni.probe.data.repositories.ResultRepository
import org.ooni.probe.data.repositories.TestDescriptorRepository
import org.ooni.probe.data.repositories.UrlRepository
import org.ooni.probe.domain.BootstrapPreferences
import org.ooni.probe.domain.BootstrapTestDescriptors
import org.ooni.probe.domain.DeleteAllResults
import org.ooni.probe.domain.DeleteTestDescriptor
Expand Down Expand Up @@ -133,6 +134,9 @@ class Dependencies(

// Domain

val bootstrapPreferences by lazy {
BootstrapPreferences(preferenceRepository, getTestDescriptors::invoke)
}
val bootstrapTestDescriptors by lazy {
BootstrapTestDescriptors(
getBootstrapTestDescriptors = getBootstrapTestDescriptors::invoke,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.ooni.probe.domain

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import org.ooni.engine.models.WebConnectivityCategory
import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.repositories.PreferenceRepository

class BootstrapPreferences(
private val preferencesRepository: PreferenceRepository,
private val getTestDescriptors: () -> Flow<List<Descriptor>>,
) {
suspend operator fun invoke() {
if (preferencesRepository.getValueByKey(SettingsKey.FIRST_RUN).first() != null) return

val allTests = getAllNetTests()
preferencesRepository.setAreNetTestsEnabled(
list = allTests,
isAutoRun = false,
isEnabled = true,
)
preferencesRepository.setAreNetTestsEnabled(
list = allTests,
isAutoRun = true,
isEnabled = true,
)

preferencesRepository.setValuesByKey(
listOf(
SettingsKey.FIRST_RUN to true,
SettingsKey.MAX_RUNTIME_ENABLED to true,
SettingsKey.MAX_RUNTIME to 90,
SettingsKey.UPLOAD_RESULTS to true,
SettingsKey.AUTOMATED_TESTING_WIFIONLY to true,
SettingsKey.AUTOMATED_TESTING_CHARGING to true,
) +
WebConnectivityCategory.entries
.mapNotNull { it.settingsKey }
.map { it to true },
)
}

private suspend fun getAllNetTests() =
getTestDescriptors()
.first()
.flatMap { descriptor ->
descriptor.netTests.map { test -> descriptor to test }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.ooni.engine.models.TestType
import org.ooni.engine.models.WebConnectivityCategory
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.UrlModel
import org.ooni.probe.data.models.toDescriptor
import org.ooni.probe.data.repositories.PreferenceRepository

class BootstrapTestDescriptors(
Expand All @@ -30,16 +29,6 @@ class BootstrapTestDescriptors(
storeUrlsByUrl(urlsToStore.flatten())
}

preferencesRepository.setAreNetTestsEnabled(
list = descriptors.flatMap { descriptor ->
descriptor.netTests?.map { test ->
descriptor.toDescriptor() to test
}.orEmpty()
},
isAutoRun = true,
isEnabled = true,
)

createOrIgnoreTestDescriptors(descriptors)
}
}