Skip to content

Commit

Permalink
Remove unused getter and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Aug 28, 2024
1 parent 8d8f84b commit 982d21a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,18 @@ class AppSettingsManager internal constructor(
val appSettings: Flow<AppSettings?>
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,63 +53,47 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
return appSettingsQuery.asFlow().mapLatest { it.obj }
}

fun getTheme(): Flow<Theme> {
return appSettingsQuery.asFlow().mapLatest { it.obj?.theme ?: Theme.SYSTEM }
}

fun getValidityPeriod(): Flow<ValidityPeriod> {
return appSettingsQuery.asFlow().mapLatest { it.obj?.validityPeriod ?: ValidityPeriod.THIRTY }
}

fun getDownloadsLimit(): Flow<DownloadLimit> {
return appSettingsQuery.asFlow().mapLatest { it.obj?.downloadLimit ?: DownloadLimit.TWOHUNDREDFIFTY}
}

fun getEmailLanguage(): Flow<EmailLanguage> {
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
}

0 comments on commit 982d21a

Please sign in to comment.