From d0a241bd2d0e41deec4d6a5459212e6993d9123f Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 26 Oct 2018 15:12:38 +0200 Subject: [PATCH] Clean a bit code and reorganize files --- .idea/caches/build_file_checksums.ser | Bin 651 -> 653 bytes matrix-sdk-android/build.gradle | 1 + .../database/model/ReadReceiptEntity.kt | 4 ++- .../database/query/ChunkEntityQueries.kt | 25 +++++++++------- .../database/query/EventEntityQueries.kt | 28 ++++++++++-------- .../query/ReadReceiptEntityQueries.kt | 13 ++++++++ .../database/query/RoomEntityQueries.kt | 8 +++-- .../query/RoomSummaryEntityQueries.kt | 6 ++-- .../interceptor/MessageEventInterceptor.kt | 3 +- .../android/internal/session/room/RoomAPI.kt | 4 +-- .../room/members/LoadRoomMembersRequest.kt | 1 - .../{model => members}/RoomMembersResponse.kt | 2 +- .../room/timeline/DefaultTimelineHolder.kt | 3 +- .../PaginationDirection.kt | 2 +- .../room/timeline/PaginationRequest.kt | 2 -- .../room/timeline/TimelineBoundaryCallback.kt | 2 -- .../{model => timeline}/TokenChunkEvent.kt | 2 +- .../session/sync/ReadReceiptHandler.kt | 4 +-- 18 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/ReadReceiptEntityQueries.kt rename matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/{model => members}/RoomMembersResponse.kt (80%) rename matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/{model => timeline}/PaginationDirection.kt (85%) rename matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/{model => timeline}/TokenChunkEvent.kt (87%) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index f980c1eab1d1fe69650e6090cef8de21fe43844f..4859402ac0691ba6321a822871cfea643abc1678 100644 GIT binary patch delta 86 zcmV-c0IC0r1&sxem;|t2@yd~$h?AcIDiB1`on1`a*n8P_?CWde_yv<@0XdV~0YoC5 sGu;y6jW3A}?37~hAqh@&04Xn3b7gXKFJ^LKZewq0bCWUwHj^;|TFlxc`Tzg` delta 90 zcmV-g0Hyzp1&alcm;{>Ql{}H0hz=(&RdZ!>b1!FMZf0^}ll=iUlVAZilOzEu6M^p7 wy2jVc;#0G0V|N*$vvdFqlWzeQldb_R5P#Oppp { - return realm.where(ChunkEntity::class.java) - .equalTo("room.roomId", roomId) - .notEqualTo("prevToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN) - .notEqualTo("nextToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN) + return realm.where() + .equalTo("${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId) + .notEqualTo(ChunkEntityFields.PREV_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN) + .notEqualTo(ChunkEntityFields.NEXT_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN) } fun ChunkEntity.Companion.findWithPrevToken(realm: Realm, roomId: String, prevToken: String?): ChunkEntity? { @@ -19,7 +22,7 @@ fun ChunkEntity.Companion.findWithPrevToken(realm: Realm, roomId: String, prevTo } return where(realm, roomId) .and() - .equalTo("prevToken", prevToken) + .equalTo(ChunkEntityFields.PREV_TOKEN, prevToken) .findFirst() } @@ -29,22 +32,22 @@ fun ChunkEntity.Companion.findWithNextToken(realm: Realm, roomId: String, nextTo } return where(realm, roomId) .and() - .equalTo("nextToken", nextToken) + .equalTo(ChunkEntityFields.NEXT_TOKEN, nextToken) .findFirst() } fun ChunkEntity.Companion.findLastLiveChunkFromRoom(realm: Realm, roomId: String): ChunkEntity? { return where(realm, roomId) .and() - .isNull("nextToken") + .isNull(ChunkEntityFields.NEXT_TOKEN) .findAll() .last(null) } fun ChunkEntity.Companion.findAllIncludingEvents(realm: Realm, eventIds: List): RealmResults { - return realm.where(ChunkEntity::class.java) - .`in`("events.eventId", eventIds.toTypedArray()) - .notEqualTo("prevToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN) - .notEqualTo("nextToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN) + return realm.where() + .`in`(ChunkEntityFields.EVENTS.EVENT_ID, eventIds.toTypedArray()) + .notEqualTo(ChunkEntityFields.PREV_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN) + .notEqualTo(ChunkEntityFields.NEXT_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN) .findAll() } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt index 05fa819c..43b4b147 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt @@ -3,49 +3,53 @@ package im.vector.matrix.android.internal.database.query import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.room.model.RoomMember import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.database.model.ChunkEntityFields import im.vector.matrix.android.internal.database.model.EventEntity +import im.vector.matrix.android.internal.database.model.EventEntityFields +import im.vector.matrix.android.internal.database.model.RoomEntityFields import io.realm.Realm import io.realm.RealmList import io.realm.RealmQuery import io.realm.Sort +import io.realm.kotlin.where fun EventEntity.Companion.where(realm: Realm, eventId: String): RealmQuery { - return realm.where(EventEntity::class.java) - .equalTo("eventId", eventId) + return realm.where() + .equalTo(EventEntityFields.EVENT_ID, eventId) } fun EventEntity.Companion.where(realm: Realm, roomId: String, type: String? = null): RealmQuery { - val query = realm.where(EventEntity::class.java) - .equalTo("chunk.room.roomId", roomId) + val query = realm.where() + .equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId) if (type != null) { - query.equalTo("type", type) + query.equalTo(EventEntityFields.TYPE, type) } return query } fun EventEntity.Companion.stateEvents(realm: Realm, roomId: String): RealmQuery { - return realm.where(EventEntity::class.java) - .equalTo("chunk.room.roomId", roomId) - .isNotNull("stateKey") + return realm.where() + .equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId) + .isNotNull(EventEntityFields.STATE_KEY) } fun RealmQuery.last(from: Long? = null): EventEntity? { if (from != null) { - this.lessThanOrEqualTo("originServerTs", from) + this.lessThanOrEqualTo(EventEntityFields.ORIGIN_SERVER_TS, from) } return this - .sort("originServerTs", Sort.DESCENDING) + .sort(EventEntityFields.ORIGIN_SERVER_TS, Sort.DESCENDING) .findFirst() } fun RealmList.fastContains(eventEntity: EventEntity): Boolean { - return this.where().equalTo("eventId", eventEntity.eventId).findFirst() != null + return this.where().equalTo(EventEntityFields.EVENT_ID, eventEntity.eventId).findFirst() != null } fun EventEntity.Companion.findAllRoomMembers(realm: Realm, roomId: String): Map { return EventEntity .where(realm, roomId, EventType.STATE_ROOM_MEMBER) - .sort("originServerTs") + .sort(EventEntityFields.ORIGIN_SERVER_TS) .findAll() .map { it.asDomain() } .associateBy { it.stateKey!! } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/ReadReceiptEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/ReadReceiptEntityQueries.kt new file mode 100644 index 00000000..7015a1fd --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/ReadReceiptEntityQueries.kt @@ -0,0 +1,13 @@ +package im.vector.matrix.android.internal.database.query + +import im.vector.matrix.android.internal.database.model.ReadReceiptEntity +import im.vector.matrix.android.internal.database.model.ReadReceiptEntityFields +import io.realm.Realm +import io.realm.RealmQuery +import io.realm.kotlin.where + +fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String): RealmQuery { + return realm.where() + .equalTo(ReadReceiptEntityFields.ROOM_ID, roomId) + .equalTo(ReadReceiptEntityFields.USER_ID, userId) +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomEntityQueries.kt index d53e47f7..969caede 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomEntityQueries.kt @@ -2,17 +2,19 @@ package im.vector.matrix.android.internal.database.query import im.vector.matrix.android.api.session.room.model.MyMembership import im.vector.matrix.android.internal.database.model.RoomEntity +import im.vector.matrix.android.internal.database.model.RoomEntityFields import io.realm.Realm import io.realm.RealmQuery +import io.realm.kotlin.where fun RoomEntity.Companion.where(realm: Realm, roomId: String): RealmQuery { - return realm.where(RoomEntity::class.java).equalTo("roomId", roomId) + return realm.where().equalTo(RoomEntityFields.ROOM_ID, roomId) } fun RoomEntity.Companion.where(realm: Realm, membership: MyMembership? = null): RealmQuery { - val query = realm.where(RoomEntity::class.java) + val query = realm.where() if (membership != null) { - query.equalTo("membership", membership.name) + query.equalTo(RoomEntityFields.MEMBERSHIP_STR, membership.name) } return query } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomSummaryEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomSummaryEntityQueries.kt index aeccc794..3632ae4c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomSummaryEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/RoomSummaryEntityQueries.kt @@ -1,13 +1,15 @@ package im.vector.matrix.android.internal.database.query import im.vector.matrix.android.internal.database.model.RoomSummaryEntity +import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields import io.realm.Realm import io.realm.RealmQuery +import io.realm.kotlin.where fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = null): RealmQuery { - val query = realm.where(RoomSummaryEntity::class.java) + val query = realm.where() if (roomId != null) { - query.equalTo("roomId", roomId) + query.equalTo(RoomSummaryEntityFields.ROOM_ID, roomId) } return query } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/events/interceptor/MessageEventInterceptor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/events/interceptor/MessageEventInterceptor.kt index 91e0097c..5b2d80fb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/events/interceptor/MessageEventInterceptor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/events/interceptor/MessageEventInterceptor.kt @@ -6,6 +6,7 @@ import im.vector.matrix.android.api.session.events.model.EnrichedEvent import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.EventEntity +import im.vector.matrix.android.internal.database.model.EventEntityFields import im.vector.matrix.android.internal.database.query.last import im.vector.matrix.android.internal.database.query.where @@ -19,7 +20,7 @@ class MessageEventInterceptor(val monarchy: Monarchy) : EnrichedEventInterceptor monarchy.doWithRealm { realm -> val roomMember = EventEntity .where(realm, roomId, EventType.STATE_ROOM_MEMBER) - .equalTo("stateKey", event.root.sender) + .equalTo(EventEntityFields.STATE_KEY, event.root.sender) .last(from = event.root.originServerTs) ?.asDomain() event.enrichWith(roomMember) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt index a93e3107..9909db71 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt @@ -1,8 +1,8 @@ package im.vector.matrix.android.internal.session.room import im.vector.matrix.android.internal.network.NetworkConstants -import im.vector.matrix.android.internal.session.room.model.RoomMembersResponse -import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent +import im.vector.matrix.android.internal.session.room.members.RoomMembersResponse +import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEvent import kotlinx.coroutines.Deferred import retrofit2.Response import retrofit2.http.GET diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/LoadRoomMembersRequest.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/LoadRoomMembersRequest.kt index c214bced..4080a41f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/LoadRoomMembersRequest.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/LoadRoomMembersRequest.kt @@ -14,7 +14,6 @@ import im.vector.matrix.android.internal.database.query.findAllRoomMembers import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI -import im.vector.matrix.android.internal.session.room.model.RoomMembersResponse import im.vector.matrix.android.internal.session.sync.StateEventsChunkHandler import im.vector.matrix.android.internal.util.CancelableCoroutine import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/RoomMembersResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/RoomMembersResponse.kt similarity index 80% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/RoomMembersResponse.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/RoomMembersResponse.kt index 6143a35e..f7d590f9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/RoomMembersResponse.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/members/RoomMembersResponse.kt @@ -1,4 +1,4 @@ -package im.vector.matrix.android.internal.session.room.model +package im.vector.matrix.android.internal.session.room.members import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt index 9ac389d4..4028ecdd 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt @@ -9,6 +9,7 @@ import im.vector.matrix.android.api.session.events.model.EnrichedEvent import im.vector.matrix.android.api.session.room.TimelineHolder import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.ChunkEntity +import im.vector.matrix.android.internal.database.model.EventEntityFields import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.session.events.interceptor.MessageEventInterceptor import io.realm.Sort @@ -30,7 +31,7 @@ class DefaultTimelineHolder(private val roomId: String, .findAll() .last(null) ?.let { - it.events.where().sort("originServerTs", Sort.DESCENDING) + it.events.where().sort(EventEntityFields.ORIGIN_SERVER_TS, Sort.DESCENDING) } } val domainSourceFactory = realmDataSourceFactory diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/PaginationDirection.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationDirection.kt similarity index 85% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/PaginationDirection.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationDirection.kt index dbad1ea9..f76e25d4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/PaginationDirection.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationDirection.kt @@ -1,4 +1,4 @@ -package im.vector.matrix.android.internal.session.room.model +package im.vector.matrix.android.internal.session.room.timeline enum class PaginationDirection(val value: String) { /** diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationRequest.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationRequest.kt index 4b57b07d..2451a4ed 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationRequest.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/PaginationRequest.kt @@ -18,8 +18,6 @@ import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.legacy.util.FilterUtil import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI -import im.vector.matrix.android.internal.session.room.model.PaginationDirection -import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent import im.vector.matrix.android.internal.session.sync.StateEventsChunkHandler import im.vector.matrix.android.internal.util.CancelableCoroutine import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt index 0c0408ac..63d3f1ff 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt @@ -7,8 +7,6 @@ import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.session.events.model.EnrichedEvent import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.query.findAllIncludingEvents -import im.vector.matrix.android.internal.session.room.model.PaginationDirection -import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent import im.vector.matrix.android.internal.util.PagingRequestHelper import java.util.* import java.util.concurrent.Executor diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/TokenChunkEvent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEvent.kt similarity index 87% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/TokenChunkEvent.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEvent.kt index 04b60149..e39e3282 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/model/TokenChunkEvent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEvent.kt @@ -1,4 +1,4 @@ -package im.vector.matrix.android.internal.session.room.model +package im.vector.matrix.android.internal.session.room.timeline import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt index a240504c..e54c8a23 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt @@ -24,8 +24,8 @@ class ReadReceiptHandler { .flatMap { (_, userIdsDict) -> userIdsDict.map { (userId, paramsDict) -> val ts = paramsDict.filterKeys { it == "ts" } - .values - .firstOrNull() ?: 0.0 + .values + .firstOrNull() ?: 0.0 val primaryKey = roomId + userId ReadReceiptEntity(primaryKey, userId, eventId, roomId, ts) }