Prefill "Your name" in display options if possible

This commit is contained in:
cketti 2024-01-18 15:31:33 +01:00
parent 28892810f4
commit 75af1c9975
6 changed files with 62 additions and 2 deletions

View file

@ -111,6 +111,7 @@ val featureAccountSetupModule: Module = module {
DisplayOptionsViewModel(
validator = get(),
accountStateRepository = get(),
accountOwnerNameProvider = get(),
)
}

View file

@ -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 },
),
)
}

View file

@ -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<State, Event, Effect>(
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
}
}
}
}

View file

@ -60,6 +60,7 @@ class AccountSetupModuleKtTest : KoinTest {
}
single<LocalKeyStore> { mock() }
single<AccountCommonExternalContract.AccountStateLoader> { mock() }
factory<AccountSetupExternalContract.AccountOwnerNameProvider> { mock() }
}
@Test
@ -83,6 +84,7 @@ class AccountSetupModuleKtTest : KoinTest {
InteractionMode::class,
SpecialFoldersContract.State::class,
CreateAccountContract.State::class,
AccountSetupExternalContract.AccountOwnerNameProvider::class,
),
)

View file

@ -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
}

View file

@ -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
}
}