adding the identity terms to the discovery page state

This commit is contained in:
Adam Brown 2021-10-06 15:20:33 +01:00
parent 58b69b1fe4
commit a2f64177eb
4 changed files with 42 additions and 19 deletions

View file

@ -62,7 +62,7 @@ class DiscoverySettingsController @Inject constructor(
}
is Success -> {
buildIdentityServerSection(data)
val hasIdentityServer = data.identityServer().isNullOrBlank().not()
val hasIdentityServer = data.identityServer()?.serverUrl.isNullOrBlank().not()
if (hasIdentityServer && !data.termsNotSigned) {
buildConsentSection(data)
buildEmailsSection(data.emailList)
@ -106,7 +106,7 @@ class DiscoverySettingsController @Inject constructor(
}
private fun buildIdentityServerSection(data: DiscoverySettingsState) {
val identityServer = data.identityServer() ?: stringProvider.getString(R.string.none)
val identityServer = data.identityServer()?.serverUrl ?: stringProvider.getString(R.string.none)
val host = this
settingsSectionTitleItem {

View file

@ -111,7 +111,7 @@ class DiscoverySettingsFragment @Inject constructor(
requireContext(),
termsActivityResultLauncher,
TermsService.ServiceType.IdentityService,
state.identityServer()?.ensureProtocol() ?: "",
state.identityServer()?.serverUrl?.ensureProtocol() ?: "",
null)
}
}
@ -179,7 +179,7 @@ class DiscoverySettingsFragment @Inject constructor(
override fun onTapUpdateUserConsent(newValue: Boolean) {
if (newValue) {
withState(viewModel) { state ->
requireContext().showIdentityServerConsentDialog(state.identityServer.invoke()) {
requireContext().showIdentityServerConsentDialog(state.identityServer.invoke()?.serverUrl) {
viewModel.handle(DiscoverySettingsAction.UpdateUserConsent(true))
}
}

View file

@ -19,12 +19,18 @@ package im.vector.app.features.discovery
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
import org.matrix.android.sdk.internal.auth.registration.LocalizedFlowDataLoginTerms
data class DiscoverySettingsState(
val identityServer: Async<String?> = Uninitialized,
val identityServer: Async<IdentityServerWithTerms?> = Uninitialized,
val emailList: Async<List<PidInfo>> = Uninitialized,
val phoneNumbersList: Async<List<PidInfo>> = Uninitialized,
// Can be true if terms are updated
val termsNotSigned: Boolean = false,
val userConsent: Boolean = false
) : MvRxState
data class IdentityServerWithTerms(
val serverUrl: String?,
val terms: List<LocalizedFlowDataLoginTerms>
)

View file

@ -27,20 +27,25 @@ import com.airbnb.mvrx.ViewModelContext
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.R
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.ensureProtocol
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.IdentityServiceError
import org.matrix.android.sdk.api.session.identity.IdentityServiceListener
import org.matrix.android.sdk.api.session.identity.SharedState
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.rx.rx
class DiscoverySettingsViewModel @AssistedInject constructor(
@Assisted initialState: DiscoverySettingsState,
private val session: Session) :
VectorViewModel<DiscoverySettingsState, DiscoverySettingsAction, DiscoverySettingsViewEvents>(initialState) {
private val session: Session,
private val stringProvider: StringProvider
) : VectorViewModel<DiscoverySettingsState, DiscoverySettingsAction, DiscoverySettingsViewEvents>(initialState) {
@AssistedFactory
interface Factory {
@ -57,25 +62,37 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
}
private val identityService = session.identityService()
private val termsService: TermsService = session
private val identityServerManagerListener = object : IdentityServiceListener {
override fun onIdentityServerChange() = withState { state ->
val identityServerUrl = identityService.getCurrentIdentityServerUrl()
val currentIS = state.identityServer()
setState {
copy(
identityServer = Success(identityServerUrl),
userConsent = identityService.getUserConsent()
)
viewModelScope.launch {
val identityServer = fetchIdentityServerWithTerms()
val currentIS = state.identityServer()
setState {
copy(
identityServer = Success(identityServer),
userConsent = identityService.getUserConsent()
)
}
if (currentIS != identityServer) retrieveBinding()
}
if (currentIS != identityServerUrl) retrieveBinding()
}
}
private suspend fun fetchIdentityServerWithTerms(): IdentityServerWithTerms {
val identityServerUrl = identityService.getCurrentIdentityServerUrl()
val policies = if (identityServerUrl == null) emptyList() else {
val terms = termsService.getTerms(TermsService.ServiceType.IdentityService, identityServerUrl.ensureProtocol())
terms.serverResponse.getLocalizedTerms(stringProvider.getString(R.string.resources_language))
}
return IdentityServerWithTerms(identityServerUrl, policies)
}
init {
setState {
copy(
identityServer = Success(identityService.getCurrentIdentityServerUrl()),
identityServer = Success(IdentityServerWithTerms(identityService.getCurrentIdentityServerUrl(), emptyList())),
userConsent = identityService.getUserConsent()
)
}
@ -143,7 +160,7 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
val data = session.identityService().setNewIdentityServer(action.url)
setState {
copy(
identityServer = Success(data),
identityServer = Success(IdentityServerWithTerms(data, emptyList())),
userConsent = false
)
}
@ -287,7 +304,7 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
}
private fun retrieveBinding(threePids: List<ThreePid>) = withState { state ->
if (state.identityServer().isNullOrBlank()) return@withState
if (state.identityServer()?.serverUrl.isNullOrBlank()) return@withState
val emails = threePids.filterIsInstance<ThreePid.Email>()
val msisdns = threePids.filterIsInstance<ThreePid.Msisdn>()
@ -335,7 +352,7 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
}
private fun submitMsisdnToken(action: DiscoverySettingsAction.SubmitMsisdnToken) = withState { state ->
if (state.identityServer().isNullOrBlank()) return@withState
if (state.identityServer()?.serverUrl.isNullOrBlank()) return@withState
changeThreePidSubmitState(action.threePid, Loading())