Repository: thunderbird/thunderbird-android
File: feature/account/settings/impl/src/main/kotlin/net/thunderbird/feature/account/settings/impl/ui/general/GeneralSettingsViewModel.kt (Kotlin)

Code before:
import net.thunderbird.feature.account.settings.impl.domain.AccountSettingsDomainContract.UseCase
import net.thunderbird.feature.account.settings.impl.domain.usecase.GetAccountName
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.Effect
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.Event
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.State

internal class GeneralSettingsViewModel(
    private val accountId: AccountId,
    private val getAccountName: UseCase.GetAccountName,
    private val getGeneralPreferences: UseCase.GetGeneralPreferences,
    private val updateGeneralPreferences: UseCase.UpdateGeneralPreferences,
    initialState: State = State(),
) : BaseViewModel<State, Event, Effect>(initialState), GeneralSettingsContract.ViewModel {

    init {
        viewModelScope.launch {
            getAccountName(accountId).collect { outcome ->
                outcome.handle(
                    onSuccess = { accountName ->
                        updateState { state ->
                            state.copy(
                                subtitle = accountName,
                            )
                        }
                    },
                    onFailure = {},
                )
            }
        }

        viewModelScope.launch {
            getGeneralPreferences(accountId).collect { outcome ->
                outcome.handle(
                    onSuccess = { preferences ->
                        updateState { state ->
                            state.copy(
                                preferences = preferences,
                            )
                        }
                    },
                    onFailure = {},
                )
            }
        }
    }

    override fun event(event: Event) {
        when (event) {
            is Event.OnPreferenceSettingChange -> updatePreference(event.preference)
            is Event.OnBackPressed -> emitEffect(Effect.NavigateBack)
        }

Review comment (bug):
Although we do not show the failure to the user, I recommend logging it to maintain a record of when failures occur.

Code after:
import net.thunderbird.feature.account.settings.impl.domain.AccountSettingsDomainContract.SettingsError
import net.thunderbird.feature.account.settings.impl.domain.AccountSettingsDomainContract.UseCase
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.Effect
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.Event
import net.thunderbird.feature.account.settings.impl.ui.general.GeneralSettingsContract.State
import timber.log.Timber

internal class GeneralSettingsViewModel(
    private val accountId: AccountId,
    private val getAccountName: UseCase.GetAccountName,
    private val getGeneralPreferences: UseCase.GetGeneralPreferences,
    private val updateGeneralPreferences: UseCase.UpdateGeneralPreferences,
    initialState: State = State(),
) : BaseViewModel<State, Event, Effect>(initialState), GeneralSettingsContract.ViewModel {

    init {
        viewModelScope.launch {
            getAccountName(accountId).collect { outcome ->
                outcome.handle(
                    onSuccess = { accountName ->
                        updateState { state ->
                            state.copy(
                                subtitle = accountName,
                            )
                        }
                    },
                    onFailure = { handleError(it) },
                )
            }
        }

        viewModelScope.launch {
            getGeneralPreferences(accountId).collect { outcome ->
                outcome.handle(
                    onSuccess = { preferences ->
                        updateState { state ->
                            state.copy(
                                preferences = preferences,
                            )
                        }
                    },
                    onFailure = { handleError(it) },
                )
            }
        }
    }

    override fun event(event: Event) {
        when (event) {
            is Event.OnPreferenceSettingChange -> updatePreference(event.preference)
            is Event.OnBackPressed -> emitEffect(Effect.NavigateBack)