Clean a bit code and reorganize files

This commit is contained in:
ganfra 2018-10-26 15:12:38 +02:00
parent e942b54b56
commit d0a241bd2d
18 changed files with 67 additions and 43 deletions

View File

@ -70,6 +70,7 @@ dependencies {

// Database
implementation 'com.github.Zhuinden:realm-monarchy:0.5.1'
kapt 'dk.ilios:realmfieldnameshelper:1.1.1'

// Paging
implementation "android.arch.paging:runtime:1.0.1"

View File

@ -8,4 +8,6 @@ open class ReadReceiptEntity(@PrimaryKey var primaryKey: String = "",
var eventId: String = "",
var roomId: String = "",
var originServerTs: Double = 0.0
) : RealmObject()
) : RealmObject() {
companion object
}

View File

@ -2,15 +2,18 @@ package im.vector.matrix.android.internal.database.query

import im.vector.matrix.android.internal.database.DBConstants
import im.vector.matrix.android.internal.database.model.ChunkEntity
import im.vector.matrix.android.internal.database.model.ChunkEntityFields
import im.vector.matrix.android.internal.database.model.RoomEntityFields
import io.realm.Realm
import io.realm.RealmQuery
import io.realm.RealmResults
import io.realm.kotlin.where

fun ChunkEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ChunkEntity> {
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<ChunkEntity>()
.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<String>): RealmResults<ChunkEntity> {
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<ChunkEntity>()
.`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()
}

View File

@ -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<EventEntity> {
return realm.where(EventEntity::class.java)
.equalTo("eventId", eventId)
return realm.where<EventEntity>()
.equalTo(EventEntityFields.EVENT_ID, eventId)
}

fun EventEntity.Companion.where(realm: Realm, roomId: String, type: String? = null): RealmQuery<EventEntity> {
val query = realm.where(EventEntity::class.java)
.equalTo("chunk.room.roomId", roomId)
val query = realm.where<EventEntity>()
.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<EventEntity> {
return realm.where(EventEntity::class.java)
.equalTo("chunk.room.roomId", roomId)
.isNotNull("stateKey")
return realm.where<EventEntity>()
.equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId)
.isNotNull(EventEntityFields.STATE_KEY)
}

fun RealmQuery<EventEntity>.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<EventEntity>.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<String, RoomMember> {
return EventEntity
.where(realm, roomId, EventType.STATE_ROOM_MEMBER)
.sort("originServerTs")
.sort(EventEntityFields.ORIGIN_SERVER_TS)
.findAll()
.map { it.asDomain() }
.associateBy { it.stateKey!! }

View File

@ -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<ReadReceiptEntity> {
return realm.where<ReadReceiptEntity>()
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
.equalTo(ReadReceiptEntityFields.USER_ID, userId)
}

View File

@ -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<RoomEntity> {
return realm.where<RoomEntity>(RoomEntity::class.java).equalTo("roomId", roomId)
return realm.where<RoomEntity>().equalTo(RoomEntityFields.ROOM_ID, roomId)
}

fun RoomEntity.Companion.where(realm: Realm, membership: MyMembership? = null): RealmQuery<RoomEntity> {
val query = realm.where(RoomEntity::class.java)
val query = realm.where<RoomEntity>()
if (membership != null) {
query.equalTo("membership", membership.name)
query.equalTo(RoomEntityFields.MEMBERSHIP_STR, membership.name)
}
return query
}

View File

@ -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<RoomSummaryEntity> {
val query = realm.where(RoomSummaryEntity::class.java)
val query = realm.where<RoomSummaryEntity>()
if (roomId != null) {
query.equalTo("roomId", roomId)
query.equalTo(RoomSummaryEntityFields.ROOM_ID, roomId)
}
return query
}

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {
/**

View File

@ -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

View File

@ -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

View File

@ -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