forked from GitHub-Mirror/riotX-android
Timeline : try to get a better PagedList/Epoxy integration. Still need to be refined.
This commit is contained in:
@ -56,6 +56,8 @@ dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation 'com.android.support:appcompat-v7:28.0.0'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||
// Paging
|
||||
implementation "android.arch.paging:runtime:1.0.1"
|
||||
|
||||
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'
|
||||
implementation 'com.jakewharton.timber:timber:4.7.1'
|
||||
@ -66,7 +68,6 @@ dependencies {
|
||||
|
||||
implementation("com.airbnb.android:epoxy:$epoxy_version")
|
||||
kapt "com.airbnb.android:epoxy-processor:$epoxy_version"
|
||||
implementation "com.airbnb.android:epoxy-paging:$epoxy_version"
|
||||
implementation 'com.airbnb.android:mvrx:0.6.0'
|
||||
|
||||
// FP
|
||||
|
@ -73,9 +73,8 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
|
||||
val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
|
||||
scrollOnNewMessageCallback = ScrollOnNewMessageCallback(layoutManager)
|
||||
recyclerView.layoutManager = layoutManager
|
||||
//timelineEventController.addModelBuildListener { it.dispatchTo(scrollOnNewMessageCallback) }
|
||||
recyclerView.setHasFixedSize(true)
|
||||
recyclerView.setItemViewCacheSize(20)
|
||||
//timelineEventController.addModelBuildListener { it.dispatchTo(scrollOnNewMessageCallback) }
|
||||
recyclerView.setController(timelineEventController)
|
||||
timelineEventController.callback = this
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.epoxy.KotlinModel
|
||||
import im.vector.riotredesign.features.home.AvatarRenderer
|
||||
|
||||
data class MessageItem(
|
||||
class MessageItem(
|
||||
val message: CharSequence? = null,
|
||||
val time: CharSequence? = null,
|
||||
val avatarUrl: String?,
|
||||
|
@ -4,7 +4,7 @@ import android.widget.TextView
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.epoxy.KotlinModel
|
||||
|
||||
data class TextItem(
|
||||
class TextItem(
|
||||
val text: CharSequence? = null
|
||||
) : KotlinModel(R.layout.item_event_text) {
|
||||
|
||||
|
@ -1,106 +1,80 @@
|
||||
package im.vector.riotredesign.features.home.room.detail.timeline
|
||||
|
||||
import android.arch.paging.PagedList
|
||||
import com.airbnb.epoxy.EpoxyAsyncUtil
|
||||
import com.airbnb.epoxy.EpoxyController
|
||||
import com.airbnb.epoxy.EpoxyModel
|
||||
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
|
||||
import im.vector.matrix.android.api.session.events.model.EventType
|
||||
import im.vector.matrix.android.api.session.room.timeline.TimelineData
|
||||
import im.vector.riotredesign.core.extensions.localDateTime
|
||||
import im.vector.riotredesign.features.home.LoadingItemModel_
|
||||
import im.vector.riotredesign.features.home.room.detail.timeline.paging.PagedListEpoxyController
|
||||
|
||||
class TimelineEventController(private val roomId: String,
|
||||
private val messageItemFactory: MessageItemFactory,
|
||||
private val textItemFactory: TextItemFactory,
|
||||
private val dateFormatter: TimelineDateFormatter
|
||||
) : EpoxyController(
|
||||
) : PagedListEpoxyController<EnrichedEvent>(
|
||||
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
|
||||
EpoxyAsyncUtil.getAsyncBackgroundHandler()
|
||||
) {
|
||||
|
||||
init {
|
||||
setFilterDuplicates(true)
|
||||
}
|
||||
|
||||
private val pagedListCallback = object : PagedList.Callback() {
|
||||
override fun onChanged(position: Int, count: Int) {
|
||||
buildSnapshotList()
|
||||
}
|
||||
private var isLoadingForward: Boolean = false
|
||||
private var isLoadingBackward: Boolean = false
|
||||
|
||||
override fun onInserted(position: Int, count: Int) {
|
||||
buildSnapshotList()
|
||||
}
|
||||
|
||||
override fun onRemoved(position: Int, count: Int) {
|
||||
buildSnapshotList()
|
||||
}
|
||||
}
|
||||
|
||||
private var snapshotList: List<EnrichedEvent> = emptyList()
|
||||
private var timelineData: TimelineData? = null
|
||||
var callback: Callback? = null
|
||||
|
||||
fun update(timelineData: TimelineData?) {
|
||||
timelineData?.events?.removeWeakCallback(pagedListCallback)
|
||||
this.timelineData = timelineData
|
||||
timelineData?.events?.addWeakCallback(null, pagedListCallback)
|
||||
buildSnapshotList()
|
||||
isLoadingForward = timelineData?.isLoadingForward ?: false
|
||||
isLoadingBackward = timelineData?.isLoadingBackward ?: false
|
||||
submitList(timelineData?.events)
|
||||
requestModelBuild()
|
||||
}
|
||||
|
||||
override fun buildModels() {
|
||||
buildModelsWith(
|
||||
snapshotList,
|
||||
timelineData?.isLoadingForward ?: false,
|
||||
timelineData?.isLoadingBackward ?: false
|
||||
)
|
||||
}
|
||||
|
||||
private fun buildModelsWith(events: List<EnrichedEvent?>,
|
||||
isLoadingForward: Boolean,
|
||||
isLoadingBackward: Boolean) {
|
||||
if (events.isEmpty()) {
|
||||
return
|
||||
override fun buildItemModels(currentPosition: Int, items: List<EnrichedEvent?>): List<EpoxyModel<*>> {
|
||||
if (items.isNullOrEmpty()) {
|
||||
return emptyList()
|
||||
}
|
||||
val epoxyModels = ArrayList<EpoxyModel<*>>()
|
||||
val event = items[currentPosition] ?: return emptyList()
|
||||
val nextEvent = if (currentPosition + 1 < items.size) items[currentPosition + 1] else null
|
||||
|
||||
val date = event.root.localDateTime()
|
||||
val nextDate = nextEvent?.root?.localDateTime()
|
||||
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
||||
|
||||
val item = when (event.root.type) {
|
||||
EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, addDaySeparator, date, callback)
|
||||
else -> textItemFactory.create(event)
|
||||
}
|
||||
item?.also {
|
||||
it.id(event.localId)
|
||||
epoxyModels.add(it)
|
||||
}
|
||||
|
||||
if (addDaySeparator) {
|
||||
val formattedDay = dateFormatter.formatMessageDay(date)
|
||||
val daySeparatorItem = DaySeparatorItem(formattedDay).id(roomId + formattedDay)
|
||||
epoxyModels.add(daySeparatorItem)
|
||||
}
|
||||
return epoxyModels
|
||||
}
|
||||
|
||||
override fun addModels(models: List<EpoxyModel<*>>) {
|
||||
LoadingItemModel_()
|
||||
.id(roomId + "forward_loading_item")
|
||||
.addIf(isLoadingForward, this)
|
||||
|
||||
for (index in 0 until events.size) {
|
||||
val event = events[index] ?: continue
|
||||
val nextEvent = if (index + 1 < events.size) events[index + 1] else null
|
||||
|
||||
val date = event.root.localDateTime()
|
||||
val nextDate = nextEvent?.root?.localDateTime()
|
||||
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
||||
|
||||
val item = when (event.root.type) {
|
||||
EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, addDaySeparator, date, callback)
|
||||
else -> textItemFactory.create(event)
|
||||
}
|
||||
|
||||
item
|
||||
?.onBind {
|
||||
timelineData?.events?.loadAround(index)
|
||||
}
|
||||
?.id(event.localId)
|
||||
?.addTo(this)
|
||||
|
||||
if (addDaySeparator) {
|
||||
val formattedDay = dateFormatter.formatMessageDay(date)
|
||||
DaySeparatorItem(formattedDay).id(roomId + formattedDay).addTo(this)
|
||||
}
|
||||
}
|
||||
super.add(models)
|
||||
|
||||
LoadingItemModel_()
|
||||
.id(roomId + "backward_loading_item")
|
||||
.addIf(isLoadingBackward, this)
|
||||
|
||||
}
|
||||
|
||||
private fun buildSnapshotList() {
|
||||
snapshotList = timelineData?.events?.snapshot() ?: emptyList()
|
||||
requestModelBuild()
|
||||
}
|
||||
|
||||
interface Callback {
|
||||
fun onUrlClicked(url: String)
|
||||
|
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2018 The Android Open Source Project
|
||||
*
|
||||
* 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.paging
|
||||
|
||||
import android.arch.paging.PagedList
|
||||
import android.os.Handler
|
||||
import android.support.v7.util.DiffUtil
|
||||
import com.airbnb.epoxy.EpoxyController
|
||||
import com.airbnb.epoxy.EpoxyModel
|
||||
import com.airbnb.epoxy.EpoxyViewHolder
|
||||
|
||||
/**
|
||||
* An [EpoxyController] that can work with a [PagedList].
|
||||
*
|
||||
* Internally, it caches the model for each item in the [PagedList]. You should override
|
||||
* [buildItemModel] method to build the model for the given item. Since [PagedList] might include
|
||||
* `null` items if placeholders are enabled, this method needs to handle `null` values in the list.
|
||||
*
|
||||
* By default, the model for each item is added to the model list. To change this behavior (to
|
||||
* filter items or inject extra items), you can override [addModels] function and manually add built
|
||||
* models.
|
||||
*
|
||||
* @param T The type of the items in the [PagedList].
|
||||
*/
|
||||
abstract class PagedListEpoxyController<T>(
|
||||
/**
|
||||
* The handler to use for building models. By default this uses the main thread, but you can use
|
||||
* [EpoxyAsyncUtil.getAsyncBackgroundHandler] to do model building in the background.
|
||||
*
|
||||
* The notify thread of your PagedList (from setNotifyExecutor in the PagedList Builder) must be
|
||||
* the same as this thread. Otherwise Epoxy will crash.
|
||||
*/
|
||||
modelBuildingHandler: Handler = EpoxyController.defaultModelBuildingHandler,
|
||||
/**
|
||||
* The handler to use when calculating the diff between built model lists.
|
||||
* By default this uses the main thread, but you can use
|
||||
* [EpoxyAsyncUtil.getAsyncBackgroundHandler] to do diffing in the background.
|
||||
*/
|
||||
diffingHandler: Handler = EpoxyController.defaultDiffingHandler,
|
||||
/**
|
||||
* [PagedListEpoxyController] uses an [DiffUtil.ItemCallback] to detect changes between
|
||||
* [PagedList]s. By default, it relies on simple object equality but you can provide a custom
|
||||
* one if you don't use all fields in the object in your models.
|
||||
*/
|
||||
itemDiffCallback: DiffUtil.ItemCallback<T> = DEFAULT_ITEM_DIFF_CALLBACK as DiffUtil.ItemCallback<T>
|
||||
) : EpoxyController(modelBuildingHandler, diffingHandler) {
|
||||
// this is where we keep the already built models
|
||||
protected val modelCache = PagedListModelCache(
|
||||
modelBuilder = { pos, item ->
|
||||
buildItemModels(pos, item)
|
||||
},
|
||||
rebuildCallback = {
|
||||
requestModelBuild()
|
||||
},
|
||||
itemDiffCallback = itemDiffCallback,
|
||||
modelBuildingHandler = modelBuildingHandler
|
||||
)
|
||||
|
||||
final override fun buildModels() {
|
||||
addModels(modelCache.getModels())
|
||||
}
|
||||
|
||||
override fun onModelBound(
|
||||
holder: EpoxyViewHolder,
|
||||
boundModel: EpoxyModel<*>,
|
||||
position: Int,
|
||||
previouslyBoundModel: EpoxyModel<*>?
|
||||
) {
|
||||
modelCache.loadAround(boundModel)
|
||||
}
|
||||
|
||||
/**
|
||||
* This function adds all built models to the adapter. You can override this method to add extra
|
||||
* items into the model list or remove some.
|
||||
*/
|
||||
open fun addModels(models: List<EpoxyModel<*>>) {
|
||||
super.add(models)
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the model for a given item. This must return a single model for each item. If you want
|
||||
* to inject headers etc, you can override [addModels] function.
|
||||
*
|
||||
* If the `item` is `null`, you should provide the placeholder. If your [PagedList] is configured
|
||||
* without placeholders, you don't need to handle the `null` case.
|
||||
*/
|
||||
abstract fun buildItemModels(currentPosition: Int, items: List<T?>): List<EpoxyModel<*>>
|
||||
|
||||
/**
|
||||
* Submit a new paged list.
|
||||
*
|
||||
* A diff will be calculated between this list and the previous list so you may still get calls
|
||||
* to [buildItemModel] with items from the previous list.
|
||||
*/
|
||||
fun submitList(newList: PagedList<T>?) {
|
||||
modelCache.submitList(newList)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* [PagedListEpoxyController] calculates a diff on top of the PagedList to check which
|
||||
* models are invalidated.
|
||||
* This is the default [DiffUtil.ItemCallback] which uses object equality.
|
||||
*/
|
||||
val DEFAULT_ITEM_DIFF_CALLBACK = object : DiffUtil.ItemCallback<Any>() {
|
||||
override fun areItemsTheSame(oldItem: Any, newItem: Any) = oldItem == newItem
|
||||
|
||||
override fun areContentsTheSame(oldItem: Any, newItem: Any) = oldItem == newItem
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright 2018 The Android Open Source Project
|
||||
*
|
||||
* 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.paging
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.arch.paging.AsyncPagedListDiffer
|
||||
import android.arch.paging.PagedList
|
||||
import android.os.Handler
|
||||
import android.support.v7.recyclerview.extensions.AsyncDifferConfig
|
||||
import android.support.v7.util.DiffUtil
|
||||
import android.support.v7.util.ListUpdateCallback
|
||||
import android.util.Log
|
||||
import com.airbnb.epoxy.EpoxyController
|
||||
import com.airbnb.epoxy.EpoxyModel
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/**
|
||||
* A PagedList stream wrapper that caches models built for each item. It tracks changes in paged lists and caches
|
||||
* models for each item when they are invalidated to avoid rebuilding models for the whole list when PagedList is
|
||||
* updated.
|
||||
*/
|
||||
class PagedListModelCache<T>(
|
||||
private val modelBuilder: (itemIndex: Int, items: List<T>) -> List<EpoxyModel<*>>,
|
||||
private val rebuildCallback: () -> Unit,
|
||||
private val itemDiffCallback: DiffUtil.ItemCallback<T>,
|
||||
private val diffExecutor: Executor? = null,
|
||||
private val modelBuildingHandler: Handler
|
||||
) {
|
||||
|
||||
|
||||
// Int is the index of the pagedList item
|
||||
// We have to be able to find the pagedlist position coming from an epoxy model to trigger
|
||||
// LoadAround with accuracy
|
||||
private val modelCache = linkedMapOf<EpoxyModel<*>, Int>()
|
||||
private var isCacheStale = AtomicBoolean(true)
|
||||
|
||||
/**
|
||||
* Tracks the last accessed position so that we can report it back to the paged list when models are built.
|
||||
*/
|
||||
private var lastPosition: Int? = null
|
||||
|
||||
/**
|
||||
* Observer for the PagedList changes that invalidates the model cache when data is updated.
|
||||
*/
|
||||
private val updateCallback = object : ListUpdateCallback {
|
||||
override fun onChanged(position: Int, count: Int, payload: Any?) {
|
||||
invalidate()
|
||||
rebuildCallback()
|
||||
}
|
||||
|
||||
override fun onMoved(fromPosition: Int, toPosition: Int) {
|
||||
invalidate()
|
||||
rebuildCallback()
|
||||
}
|
||||
|
||||
override fun onInserted(position: Int, count: Int) {
|
||||
invalidate()
|
||||
rebuildCallback()
|
||||
}
|
||||
|
||||
override fun onRemoved(position: Int, count: Int) {
|
||||
invalidate()
|
||||
rebuildCallback()
|
||||
}
|
||||
}
|
||||
|
||||
private val asyncDiffer = @SuppressLint("RestrictedApi")
|
||||
object : AsyncPagedListDiffer<T>(
|
||||
updateCallback,
|
||||
AsyncDifferConfig.Builder<T>(
|
||||
itemDiffCallback
|
||||
).also { builder ->
|
||||
if (diffExecutor != null) {
|
||||
builder.setBackgroundThreadExecutor(diffExecutor)
|
||||
}
|
||||
// we have to reply on this private API, otherwise, paged list might be changed when models are being built,
|
||||
// potentially creating concurrent modification problems.
|
||||
builder.setMainThreadExecutor { runnable: Runnable ->
|
||||
modelBuildingHandler.post(runnable)
|
||||
}
|
||||
}.build()
|
||||
) {
|
||||
init {
|
||||
if (modelBuildingHandler != EpoxyController.defaultModelBuildingHandler) {
|
||||
try {
|
||||
// looks like AsyncPagedListDiffer in 1.x ignores the config.
|
||||
// Reflection to the rescue.
|
||||
val mainThreadExecutorField =
|
||||
AsyncPagedListDiffer::class.java.getDeclaredField("mMainThreadExecutor")
|
||||
mainThreadExecutorField.isAccessible = true
|
||||
mainThreadExecutorField.set(this, Executor {
|
||||
modelBuildingHandler.post(it)
|
||||
})
|
||||
} catch (t: Throwable) {
|
||||
val msg = "Failed to hijack update handler in AsyncPagedListDiffer." +
|
||||
"You can only build models on the main thread"
|
||||
Log.e("PagedListModelCache", msg, t)
|
||||
throw IllegalStateException(msg, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun submitList(pagedList: PagedList<T>?) {
|
||||
asyncDiffer.submitList(pagedList)
|
||||
}
|
||||
|
||||
fun getModels(): List<EpoxyModel<*>> {
|
||||
if (isCacheStale.compareAndSet(true, false)) {
|
||||
asyncDiffer.currentList?.forEachIndexed { position, _ ->
|
||||
buildModel(position)
|
||||
}
|
||||
}
|
||||
lastPosition?.let {
|
||||
triggerLoadAround(it)
|
||||
}
|
||||
return modelCache.keys.toList()
|
||||
}
|
||||
|
||||
fun loadAround(model: EpoxyModel<*>) {
|
||||
modelCache[model]?.let { itemPosition ->
|
||||
triggerLoadAround(itemPosition)
|
||||
lastPosition = itemPosition
|
||||
}
|
||||
}
|
||||
|
||||
// PRIVATE METHODS *****************************************************************************
|
||||
|
||||
private fun invalidate() {
|
||||
modelCache.clear()
|
||||
isCacheStale.set(true)
|
||||
}
|
||||
|
||||
private fun cacheModelsAtPosition(itemPosition: Int, epoxyModels: Set<EpoxyModel<*>>) {
|
||||
epoxyModels.forEach {
|
||||
modelCache[it] = itemPosition
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildModel(pos: Int) {
|
||||
if (pos >= asyncDiffer.currentList?.size ?: 0) {
|
||||
return
|
||||
}
|
||||
modelBuilder(pos, asyncDiffer.currentList as List<T>).also {
|
||||
cacheModelsAtPosition(pos, it.toSet())
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerLoadAround(position: Int) {
|
||||
asyncDiffer.currentList?.let {
|
||||
if (it.size > 0) {
|
||||
it.loadAround(Math.min(position, it.size - 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:paddingBottom="8dp"
|
||||
android:textSize="15sp"
|
||||
android:textSize="16sp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/toolbarSubtitleView"
|
||||
app:layout_constraintEnd_toStartOf="@+id/messageTimeView"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
@ -55,7 +55,7 @@
|
||||
android:layout_marginStart="64dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:textColor="@color/dark_grey"
|
||||
android:textSize="14sp"
|
||||
android:textSize="16sp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
Reference in New Issue
Block a user