From a060431aaf778caf3e5a0365f5c8d2886daa85c3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 26 Sep 2019 13:51:44 +0200 Subject: [PATCH] Fix my dirty code --- .../homeserver/HomeServerCapabilities.kt | 2 +- .../mapper/HomeServerCapabilitiesMapper.kt | 4 ++-- .../query/HomeServerCapabilitiesQueries.kt | 17 ++++++++--------- .../DefaultGetHomeServerCapabilitiesTask.kt | 15 +++++++-------- .../DefaultHomeServerCapabilitiesService.kt | 18 ++++++------------ 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt index 215516a6c1..9b01cdef3b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt @@ -20,7 +20,7 @@ data class HomeServerCapabilities( /** * Max size of file which can be uploaded to the homeserver in bytes. [MAX_UPLOAD_FILE_SIZE_UNKNOWN] if unknown or not retrieved yet */ - val maxUploadFileSize: Long + val maxUploadFileSize: Long = MAX_UPLOAD_FILE_SIZE_UNKNOWN ) { companion object { const val MAX_UPLOAD_FILE_SIZE_UNKNOWN = -1L diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt index 23ab7b64be..44f3d69db5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt @@ -26,13 +26,13 @@ internal object HomeServerCapabilitiesMapper { fun map(entity: HomeServerCapabilitiesEntity): HomeServerCapabilities { return HomeServerCapabilities( - entity.maxUploadFileSize + maxUploadFileSize = entity.maxUploadFileSize ) } fun map(domain: HomeServerCapabilities): HomeServerCapabilitiesEntity { return HomeServerCapabilitiesEntity( - domain.maxUploadFileSize + maxUploadFileSize = domain.maxUploadFileSize ) } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/HomeServerCapabilitiesQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/HomeServerCapabilitiesQueries.kt index 64cd6e4770..e7002a9005 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/HomeServerCapabilitiesQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/HomeServerCapabilitiesQueries.kt @@ -21,17 +21,16 @@ import io.realm.Realm import io.realm.kotlin.createObject import io.realm.kotlin.where +/** + * Get the current HomeServerCapabilitiesEntity, return null if it does not exist + */ +internal fun HomeServerCapabilitiesEntity.Companion.get(realm: Realm): HomeServerCapabilitiesEntity? { + return realm.where().findFirst() +} + /** * Get the current HomeServerCapabilitiesEntity, create one if it does not exist */ internal fun HomeServerCapabilitiesEntity.Companion.getOrCreate(realm: Realm): HomeServerCapabilitiesEntity { - var homeServerCapabilitiesEntity = realm.where().findFirst() - if (homeServerCapabilitiesEntity == null) { - realm.executeTransaction { - realm.createObject() - } - homeServerCapabilitiesEntity = realm.where().findFirst()!! - } - - return homeServerCapabilitiesEntity + return get(realm) ?: realm.createObject() } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt index defcfbaa81..f88178cebd 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt @@ -56,16 +56,15 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( } - private fun insertInDb(getUploadCapabilitiesResult: GetUploadCapabilitiesResult) { - monarchy - .writeAsync { realm -> - val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) + private suspend fun insertInDb(getUploadCapabilitiesResult: GetUploadCapabilitiesResult) { + monarchy.awaitTransaction { realm -> + val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) - homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize - ?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN + homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize + ?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN - homeServerCapabilitiesEntity.lastUpdatedTimestamp = Date().time - } + homeServerCapabilitiesEntity.lastUpdatedTimestamp = Date().time + } } companion object { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultHomeServerCapabilitiesService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultHomeServerCapabilitiesService.kt index 0af849af2e..ea41eb12b5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultHomeServerCapabilitiesService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultHomeServerCapabilitiesService.kt @@ -21,24 +21,18 @@ import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilities import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService import im.vector.matrix.android.internal.database.mapper.HomeServerCapabilitiesMapper import im.vector.matrix.android.internal.database.model.HomeServerCapabilitiesEntity -import im.vector.matrix.android.internal.database.query.getOrCreate +import im.vector.matrix.android.internal.database.query.get +import io.realm.Realm import javax.inject.Inject internal class DefaultHomeServerCapabilitiesService @Inject constructor(private val monarchy: Monarchy) : HomeServerCapabilitiesService { override fun getHomeServerCapabilities(): HomeServerCapabilities { - var entity: HomeServerCapabilitiesEntity? = null - monarchy.doWithRealm { realm -> - entity = HomeServerCapabilitiesEntity.getOrCreate(realm) - } - - return with(entity) { - if (this != null) { - HomeServerCapabilitiesMapper.map(this) - } else { - // Should not happen - HomeServerCapabilities(HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN) + return Realm.getInstance(monarchy.realmConfiguration).use { realm -> + HomeServerCapabilitiesEntity.get(realm)?.let { + HomeServerCapabilitiesMapper.map(it) } } + ?: HomeServerCapabilities() } }