Skip to content

Commit 1a1a2af

Browse files
authored
Merge pull request #119 from ooni/bootstrap-preferences
Bootstrap preferences
2 parents c9e56cf + 4f28dcb commit 1a1a2af

File tree

5 files changed

+107
-22
lines changed

5 files changed

+107
-22
lines changed

composeApp/src/commonMain/kotlin/org/ooni/probe/App.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fun App(
8383
}
8484
LaunchedEffect(Unit) {
8585
dependencies.bootstrapTestDescriptors()
86+
dependencies.bootstrapPreferences()
8687
}
8788
LaunchedEffect(Unit) {
8889
dependencies.observeAndConfigureAutoRun()

composeApp/src/commonMain/kotlin/org/ooni/probe/data/repositories/PreferenceRepository.kt

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,32 @@ class PreferenceRepository(
100100
}
101101
}
102102

103-
suspend fun <T> setValueByKey(
103+
suspend fun setValueByKey(
104104
key: SettingsKey,
105-
value: T,
105+
value: Any?,
106106
) {
107+
setValuesByKey(listOf(key to value))
108+
}
109+
110+
suspend fun setValuesByKey(pairs: List<Pair<SettingsKey, Any?>>) {
107111
dataStore.edit {
108-
when (val preferenceKey = preferenceKeyFromSettingsKey(key)) {
109-
is PreferenceKey.IntKey -> it[preferenceKey.preferenceKey] = value as Int
110-
is PreferenceKey.StringKey -> it[preferenceKey.preferenceKey] = value as String
111-
is PreferenceKey.BooleanKey -> it[preferenceKey.preferenceKey] = value as Boolean
112-
is PreferenceKey.FloatKey -> it[preferenceKey.preferenceKey] = value as Float
113-
is PreferenceKey.LongKey -> it[preferenceKey.preferenceKey] = value as Long
112+
pairs.forEach { (key, value) ->
113+
when (val preferenceKey = preferenceKeyFromSettingsKey(key)) {
114+
is PreferenceKey.IntKey ->
115+
it[preferenceKey.preferenceKey] = value as Int
116+
117+
is PreferenceKey.StringKey ->
118+
it[preferenceKey.preferenceKey] = value as String
119+
120+
is PreferenceKey.BooleanKey ->
121+
it[preferenceKey.preferenceKey] = value as Boolean
122+
123+
is PreferenceKey.FloatKey ->
124+
it[preferenceKey.preferenceKey] = value as Float
125+
126+
is PreferenceKey.LongKey ->
127+
it[preferenceKey.preferenceKey] = value as Long
128+
}
114129
}
115130
}
116131
}
@@ -146,7 +161,17 @@ class PreferenceRepository(
146161
dataStore.data.map {
147162
list.associate { (descriptor, netTest) ->
148163
Pair(descriptor, netTest) to
149-
(it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true)
164+
(
165+
it[
166+
booleanPreferencesKey(
167+
getNetTestKey(
168+
descriptor,
169+
netTest,
170+
isAutoRun,
171+
),
172+
),
173+
] == true
174+
)
150175
}
151176
}
152177

@@ -176,8 +201,24 @@ class PreferenceRepository(
176201
suspend fun removeDescriptorPreferences(descriptor: Descriptor) {
177202
descriptor.netTests.forEach { nettest ->
178203
dataStore.edit {
179-
it.remove(booleanPreferencesKey(getNetTestKey(descriptor, nettest, isAutoRun = true)))
180-
it.remove(booleanPreferencesKey(getNetTestKey(descriptor, nettest, isAutoRun = false)))
204+
it.remove(
205+
booleanPreferencesKey(
206+
getNetTestKey(
207+
descriptor,
208+
nettest,
209+
isAutoRun = true,
210+
),
211+
),
212+
)
213+
it.remove(
214+
booleanPreferencesKey(
215+
getNetTestKey(
216+
descriptor,
217+
nettest,
218+
isAutoRun = false,
219+
),
220+
),
221+
)
181222
}
182223
}
183224
}

composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.ooni.probe.data.repositories.PreferenceRepository
3838
import org.ooni.probe.data.repositories.ResultRepository
3939
import org.ooni.probe.data.repositories.TestDescriptorRepository
4040
import org.ooni.probe.data.repositories.UrlRepository
41+
import org.ooni.probe.domain.BootstrapPreferences
4142
import org.ooni.probe.domain.BootstrapTestDescriptors
4243
import org.ooni.probe.domain.DeleteAllResults
4344
import org.ooni.probe.domain.DeleteTestDescriptor
@@ -133,6 +134,9 @@ class Dependencies(
133134

134135
// Domain
135136

137+
val bootstrapPreferences by lazy {
138+
BootstrapPreferences(preferenceRepository, getTestDescriptors::invoke)
139+
}
136140
val bootstrapTestDescriptors by lazy {
137141
BootstrapTestDescriptors(
138142
getBootstrapTestDescriptors = getBootstrapTestDescriptors::invoke,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.ooni.probe.domain
2+
3+
import kotlinx.coroutines.flow.Flow
4+
import kotlinx.coroutines.flow.first
5+
import org.ooni.engine.models.WebConnectivityCategory
6+
import org.ooni.probe.data.models.Descriptor
7+
import org.ooni.probe.data.models.SettingsKey
8+
import org.ooni.probe.data.repositories.PreferenceRepository
9+
10+
class BootstrapPreferences(
11+
private val preferencesRepository: PreferenceRepository,
12+
private val getTestDescriptors: () -> Flow<List<Descriptor>>,
13+
) {
14+
suspend operator fun invoke() {
15+
if (preferencesRepository.getValueByKey(SettingsKey.FIRST_RUN).first() != null) return
16+
17+
val allTests = getAllNetTests()
18+
preferencesRepository.setAreNetTestsEnabled(
19+
list = allTests,
20+
isAutoRun = false,
21+
isEnabled = true,
22+
)
23+
preferencesRepository.setAreNetTestsEnabled(
24+
list = allTests,
25+
isAutoRun = true,
26+
isEnabled = true,
27+
)
28+
29+
preferencesRepository.setValuesByKey(
30+
listOf(
31+
SettingsKey.FIRST_RUN to true,
32+
SettingsKey.MAX_RUNTIME_ENABLED to true,
33+
SettingsKey.MAX_RUNTIME to 90,
34+
SettingsKey.UPLOAD_RESULTS to true,
35+
SettingsKey.AUTOMATED_TESTING_WIFIONLY to true,
36+
SettingsKey.AUTOMATED_TESTING_CHARGING to true,
37+
) +
38+
WebConnectivityCategory.entries
39+
.mapNotNull { it.settingsKey }
40+
.map { it to true },
41+
)
42+
}
43+
44+
private suspend fun getAllNetTests() =
45+
getTestDescriptors()
46+
.first()
47+
.flatMap { descriptor ->
48+
descriptor.netTests.map { test -> descriptor to test }
49+
}
50+
}

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/BootstrapTestDescriptors.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.ooni.engine.models.TestType
44
import org.ooni.engine.models.WebConnectivityCategory
55
import org.ooni.probe.data.models.InstalledTestDescriptorModel
66
import org.ooni.probe.data.models.UrlModel
7-
import org.ooni.probe.data.models.toDescriptor
87
import org.ooni.probe.data.repositories.PreferenceRepository
98

109
class BootstrapTestDescriptors(
@@ -30,16 +29,6 @@ class BootstrapTestDescriptors(
3029
storeUrlsByUrl(urlsToStore.flatten())
3130
}
3231

33-
preferencesRepository.setAreNetTestsEnabled(
34-
list = descriptors.flatMap { descriptor ->
35-
descriptor.netTests?.map { test ->
36-
descriptor.toDescriptor() to test
37-
}.orEmpty()
38-
},
39-
isAutoRun = true,
40-
isEnabled = true,
41-
)
42-
4332
createOrIgnoreTestDescriptors(descriptors)
4433
}
4534
}

0 commit comments

Comments
 (0)