Breadcrumbs: nicer algorithm

This commit is contained in:
Benoit Marty 2019-12-05 22:05:37 +01:00
parent 65333e6031
commit 0768bd5c88

View File

@ -24,13 +24,12 @@ import im.vector.matrix.android.internal.task.Task
import im.vector.matrix.android.internal.util.fetchCopied import im.vector.matrix.android.internal.util.fetchCopied
import javax.inject.Inject import javax.inject.Inject
// Use same arbitrary value than Riot-Web // Use the same arbitrary value than Riot-Web
private const val MAX_BREADCRUMBS_ROOMS_NUMBER = 20 private const val MAX_BREADCRUMBS_ROOMS_NUMBER = 20
internal interface UpdateBreadcrumbsTask : Task<UpdateBreadcrumbsTask.Params, Unit> { internal interface UpdateBreadcrumbsTask : Task<UpdateBreadcrumbsTask.Params, Unit> {
data class Params( data class Params(
// Last seen roomId val newTopRoomId: String
val roomId: String
) )
} }
@ -41,32 +40,27 @@ internal class DefaultUpdateBreadcrumbsTask @Inject constructor(
) : UpdateBreadcrumbsTask { ) : UpdateBreadcrumbsTask {
override suspend fun execute(params: UpdateBreadcrumbsTask.Params) { override suspend fun execute(params: UpdateBreadcrumbsTask.Params) {
// Get the current breadcrumbs in DB val newBreadcrumbs =
val bc = monarchy.fetchCopied { realm -> // Get the breadcrumbs entity, if any
// Get the breadcrumbs entity, if any monarchy.fetchCopied { BreadcrumbsEntity.get(it) }
BreadcrumbsEntity.get(realm) ?.recentRoomIds
} ?.apply {
// Modify the list to add the newTopRoomId first
// Modify the list to add the roomId first // Ensure the roomId is not already in the list
val newRecentRoomIds = if (bc != null) { remove(params.newTopRoomId)
// Ensure the roomId is not already in the list // Add the room at first position
bc.recentRoomIds.remove(params.roomId) add(0, params.newTopRoomId)
// Add the room at first position }
bc.recentRoomIds.add(0, params.roomId) ?.take(MAX_BREADCRUMBS_ROOMS_NUMBER)
bc.recentRoomIds.take(MAX_BREADCRUMBS_ROOMS_NUMBER) ?: listOf(params.newTopRoomId)
} else {
// FIXME It can remove the previous breadcrumbs, if not synced yet
listOf(params.roomId)
}
// Update the DB locally, do not wait for the sync // Update the DB locally, do not wait for the sync
saveBreadcrumbsTask.execute(SaveBreadcrumbsTask.Params(newRecentRoomIds)) saveBreadcrumbsTask.execute(SaveBreadcrumbsTask.Params(newBreadcrumbs))
// FIXME It can remove the previous breadcrumbs, if not synced yet
// And update account data // And update account data
updateUserAccountDataTask.execute(UpdateUserAccountDataTask.BreadcrumbsParams( updateUserAccountDataTask.execute(UpdateUserAccountDataTask.BreadcrumbsParams(
breadcrumbsContent = BreadcrumbsContent( breadcrumbsContent = BreadcrumbsContent(newBreadcrumbs)
newRecentRoomIds
)
)) ))
} }
} }