Login screens: better API

This commit is contained in:
Benoit Marty 2019-11-21 22:46:37 +01:00
parent a3111dc2d8
commit 49f7ce3554
3 changed files with 53 additions and 53 deletions

View file

@ -32,20 +32,20 @@ import im.vector.matrix.android.internal.auth.data.LoginFlowResponse
interface AuthenticationService {
/**
* Request the supported login flows for this homeserver
* Request the supported login flows for this homeserver.
* This is the first method to call to be able to get a wizard to login or the create an account
*/
fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResponse>): Cancelable
/**
* Return an LoginWizard
* @param homeServerConnectionConfig this param is used to request the Homeserver
* Return a LoginWizard, to login to the homeserver
*/
fun createLoginWizard(homeServerConnectionConfig: HomeServerConnectionConfig): LoginWizard
fun createLoginWizard(): LoginWizard
/**
* Return a RegistrationWizard, to create an matrix account on a homeserver
* Return a RegistrationWizard, to create an matrix account on the homeserver
*/
fun getOrCreateRegistrationWizard(homeServerConnectionConfig: HomeServerConnectionConfig): RegistrationWizard
fun getOrCreateRegistrationWizard(): RegistrationWizard
/**
* Check if there is an authenticated [Session].

View file

@ -50,6 +50,8 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
private val sessionManager: SessionManager
) : AuthenticationService {
private var currentHomeServerConnectionConfig: HomeServerConnectionConfig? = null
override fun hasAuthenticatedSessions(): Boolean {
return sessionParamsStore.getLast() != null
}
@ -66,11 +68,22 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
}
override fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResponse>): Cancelable {
currentHomeServerConnectionConfig = null
val job = GlobalScope.launch(coroutineDispatchers.main) {
val result = runCatching {
getLoginFlowInternal(homeServerConnectionConfig)
}
result.foldToCallback(callback)
result.fold(
{
// The homeserver exists, keep the config
currentHomeServerConnectionConfig = homeServerConnectionConfig
callback.onSuccess(it)
},
{
callback.onFailure(it)
}
)
}
return CancelableCoroutine(job)
}
@ -83,25 +96,31 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
}
}
override fun getOrCreateRegistrationWizard(homeServerConnectionConfig: HomeServerConnectionConfig): RegistrationWizard {
// TODO Persist the wizard?
return DefaultRegistrationWizard(homeServerConnectionConfig,
okHttpClient,
retrofitFactory,
coroutineDispatchers,
sessionParamsStore,
sessionManager)
override fun getOrCreateRegistrationWizard(): RegistrationWizard {
currentHomeServerConnectionConfig?.let {
// TODO Persist the wizard?
return DefaultRegistrationWizard(
it,
okHttpClient,
retrofitFactory,
coroutineDispatchers,
sessionParamsStore,
sessionManager
)
} ?: error("Please call getLoginFlow() with success first")
}
override fun createLoginWizard(homeServerConnectionConfig: HomeServerConnectionConfig): LoginWizard {
return DefaultLoginWizard(
homeServerConnectionConfig,
coroutineDispatchers,
sessionParamsStore,
sessionManager,
retrofitFactory,
okHttpClient
)
override fun createLoginWizard(): LoginWizard {
currentHomeServerConnectionConfig?.let {
return DefaultLoginWizard(
it,
coroutineDispatchers,
sessionParamsStore,
sessionManager,
retrofitFactory,
okHttpClient
)
} ?: error("Please call getLoginFlow() with success first")
}
override fun createSessionFromSso(homeServerConnectionConfig: HomeServerConnectionConfig,

View file

@ -21,9 +21,9 @@ import com.airbnb.mvrx.*
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.login.LoginWizard
import im.vector.matrix.android.api.auth.AuthenticationService
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.login.LoginWizard
import im.vector.matrix.android.api.auth.registration.FlowResult
import im.vector.matrix.android.api.auth.registration.RegistrationResult
import im.vector.matrix.android.api.auth.registration.RegistrationWizard
@ -421,38 +421,19 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi
}
private fun startRegistrationFlow() {
val homeServerConnectionConfigFinal = homeServerConnectionConfig
if (homeServerConnectionConfigFinal == null) {
// Notify the user
_viewEvents.post(LoginViewEvents.RegistrationError(Throwable("Bad configuration")))
setState {
copy(
asyncRegistration = Uninitialized
)
}
} else {
setState {
copy(
asyncRegistration = Loading()
)
}
registrationWizard = authenticationService.getOrCreateRegistrationWizard(homeServerConnectionConfigFinal)
currentTask = registrationWizard?.getRegistrationFlow(registrationCallback)
setState {
copy(
asyncRegistration = Loading()
)
}
registrationWizard = authenticationService.getOrCreateRegistrationWizard()
currentTask = registrationWizard?.getRegistrationFlow(registrationCallback)
}
private fun startAuthenticationFlow() {
val homeServerConnectionConfigFinal = homeServerConnectionConfig
if (homeServerConnectionConfigFinal == null) {
// Notify the user
_viewEvents.post(LoginViewEvents.RegistrationError(Throwable("Bad configuration")))
} else {
loginWizard = authenticationService.createLoginWizard(homeServerConnectionConfigFinal)
}
loginWizard = authenticationService.createLoginWizard()
}
private fun onFlowResponse(flowResult: FlowResult) {