diff --git a/mail-settings/data/src/main/kotlin/ch/protonmail/android/mailsettings/data/repository/AppLanguageRepositoryImpl.kt b/mail-settings/data/src/main/kotlin/ch/protonmail/android/mailsettings/data/repository/AppLanguageRepositoryImpl.kt index 72745c3210..f1a96ad09a 100644 --- a/mail-settings/data/src/main/kotlin/ch/protonmail/android/mailsettings/data/repository/AppLanguageRepositoryImpl.kt +++ b/mail-settings/data/src/main/kotlin/ch/protonmail/android/mailsettings/data/repository/AppLanguageRepositoryImpl.kt @@ -20,6 +20,9 @@ package ch.protonmail.android.mailsettings.data.repository import androidx.appcompat.app.AppCompatDelegate import androidx.core.os.LocaleListCompat +import arrow.core.Either +import arrow.core.raise.either +import ch.protonmail.android.mailcommon.domain.model.PreferencesError import ch.protonmail.android.mailcommon.domain.repository.AppLocaleRepository import ch.protonmail.android.mailsettings.domain.model.AppLanguage import ch.protonmail.android.mailsettings.domain.repository.AppLanguageRepository @@ -30,31 +33,43 @@ import javax.inject.Inject class AppLanguageRepositoryImpl @Inject constructor(private val appLocaleRepository: AppLocaleRepository) : AppLanguageRepository { - private val languagePreferenceFlow = MutableSharedFlow(replay = 1) + private val languagePreferenceFlow = MutableSharedFlow>(replay = 1) - override fun observe(): Flow { - val savedAppLocales = AppCompatDelegate.getApplicationLocales() - val languageTag = savedAppLocales[0]?.toLanguageTag() + init { + val result = either { + val savedAppLocales = AppCompatDelegate.getApplicationLocales() + val languageTag = savedAppLocales[0]?.toLanguageTag() + val appLanguage = AppLanguage.fromTag(languageTag) + appLanguage + }.mapLeft { _ -> PreferencesError } - languagePreferenceFlow.tryEmit(AppLanguage.fromTag(languageTag)) + languagePreferenceFlow.tryEmit(result) + } + override fun observe(): Flow> { return languagePreferenceFlow } override fun save(language: AppLanguage) { val locales = LocaleListCompat.forLanguageTags(language.langTag) AppCompatDelegate.setApplicationLocales(locales) - languagePreferenceFlow.tryEmit(language) + val result = either { + language + }.mapLeft { _ -> PreferencesError } + languagePreferenceFlow.tryEmit(result) appLocaleRepository.refresh() } override fun clear() { val emptyLocales = LocaleListCompat.getEmptyLocaleList() AppCompatDelegate.setApplicationLocales(emptyLocales) - languagePreferenceFlow.tryEmit(null) + val result = either { + null + }.mapLeft { _ -> PreferencesError } + + languagePreferenceFlow.tryEmit(result) appLocaleRepository.refresh() } - -} +} \ No newline at end of file diff --git a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/model/AppLanguage.kt b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/model/AppLanguage.kt index e0882716ed..6846d65afc 100644 --- a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/model/AppLanguage.kt +++ b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/model/AppLanguage.kt @@ -48,7 +48,7 @@ enum class AppLanguage(val langName: String, val langTag: String) { UKRAINIAN("Українська", "uk"); companion object { - private val map = values().associateBy { it.langTag } + private val map = entries.associateBy { it.langTag } fun fromTag(tag: String?): AppLanguage? = map[tag] } } diff --git a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/repository/AppLanguageRepository.kt b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/repository/AppLanguageRepository.kt index 82a96f40d4..99f4a7d159 100644 --- a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/repository/AppLanguageRepository.kt +++ b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/repository/AppLanguageRepository.kt @@ -18,11 +18,13 @@ package ch.protonmail.android.mailsettings.domain.repository +import arrow.core.Either +import ch.protonmail.android.mailcommon.domain.model.PreferencesError import ch.protonmail.android.mailsettings.domain.model.AppLanguage import kotlinx.coroutines.flow.Flow interface AppLanguageRepository { - fun observe(): Flow + fun observe(): Flow> fun save(language: AppLanguage) fun clear() } diff --git a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/usecase/ObserveAppSettings.kt b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/usecase/ObserveAppSettings.kt index 07c5fb45c8..1be9ebd46c 100644 --- a/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/usecase/ObserveAppSettings.kt +++ b/mail-settings/domain/src/main/kotlin/ch/protonmail/android/mailsettings/domain/usecase/ObserveAppSettings.kt @@ -45,14 +45,15 @@ class ObserveAppSettings @Inject constructor( appLanguageRepository.observe(), combinedContactsRepository.observe() ) { autoLockPref, alternativeRouting, customLanguage, combinedContacts -> - val hasAutoLock = autoLockPref.getOrElse { AutoLockPreference(isEnabled = false) } + val hasAutoLock = autoLockPref.getOrElse { AutoLockPreference(isEnabled = false) }.isEnabled val hasCombinedContacts = (combinedContacts as Either.Right).value.isEnabled val hasAlternativeRouting = (alternativeRouting as Either.Right).value.isEnabled + val customAppLanguage = customLanguage.getOrElse { null }?.langName AppSettings( - hasAutoLock = hasAutoLock.isEnabled, + hasAutoLock = hasAutoLock, hasAlternativeRouting = hasAlternativeRouting, - customAppLanguage = customLanguage?.langName, + customAppLanguage = customAppLanguage, hasCombinedContacts = hasCombinedContacts ) } diff --git a/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsState.kt b/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsState.kt index d01275e0ee..fe744bc6fb 100644 --- a/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsState.kt +++ b/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsState.kt @@ -27,7 +27,7 @@ sealed class LanguageSettingsState { val languages: List ) : LanguageSettingsState() - object Loading : LanguageSettingsState() + data object Loading : LanguageSettingsState() } data class LanguageUiModel( diff --git a/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsViewModel.kt b/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsViewModel.kt index c5764d07df..65b2251077 100644 --- a/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsViewModel.kt +++ b/mail-settings/presentation/src/main/kotlin/ch/protonmail/android/mailsettings/presentation/settings/language/LanguageSettingsViewModel.kt @@ -20,6 +20,7 @@ package ch.protonmail.android.mailsettings.presentation.settings.language import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import arrow.core.Either import ch.protonmail.android.mailsettings.domain.model.AppLanguage import ch.protonmail.android.mailsettings.domain.repository.AppLanguageRepository import ch.protonmail.android.mailsettings.presentation.settings.language.LanguageSettingsState.Loading @@ -39,10 +40,19 @@ class LanguageSettingsViewModel @Inject constructor( val state: Flow = languageRepository .observe() - .mapLatest { selectedLang -> - val languages = getAppLanguageUiModels(selectedLang).sortedBy { it.name } - val isSystemDefault = selectedLang == null - LanguageSettingsState.Data(isSystemDefault, languages) + .mapLatest { either -> + when (either) { + is Either.Right -> { + val selectedLang = either.value + val languages = getAppLanguageUiModels(selectedLang).sortedBy { it.name } + val isSystemDefault = selectedLang == null + LanguageSettingsState.Data(isSystemDefault, languages) + } + is Either.Left -> { + val defaultLanguages = getAppLanguageUiModels(null).sortedBy { it.name } + LanguageSettingsState.Data(isSystemDefault = true, languages = defaultLanguages) + } + } } .stateIn( viewModelScope, @@ -58,7 +68,7 @@ class LanguageSettingsViewModel @Inject constructor( languageRepository.clear() } - private fun getAppLanguageUiModels(selectedAppLanguage: AppLanguage?) = AppLanguage.values().map { + private fun getAppLanguageUiModels(selectedAppLanguage: AppLanguage?) = AppLanguage.entries.map { LanguageUiModel( language = it, isSelected = it == selectedAppLanguage,