Compare commits

...

5 Commits

23 changed files with 471 additions and 55 deletions

View File

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

def epoxy_version = "3.0.0"
def arrow_version = "0.8.2"
def glide_version = "4.8.0"

implementation project(":matrix-sdk-android")
implementation project(":matrix-sdk-android-rx")
@ -84,8 +85,10 @@ dependencies {
implementation "io.arrow-kt:arrow-core:$arrow_version"

// UI
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'
implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"

implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'com.google.android.material:material:1.1.0-alpha02'


View File

@ -16,8 +16,19 @@

package im.vector.riotredesign.core.glide;

import android.content.Context;
import android.util.Log;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}
public final class MyAppGlideModule extends AppGlideModule {

@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setLogLevel(Log.ERROR);
}

}

View File

@ -52,11 +52,11 @@ class HomeActivityViewModel(state: EmptyState,
get() = _openRoomLiveData

init {
val lastSelectedRoom = roomSelectionRepository.lastSelectedRoom()
if (lastSelectedRoom == null) {
val lastSelectedRoomId = roomSelectionRepository.lastSelectedRoom()
if (lastSelectedRoomId == null || session.getRoom(lastSelectedRoomId) == null) {
getTheFirstRoomWhenAvailable()
} else {
_openRoomLiveData.postValue(LiveEvent(lastSelectedRoom))
_openRoomLiveData.postValue(LiveEvent(lastSelectedRoomId))
}
}


View File

@ -23,18 +23,20 @@ import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.rx.rx
import im.vector.riotredesign.core.extensions.lastMinBy
import im.vector.riotredesign.core.platform.RiotViewModel
import im.vector.riotredesign.features.home.room.VisibleRoomHolder
import io.reactivex.rxkotlin.subscribeBy
import org.koin.android.ext.android.get
import timber.log.Timber
import java.util.concurrent.TimeUnit

class RoomDetailViewModel(initialState: RoomDetailViewState,
private val session: Session,
private val visibleRoomHolder: VisibleRoomHolder
) : RiotViewModel<RoomDetailViewState>(initialState) {
) : RiotViewModel<RoomDetailViewState>(initialState), Room.Listener {

private val room = session.getRoom(initialState.roomId)!!
private val roomId = initialState.roomId
@ -57,6 +59,7 @@ class RoomDetailViewModel(initialState: RoomDetailViewState,
observeTimeline()
observeDisplayedEvents()
room.loadRoomMembersIfNeeded()
room.addListener(this)
}

fun process(action: RoomDetailActions) {
@ -67,6 +70,17 @@ class RoomDetailViewModel(initialState: RoomDetailViewState,
}
}

override fun onCleared() {
room.removeListener(this)
super.onCleared()
}

// Room.Listener *******************************************************************************

override fun onReadReceiptsUpdated() {
Timber.v("On read receipts updated")
}

// PRIVATE METHODS *****************************************************************************

private fun handleSendMessage(action: RoomDetailActions.SendMessage) {
@ -75,6 +89,7 @@ class RoomDetailViewModel(initialState: RoomDetailViewState,

private fun handleEventDisplayed(action: RoomDetailActions.EventDisplayed) {
displayedEventsObservable.accept(action)

}

private fun handleIsDisplayed() {
@ -88,6 +103,18 @@ class RoomDetailViewModel(initialState: RoomDetailViewState,
.buffer(1, TimeUnit.SECONDS)
.filter { it.isNotEmpty() }
.subscribeBy(onNext = { actions ->
val eventIds = actions.mapNotNull { it.event.root.eventId }
withState { state ->
val newMapOfReadReceipts = HashMap(state.readReceiptsForEventId)
eventIds.forEach {
if (newMapOfReadReceipts.containsKey(it).not()) {
val readReceipts = room.readReceipts(it)
newMapOfReadReceipts[it] = readReceipts
}
}
setState { copy(readReceiptsForEventId = newMapOfReadReceipts) }
}

val mostRecentEvent = actions.lastMinBy { it.index }
mostRecentEvent?.event?.root?.eventId?.let { eventId ->
room.setReadReceipt(eventId, callback = object : MatrixCallback<Void> {})

View File

@ -19,12 +19,14 @@ package im.vector.riotredesign.features.home.room.detail
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
import im.vector.matrix.android.api.session.room.model.ReadReceipt
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.room.timeline.TimelineData

data class RoomDetailViewState(
val roomId: String,
val eventId: String?,
val readReceiptsForEventId: Map<String, List<ReadReceipt>> = emptyMap(),
val asyncRoomSummary: Async<RoomSummary> = Uninitialized,
val asyncTimelineData: Async<TimelineData> = Uninitialized
) : MvRxState {

View File

@ -0,0 +1,39 @@
/*
*
* * Copyright 2019 New Vector Ltd
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package im.vector.riotredesign.features.home.room.detail.timeline

import android.widget.ImageView
import android.widget.TextView
import im.vector.riotredesign.R
import im.vector.riotredesign.core.epoxy.KotlinModel

class ReadReceiptsItem() : KotlinModel(R.layout.item_timeline_read_receipts) {

private val moreText by bind<TextView>(R.id.message_more_than_expected)
private val avatarReceipt1 by bind<ImageView>(R.id.message_avatar_receipt_1)
private val avatarReceipt2 by bind<ImageView>(R.id.message_avatar_receipt_1)
private val avatarReceipt3 by bind<ImageView>(R.id.message_avatar_receipt_1)
private val avatarReceipt4 by bind<ImageView>(R.id.message_avatar_receipt_1)
private val avatarReceipt5 by bind<ImageView>(R.id.message_avatar_receipt_1)
private val avatarReceipts = listOf(avatarReceipt1, avatarReceipt2, avatarReceipt3, avatarReceipt4, avatarReceipt5)

override fun bind() {

}
}

View File

@ -55,11 +55,9 @@
android:layout_marginBottom="8dp"
android:textColor="@color/dark_grey"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/messageMemberNameView"
tools:text="Alright finished work, heading there in about 20 mins…Ping me when youre outside" />


</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/read_receipt_avatars_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">

<TextView
android:id="@+id/message_more_than_expected"
android:layout_width="20dp"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="9sp"
tools:text="999+" />

<ImageView
android:id="@+id/message_avatar_receipt_5"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/message_avatar_receipt_4"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/message_avatar_receipt_3"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/message_avatar_receipt_2"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/message_avatar_receipt_1"
android:layout_width="19dp"
android:layout_height="12dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />


</LinearLayout>

</RelativeLayout>

View File

@ -1,3 +1,4 @@

/*
* Copyright 2019 New Vector Ltd
*
@ -17,6 +18,7 @@
package im.vector.matrix.rx

import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.model.ReadReceipt
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.room.timeline.TimelineData
import io.reactivex.Observable
@ -31,6 +33,10 @@ class RxRoom(private val room: Room) {
return room.timeline(eventId).asObservable()
}

fun liveReadReceipts(): Observable<List<ReadReceipt>> {
return room.readReceipts().asObservable()
}

}

fun Room.rx(): RxRoom {

View File

@ -10,7 +10,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "io.realm:realm-gradle-plugin:5.8.0"
classpath "io.realm:realm-gradle-plugin:5.9.0"
}
}


View File

@ -17,16 +17,16 @@
package im.vector.matrix.android.api.session.room

import androidx.lifecycle.LiveData
import im.vector.matrix.android.api.session.room.members.RoomMembersService
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.room.read.ReadService
import im.vector.matrix.android.api.session.room.send.SendService
import im.vector.matrix.android.api.session.room.timeline.TimelineService
import im.vector.matrix.android.api.util.Cancelable

/**
* This interface defines methods to interact within a room.
*/
interface Room : TimelineService, SendService, ReadService {
interface Room : TimelineService, SendService, ReadService, RoomMembersService {

/**
* The roomId of this room
@ -40,9 +40,25 @@ interface Room : TimelineService, SendService, ReadService {
val roomSummary: LiveData<RoomSummary>

/**
* This methods load all room members if it was done yet.
* @return a [Cancelable]
* Add a listener to the room.
* @param listener the listener to add.
*/
fun loadRoomMembersIfNeeded(): Cancelable
fun addListener(listener: Listener)

/**
* Remove a listener from the room.
* @param listener the listener to remove.
*/
fun removeListener(listener: Listener)

/**
* A listener defined at the room level to listen for some events from the different room services.
*/
interface Listener : ReadService.Listener {

override fun onReadReceiptsUpdated() {
// no-op
}
}

}

View File

@ -0,0 +1,52 @@
/*
*
* * Copyright 2019 New Vector Ltd
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package im.vector.matrix.android.api.session.room.members

import androidx.lifecycle.LiveData
import im.vector.matrix.android.api.session.room.model.RoomMember
import im.vector.matrix.android.api.util.Cancelable

/**
* This interface defines methods to retrieve room members of a room. It's implemented at the room level.
*/
interface RoomMembersService {

/**
* This methods load all room members if it was done yet.
* @return a [Cancelable]
*/
fun loadRoomMembersIfNeeded(): Cancelable

/**
* Return the roomMember with userId or null.
* @param userId the userId param to look for
*
* @return the roomMember with userId or null
*/
fun getRoomMember(userId: String): RoomMember?

/**
* Return all the roomMembers of the room
*
* @return a [LiveData] of roomMember list.
*/
fun getRoomMembersLive(): LiveData<List<RoomMember>>


}

View File

@ -17,7 +17,7 @@
package im.vector.matrix.android.api.session.room.model

data class ReadReceipt(
val userId: String,
val roomMember: RoomMember?,
val eventId: String,
val originServerTs: Long
)

View File

@ -16,7 +16,9 @@

package im.vector.matrix.android.api.session.room.read

import androidx.lifecycle.LiveData
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.room.model.ReadReceipt

/**
* This interface defines methods to handle read receipts and read marker in a room. It's implemented at the room level.
@ -38,4 +40,25 @@ interface ReadService {
*/
fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback<Void>)

/**
* * @return the current list of read receipt as [LiveData]
*/
fun readReceipts(): LiveData<List<ReadReceipt>>

/**
* @param eventId to look for receipts.
* * @return the list of receipts associated with the eventId
*/
fun readReceipts(eventId: String): List<ReadReceipt>

/**
* A listener defined at the room level to listen for some read events.
*/
interface Listener {
/**
* Called when read receipts are updated in this room.
*/
fun onReadReceiptsUpdated()
}

}

View File

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.database.model

import io.realm.RealmObject
import io.realm.RealmResults
import io.realm.annotations.Index
import io.realm.annotations.LinkingObjects
import io.realm.annotations.PrimaryKey
import java.util.*
@ -28,7 +29,7 @@ internal open class EventEntity(@PrimaryKey var localId: String = UUID.randomUUI
var type: String = "",
var content: String? = null,
var prevContent: String? = null,
var stateKey: String? = null,
@Index var stateKey: String? = null,
var originServerTs: Long? = null,
var sender: String? = null,
var age: Long? = 0,

View File

@ -26,4 +26,9 @@ internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, use
return realm.where<ReadReceiptEntity>()
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
.equalTo(ReadReceiptEntityFields.USER_ID, userId)
}
}

internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ReadReceiptEntity> {
return realm.where<ReadReceiptEntity>()
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
}

View File

@ -20,34 +20,34 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.members.RoomMembersService
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.room.read.ReadService
import im.vector.matrix.android.api.session.room.send.SendService
import im.vector.matrix.android.api.session.room.timeline.TimelineService
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.database.mapper.asDomain
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.session.room.members.LoadRoomMembersTask
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import im.vector.matrix.android.internal.session.room.members.DefaultRoomMembersService
import im.vector.matrix.android.internal.session.room.read.DefaultReadService
import im.vector.matrix.android.internal.session.room.read.ReadServiceListeners
import im.vector.matrix.android.internal.session.room.send.DefaultSendService
import im.vector.matrix.android.internal.session.room.timeline.DefaultTimelineService

internal data class DefaultRoom(
internal class DefaultRoom(
override val roomId: String,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val monarchy: Monarchy,
private val timelineService: TimelineService,
private val sendService: SendService,
private val readService: ReadService,
private val taskExecutor: TaskExecutor


private val timelineService: DefaultTimelineService,
private val sendService: DefaultSendService,
private val readService: DefaultReadService,
private val readServiceListeners: ReadServiceListeners,
private val roomMembersService: DefaultRoomMembersService
) : Room,
TimelineService by timelineService,
SendService by sendService,
ReadService by readService {
ReadService by readService,
RoomMembersService by roomMembersService {

override val roomSummary: LiveData<RoomSummary> by lazy {
val liveData = monarchy
@ -60,8 +60,12 @@ internal data class DefaultRoom(
}
}

override fun loadRoomMembersIfNeeded(): Cancelable {
val params = LoadRoomMembersTask.Params(roomId, Membership.LEAVE)
return loadRoomMembersTask.configureWith(params).executeBy(taskExecutor)
override fun addListener(listener: Room.Listener) {
readServiceListeners.addListener(listener)
}

override fun removeListener(listener: Room.Listener) {
readServiceListeners.removeListener(listener)
}

}

View File

@ -17,11 +17,12 @@
package im.vector.matrix.android.internal.session.room

import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.internal.session.room.members.DefaultRoomMembersService
import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersTask
import im.vector.matrix.android.internal.session.room.members.RoomMemberExtractor
import im.vector.matrix.android.internal.session.room.read.DefaultReadService
import im.vector.matrix.android.internal.session.room.read.ReadServiceListeners
import im.vector.matrix.android.internal.session.room.read.SetReadMarkersTask
import im.vector.matrix.android.internal.session.room.send.DefaultSendService
import im.vector.matrix.android.internal.session.room.send.EventFactory
@ -35,7 +36,6 @@ import java.util.concurrent.Executors

internal class RoomFactory(private val loadRoomMembersTask: LoadRoomMembersTask,
private val monarchy: Monarchy,
private val credentials: Credentials,
private val paginationTask: PaginationTask,
private val contextOfEventTask: GetContextOfEventTask,
private val setReadMarkersTask: SetReadMarkersTask,
@ -48,15 +48,18 @@ internal class RoomFactory(private val loadRoomMembersTask: LoadRoomMembersTask,
val roomMemberExtractor = RoomMemberExtractor(monarchy, roomId)
val timelineService = DefaultTimelineService(roomId, monarchy, taskExecutor, timelineBoundaryCallback, contextOfEventTask, roomMemberExtractor)
val sendService = DefaultSendService(roomId, eventFactory, monarchy)
val readService = DefaultReadService(roomId, monarchy, setReadMarkersTask, taskExecutor)
val roomMembersService = DefaultRoomMembersService(roomId, monarchy, loadRoomMembersTask, taskExecutor)
val readService = DefaultReadService(roomId, monarchy, roomMembersService, setReadMarkersTask, taskExecutor)
val readServiceListeners = ReadServiceListeners(roomId, monarchy)

return DefaultRoom(
roomId,
loadRoomMembersTask,
monarchy,
timelineService,
sendService,
readService,
taskExecutor
readServiceListeners,
roomMembersService
)
}


View File

@ -22,11 +22,7 @@ import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersTas
import im.vector.matrix.android.internal.session.room.read.DefaultSetReadMarkersTask
import im.vector.matrix.android.internal.session.room.read.SetReadMarkersTask
import im.vector.matrix.android.internal.session.room.send.EventFactory
import im.vector.matrix.android.internal.session.room.timeline.DefaultGetContextOfEventTask
import im.vector.matrix.android.internal.session.room.timeline.DefaultPaginationTask
import im.vector.matrix.android.internal.session.room.timeline.GetContextOfEventTask
import im.vector.matrix.android.internal.session.room.timeline.PaginationTask
import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor
import im.vector.matrix.android.internal.session.room.timeline.*
import org.koin.dsl.module.module
import retrofit2.Retrofit

@ -57,7 +53,7 @@ class RoomModule {
}

scope(DefaultSession.SCOPE) {
DefaultSetReadMarkersTask(get(), get(),get()) as SetReadMarkersTask
DefaultSetReadMarkersTask(get(), get(), get()) as SetReadMarkersTask
}

scope(DefaultSession.SCOPE) {
@ -65,7 +61,7 @@ class RoomModule {
}

scope(DefaultSession.SCOPE) {
RoomFactory(get(), get(), get(), get(), get(), get(), get(), get())
RoomFactory(get(), get(), get(), get(), get(), get(), get())
}

}

View File

@ -0,0 +1,61 @@
/*
*
* * Copyright 2019 New Vector Ltd
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package im.vector.matrix.android.internal.session.room.members

import androidx.lifecycle.LiveData
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.members.RoomMembersService
import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.model.RoomMember
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import im.vector.matrix.android.internal.util.fetchCopied

internal class DefaultRoomMembersService(private val roomId: String,
private val monarchy: Monarchy,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val taskExecutor: TaskExecutor
) : RoomMembersService {

override fun loadRoomMembersIfNeeded(): Cancelable {
val params = LoadRoomMembersTask.Params(roomId, Membership.LEAVE)
return loadRoomMembersTask.configureWith(params).executeBy(taskExecutor)
}

override fun getRoomMember(userId: String): RoomMember? {
val eventEntity = monarchy.fetchCopied {
RoomMembers(it, roomId).queryRoomMemberEvent(userId).findFirst()
}
return eventEntity?.asDomain()?.content.toModel()
}

override fun getRoomMembersLive(): LiveData<List<RoomMember>> {
return monarchy.findAllMappedWithChanges(
{
RoomMembers(it, roomId).queryRoomMembersEvent()
},
{
it.asDomain().content.toModel<RoomMember>()!!
}
)
}
}

View File

@ -26,6 +26,8 @@ import im.vector.matrix.android.internal.database.model.EventEntityFields
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
import im.vector.matrix.android.internal.database.query.where
import io.realm.Realm
import io.realm.RealmQuery
import io.realm.Sort

internal class RoomMembers(private val realm: Realm,
private val roomId: String
@ -35,10 +37,21 @@ internal class RoomMembers(private val realm: Realm,
RoomSummaryEntity.where(realm, roomId).findFirst()
}

fun getLoaded(): Map<String, RoomMember> {
fun queryRoomMembersEvent(): RealmQuery<EventEntity> {
return EventEntity
.where(realm, roomId, EventType.STATE_ROOM_MEMBER)
.sort(EventEntityFields.STATE_INDEX)
.sort(EventEntityFields.STATE_INDEX, Sort.DESCENDING)
.distinct(EventEntityFields.STATE_KEY)
.isNotNull(EventEntityFields.CONTENT)
}

fun queryRoomMemberEvent(userId: String): RealmQuery<EventEntity> {
return queryRoomMembersEvent()
.equalTo(EventEntityFields.STATE_KEY, userId)
}

fun getLoaded(): Map<String, RoomMember> {
return queryRoomMembersEvent()
.findAll()
.map { it.asDomain() }
.associateBy { it.stateKey!! }
@ -48,12 +61,12 @@ internal class RoomMembers(private val realm: Realm,

fun getNumberOfJoinedMembers(): Int {
return roomSummary?.joinedMembersCount
?: getLoaded().filterValues { it.membership == Membership.JOIN }.size
?: getLoaded().filterValues { it.membership == Membership.JOIN }.size
}

fun getNumberOfInvitedMembers(): Int {
return roomSummary?.invitedMembersCount
?: getLoaded().filterValues { it.membership == Membership.INVITE }.size
?: getLoaded().filterValues { it.membership == Membership.INVITE }.size
}

fun getNumberOfMembers(): Int {

View File

@ -16,22 +16,29 @@

package im.vector.matrix.android.internal.session.room.read

import androidx.lifecycle.LiveData
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.room.members.RoomMembersService
import im.vector.matrix.android.api.session.room.model.ReadReceipt
import im.vector.matrix.android.api.session.room.read.ReadService
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
import im.vector.matrix.android.internal.database.model.ReadReceiptEntityFields
import im.vector.matrix.android.internal.database.query.latestEvent
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import im.vector.matrix.android.internal.util.fetchCopied

internal class DefaultReadService(private val roomId: String,
private val monarchy: Monarchy,
private val roomMembersService: RoomMembersService,
private val setReadMarkersTask: SetReadMarkersTask,
private val taskExecutor: TaskExecutor) : ReadService {

override fun markAllAsRead(callback: MatrixCallback<Void>) {
val latestEvent = getLatestEvent()
val latestEvent = monarchy.fetchCopied { EventEntity.latestEvent(it, roomId) }
val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = latestEvent?.eventId, readReceiptEventId = latestEvent?.eventId)
setReadMarkersTask.configureWith(params).executeBy(taskExecutor)
}
@ -46,9 +53,26 @@ internal class DefaultReadService(private val roomId: String,
setReadMarkersTask.configureWith(params).executeBy(taskExecutor)
}

private fun getLatestEvent(): EventEntity? {
return monarchy.fetchCopied { EventEntity.latestEvent(it, roomId) }
override fun readReceipts(): LiveData<List<ReadReceipt>> {
return monarchy.findAllMappedWithChanges(
{ realm -> ReadReceiptEntity.where(realm, roomId) },
{
val roomMember = roomMembersService.getRoomMember(it.userId)
ReadReceipt(roomMember, it.eventId, it.originServerTs.toLong())
}
)
}


override fun readReceipts(eventId: String): List<ReadReceipt> {
return monarchy.fetchAllMappedSync(
{ realm ->
ReadReceiptEntity.where(realm, roomId)
.equalTo(ReadReceiptEntityFields.EVENT_ID, eventId)
},
{
val roomMember = roomMembersService.getRoomMember(it.userId)
ReadReceipt(roomMember, it.eventId, it.originServerTs.toLong())
}
)
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.matrix.android.internal.session.room.read

import androidx.lifecycle.Observer
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.room.read.ReadService
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
import im.vector.matrix.android.internal.database.query.where

class ReadServiceListeners(private val roomId: String,
private val monarchy: Monarchy
) {

private val listeners = ArrayList<ReadService.Listener>()
private val readReceiptsLive by lazy {
monarchy.findAllManagedWithChanges { realm -> ReadReceiptEntity.where(realm, roomId) }
}
private val readReceiptLiveObserver = Observer<Monarchy.ManagedChangeSet<ReadReceiptEntity>> { listeners.forEach { it.onReadReceiptsUpdated() } }

fun addListener(listener: ReadService.Listener) {
if (listeners.isEmpty()) {
startObservingData()
}
listeners.add(listener)
}

fun removeListener(listener: ReadService.Listener) {
listeners.remove(listener)
if (listeners.isEmpty()) {
stopObservingData()
}
}

private fun startObservingData() {
readReceiptsLive.observeForever(readReceiptLiveObserver)
}

private fun stopObservingData() {
readReceiptsLive.removeObserver(readReceiptLiveObserver)
}


}