forked from GitHub-Mirror/riotX-android
RoomSummary: don't fetch last event by default as it takes some time
This commit is contained in:
@ -47,8 +47,8 @@ interface Room :
|
||||
* A live [RoomSummary] associated with the room
|
||||
* You can observe this summary to get dynamic data from this room.
|
||||
*/
|
||||
val liveRoomSummary: LiveData<RoomSummary>
|
||||
fun liveRoomSummary(fetchLastEvent: Boolean = false): LiveData<RoomSummary>
|
||||
|
||||
val roomSummary: RoomSummary?
|
||||
fun roomSummary(fetchLastEvent: Boolean = false): RoomSummary?
|
||||
|
||||
}
|
@ -43,6 +43,6 @@ interface RoomService {
|
||||
* Get a live list of room summaries. This list is refreshed as soon as the data changes.
|
||||
* @return the [LiveData] of [RoomSummary]
|
||||
*/
|
||||
fun liveRoomSummaries(): LiveData<List<RoomSummary>>
|
||||
fun liveRoomSummaries(fetchLastEvents: Boolean = true): LiveData<List<RoomSummary>>
|
||||
|
||||
}
|
@ -16,29 +16,24 @@
|
||||
|
||||
package im.vector.matrix.android.internal.database.mapper
|
||||
|
||||
import com.zhuinden.monarchy.Monarchy
|
||||
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
import im.vector.matrix.android.api.session.room.model.tag.RoomTag
|
||||
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
||||
import im.vector.matrix.android.internal.session.room.timeline.TimelineEventFactory
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class RoomSummaryMapper @Inject constructor(private val timelineEventFactory: TimelineEventFactory) {
|
||||
|
||||
internal class RoomSummaryMapper @Inject constructor(
|
||||
private val timelineEventFactory: TimelineEventFactory,
|
||||
private val monarchy: Monarchy) {
|
||||
|
||||
fun map(roomSummaryEntity: RoomSummaryEntity): RoomSummary {
|
||||
fun map(roomSummaryEntity: RoomSummaryEntity, getLatestEvent: Boolean = false): RoomSummary {
|
||||
val tags = roomSummaryEntity.tags.map {
|
||||
RoomTag(it.tagName, it.tagOrder)
|
||||
}
|
||||
val latestEvent = roomSummaryEntity.latestEvent?.let {
|
||||
var ev: TimelineEvent? = null
|
||||
monarchy.doWithRealm { realm ->
|
||||
ev = timelineEventFactory.create(it, realm)
|
||||
val latestEvent = if (getLatestEvent) {
|
||||
roomSummaryEntity.latestEvent?.let {
|
||||
timelineEventFactory.create(it, it.realm)
|
||||
}
|
||||
ev
|
||||
} else {
|
||||
null
|
||||
}
|
||||
return RoomSummary(
|
||||
roomId = roomSummaryEntity.roomId,
|
||||
|
@ -34,6 +34,7 @@ import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.util.fetchCopied
|
||||
import im.vector.matrix.android.internal.util.fetchCopyMap
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class DefaultRoom @Inject constructor(override val roomId: String,
|
||||
@ -47,19 +48,19 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
|
||||
private val relationService: RelationService,
|
||||
private val roomMembersService: MembershipService
|
||||
) : Room,
|
||||
TimelineService by timelineService,
|
||||
SendService by sendService,
|
||||
StateService by stateService,
|
||||
ReadService by readService,
|
||||
RelationService by relationService,
|
||||
MembershipService by roomMembersService {
|
||||
TimelineService by timelineService,
|
||||
SendService by sendService,
|
||||
StateService by stateService,
|
||||
ReadService by readService,
|
||||
RelationService by relationService,
|
||||
MembershipService by roomMembersService {
|
||||
|
||||
override val liveRoomSummary: LiveData<RoomSummary> by lazy {
|
||||
override fun liveRoomSummary(fetchLastEvent: Boolean): LiveData<RoomSummary> {
|
||||
val liveRealmData = RealmLiveData<RoomSummaryEntity>(monarchy.realmConfiguration) { realm ->
|
||||
RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME)
|
||||
}
|
||||
Transformations.map(liveRealmData) { results ->
|
||||
val roomSummaries = results.map { roomSummaryMapper.map(it) }
|
||||
return Transformations.map(liveRealmData) { results ->
|
||||
val roomSummaries = results.map { roomSummaryMapper.map(it, fetchLastEvent) }
|
||||
|
||||
if (roomSummaries.isEmpty()) {
|
||||
// Create a dummy RoomSummary to avoid Crash during Sign Out or clear cache
|
||||
@ -70,11 +71,12 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
|
||||
}
|
||||
}
|
||||
|
||||
override val roomSummary: RoomSummary?
|
||||
get() {
|
||||
var sum: RoomSummaryEntity? = monarchy.fetchCopied { RoomSummaryEntity.where(it, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME).findFirst() }
|
||||
return sum?.let { roomSummaryMapper.map(it) }
|
||||
}
|
||||
override fun roomSummary(fetchLastEvent: Boolean): RoomSummary? {
|
||||
return monarchy.fetchAllMappedSync(
|
||||
{ realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) },
|
||||
{ roomSummaryMapper.map(it, fetchLastEvent) }
|
||||
).firstOrNull()
|
||||
}
|
||||
|
||||
override fun isEncrypted(): Boolean {
|
||||
return cryptoService.isRoomEncrypted(roomId)
|
||||
|
@ -52,10 +52,10 @@ internal class DefaultRoomService @Inject constructor(private val monarchy: Mona
|
||||
return roomFactory.create(roomId)
|
||||
}
|
||||
|
||||
override fun liveRoomSummaries(): LiveData<List<RoomSummary>> {
|
||||
override fun liveRoomSummaries(fetchLastEvents: Boolean): LiveData<List<RoomSummary>> {
|
||||
return monarchy.findAllMappedWithChanges(
|
||||
{ realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) },
|
||||
{ roomSummaryMapper.map(it) }
|
||||
{ roomSummaryMapper.map(it, fetchLastEvents) }
|
||||
)
|
||||
}
|
||||
}
|
@ -80,7 +80,7 @@ internal class SimpleTimelineEventFactory @Inject constructor(private val roomMe
|
||||
val result = cryptoService.decryptEvent(event, UUID.randomUUID().toString())
|
||||
event.setClearData(result)
|
||||
} catch (failure: Throwable) {
|
||||
Timber.e(failure, "Encrypted event: decryption failed")
|
||||
Timber.e("Encrypted event: decryption failed")
|
||||
if (failure is MXDecryptionException) {
|
||||
event.setCryptoError(failure.cryptoError)
|
||||
}
|
||||
|
Reference in New Issue
Block a user