Removing internal runBlockings
This commit is contained in:
@@ -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),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+68
-67
@@ -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,74 +80,58 @@ 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 =
|
try {
|
||||||
runBlocking {
|
// Get the preference flow and take the first value.
|
||||||
TorSettingsFlow.build(torPreferences() ?: TorSettings())
|
val preferences = context.sharedPreferencesDataStore.data.first()
|
||||||
}
|
TorSettings(
|
||||||
|
torType = preferences[TOR_TYPE_KEY]?.let { TorType.valueOf(it) } ?: TorType.INTERNAL,
|
||||||
@OptIn(FlowPreview::class)
|
externalSocksPort = preferences[EXTERNAL_SOCKS_PORT_KEY] ?: 9050,
|
||||||
val saving =
|
onionRelaysViaTor = preferences[ONION_RELAYS_VIA_TOR_KEY] ?: true,
|
||||||
value.propertyWatchFlow
|
dmRelaysViaTor = preferences[DM_RELAYS_VIA_TOR_KEY] ?: true,
|
||||||
.debounce(1000)
|
newRelaysViaTor = preferences[NEW_RELAYS_VIA_TOR_KEY] ?: true,
|
||||||
.distinctUntilChanged()
|
trustedRelaysViaTor = preferences[TRUSTED_RELAYS_VIA_TOR_KEY] ?: false,
|
||||||
.onEach(::save)
|
urlPreviewsViaTor = preferences[URL_PREVIEWS_VIA_TOR_KEY] ?: false,
|
||||||
.flowOn(Dispatchers.IO)
|
profilePicsViaTor = preferences[PROFILE_PICS_VIA_TOR_KEY] ?: false,
|
||||||
.stateIn(
|
imagesViaTor = preferences[IMAGES_VIA_TOR_KEY] ?: false,
|
||||||
scope,
|
videosViaTor = preferences[VIDEOS_VIA_TOR_KEY] ?: false,
|
||||||
SharingStarted.Eagerly,
|
moneyOperationsViaTor = preferences[MONEY_OPERATIONS_VIA_TOR_KEY] ?: false,
|
||||||
value.toSettings(),
|
nip05VerificationsViaTor = preferences[NIP05_VERIFICATIONS_VIA_TOR_KEY] ?: false,
|
||||||
)
|
mediaUploadsViaTor = preferences[MEDIA_UPLOADS_VIA_TOR_KEY] ?: false,
|
||||||
|
)
|
||||||
suspend fun torPreferences(): TorSettings? =
|
} catch (e: Exception) {
|
||||||
try {
|
if (e is CancellationException) throw e
|
||||||
// Get the preference flow and take the first value.
|
// Log any errors that occur while reading the DataStore.
|
||||||
val preferences = context.sharedPreferencesDataStore.data.first()
|
Log.e("SharedPreferences") { "Error reading DataStore preferences: ${e.message}" }
|
||||||
TorSettings(
|
null
|
||||||
torType = preferences[TOR_TYPE_KEY]?.let { TorType.valueOf(it) } ?: TorType.INTERNAL,
|
}
|
||||||
externalSocksPort = preferences[EXTERNAL_SOCKS_PORT_KEY] ?: 9050,
|
|
||||||
onionRelaysViaTor = preferences[ONION_RELAYS_VIA_TOR_KEY] ?: true,
|
suspend fun save(
|
||||||
dmRelaysViaTor = preferences[DM_RELAYS_VIA_TOR_KEY] ?: true,
|
torSettings: TorSettings,
|
||||||
newRelaysViaTor = preferences[NEW_RELAYS_VIA_TOR_KEY] ?: true,
|
context: Context,
|
||||||
trustedRelaysViaTor = preferences[TRUSTED_RELAYS_VIA_TOR_KEY] ?: false,
|
) {
|
||||||
urlPreviewsViaTor = preferences[URL_PREVIEWS_VIA_TOR_KEY] ?: false,
|
try {
|
||||||
profilePicsViaTor = preferences[PROFILE_PICS_VIA_TOR_KEY] ?: false,
|
context.sharedPreferencesDataStore.edit { preferences ->
|
||||||
imagesViaTor = preferences[IMAGES_VIA_TOR_KEY] ?: false,
|
preferences[TOR_TYPE_KEY] = torSettings.torType.name
|
||||||
videosViaTor = preferences[VIDEOS_VIA_TOR_KEY] ?: false,
|
preferences[EXTERNAL_SOCKS_PORT_KEY] = torSettings.externalSocksPort
|
||||||
moneyOperationsViaTor = preferences[MONEY_OPERATIONS_VIA_TOR_KEY] ?: false,
|
preferences[ONION_RELAYS_VIA_TOR_KEY] = torSettings.onionRelaysViaTor
|
||||||
nip05VerificationsViaTor = preferences[NIP05_VERIFICATIONS_VIA_TOR_KEY] ?: false,
|
preferences[DM_RELAYS_VIA_TOR_KEY] = torSettings.dmRelaysViaTor
|
||||||
mediaUploadsViaTor = preferences[MEDIA_UPLOADS_VIA_TOR_KEY] ?: false,
|
preferences[NEW_RELAYS_VIA_TOR_KEY] = torSettings.newRelaysViaTor
|
||||||
)
|
preferences[TRUSTED_RELAYS_VIA_TOR_KEY] = torSettings.trustedRelaysViaTor
|
||||||
} catch (e: Exception) {
|
preferences[URL_PREVIEWS_VIA_TOR_KEY] = torSettings.urlPreviewsViaTor
|
||||||
if (e is CancellationException) throw e
|
preferences[PROFILE_PICS_VIA_TOR_KEY] = torSettings.profilePicsViaTor
|
||||||
// Log any errors that occur while reading the DataStore.
|
preferences[IMAGES_VIA_TOR_KEY] = torSettings.imagesViaTor
|
||||||
Log.e("SharedPreferences") { "Error reading DataStore preferences: ${e.message}" }
|
preferences[VIDEOS_VIA_TOR_KEY] = torSettings.videosViaTor
|
||||||
null
|
preferences[MONEY_OPERATIONS_VIA_TOR_KEY] = torSettings.moneyOperationsViaTor
|
||||||
}
|
preferences[NIP05_VERIFICATIONS_VIA_TOR_KEY] = torSettings.nip05VerificationsViaTor
|
||||||
|
preferences[MEDIA_UPLOADS_VIA_TOR_KEY] = torSettings.mediaUploadsViaTor
|
||||||
suspend fun save(torSettings: TorSettings) {
|
}
|
||||||
try {
|
} catch (e: Exception) {
|
||||||
context.sharedPreferencesDataStore.edit { preferences ->
|
if (e is CancellationException) throw e
|
||||||
preferences[TOR_TYPE_KEY] = torSettings.torType.name
|
// Log any errors that occur while reading the DataStore.
|
||||||
preferences[EXTERNAL_SOCKS_PORT_KEY] = torSettings.externalSocksPort
|
Log.e("SharedPreferences") { "Error saving DataStore preferences: ${e.message}" }
|
||||||
preferences[ONION_RELAYS_VIA_TOR_KEY] = torSettings.onionRelaysViaTor
|
|
||||||
preferences[DM_RELAYS_VIA_TOR_KEY] = torSettings.dmRelaysViaTor
|
|
||||||
preferences[NEW_RELAYS_VIA_TOR_KEY] = torSettings.newRelaysViaTor
|
|
||||||
preferences[TRUSTED_RELAYS_VIA_TOR_KEY] = torSettings.trustedRelaysViaTor
|
|
||||||
preferences[URL_PREVIEWS_VIA_TOR_KEY] = torSettings.urlPreviewsViaTor
|
|
||||||
preferences[PROFILE_PICS_VIA_TOR_KEY] = torSettings.profilePicsViaTor
|
|
||||||
preferences[IMAGES_VIA_TOR_KEY] = torSettings.imagesViaTor
|
|
||||||
preferences[VIDEOS_VIA_TOR_KEY] = torSettings.videosViaTor
|
|
||||||
preferences[MONEY_OPERATIONS_VIA_TOR_KEY] = torSettings.moneyOperationsViaTor
|
|
||||||
preferences[NIP05_VERIFICATIONS_VIA_TOR_KEY] = torSettings.nip05VerificationsViaTor
|
|
||||||
preferences[MEDIA_UPLOADS_VIA_TOR_KEY] = torSettings.mediaUploadsViaTor
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
if (e is CancellationException) throw e
|
|
||||||
// Log any errors that occur while reading the DataStore.
|
|
||||||
Log.e("SharedPreferences") { "Error saving DataStore preferences: ${e.message}" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+71
-70
@@ -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,68 +80,87 @@ 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 {
|
||||||
try {
|
// loads faster when individualized
|
||||||
// Get the preference flow and take the first value.
|
val UI_THEME = stringPreferencesKey("ui.theme")
|
||||||
val preferences = context.sharedPreferencesDataStore.data.first()
|
val UI_LANGUAGE = stringPreferencesKey("ui.language")
|
||||||
|
val UI_SHOW_IMAGES = stringPreferencesKey("ui.show_images")
|
||||||
UiSettings(
|
val UI_START_PLAYBACK = stringPreferencesKey("ui.start_playback")
|
||||||
theme = preferences[UI_THEME]?.let { ThemeType.valueOf(it) } ?: ThemeType.SYSTEM,
|
val UI_SHOW_URL_PREVIEW = stringPreferencesKey("ui.show_url_preview")
|
||||||
preferredLanguage = preferences[UI_LANGUAGE]?.ifBlank { null },
|
val UI_HIDE_NAVIGATION_BARS = stringPreferencesKey("ui.hide_navigation_bars")
|
||||||
automaticallyShowImages = preferences[UI_SHOW_IMAGES]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
val UI_SHOW_PROFILE_PICTURES = stringPreferencesKey("ui.show_profile_pictures")
|
||||||
automaticallyStartPlayback = preferences[UI_START_PLAYBACK]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
val UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR = booleanPreferencesKey("ui.dont_show_push_notification_selector")
|
||||||
automaticallyShowUrlPreview = preferences[UI_SHOW_URL_PREVIEW]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
val UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS = booleanPreferencesKey("ui.dont_ask_for_notification_permissions")
|
||||||
automaticallyHideNavigationBars = preferences[UI_HIDE_NAVIGATION_BARS]?.let { BooleanType.valueOf(it) } ?: BooleanType.ALWAYS,
|
val UI_FEATURE_SET = stringPreferencesKey("ui.feature_set")
|
||||||
automaticallyShowProfilePictures = preferences[UI_SHOW_PROFILE_PICTURES]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
val UI_GALLERY_SET = stringPreferencesKey("ui.gallery_set")
|
||||||
dontShowPushNotificationSelector = preferences[UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR] ?: false,
|
|
||||||
dontAskForNotificationPermissions = preferences[UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS] ?: false,
|
|
||||||
featureSet = preferences[UI_FEATURE_SET]?.let { FeatureSetType.valueOf(it) } ?: FeatureSetType.SIMPLIFIED,
|
|
||||||
gallerySet = preferences[UI_GALLERY_SET]?.let { ProfileGalleryType.valueOf(it) } ?: ProfileGalleryType.CLASSIC,
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
if (e is CancellationException) throw e
|
|
||||||
// Log any errors that occur while reading the DataStore.
|
|
||||||
Log.e("SharedPreferences") { "Error reading DataStore preferences: ${e.message}" }
|
|
||||||
|
|
||||||
|
suspend fun uiPreferences(context: Context): UiSettings? =
|
||||||
try {
|
try {
|
||||||
val oldVersion = LocalPreferences.loadSharedSettings()
|
// Get the preference flow and take the first value.
|
||||||
if (oldVersion != null) {
|
val preferences = context.sharedPreferencesDataStore.data.first()
|
||||||
save(oldVersion)
|
|
||||||
}
|
UiSettings(
|
||||||
oldVersion
|
theme = preferences[UI_THEME]?.let { ThemeType.valueOf(it) } ?: ThemeType.SYSTEM,
|
||||||
|
preferredLanguage = preferences[UI_LANGUAGE]?.ifBlank { null },
|
||||||
|
automaticallyShowImages = preferences[UI_SHOW_IMAGES]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
||||||
|
automaticallyStartPlayback = preferences[UI_START_PLAYBACK]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
||||||
|
automaticallyShowUrlPreview = preferences[UI_SHOW_URL_PREVIEW]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
||||||
|
automaticallyHideNavigationBars = preferences[UI_HIDE_NAVIGATION_BARS]?.let { BooleanType.valueOf(it) } ?: BooleanType.ALWAYS,
|
||||||
|
automaticallyShowProfilePictures = preferences[UI_SHOW_PROFILE_PICTURES]?.let { ConnectivityType.valueOf(it) } ?: ConnectivityType.ALWAYS,
|
||||||
|
dontShowPushNotificationSelector = preferences[UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR] ?: false,
|
||||||
|
dontAskForNotificationPermissions = preferences[UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS] ?: false,
|
||||||
|
featureSet = preferences[UI_FEATURE_SET]?.let { FeatureSetType.valueOf(it) } ?: FeatureSetType.SIMPLIFIED,
|
||||||
|
gallerySet = preferences[UI_GALLERY_SET]?.let { ProfileGalleryType.valueOf(it) } ?: ProfileGalleryType.CLASSIC,
|
||||||
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
null
|
// Log any errors that occur while reading the DataStore.
|
||||||
}
|
Log.e("SharedPreferences") { "Error reading DataStore preferences: ${e.message}" }
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun save(sharedSettings: UiSettings) {
|
try {
|
||||||
try {
|
val oldVersion = LocalPreferences.loadSharedSettings()
|
||||||
context.sharedPreferencesDataStore.edit { preferences ->
|
if (oldVersion != null) {
|
||||||
preferences[UI_THEME] = sharedSettings.theme.name
|
save(oldVersion, context)
|
||||||
preferences[UI_LANGUAGE] = sharedSettings.preferredLanguage ?: ""
|
}
|
||||||
preferences[UI_SHOW_IMAGES] = sharedSettings.automaticallyShowImages.name
|
oldVersion
|
||||||
preferences[UI_START_PLAYBACK] = sharedSettings.automaticallyStartPlayback.name
|
} catch (e: Exception) {
|
||||||
preferences[UI_SHOW_URL_PREVIEW] = sharedSettings.automaticallyShowUrlPreview.name
|
if (e is CancellationException) throw e
|
||||||
preferences[UI_HIDE_NAVIGATION_BARS] = sharedSettings.automaticallyHideNavigationBars.name
|
null
|
||||||
preferences[UI_SHOW_PROFILE_PICTURES] = sharedSettings.automaticallyShowProfilePictures.name
|
}
|
||||||
preferences[UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR] = sharedSettings.dontShowPushNotificationSelector
|
}
|
||||||
preferences[UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS] = sharedSettings.dontAskForNotificationPermissions
|
|
||||||
preferences[UI_FEATURE_SET] = sharedSettings.featureSet.name
|
suspend fun save(
|
||||||
preferences[UI_GALLERY_SET] = sharedSettings.gallerySet.name
|
sharedSettings: UiSettings,
|
||||||
|
context: Context,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
context.sharedPreferencesDataStore.edit { preferences ->
|
||||||
|
preferences[UI_THEME] = sharedSettings.theme.name
|
||||||
|
preferences[UI_LANGUAGE] = sharedSettings.preferredLanguage ?: ""
|
||||||
|
preferences[UI_SHOW_IMAGES] = sharedSettings.automaticallyShowImages.name
|
||||||
|
preferences[UI_START_PLAYBACK] = sharedSettings.automaticallyStartPlayback.name
|
||||||
|
preferences[UI_SHOW_URL_PREVIEW] = sharedSettings.automaticallyShowUrlPreview.name
|
||||||
|
preferences[UI_HIDE_NAVIGATION_BARS] = sharedSettings.automaticallyHideNavigationBars.name
|
||||||
|
preferences[UI_SHOW_PROFILE_PICTURES] = sharedSettings.automaticallyShowProfilePictures.name
|
||||||
|
preferences[UI_DONT_SHOW_PUSH_NOTIFICATION_SELECTOR] = sharedSettings.dontShowPushNotificationSelector
|
||||||
|
preferences[UI_DONT_ASK_FOR_NOTIFICATION_PERMISSIONS] = sharedSettings.dontAskForNotificationPermissions
|
||||||
|
preferences[UI_FEATURE_SET] = sharedSettings.featureSet.name
|
||||||
|
preferences[UI_GALLERY_SET] = sharedSettings.gallerySet.name
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
if (e is CancellationException) throw e
|
||||||
|
// Log any errors that occur while reading the DataStore.
|
||||||
|
Log.e("SharedPreferences") { "Error saving DataStore preferences: ${e.message}" }
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
if (e is CancellationException) throw e
|
|
||||||
// Log any errors that occur while reading the DataStore.
|
|
||||||
Log.e("SharedPreferences") { "Error saving DataStore preferences: ${e.message}" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user