Skip to content

Commit

Permalink
Merge pull request #267 from ooni/improve-preferences-performance
Browse files Browse the repository at this point in the history
Limit amount of events from preferences
  • Loading branch information
sdsantos authored Nov 13, 2024
2 parents cf4bdb2 + 40b5207 commit 7e2f903
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import org.ooni.probe.data.models.Descriptor
Expand Down Expand Up @@ -83,19 +84,18 @@ class PreferenceRepository(
keys.associateWith { key ->
it[preferenceKeyFromSettingsKey(key, prefix, autoRun).preferenceKey]
}
}
}.distinctUntilChanged()

fun getValueByKey(key: SettingsKey): Flow<Any?> {
return dataStore.data.map {
fun getValueByKey(key: SettingsKey): Flow<Any?> =
dataStore.data.map {
when (val preferenceKey = preferenceKeyFromSettingsKey(key)) {
is PreferenceKey.IntKey -> it[preferenceKey.preferenceKey]
is PreferenceKey.StringKey -> it[preferenceKey.preferenceKey]
is PreferenceKey.BooleanKey -> it[preferenceKey.preferenceKey]
is PreferenceKey.FloatKey -> it[preferenceKey.preferenceKey]
is PreferenceKey.LongKey -> it[preferenceKey.preferenceKey]
}
}
}
}.distinctUntilChanged()

suspend fun setValueByKey(
key: SettingsKey,
Expand Down Expand Up @@ -139,7 +139,7 @@ class PreferenceRepository(
)
return dataStore.data.map {
it[booleanPreferencesKey(key)] == true
}
}.distinctUntilChanged()
}

fun isNetTestEnabled(
Expand All @@ -149,7 +149,7 @@ class PreferenceRepository(
): Flow<Boolean> =
dataStore.data.map {
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true
}
}.distinctUntilChanged()

fun areNetTestsEnabled(
list: List<Pair<Descriptor, NetTest>>,
Expand All @@ -161,7 +161,7 @@ class PreferenceRepository(
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true
)
}
}
}.distinctUntilChanged()

suspend fun setAreNetTestsEnabled(
list: List<Pair<Descriptor, NetTest>>,
Expand Down

0 comments on commit 7e2f903

Please sign in to comment.