Login screens: better API
This commit is contained in:
parent
a3111dc2d8
commit
49f7ce3554
@ -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].
|
||||||
|
@ -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 {
|
||||||
// TODO Persist the wizard?
|
currentHomeServerConnectionConfig?.let {
|
||||||
return DefaultRegistrationWizard(homeServerConnectionConfig,
|
// TODO Persist the wizard?
|
||||||
okHttpClient,
|
return DefaultRegistrationWizard(
|
||||||
retrofitFactory,
|
it,
|
||||||
coroutineDispatchers,
|
okHttpClient,
|
||||||
sessionParamsStore,
|
retrofitFactory,
|
||||||
sessionManager)
|
coroutineDispatchers,
|
||||||
|
sessionParamsStore,
|
||||||
|
sessionManager
|
||||||
|
)
|
||||||
|
} ?: error("Please call getLoginFlow() with success first")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createLoginWizard(homeServerConnectionConfig: HomeServerConnectionConfig): LoginWizard {
|
override fun createLoginWizard(): LoginWizard {
|
||||||
return DefaultLoginWizard(
|
currentHomeServerConnectionConfig?.let {
|
||||||
homeServerConnectionConfig,
|
return DefaultLoginWizard(
|
||||||
coroutineDispatchers,
|
it,
|
||||||
sessionParamsStore,
|
coroutineDispatchers,
|
||||||
sessionManager,
|
sessionParamsStore,
|
||||||
retrofitFactory,
|
sessionManager,
|
||||||
okHttpClient
|
retrofitFactory,
|
||||||
)
|
okHttpClient
|
||||||
|
)
|
||||||
|
} ?: error("Please call getLoginFlow() with success first")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createSessionFromSso(homeServerConnectionConfig: HomeServerConnectionConfig,
|
override fun createSessionFromSso(homeServerConnectionConfig: HomeServerConnectionConfig,
|
||||||
|
@ -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
|
setState {
|
||||||
|
copy(
|
||||||
if (homeServerConnectionConfigFinal == null) {
|
asyncRegistration = Loading()
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registrationWizard = authenticationService.getOrCreateRegistrationWizard()
|
||||||
|
|
||||||
|
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) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user