Removing internal runBlockings

This commit is contained in:
Vitor Pamplona
2026-03-29 10:43:38 -04:00
parent ebbcb603fb
commit 9dc208deba
4 changed files with 157 additions and 151 deletions
@@ -28,6 +28,7 @@ import com.vitorpamplona.amethyst.commons.model.NoteState
import com.vitorpamplona.amethyst.commons.robohash.CachedRobohash import com.vitorpamplona.amethyst.commons.robohash.CachedRobohash
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.UiSettings
import com.vitorpamplona.amethyst.model.accountsCache.AccountCacheState import com.vitorpamplona.amethyst.model.accountsCache.AccountCacheState
import com.vitorpamplona.amethyst.model.nip03Timestamp.IncomingOtsEventVerifier import com.vitorpamplona.amethyst.model.nip03Timestamp.IncomingOtsEventVerifier
import com.vitorpamplona.amethyst.model.nip03Timestamp.TorAwareOkHttpOtsResolverBuilder import com.vitorpamplona.amethyst.model.nip03Timestamp.TorAwareOkHttpOtsResolverBuilder
@@ -67,6 +68,7 @@ import com.vitorpamplona.amethyst.ui.resourceCacheInit
import com.vitorpamplona.amethyst.ui.screen.AccountSessionManager import com.vitorpamplona.amethyst.ui.screen.AccountSessionManager
import com.vitorpamplona.amethyst.ui.screen.UiSettingsState import com.vitorpamplona.amethyst.ui.screen.UiSettingsState
import com.vitorpamplona.amethyst.ui.tor.TorManager import com.vitorpamplona.amethyst.ui.tor.TorManager
import com.vitorpamplona.amethyst.ui.tor.TorSettings
import com.vitorpamplona.quartz.nip01Core.core.Address import com.vitorpamplona.quartz.nip01Core.core.Address
import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
@@ -117,12 +119,14 @@ class AppModules(
// reduces total blocking time from (torPrefs + uiPrefs) to ~max(torPrefs, uiPrefs). // reduces total blocking time from (torPrefs + uiPrefs) to ~max(torPrefs, uiPrefs).
private val uiPrefsDeferred = private val uiPrefsDeferred =
applicationIOScope.async { applicationIOScope.async {
UiSharedPreferences(appContext, applicationIOScope) val prefs = UiSharedPreferences.uiPreferences(appContext) ?: UiSettings()
UiSharedPreferences(prefs, appContext, applicationIOScope)
} }
private val torPrefsDeferred = private val torPrefsDeferred =
applicationIOScope.async { applicationIOScope.async {
TorSharedPreferences(appContext, applicationIOScope) val prefs = TorSharedPreferences.torPreferences(appContext) ?: TorSettings()
TorSharedPreferences(prefs, appContext, applicationIOScope)
} }
// Blocking load of UI Preferences to avoid theme/language blinking // Blocking load of UI Preferences to avoid theme/language blinking
@@ -151,19 +151,19 @@ class UiSettingsFlow(
} }
companion object { companion object {
fun build(torSettings: UiSettings): UiSettingsFlow = fun build(uiSettings: UiSettings): UiSettingsFlow =
UiSettingsFlow( UiSettingsFlow(
MutableStateFlow(torSettings.theme), MutableStateFlow(uiSettings.theme),
MutableStateFlow(torSettings.preferredLanguage), MutableStateFlow(uiSettings.preferredLanguage),
MutableStateFlow(torSettings.automaticallyShowImages), MutableStateFlow(uiSettings.automaticallyShowImages),
MutableStateFlow(torSettings.automaticallyStartPlayback), MutableStateFlow(uiSettings.automaticallyStartPlayback),
MutableStateFlow(torSettings.automaticallyShowUrlPreview), MutableStateFlow(uiSettings.automaticallyShowUrlPreview),
MutableStateFlow(torSettings.automaticallyHideNavigationBars), MutableStateFlow(uiSettings.automaticallyHideNavigationBars),
MutableStateFlow(torSettings.automaticallyShowProfilePictures), MutableStateFlow(uiSettings.automaticallyShowProfilePictures),
MutableStateFlow(torSettings.dontShowPushNotificationSelector), MutableStateFlow(uiSettings.dontShowPushNotificationSelector),
MutableStateFlow(torSettings.dontAskForNotificationPermissions), MutableStateFlow(uiSettings.dontAskForNotificationPermissions),
MutableStateFlow(torSettings.featureSet), MutableStateFlow(uiSettings.featureSet),
MutableStateFlow(torSettings.gallerySet), MutableStateFlow(uiSettings.gallerySet),
) )
} }
} }
@@ -40,14 +40,31 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
@Stable @Stable
class TorSharedPreferences( class TorSharedPreferences(
prefs: TorSettings,
val context: Context, val context: Context,
val scope: CoroutineScope, val scope: CoroutineScope,
) { ) {
// Tor Preferences. Makes sure to wait for it to avoid connecting with random IPs
val value = TorSettingsFlow.build(prefs)
@OptIn(FlowPreview::class)
val saving =
value.propertyWatchFlow
.debounce(1000)
.distinctUntilChanged()
.onEach {
save(it, context)
}.flowOn(Dispatchers.IO)
.stateIn(
scope,
SharingStarted.Eagerly,
value.toSettings(),
)
companion object { companion object {
// loads faster when individualized // loads faster when individualized
val TOR_TYPE_KEY = stringPreferencesKey("tor.torType") val TOR_TYPE_KEY = stringPreferencesKey("tor.torType")
@@ -63,28 +80,8 @@ class TorSharedPreferences(
val MONEY_OPERATIONS_VIA_TOR_KEY = booleanPreferencesKey("tor.moneyOperationsViaTor") val MONEY_OPERATIONS_VIA_TOR_KEY = booleanPreferencesKey("tor.moneyOperationsViaTor")
val NIP05_VERIFICATIONS_VIA_TOR_KEY = booleanPreferencesKey("tor.nip05VerificationsViaTor") val NIP05_VERIFICATIONS_VIA_TOR_KEY = booleanPreferencesKey("tor.nip05VerificationsViaTor")
val MEDIA_UPLOADS_VIA_TOR_KEY = booleanPreferencesKey("tor.mediaUploadsViaTor") val MEDIA_UPLOADS_VIA_TOR_KEY = booleanPreferencesKey("tor.mediaUploadsViaTor")
}
// Tor Preferences. Makes sure to wait for it to avoid connecting with random IPs suspend fun torPreferences(context: Context): TorSettings? =
val value =
runBlocking {
TorSettingsFlow.build(torPreferences() ?: TorSettings())
}
@OptIn(FlowPreview::class)
val saving =
value.propertyWatchFlow
.debounce(1000)
.distinctUntilChanged()
.onEach(::save)
.flowOn(Dispatchers.IO)
.stateIn(
scope,
SharingStarted.Eagerly,
value.toSettings(),
)
suspend fun torPreferences(): TorSettings? =
try { try {
// Get the preference flow and take the first value. // Get the preference flow and take the first value.
val preferences = context.sharedPreferencesDataStore.data.first() val preferences = context.sharedPreferencesDataStore.data.first()
@@ -110,7 +107,10 @@ class TorSharedPreferences(
null null
} }
suspend fun save(torSettings: TorSettings) { suspend fun save(
torSettings: TorSettings,
context: Context,
) {
try { try {
context.sharedPreferencesDataStore.edit { preferences -> context.sharedPreferencesDataStore.edit { preferences ->
preferences[TOR_TYPE_KEY] = torSettings.torType.name preferences[TOR_TYPE_KEY] = torSettings.torType.name
@@ -134,3 +134,4 @@ class TorSharedPreferences(
} }
} }
} }
}
@@ -49,36 +49,18 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
val Context.sharedPreferencesDataStore: DataStore<Preferences> by preferencesDataStore(name = "shared_settings") val Context.sharedPreferencesDataStore: DataStore<Preferences> by preferencesDataStore(name = "shared_settings")
@Stable @Stable
class UiSharedPreferences( class UiSharedPreferences(
prefs: UiSettings,
val context: Context, val context: Context,
val scope: CoroutineScope, val scope: CoroutineScope,
) { ) {
companion object {
// loads faster when individualized
val UI_THEME = stringPreferencesKey("ui.theme")
val UI_LANGUAGE = stringPreferencesKey("ui.language")
val UI_SHOW_IMAGES = stringPreferencesKey("ui.show_images")
val UI_START_PLAYBACK = stringPreferencesKey("ui.start_playback")
val UI_SHOW_URL_PREVIEW = stringPreferencesKey("ui.show_url_preview")
val UI_HIDE_NAVIGATION_BARS = stringPreferencesKey("ui.hide_navigation_bars")
val UI_SHOW_PROFILE_PICTURES = stringPreferencesKey("ui.show_profile_pictures")
val UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR = booleanPreferencesKey("ui.dont_show_push_notification_selector")
val UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS = booleanPreferencesKey("ui.dont_ask_for_notification_permissions")
val UI_FEATURE_SET = stringPreferencesKey("ui.feature_set")
val UI_GALLERY_SET = stringPreferencesKey("ui.gallery_set")
}
// UI Preferences. Makes sure to wait for it to avoid blinking themes and language preferences // UI Preferences. Makes sure to wait for it to avoid blinking themes and language preferences
val value = val value = UiSettingsFlow.build(prefs)
runBlocking {
UiSettingsFlow.build(uiPreferences() ?: UiSettings())
}
val languageUpdate = val languageUpdate =
value.preferredLanguage value.preferredLanguage
@@ -98,15 +80,30 @@ class UiSharedPreferences(
value.propertyWatchFlow value.propertyWatchFlow
.debounce(1000) .debounce(1000)
.distinctUntilChanged() .distinctUntilChanged()
.onEach(::save) .onEach {
.flowOn(Dispatchers.IO) save(it, context)
}.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope, scope,
SharingStarted.Eagerly, SharingStarted.Eagerly,
value.toSettings(), value.toSettings(),
) )
suspend fun uiPreferences(): UiSettings? = companion object {
// loads faster when individualized
val UI_THEME = stringPreferencesKey("ui.theme")
val UI_LANGUAGE = stringPreferencesKey("ui.language")
val UI_SHOW_IMAGES = stringPreferencesKey("ui.show_images")
val UI_START_PLAYBACK = stringPreferencesKey("ui.start_playback")
val UI_SHOW_URL_PREVIEW = stringPreferencesKey("ui.show_url_preview")
val UI_HIDE_NAVIGATION_BARS = stringPreferencesKey("ui.hide_navigation_bars")
val UI_SHOW_PROFILE_PICTURES = stringPreferencesKey("ui.show_profile_pictures")
val UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR = booleanPreferencesKey("ui.dont_show_push_notification_selector")
val UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS = booleanPreferencesKey("ui.dont_ask_for_notification_permissions")
val UI_FEATURE_SET = stringPreferencesKey("ui.feature_set")
val UI_GALLERY_SET = stringPreferencesKey("ui.gallery_set")
suspend fun uiPreferences(context: Context): UiSettings? =
try { try {
// Get the preference flow and take the first value. // Get the preference flow and take the first value.
val preferences = context.sharedPreferencesDataStore.data.first() val preferences = context.sharedPreferencesDataStore.data.first()
@@ -132,7 +129,7 @@ class UiSharedPreferences(
try { try {
val oldVersion = LocalPreferences.loadSharedSettings() val oldVersion = LocalPreferences.loadSharedSettings()
if (oldVersion != null) { if (oldVersion != null) {
save(oldVersion) save(oldVersion, context)
} }
oldVersion oldVersion
} catch (e: Exception) { } catch (e: Exception) {
@@ -141,7 +138,10 @@ class UiSharedPreferences(
} }
} }
suspend fun save(sharedSettings: UiSettings) { suspend fun save(
sharedSettings: UiSettings,
context: Context,
) {
try { try {
context.sharedPreferencesDataStore.edit { preferences -> context.sharedPreferencesDataStore.edit { preferences ->
preferences[UI_THEME] = sharedSettings.theme.name preferences[UI_THEME] = sharedSettings.theme.name
@@ -163,3 +163,4 @@ class UiSharedPreferences(
} }
} }
} }
}