Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions android/src/main/java/me/sectiontn/flume/FlumeAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FlumeAdapter(
private val measurer: FlumeTextMeasurer,
private val recycler: RecyclerView,
private val slots: FlumeSlots,
private val model: OffsetModel,
) : RecyclerView.Adapter<FlumeAdapter.Holder>(), FlumeStoreListener, ListNotifier {
class Holder(val view: View) : RecyclerView.ViewHolder(view) {
val cell: FlumeCellView? get() = view as? FlumeCellView
Expand Down Expand Up @@ -62,6 +63,7 @@ class FlumeAdapter(

private fun rebindAll() {
onRebindAll?.invoke()
model.invalidateLengths()
measurer.invalidate()
if (storeId != 0) FlumeJni.invalidateMeasures(storeId)
// React grid cells take their width from JS, which learns it from the slot snapshot.
Expand All @@ -79,6 +81,8 @@ class FlumeAdapter(
// The holders and their trees survive a detach; a re-attach reloads the rows from the engine's current order.
fun attach(storeId: Int) {
if (this.storeId != 0 && this.storeId != storeId) detach()
val previousStore = this.storeId
val previousCount = rows.dataCount
val reload = attached.onAttach(storeId)
val attachedCount = FlumeJni.attach(storeId, this, measurer, density)
sendRowShape()
Expand All @@ -87,6 +91,7 @@ class FlumeAdapter(
stickyIndices = sticky
if (reload || attachedCount != rows.dataCount) {
setCount(attachedCount)
if (ModelOps.resetsOnAttach(previousStore, storeId, previousCount, attachedCount)) model.reset(storeId, rows) else model.invalidateLengths()
notifyDataSetChanged()
onDataChanged?.invoke(true)
} else if (stickyChanged) {
Expand Down Expand Up @@ -116,10 +121,11 @@ class FlumeAdapter(
return
}
// The empty row comes and goes with the data, which the store's operations do not know about.
val reload = reset || rows.hasEmptyRow != rows.copy(dataCount = count).hasEmptyRow
val reload = ModelOps.reloads(reset, rows, rows.copy(dataCount = count))
val before = if (reload) null else anchorState()
val anchorLost = !reload && AnchorPolicy.losesEveryRow(laidOutDataIndices(), removed)
setCount(count)
if (reload) model.reset(storeId, rows) else model.apply(storeId, rows, false, removed, inserted, moved, changed, rows.fullSpanPositions(sticky))
ListOpsApplier.apply(reload, removed, inserted, moved, changed, this)
refreshDecorations()
reindexSlots()
Expand Down Expand Up @@ -240,6 +246,7 @@ class FlumeAdapter(
val after = before.copy(header = header, footer = footer, emptyComponent = empty)
if (after == before) return
rows = after
if (storeId != 0) model.reset(storeId, rows)
if (before.header != after.header) if (after.header) notifyItemInserted(0) else notifyItemRemoved(0)
if (before.hasEmptyRow != after.hasEmptyRow) {
if (after.hasEmptyRow) notifyItemInserted(after.headerCount) else notifyItemRemoved(after.headerCount)
Expand Down Expand Up @@ -299,7 +306,7 @@ class FlumeAdapter(
holder.slot?.let { slot ->
if (slot.horizontal != geometry.horizontal) orient(slot)
val request = slotRequest(position, holder.itemViewType)
slots.bind(slot, request.group, request.kind, request.key, request.index)
slots.bind(slot, request.group, request.kind, request.key, request.index, model.itemLengthPx(position))
return
}
val cell = holder.cell ?: return
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/me/sectiontn/flume/FlumeCellView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class FlumeCellView(context: Context, private val measurer: FlumeTextMeasurer) :
private var naturalHeightPx = 0
private var stretchedPx = 0

// Its own length along the axis, not the grid row's it stretches to, which is what the offset model holds.
fun naturalLengthPx(horizontal: Boolean): Int = if (horizontal) tree.rootWidthPx else naturalHeightPx

// A bind lays the root out at its own height, so the next measure stretches it again.
fun apply(batch: FlumeBatch) {
tree.apply(batch)
Expand Down
16 changes: 16 additions & 0 deletions android/src/main/java/me/sectiontn/flume/FlumeLayoutManagers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FlumeLinearLayoutManager(context: Context, orientation: Int, reverse: Bool
LinearLayoutManager(context, orientation, reverse) {
var drawDistancePx = -1
var onLaidOut: (() -> Unit)? = null
var onScrolledBy: (() -> Unit)? = null
var justify = ContentJustify.Start

override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
Expand All @@ -37,12 +38,20 @@ class FlumeLinearLayoutManager(context: Context, orientation: Int, reverse: Bool
super.onLayoutCompleted(state)
onLaidOut?.invoke()
}

// Each scroll step lays out the rows it brings in, whose lengths the offset model then takes.
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int =
super.scrollVerticallyBy(dy, recycler, state).also { onScrolledBy?.invoke() }

override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int =
super.scrollHorizontallyBy(dx, recycler, state).also { onScrolledBy?.invoke() }
}

class FlumeGridLayoutManager(context: Context, spanCount: Int, orientation: Int, reverse: Boolean) :
GridLayoutManager(context, spanCount, orientation, reverse) {
var drawDistancePx = -1
var onLaidOut: (() -> Unit)? = null
var onScrolledBy: (() -> Unit)? = null
var justify = ContentJustify.Start

override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
Expand All @@ -64,6 +73,13 @@ class FlumeGridLayoutManager(context: Context, spanCount: Int, orientation: Int,
super.onLayoutCompleted(state)
onLaidOut?.invoke()
}

// Each scroll step lays out the rows it brings in, whose lengths the offset model then takes.
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int =
super.scrollVerticallyBy(dy, recycler, state).also { onScrolledBy?.invoke() }

override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int =
super.scrollHorizontallyBy(dx, recycler, state).also { onScrolledBy?.invoke() }
}

// The positions that take every span of a grid row, the sticky headers; rows and columns follow from them in O(log n).
Expand Down
19 changes: 3 additions & 16 deletions android/src/main/java/me/sectiontn/flume/FlumeSlots.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ class SlotParking(context: Context) : ViewGroup(context), ReactPointerEventsView

// The host's Fabric children and the holders that show them; JS renders the slots the pool lists.
class FlumeSlots(private val host: FlumeView) {
private companion object {
companion object {
const val ESTIMATE_FLOOR_DP = 50f
}

val parking = SlotParking(host.context)
val pool = SlotPool()
val estimator = SizeEstimator((ESTIMATE_FLOOR_DP * host.resources.displayMetrics.density).roundToInt())
private val children = FabricChildren<View> { (it as? FlumeSlotView)?.slot ?: 0 }
private val holders = HashMap<Int, FlumeSlotHolder>()
private val recorded = HashMap<Int, Int>()
var cellWidthDp = -1f
private var sentCellWidthDp = Float.NaN
var widthDp = -1f
Expand All @@ -62,7 +60,6 @@ class FlumeSlots(private val host: FlumeView) {
}
view.onMeasured = { measured ->
holderShowing(measured)?.let { holder ->
recordLength(holder)
holder.refresh()
}
onRendered?.invoke(measured.slot)
Expand All @@ -76,21 +73,19 @@ class FlumeSlots(private val host: FlumeView) {
val child = children.removeAt(index)
(child as? FlumeSlotView)?.let { view ->
view.onMeasured = null
recorded.remove(view.slot)
holderShowing(view)?.show(null)
}
(child.parent as? ViewGroup)?.removeView(child)
}

fun bind(holder: FlumeSlotHolder, group: SlotGroup, kind: Int, key: String, index: Int) {
fun bind(holder: FlumeSlotHolder, group: SlotGroup, kind: Int, key: String, index: Int, estimatePx: Int) {
if (holder.slot != 0 && pool.entry(holder.slot)?.key != key) release(holder)
val slot = pool.acquire(holder.owner, group, kind, key, index)
holder.slot = slot
holder.group = group
holder.estimatePx = estimator.estimatePx(group)
holder.estimatePx = estimatePx
holders[slot] = holder
holder.show(children.bySlot(slot) as? FlumeSlotView)
recordLength(holder)
flush()
}

Expand Down Expand Up @@ -211,14 +206,6 @@ class FlumeSlots(private val host: FlumeView) {
}

private fun holderShowing(view: FlumeSlotView): FlumeSlotHolder? = holders[view.slot]?.takeIf { it.view === view }

// Each new length of a shown slot feeds its group's estimate once.
private fun recordLength(holder: FlumeSlotHolder) {
val view = holder.view?.takeIf { it.measured } ?: return
val group = holder.group ?: return
val length = view.lengthPx(holder.horizontal)
if (recorded.put(view.slot, length) != length) estimator.record(group, length)
}
}

class FlumeSlotsEvent(
Expand Down
Loading