diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt index 9b0bae9e08..2dbb580a4f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt @@ -60,6 +60,12 @@ interface RoomService { */ fun liveBreadcrumbs(): LiveData> + /** + * 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 */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/BreadcrumbsEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/BreadcrumbsEntity.kt index e430c194c5..c396795421 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/BreadcrumbsEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/BreadcrumbsEntity.kt @@ -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 = RealmList()) : RealmObject() { +internal open class BreadcrumbsEntity( + var recentRoomIds: RealmList = RealmList() +) : RealmObject() { companion object } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index 9863d12ee5..268d625622 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -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, callback: MatrixCallback): Cancelable { return joinRoomTask .configureWith(JoinRoomTask.Params(roomId, viaServers)) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt index 0436afd05f..9cc3a5a3c6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt @@ -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) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/model/accountdata/UserAccountDataBreadcrumbs.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/model/accountdata/UserAccountDataBreadcrumbs.kt index c12ab126b9..cf5ee9c5e1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/model/accountdata/UserAccountDataBreadcrumbs.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/model/accountdata/UserAccountDataBreadcrumbs.kt @@ -27,8 +27,5 @@ internal data class UserAccountDataBreadcrumbs( @JsonClass(generateAdapter = true) internal data class BreadcrumbsContent( - @Json(name = "rooms") val roomIds: List = emptyList(), - - // We also have "recent_rooms", I do not know what to do with that list @Json(name = "recent_rooms") val recentRoomIds: List = emptyList() ) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/AccountDataModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/AccountDataModule.kt index 5889ab4847..37b63dcd07 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/AccountDataModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/AccountDataModule.kt @@ -39,4 +39,7 @@ internal abstract class AccountDataModule { @Binds abstract fun bindSaveBreadcrumbsTask(saveBreadcrumbsTask: DefaultSaveBreadcrumbsTask): SaveBreadcrumbsTask + + @Binds + abstract fun bindUpdateBreadcrumsTask(saveBreadcrumbsTask: DefaultUpdateBreadcrumbsTask): UpdateBreadcrumbsTask } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/SaveBreadcrumbsTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/SaveBreadcrumbsTask.kt index 7cabb2772e..97cfba0549 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/SaveBreadcrumbsTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/SaveBreadcrumbsTask.kt @@ -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 { data class Params( - val roomIds: List + val recentRoomIds: List ) } -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().apply { addAll(params.roomIds) } + entity.recentRoomIds = RealmList().apply { addAll(params.recentRoomIds) } } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateBreadcrumbsTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateBreadcrumbsTask.kt new file mode 100644 index 0000000000..0f2f5f9a3d --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateBreadcrumbsTask.kt @@ -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 { + 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 + ) + )) + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateUserAccountDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateUserAccountDataTask.kt index 9fa71005ff..4c4f40add5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateUserAccountDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/UpdateUserAccountDataTask.kt @@ -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