diff --git a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/AppSettings.kt b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/AppSettings.kt index e3f57eca..d433d07f 100644 --- a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/AppSettings.kt +++ b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/AppSettings.kt @@ -25,7 +25,7 @@ import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod interface AppSettings { val theme: Theme - val validityPeriod: ValidityPeriod - val downloadLimit: DownloadLimit - val emailLanguage: EmailLanguage + var validityPeriod: ValidityPeriod + var downloadLimit: DownloadLimit + var emailLanguage: EmailLanguage } diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AppSettingsManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AppSettingsManager.kt index b5f318a9..3a3cb2f6 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AppSettingsManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AppSettingsManager.kt @@ -36,26 +36,18 @@ class AppSettingsManager internal constructor( val appSettings: Flow get() = appSettingsController.getAppSettingsFlow().flowOn(Dispatchers.IO) - fun getTheme() = appSettingsController.getTheme() - suspend fun setTheme(theme: Theme) = withContext(Dispatchers.IO) { appSettingsController.setTheme(theme) } - fun getValidityPeriod() = appSettingsController.getValidityPeriod() - suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) = withContext(Dispatchers.IO) { appSettingsController.setValidityPeriod(validityPeriod) } - fun getDownloadLimit() = appSettingsController.getDownloadsLimit() - suspend fun setDownloadLimit(downloadLimit: DownloadLimit) = withContext(Dispatchers.IO) { appSettingsController.setDownloadLimit(downloadLimit) } - fun getEmailLanguage() = appSettingsController.getEmailLanguage() - suspend fun setEmailLanguage(emailLanguage: EmailLanguage) = withContext(Dispatchers.IO) { appSettingsController.setEmailLanguage(emailLanguage) } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt index df1a31d3..9cca72ed 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt @@ -18,6 +18,7 @@ package com.infomaniak.multiplatform_swisstransfer.database.cache.setting +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage import com.infomaniak.multiplatform_swisstransfer.common.models.Theme @@ -52,63 +53,47 @@ class AppSettingsController(private val realmProvider: RealmProvider) { return appSettingsQuery.asFlow().mapLatest { it.obj } } - fun getTheme(): Flow { - return appSettingsQuery.asFlow().mapLatest { it.obj?.theme ?: Theme.SYSTEM } - } - - fun getValidityPeriod(): Flow { - return appSettingsQuery.asFlow().mapLatest { it.obj?.validityPeriod ?: ValidityPeriod.THIRTY } - } - - fun getDownloadsLimit(): Flow { - return appSettingsQuery.asFlow().mapLatest { it.obj?.downloadLimit ?: DownloadLimit.TWOHUNDREDFIFTY} - } - - fun getEmailLanguage(): Flow { - return appSettingsQuery.asFlow().mapLatest { it.obj?.emailLanguage ?: EmailLanguage.FRENCH } - } - //endregion //region Edit data - suspend fun setTheme(theme: Theme) { + + private suspend fun updateAppSettings(onUpdate: (AppSettings) -> Unit) { val appSettings = appSettingsQuery.find() ?: return realm.write { findLatest(appSettings)?.let { mutableAppSettings -> - mutableAppSettings.theme = theme + onUpdate(mutableAppSettings) } } } - suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) { + suspend fun setTheme(theme: Theme) { val appSettings = appSettingsQuery.find() ?: return realm.write { findLatest(appSettings)?.let { mutableAppSettings -> - mutableAppSettings.validityPeriod = validityPeriod + mutableAppSettings.theme = theme } } } - suspend fun setDownloadLimit(downloadLimit: DownloadLimit) { - val appSettings = appSettingsQuery.find() ?: return + suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) { + updateAppSettings { mutableAppSettings -> + mutableAppSettings.validityPeriod = validityPeriod + } + } - realm.write { - findLatest(appSettings)?.let { mutableAppSettings -> - mutableAppSettings.downloadLimit = downloadLimit - } + suspend fun setDownloadLimit(downloadLimit: DownloadLimit) { + updateAppSettings { mutableAppSettings -> + mutableAppSettings.downloadLimit = downloadLimit } } suspend fun setEmailLanguage(emailLanguage: EmailLanguage) { - val appSettings = appSettingsQuery.find() ?: return - - realm.write { - findLatest(appSettings)?.let { mutableAppSettings -> - mutableAppSettings.emailLanguage = emailLanguage - } + updateAppSettings { mutableAppSettings -> + mutableAppSettings.emailLanguage = emailLanguage } } + //endregion }