Do not modify the DB in debug mode, and add to history only if valid

This commit is contained in:
Benoit Marty 2020-12-14 15:22:55 +01:00
parent f3578e2538
commit 776d7699bc
1 changed files with 13 additions and 9 deletions

View File

@ -75,11 +75,15 @@ class LoginViewModel @AssistedInject constructor(
} }
init { init {
if (BuildConfig.DEBUG) { getKnownCustomHomeServersUrls()
homeServerHistoryService.addHomeServerToHistory("http://10.0.2.2:8080")
} }
private fun getKnownCustomHomeServersUrls() {
setState { setState {
copy(knownCustomHomeServersUrls = homeServerHistoryService.getKnownServersUrls()) copy(
knownCustomHomeServersUrls = homeServerHistoryService.getKnownServersUrls()
+ if (BuildConfig.DEBUG) listOf("http://10.0.2.2:8080") else emptyList()
)
} }
} }
@ -142,7 +146,6 @@ class LoginViewModel @AssistedInject constructor(
// So alter the homeserver config and retrieve again the login flow // So alter the homeserver config and retrieve again the login flow
when (val finalLastAction = lastAction) { when (val finalLastAction = lastAction) {
is LoginAction.UpdateHomeServer -> { is LoginAction.UpdateHomeServer -> {
rememberHomeServer(finalLastAction.homeServerUrl)
currentHomeServerConnectionConfig currentHomeServerConnectionConfig
?.let { it.copy(allowedFingerprints = it.allowedFingerprints + action.fingerprint) } ?.let { it.copy(allowedFingerprints = it.allowedFingerprints + action.fingerprint) }
?.let { getLoginFlow(it) } ?.let { getLoginFlow(it) }
@ -161,9 +164,7 @@ class LoginViewModel @AssistedInject constructor(
private fun rememberHomeServer(homeServerUrl: String) { private fun rememberHomeServer(homeServerUrl: String) {
homeServerHistoryService.addHomeServerToHistory(homeServerUrl) homeServerHistoryService.addHomeServerToHistory(homeServerUrl)
setState { getKnownCustomHomeServersUrls()
copy(knownCustomHomeServersUrls = homeServerHistoryService.getKnownServersUrls())
}
} }
private fun handleLoginWithToken(action: LoginAction.LoginWithToken) { private fun handleLoginWithToken(action: LoginAction.LoginWithToken) {
@ -738,7 +739,6 @@ class LoginViewModel @AssistedInject constructor(
// This is invalid // This is invalid
_viewEvents.post(LoginViewEvents.Failure(Throwable("Unable to create a HomeServerConnectionConfig"))) _viewEvents.post(LoginViewEvents.Failure(Throwable("Unable to create a HomeServerConnectionConfig")))
} else { } else {
rememberHomeServer(action.homeServerUrl)
getLoginFlow(homeServerConnectionConfig) getLoginFlow(homeServerConnectionConfig)
} }
} }
@ -772,6 +772,10 @@ class LoginViewModel @AssistedInject constructor(
} }
override fun onSuccess(data: LoginFlowResult) { override fun onSuccess(data: LoginFlowResult) {
// Valid Homeserver, add it to the history.
// Note: we add what the user has input, data.homeServerUrl can be different
rememberHomeServer(homeServerConnectionConfig.homeServerUri.toString())
when (data) { when (data) {
is LoginFlowResult.Success -> { is LoginFlowResult.Success -> {
val loginMode = when { val loginMode = when {