Split server certificate error screen into two parts

This commit is contained in:
cketti 2024-01-09 13:15:58 +01:00
parent b9a7d17bba
commit 95ae5180e7
3 changed files with 42 additions and 22 deletions

View file

@ -7,16 +7,19 @@ interface ServerCertificateErrorContract {
interface ViewModel : UnidirectionalViewModel<State, Event, Effect>
data class State(
val hostname: String = "",
val isShowServerCertificate: Boolean = false,
val errorText: String = "",
)
sealed interface Event {
object OnCertificateAcceptedClicked : Event
object OnBackClicked : Event
data object OnShowAdvancedClicked : Event
data object OnCertificateAcceptedClicked : Event
data object OnBackClicked : Event
}
sealed interface Effect {
object NavigateCertificateAccepted : Effect
object NavigateBack : Effect
data object NavigateCertificateAccepted : Effect
data object NavigateBack : Effect
}
}

View file

@ -69,11 +69,19 @@ fun ServerCertificateErrorScreen(
modifier = Modifier
.fillMaxWidth(),
)
ButtonOutlined(
text = "Accept consequences and continue",
onClick = { dispatch(Event.OnCertificateAcceptedClicked) },
modifier = Modifier.fillMaxWidth(),
)
if (state.value.isShowServerCertificate) {
ButtonOutlined(
text = "Accept consequences and continue",
onClick = { dispatch(Event.OnCertificateAcceptedClicked) },
modifier = Modifier.fillMaxWidth(),
)
} else {
ButtonOutlined(
text = "Advanced",
onClick = { dispatch(Event.OnShowAdvancedClicked) },
modifier = Modifier.fillMaxWidth(),
)
}
}
}
},
@ -110,17 +118,18 @@ fun ServerCertificateErrorScreen(
Spacer(modifier = Modifier.height(MainTheme.spacings.quadruple))
val hostname = state.value.hostname
TextBody1(
text = "The server presented an invalid TLS certificate. " +
"Sometimes, this is because of a server misconfiguration. " +
"Sometimes it is because someone is trying to attack you or your mail server. " +
"If you're not sure what's up, click \"Go back\" and contact the folks who manage your " +
"mail server.",
text = "The app detected a potential security threat and did not continue to connect to " +
"$hostname. If you continue, attackers could try to steal information like your password or " +
"emails.",
)
Spacer(modifier = Modifier.height(MainTheme.spacings.quadruple))
TextBody1(state.value.errorText)
if (state.value.isShowServerCertificate) {
TextBody1(state.value.errorText)
}
}
}
}

View file

@ -23,16 +23,30 @@ class ServerCertificateErrorViewModel(
private val serverCertificateError: ServerCertificateError? = certificateErrorRepository.getCertificateError()
init {
setErrorMessage(buildErrorMessage(serverCertificateError))
serverCertificateError?.let { serverCertificateError ->
updateState {
it.copy(
hostname = serverCertificateError.hostname,
errorText = buildErrorMessage(serverCertificateError),
)
}
}
}
override fun event(event: Event) {
when (event) {
Event.OnShowAdvancedClicked -> showAdvanced()
Event.OnCertificateAcceptedClicked -> acceptCertificate()
Event.OnBackClicked -> navigateBack()
}
}
private fun showAdvanced() {
updateState {
it.copy(isShowServerCertificate = true)
}
}
private fun acceptCertificate() {
val certificateError = requireNotNull(serverCertificateError)
@ -96,10 +110,4 @@ class ServerCertificateErrorViewModel(
}
}
}
private fun setErrorMessage(errorText: String) {
updateState {
it.copy(errorText = errorText)
}
}
}