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 { 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 fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResponse>): Cancelable
/** /**
* Return an LoginWizard * Return a LoginWizard, to login to the homeserver
* @param homeServerConnectionConfig this param is used to request 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]. * Check if there is an authenticated [Session].

View File

@ -50,6 +50,8 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
private val sessionManager: SessionManager private val sessionManager: SessionManager
) : AuthenticationService { ) : AuthenticationService {
private var currentHomeServerConnectionConfig: HomeServerConnectionConfig? = null
override fun hasAuthenticatedSessions(): Boolean { override fun hasAuthenticatedSessions(): Boolean {
return sessionParamsStore.getLast() != null return sessionParamsStore.getLast() != null
} }
@ -66,11 +68,22 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
} }
override fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResponse>): Cancelable { override fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResponse>): Cancelable {
currentHomeServerConnectionConfig = null
val job = GlobalScope.launch(coroutineDispatchers.main) { val job = GlobalScope.launch(coroutineDispatchers.main) {
val result = runCatching { val result = runCatching {
getLoginFlowInternal(homeServerConnectionConfig) getLoginFlowInternal(homeServerConnectionConfig)
} }
result.foldToCallback(callback) result.fold(
{
// The homeserver exists, keep the config
currentHomeServerConnectionConfig = homeServerConnectionConfig
callback.onSuccess(it)
},
{
callback.onFailure(it)
}
)
} }
return CancelableCoroutine(job) return CancelableCoroutine(job)
} }
@ -83,25 +96,31 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
} }
} }
override fun getOrCreateRegistrationWizard(homeServerConnectionConfig: HomeServerConnectionConfig): RegistrationWizard { override fun getOrCreateRegistrationWizard(): RegistrationWizard {
currentHomeServerConnectionConfig?.let {
// TODO Persist the wizard? // TODO Persist the wizard?
return DefaultRegistrationWizard(homeServerConnectionConfig, return DefaultRegistrationWizard(
it,
okHttpClient, okHttpClient,
retrofitFactory, retrofitFactory,
coroutineDispatchers, coroutineDispatchers,
sessionParamsStore, sessionParamsStore,
sessionManager) sessionManager
)
} ?: error("Please call getLoginFlow() with success first")
} }
override fun createLoginWizard(homeServerConnectionConfig: HomeServerConnectionConfig): LoginWizard { override fun createLoginWizard(): LoginWizard {
currentHomeServerConnectionConfig?.let {
return DefaultLoginWizard( return DefaultLoginWizard(
homeServerConnectionConfig, it,
coroutineDispatchers, coroutineDispatchers,
sessionParamsStore, sessionParamsStore,
sessionManager, sessionManager,
retrofitFactory, retrofitFactory,
okHttpClient okHttpClient
) )
} ?: error("Please call getLoginFlow() with success first")
} }
override fun createSessionFromSso(homeServerConnectionConfig: HomeServerConnectionConfig, 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.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.MatrixCallback 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.AuthenticationService
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig 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.FlowResult
import im.vector.matrix.android.api.auth.registration.RegistrationResult import im.vector.matrix.android.api.auth.registration.RegistrationResult
import im.vector.matrix.android.api.auth.registration.RegistrationWizard import im.vector.matrix.android.api.auth.registration.RegistrationWizard
@ -421,38 +421,19 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi
} }
private fun startRegistrationFlow() { 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 { setState {
copy( copy(
asyncRegistration = Loading() asyncRegistration = Loading()
) )
} }
registrationWizard = authenticationService.getOrCreateRegistrationWizard(homeServerConnectionConfigFinal) registrationWizard = authenticationService.getOrCreateRegistrationWizard()
currentTask = registrationWizard?.getRegistrationFlow(registrationCallback) currentTask = registrationWizard?.getRegistrationFlow(registrationCallback)
} }
}
private fun startAuthenticationFlow() { private fun startAuthenticationFlow() {
val homeServerConnectionConfigFinal = homeServerConnectionConfig loginWizard = authenticationService.createLoginWizard()
if (homeServerConnectionConfigFinal == null) {
// Notify the user
_viewEvents.post(LoginViewEvents.RegistrationError(Throwable("Bad configuration")))
} else {
loginWizard = authenticationService.createLoginWizard(homeServerConnectionConfigFinal)
}
} }
private fun onFlowResponse(flowResult: FlowResult) { private fun onFlowResponse(flowResult: FlowResult) {