Breadcrumbs: notify viewed rooms

This commit is contained in:
Benoit Marty 2019-12-05 16:06:47 +01:00
parent 7c561ae622
commit 4ff12605e9
10 changed files with 109 additions and 10 deletions

View File

@ -60,6 +60,12 @@ interface RoomService {
*/
fun liveBreadcrumbs(): LiveData<List<RoomSummary>>
/**
* Inform the Matrix SDK that a room is displayed.
* The SDK will update the breadcrumbs in the user account data
*/
fun onRoomDisplayed(roomId: String): Cancelable
/**
* Mark all rooms as read
*/

View File

@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.database.model
import io.realm.RealmList
import io.realm.RealmObject
internal open class BreadcrumbsEntity(var roomIds: RealmList<String> = RealmList()) : RealmObject() {
internal open class BreadcrumbsEntity(
var recentRoomIds: RealmList<String> = RealmList()
) : RealmObject() {
companion object
}

View File

@ -34,6 +34,7 @@ import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.room.create.CreateRoomTask
import im.vector.matrix.android.internal.session.room.membership.joining.JoinRoomTask
import im.vector.matrix.android.internal.session.room.read.MarkAllRoomsReadTask
import im.vector.matrix.android.internal.session.user.accountdata.UpdateBreadcrumbsTask
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import io.realm.Realm
@ -45,6 +46,7 @@ internal class DefaultRoomService @Inject constructor(private val monarchy: Mona
private val createRoomTask: CreateRoomTask,
private val joinRoomTask: JoinRoomTask,
private val markAllRoomsReadTask: MarkAllRoomsReadTask,
private val updateBreadcrumbsTask: UpdateBreadcrumbsTask,
private val roomFactory: RoomFactory,
private val taskExecutor: TaskExecutor) : RoomService {
@ -83,12 +85,18 @@ internal class DefaultRoomService @Inject constructor(private val monarchy: Mona
// TODO Improve this query, it's not live when breadcrumbs changes
realm.where(RoomSummaryEntity::class.java)
.`in`(RoomSummaryEntityFields.ROOM_ID,
(realm.where(BreadcrumbsEntity::class.java).findFirst()?.roomIds ?: RealmList()).toTypedArray())
(realm.where(BreadcrumbsEntity::class.java).findFirst()?.recentRoomIds ?: RealmList()).toTypedArray())
},
{ roomSummaryMapper.map(it) }
)
}
override fun onRoomDisplayed(roomId: String): Cancelable {
return updateBreadcrumbsTask
.configureWith(UpdateBreadcrumbsTask.Params(roomId))
.executeBy(taskExecutor)
}
override fun joinRoom(roomId: String, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
return joinRoomTask
.configureWith(JoinRoomTask.Params(roomId, viaServers)) {

View File

@ -136,7 +136,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(private val monarc
private fun handleBreadcrumbs(userAccountDataBreadcrumbs: UserAccountDataBreadcrumbs) {
saveBreadcrumbsTask
.configureWith(SaveBreadcrumbsTask.Params(userAccountDataBreadcrumbs.content.roomIds))
.configureWith(SaveBreadcrumbsTask.Params(userAccountDataBreadcrumbs.content.recentRoomIds))
.executeBy(taskExecutor)
}
}

View File

@ -27,8 +27,5 @@ internal data class UserAccountDataBreadcrumbs(
@JsonClass(generateAdapter = true)
internal data class BreadcrumbsContent(
@Json(name = "rooms") val roomIds: List<String> = emptyList(),
// We also have "recent_rooms", I do not know what to do with that list
@Json(name = "recent_rooms") val recentRoomIds: List<String> = emptyList()
)

View File

@ -39,4 +39,7 @@ internal abstract class AccountDataModule {
@Binds
abstract fun bindSaveBreadcrumbsTask(saveBreadcrumbsTask: DefaultSaveBreadcrumbsTask): SaveBreadcrumbsTask
@Binds
abstract fun bindUpdateBreadcrumsTask(saveBreadcrumbsTask: DefaultUpdateBreadcrumbsTask): UpdateBreadcrumbsTask
}

View File

@ -23,15 +23,17 @@ import io.realm.RealmList
import javax.inject.Inject
/**
* Save the Breadcrumbs roomId list in DB
* Save the Breadcrumbs roomId list in DB, either from the sync, or updated locally
*/
internal interface SaveBreadcrumbsTask : Task<SaveBreadcrumbsTask.Params, Unit> {
data class Params(
val roomIds: List<String>
val recentRoomIds: List<String>
)
}
internal class DefaultSaveBreadcrumbsTask @Inject constructor(private val monarchy: Monarchy) : SaveBreadcrumbsTask {
internal class DefaultSaveBreadcrumbsTask @Inject constructor(
private val monarchy: Monarchy
) : SaveBreadcrumbsTask {
override suspend fun execute(params: SaveBreadcrumbsTask.Params) {
monarchy.awaitTransaction { realm ->
@ -40,7 +42,7 @@ internal class DefaultSaveBreadcrumbsTask @Inject constructor(private val monarc
?: realm.createObject(BreadcrumbsEntity::class.java)
// And save the new received list
entity.roomIds = RealmList<String>().apply { addAll(params.roomIds) }
entity.recentRoomIds = RealmList<String>().apply { addAll(params.recentRoomIds) }
}
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.user.accountdata
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
import im.vector.matrix.android.internal.session.sync.model.accountdata.BreadcrumbsContent
import im.vector.matrix.android.internal.task.Task
import im.vector.matrix.android.internal.util.fetchCopied
import javax.inject.Inject
internal interface UpdateBreadcrumbsTask : Task<UpdateBreadcrumbsTask.Params, Unit> {
data class Params(
// Last seen roomId
val roomId: String
)
}
internal class DefaultUpdateBreadcrumbsTask @Inject constructor(
private val saveBreadcrumbsTask: SaveBreadcrumbsTask,
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
private val monarchy: Monarchy
) : UpdateBreadcrumbsTask {
override suspend fun execute(params: UpdateBreadcrumbsTask.Params) {
// Get the current breadcrumbs in DB
val bc = monarchy.fetchCopied { realm ->
// Get the breadcrumbs entity, if any
realm.where(BreadcrumbsEntity::class.java).findFirst()
}
// Modify the list to add the roomId first
val newRecentRoomIds = if (bc != null) {
// Ensure the roomId is not already in the list
bc.recentRoomIds.remove(params.roomId)
// Add the room at first position
bc.recentRoomIds.add(0, params.roomId)
bc.recentRoomIds.toList()
} else {
listOf(params.roomId)
}
// Update the DB locally, do not wait for the sync
saveBreadcrumbsTask.execute(SaveBreadcrumbsTask.Params(newRecentRoomIds))
// And update account data
updateUserAccountDataTask.execute(UpdateUserAccountDataTask.BreadcrumbsParams(
breadcrumbsContent = BreadcrumbsContent(
newRecentRoomIds
)
))
}
}

View File

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.user.accountdata
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.sync.model.accountdata.BreadcrumbsContent
import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountData
import im.vector.matrix.android.internal.task.Task
import javax.inject.Inject
@ -38,6 +39,15 @@ internal interface UpdateUserAccountDataTask : Task<UpdateUserAccountDataTask.Pa
return directMessages
}
}
data class BreadcrumbsParams(override val type: String = UserAccountData.TYPE_BREADCRUMBS,
private val breadcrumbsContent: BreadcrumbsContent
) : Params {
override fun getData(): Any {
return breadcrumbsContent
}
}
}
internal class DefaultUpdateUserAccountDataTask @Inject constructor(private val accountDataApi: AccountDataAPI,

View File

@ -143,6 +143,9 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
timeline.addListener(this)
timeline.start()
setState { copy(timeline = this@RoomDetailViewModel.timeline) }
// Inform the SDK that the room is displayed
session.onRoomDisplayed(initialState.roomId)
}
override fun handle(action: RoomDetailAction) {