Identity: bind/unbnd: restore the bind in progress State

This commit is contained in:
Benoit Marty 2020-05-09 04:17:19 +02:00
parent 3e808dec90
commit 1535f3e2e5
4 changed files with 43 additions and 16 deletions

View file

@ -65,6 +65,15 @@ interface IdentityService {
*/
fun lookUp(threePids: List<ThreePid>, callback: MatrixCallback<List<FoundThreePid>>): Cancelable
/**
* Get the status of the current user's threePid
* A lookup will be performed, but also pending binding state will be restored
*
* @param threePids the list of threePid the user owns (retrieved form the homeserver)
* @param callback onSuccess will be called with a map of ThreePid -> SharedState
*/
fun getShareStatus(threePids: List<ThreePid>, callback: MatrixCallback<Map<ThreePid, SharedState>>): Cancelable
fun addListener(listener: IdentityServiceListener)
fun removeListener(listener: IdentityServiceListener)
}

View file

@ -16,11 +16,6 @@
package im.vector.matrix.android.api.session.identity
data class ThreePidStatus(
val threePid: ThreePid,
val shareState: SharedState
)
enum class SharedState {
SHARED,
NOT_SHARED,

View file

@ -29,6 +29,7 @@ import im.vector.matrix.android.api.session.identity.FoundThreePid
import im.vector.matrix.android.api.session.identity.IdentityService
import im.vector.matrix.android.api.session.identity.IdentityServiceError
import im.vector.matrix.android.api.session.identity.IdentityServiceListener
import im.vector.matrix.android.api.session.identity.SharedState
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.api.util.NoOpCancellable
@ -208,6 +209,30 @@ internal class DefaultIdentityService @Inject constructor(
}
}
override fun getShareStatus(threePids: List<ThreePid>, callback: MatrixCallback<Map<ThreePid, SharedState>>): Cancelable {
if (threePids.isEmpty()) {
callback.onSuccess(emptyMap())
return NoOpCancellable
}
return GlobalScope.launchToCallback(coroutineDispatchers.main, callback) {
val lookupResult = lookUpInternal(true, threePids)
threePids.associateWith { threePid ->
// If not in lookup result, check if there is a pending binding
if (lookupResult.firstOrNull { it.threePid == threePid } == null) {
if (identityServiceStore.getPendingBinding(threePid) == null) {
SharedState.NOT_SHARED
} else {
SharedState.BINDING_IN_PROGRESS
}
} else {
SharedState.SHARED
}
}
}
}
private suspend fun lookUpInternal(canRetry: Boolean, threePids: List<ThreePid>): List<FoundThreePid> {
ensureToken()

View file

@ -29,7 +29,6 @@ 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.session.Session
import im.vector.matrix.android.api.session.identity.FoundThreePid
import im.vector.matrix.android.api.session.identity.IdentityServiceError
import im.vector.matrix.android.api.session.identity.IdentityServiceListener
import im.vector.matrix.android.api.session.identity.SharedState
@ -282,13 +281,13 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
)
}
identityService.lookUp(threePids,
object : MatrixCallback<List<FoundThreePid>> {
override fun onSuccess(data: List<FoundThreePid>) {
identityService.getShareStatus(threePids,
object : MatrixCallback<Map<ThreePid, SharedState>> {
override fun onSuccess(data: Map<ThreePid, SharedState>) {
setState {
copy(
emailList = Success(toPidInfoList(emails, data)),
phoneNumbersList = Success(toPidInfoList(msisdns, data))
emailList = Success(toPidInfoList(data.filter { it.key is ThreePid.Email })),
phoneNumbersList = Success(toPidInfoList(data.filter { it.key is ThreePid.Msisdn }))
)
}
}
@ -313,12 +312,11 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
})
}
private fun toPidInfoList(threePids: List<ThreePid>, foundThreePids: List<FoundThreePid>): List<PidInfo> {
return threePids.map { threePid ->
val hasMatrixId = foundThreePids.any { it.threePid == threePid }
private fun toPidInfoList(threePidStatuses: Map<ThreePid, SharedState>): List<PidInfo> {
return threePidStatuses.map { threePidStatus ->
PidInfo(
threePid = threePid,
isShared = Success(SharedState.SHARED.takeIf { hasMatrixId } ?: SharedState.NOT_SHARED)
threePid = threePidStatus.key,
isShared = Success(threePidStatus.value)
)
}
}