From 76085a42845840865cd97db794b53bc2a611673a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 14 Feb 2020 21:26:16 +0100 Subject: [PATCH] AccountData cleanup and Javadoc --- .../java/im/vector/matrix/rx/RxSession.kt | 6 ++-- .../session/accountdata/AccountDataService.kt | 25 ++++++++++++++--- .../android/internal/session/SessionModule.kt | 2 +- .../accountdata/DefaultAccountDataService.kt | 28 +++++++++---------- .../settings/devtools/AccountDataViewModel.kt | 2 +- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt index 0417504cb7..960c00bb71 100644 --- a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt +++ b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt @@ -123,10 +123,10 @@ class RxSession(private val session: Session) { } } - fun liveAccountData(filter: List): Observable> { - return session.getLiveAccountDataEvents(filter).asObservable() + fun liveAccountData(types: Set): Observable> { + return session.getLiveAccountDataEvents(types).asObservable() .startWithCallable { - session.getAccountDataEvents(filter) + session.getAccountDataEvents(types) } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/accountdata/AccountDataService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/accountdata/AccountDataService.kt index 7af7fea214..ee13d1f097 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/accountdata/AccountDataService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/accountdata/AccountDataService.kt @@ -19,18 +19,35 @@ package im.vector.matrix.android.api.session.accountdata import androidx.lifecycle.LiveData import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.events.model.Content +import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.Optional import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountDataEvent interface AccountDataService { - + /** + * Retrieve the account data with the provided type or null if not found + */ fun getAccountDataEvent(type: String): UserAccountDataEvent? + /** + * Observe the account data with the provided type + */ fun getLiveAccountDataEvent(type: String): LiveData> - fun getAccountDataEvents(filterType: List): List + /** + * Retrieve the account data with the provided types. The return list can have a different size that + * the size of the types set, because some AccountData may not exist. + * If an empty set is provided, all the AccountData are retrieved + */ + fun getAccountDataEvents(types: Set): List - fun getLiveAccountDataEvents(filterType: List): LiveData> + /** + * Observe the account data with the provided types. If an empty set is provided, all the AccountData are observed + */ + fun getLiveAccountDataEvents(types: Set): LiveData> - fun updateAccountData(type: String, content: Content, callback: MatrixCallback? = null) + /** + * Update the account data with the provided type and the provided account data content + */ + fun updateAccountData(type: String, content: Content, callback: MatrixCallback? = null): Cancelable } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 7352b79073..908c610914 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -269,7 +269,7 @@ internal abstract class SessionModule { abstract fun bindHomeServerCapabilitiesService(homeServerCapabilitiesService: DefaultHomeServerCapabilitiesService): HomeServerCapabilitiesService @Binds - abstract fun bindAccountDataService(accountDataService: DefaultAccountDataService): AccountDataService + abstract fun bindAccountDataService(service: DefaultAccountDataService): AccountDataService @Binds abstract fun bindSharedSecretStorageService(service: DefaultSharedSecretStorageService): SharedSecretStorageService diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DefaultAccountDataService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DefaultAccountDataService.kt index b40c75992a..7756b22510 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DefaultAccountDataService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DefaultAccountDataService.kt @@ -22,13 +22,13 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.accountdata.AccountDataService import im.vector.matrix.android.api.session.events.model.Content +import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.JSON_DICT_PARAMETERIZED_TYPE import im.vector.matrix.android.api.util.Optional import im.vector.matrix.android.api.util.toOptional import im.vector.matrix.android.internal.database.model.UserAccountDataEntity import im.vector.matrix.android.internal.database.model.UserAccountDataEntityFields import im.vector.matrix.android.internal.di.MoshiProvider -import im.vector.matrix.android.internal.di.SessionId import im.vector.matrix.android.internal.session.sync.UserAccountDataSyncHandler import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountDataEvent import im.vector.matrix.android.internal.task.TaskExecutor @@ -37,7 +37,6 @@ import javax.inject.Inject internal class DefaultAccountDataService @Inject constructor( private val monarchy: Monarchy, - @SessionId private val sessionId: String, private val updateUserAccountDataTask: UpdateUserAccountDataTask, private val userAccountDataSyncHandler: UserAccountDataSyncHandler, private val taskExecutor: TaskExecutor @@ -47,39 +46,39 @@ internal class DefaultAccountDataService @Inject constructor( private val adapter = moshi.adapter>(JSON_DICT_PARAMETERIZED_TYPE) override fun getAccountDataEvent(type: String): UserAccountDataEvent? { - return getAccountDataEvents(listOf(type)).firstOrNull() + return getAccountDataEvents(setOf(type)).firstOrNull() } override fun getLiveAccountDataEvent(type: String): LiveData> { - return Transformations.map(getLiveAccountDataEvents(listOf(type))) { + return Transformations.map(getLiveAccountDataEvents(setOf(type))) { it.firstOrNull()?.toOptional() } } - override fun getAccountDataEvents(filterType: List): List { + override fun getAccountDataEvents(types: Set): List { return monarchy.fetchAllCopiedSync { realm -> realm.where(UserAccountDataEntity::class.java) .apply { - if (filterType.isNotEmpty()) { - `in`(UserAccountDataEntityFields.TYPE, filterType.toTypedArray()) + if (types.isNotEmpty()) { + `in`(UserAccountDataEntityFields.TYPE, types.toTypedArray()) } } - }?.mapNotNull { entity -> + }.mapNotNull { entity -> entity.type?.let { type -> UserAccountDataEvent( type = type, content = entity.contentStr?.let { adapter.fromJson(it) } ?: emptyMap() ) } - } ?: emptyList() + } } - override fun getLiveAccountDataEvents(filterType: List): LiveData> { + override fun getLiveAccountDataEvents(types: Set): LiveData> { return monarchy.findAllMappedWithChanges({ realm -> realm.where(UserAccountDataEntity::class.java) .apply { - if (filterType.isNotEmpty()) { - `in`(UserAccountDataEntityFields.TYPE, filterType.toTypedArray()) + if (types.isNotEmpty()) { + `in`(UserAccountDataEntityFields.TYPE, types.toTypedArray()) } } }, { entity -> @@ -90,14 +89,15 @@ internal class DefaultAccountDataService @Inject constructor( }) } - override fun updateAccountData(type: String, content: Content, callback: MatrixCallback?) { - updateUserAccountDataTask.configureWith(UpdateUserAccountDataTask.AnyParams( + override fun updateAccountData(type: String, content: Content, callback: MatrixCallback?): Cancelable { + return updateUserAccountDataTask.configureWith(UpdateUserAccountDataTask.AnyParams( type = type, any = content )) { this.retryCount = 5 this.callback = object : MatrixCallback { override fun onSuccess(data: Unit) { + // TODO Move that to the task (but it created a circular dependencies...) monarchy.runTransactionSync { realm -> userAccountDataSyncHandler.handleGenericAccountData(realm, type, content) } diff --git a/vector/src/main/java/im/vector/riotx/features/settings/devtools/AccountDataViewModel.kt b/vector/src/main/java/im/vector/riotx/features/settings/devtools/AccountDataViewModel.kt index b0b23a62d1..32ce17c660 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/devtools/AccountDataViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/devtools/AccountDataViewModel.kt @@ -40,7 +40,7 @@ class AccountDataViewModel @AssistedInject constructor(@Assisted initialState: A : VectorViewModel(initialState) { init { - session.rx().liveAccountData(emptyList()) + session.rx().liveAccountData(emptySet()) .execute { copy(accountData = it) }