forked from GitHub-Mirror/riotX-android
Fix impure reducer and use live event
This commit is contained in:
@ -80,5 +80,5 @@ interface RelationService {
|
||||
*/
|
||||
fun replyToMessage(eventReplied: Event, replyText: String): Cancelable?
|
||||
|
||||
fun getEventSummaryLive(eventId: String): LiveData<List<EventAnnotationsSummary>>
|
||||
fun getEventSummaryLive(eventId: String): LiveData<EventAnnotationsSummary>
|
||||
}
|
@ -16,6 +16,8 @@
|
||||
|
||||
package im.vector.matrix.android.api.session.room.timeline
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
|
||||
/**
|
||||
* This interface defines methods to interact with the timeline. It's implemented at the room level.
|
||||
*/
|
||||
@ -32,4 +34,7 @@ interface TimelineService {
|
||||
|
||||
|
||||
fun getTimeLineEvent(eventId: String): TimelineEvent?
|
||||
|
||||
|
||||
fun liveTimeLineEvent(eventId: String): LiveData<TimelineEvent>
|
||||
}
|
@ -17,6 +17,7 @@ package im.vector.matrix.android.internal.session.room.relation
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Transformations
|
||||
import androidx.work.OneTimeWorkRequest
|
||||
import com.zhuinden.monarchy.Monarchy
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
@ -27,6 +28,7 @@ import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary
|
||||
import im.vector.matrix.android.api.session.room.model.message.MessageType
|
||||
import im.vector.matrix.android.api.session.room.model.relation.RelationService
|
||||
import im.vector.matrix.android.api.util.Cancelable
|
||||
import im.vector.matrix.android.internal.database.RealmLiveData
|
||||
import im.vector.matrix.android.internal.database.helper.addSendingEvent
|
||||
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
@ -155,15 +157,13 @@ internal class DefaultRelationService @Inject constructor(private val context: C
|
||||
return workRequest
|
||||
}
|
||||
|
||||
override fun getEventSummaryLive(eventId: String): LiveData<List<EventAnnotationsSummary>> {
|
||||
return monarchy.findAllMappedWithChanges(
|
||||
{ realm ->
|
||||
EventAnnotationsSummaryEntity.where(realm, eventId)
|
||||
},
|
||||
{
|
||||
it.asDomain()
|
||||
}
|
||||
)
|
||||
override fun getEventSummaryLive(eventId: String): LiveData<EventAnnotationsSummary> {
|
||||
val liveEntity = RealmLiveData(monarchy.realmConfiguration) { realm ->
|
||||
EventAnnotationsSummaryEntity.where(realm, eventId)
|
||||
}
|
||||
return Transformations.map(liveEntity) { realmResults ->
|
||||
realmResults.firstOrNull()?.asDomain() ?: EventAnnotationsSummary(eventId, emptyList(), null)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,10 +16,14 @@
|
||||
|
||||
package im.vector.matrix.android.internal.session.room.timeline
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MediatorLiveData
|
||||
import com.zhuinden.monarchy.Monarchy
|
||||
import im.vector.matrix.android.api.session.room.timeline.Timeline
|
||||
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
||||
import im.vector.matrix.android.api.session.room.timeline.TimelineService
|
||||
import im.vector.matrix.android.internal.database.RealmLiveData
|
||||
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
|
||||
import im.vector.matrix.android.internal.database.model.EventEntity
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.task.TaskExecutor
|
||||
@ -47,5 +51,27 @@ internal class DefaultTimelineService @Inject constructor(private val roomId: St
|
||||
})
|
||||
}
|
||||
|
||||
override fun liveTimeLineEvent(eventId: String): LiveData<TimelineEvent> {
|
||||
val liveEventEntity = RealmLiveData(monarchy.realmConfiguration) { realm ->
|
||||
EventEntity.where(realm, eventId = eventId)
|
||||
}
|
||||
val liveAnnotationsEntity = RealmLiveData(monarchy.realmConfiguration) { realm ->
|
||||
EventAnnotationsSummaryEntity.where(realm, eventId = eventId)
|
||||
}
|
||||
val result = MediatorLiveData<TimelineEvent>()
|
||||
result.addSource(liveEventEntity) { realmResults ->
|
||||
result.value = realmResults.firstOrNull()?.let { timelineEventFactory.create(it) }
|
||||
}
|
||||
|
||||
result.addSource(liveAnnotationsEntity) {
|
||||
liveEventEntity.value?.let {
|
||||
result.value = liveEventEntity.value?.let { realmResults ->
|
||||
//recreate the timeline event
|
||||
realmResults.firstOrNull()?.let { timelineEventFactory.create(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user