diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/AccountSetupModule.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/AccountSetupModule.kt index 5c8068934..bc2eb62c3 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/AccountSetupModule.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/AccountSetupModule.kt @@ -111,6 +111,7 @@ val featureAccountSetupModule: Module = module { DisplayOptionsViewModel( validator = get(), accountStateRepository = get(), + accountOwnerNameProvider = get(), ) } diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsScreen.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsScreen.kt index 4aaa9c33d..1811fd16b 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsScreen.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsScreen.kt @@ -72,6 +72,7 @@ internal fun DisplayOptionsScreenK9Preview() { viewModel = DisplayOptionsViewModel( validator = DisplayOptionsValidator(), accountStateRepository = PreviewAccountStateRepository(), + accountOwnerNameProvider = { null }, ), ) } @@ -87,6 +88,7 @@ internal fun DisplayOptionsScreenThunderbirdPreview() { viewModel = DisplayOptionsViewModel( validator = DisplayOptionsValidator(), accountStateRepository = PreviewAccountStateRepository(), + accountOwnerNameProvider = { null }, ), ) } diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModel.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModel.kt index 65d00e3b5..76a9889f8 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModel.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModel.kt @@ -1,17 +1,22 @@ package app.k9mail.feature.account.setup.ui.options.display +import androidx.lifecycle.viewModelScope import app.k9mail.core.common.domain.usecase.validation.ValidationResult import app.k9mail.core.ui.compose.common.mvi.BaseViewModel import app.k9mail.feature.account.common.domain.AccountDomainContract +import app.k9mail.feature.account.common.domain.input.StringInputField +import app.k9mail.feature.account.setup.AccountSetupExternalContract import app.k9mail.feature.account.setup.ui.options.display.DisplayOptionsContract.Effect import app.k9mail.feature.account.setup.ui.options.display.DisplayOptionsContract.Event import app.k9mail.feature.account.setup.ui.options.display.DisplayOptionsContract.State import app.k9mail.feature.account.setup.ui.options.display.DisplayOptionsContract.Validator import app.k9mail.feature.account.setup.ui.options.display.DisplayOptionsContract.ViewModel +import kotlinx.coroutines.launch internal class DisplayOptionsViewModel( private val validator: Validator, private val accountStateRepository: AccountDomainContract.AccountStateRepository, + private val accountOwnerNameProvider: AccountSetupExternalContract.AccountOwnerNameProvider, initialState: State? = null, ) : BaseViewModel( initialState = initialState ?: accountStateRepository.getState().toDisplayOptionsState(), @@ -46,8 +51,19 @@ internal class DisplayOptionsViewModel( } private fun loadAccountState() { - updateState { - accountStateRepository.getState().toDisplayOptionsState() + viewModelScope.launch { + val ownerName = accountOwnerNameProvider.getOwnerName().orEmpty() + + updateState { + val displayOptionsState = accountStateRepository.getState().toDisplayOptionsState() + if (displayOptionsState.displayName.value.isEmpty()) { + displayOptionsState.copy( + displayName = StringInputField(value = ownerName), + ) + } else { + displayOptionsState + } + } } } diff --git a/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/AccountSetupModuleKtTest.kt b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/AccountSetupModuleKtTest.kt index 119c5d714..b327d5217 100644 --- a/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/AccountSetupModuleKtTest.kt +++ b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/AccountSetupModuleKtTest.kt @@ -60,6 +60,7 @@ class AccountSetupModuleKtTest : KoinTest { } single { mock() } single { mock() } + factory { mock() } } @Test @@ -83,6 +84,7 @@ class AccountSetupModuleKtTest : KoinTest { InteractionMode::class, SpecialFoldersContract.State::class, CreateAccountContract.State::class, + AccountSetupExternalContract.AccountOwnerNameProvider::class, ), ) diff --git a/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModelTest.kt b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModelTest.kt index a9deea0cd..ff7074cd8 100644 --- a/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModelTest.kt +++ b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/DisplayOptionsViewModelTest.kt @@ -22,9 +22,11 @@ class DisplayOptionsViewModelTest { @get:Rule val mainDispatcherRule = MainDispatcherRule() + private val accountOwnerNameProvider = FakeAccountOwnerNameProvider() private val testSubject = DisplayOptionsViewModel( validator = FakeDisplayOptionsValidator(), accountStateRepository = InMemoryAccountStateRepository(), + accountOwnerNameProvider = accountOwnerNameProvider, ) @Test @@ -101,6 +103,7 @@ class DisplayOptionsViewModelTest { accountNameAnswer = ValidationResult.Failure(TestError), ), accountStateRepository = InMemoryAccountStateRepository(), + accountOwnerNameProvider = accountOwnerNameProvider, ) val stateTurbine = viewModel.state.testIn(backgroundScope) val effectTurbine = viewModel.effect.testIn(backgroundScope) @@ -153,5 +156,30 @@ class DisplayOptionsViewModelTest { } } + @Test + fun `should set owner name when LoadAccountState event received`() = runTest { + accountOwnerNameProvider.ownerName = "Alice Example" + val viewModel = testSubject + val stateTurbine = viewModel.state.testIn(backgroundScope) + val effectTurbine = viewModel.effect.testIn(backgroundScope) + val turbines = listOf(stateTurbine, effectTurbine) + + assertThatAndTurbinesConsumed( + actual = stateTurbine.awaitItem(), + turbines = turbines, + ) { + isEqualTo(State()) + } + + viewModel.event(Event.LoadAccountState) + + assertThatAndTurbinesConsumed( + actual = stateTurbine.awaitItem(), + turbines = turbines, + ) { + isEqualTo(State(displayName = StringInputField("Alice Example"))) + } + } + private object TestError : ValidationError } diff --git a/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/FakeAccountOwnerNameProvider.kt b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/FakeAccountOwnerNameProvider.kt new file mode 100644 index 000000000..ec1dcb5be --- /dev/null +++ b/feature/account/setup/src/test/kotlin/app/k9mail/feature/account/setup/ui/options/display/FakeAccountOwnerNameProvider.kt @@ -0,0 +1,11 @@ +package app.k9mail.feature.account.setup.ui.options.display + +import app.k9mail.feature.account.setup.AccountSetupExternalContract + +class FakeAccountOwnerNameProvider : AccountSetupExternalContract.AccountOwnerNameProvider { + var ownerName: String? = null + + override suspend fun getOwnerName(): String? { + return ownerName + } +}