From 2ab632f72282972ba1cf4547e2db4d5531c8b85d Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Sun, 29 Nov 2026 00:21:38 +0100 Subject: [PATCH] feat(android): add the offset model natives --- android/src/main/cpp/FlumeJni.cpp | 6 +- android/src/main/cpp/FlumeModelJni.cpp | 185 ++++++++++++++++++ android/src/main/cpp/FlumeModelJni.h | 8 + .../main/java/me/sectiontn/flume/FlumeJni.kt | 41 ++++ .../java/me/sectiontn/flume/OffsetModel.kt | 132 +++++++++++++ .../sectiontn/flume/OffsetModelConfigTest.kt | 18 ++ cpp/core/ListLayout.cpp | 36 +++- cpp/core/ListLayout.h | 12 ++ cpp/core/ListOpsArrays.cpp | 18 ++ cpp/core/ListOpsArrays.h | 13 ++ cpp/tests/list_layout_test.cpp | 47 +++++ cpp/tests/list_ops_arrays_test.cpp | 18 ++ 12 files changed, 531 insertions(+), 3 deletions(-) create mode 100644 android/src/main/cpp/FlumeModelJni.cpp create mode 100644 android/src/main/cpp/FlumeModelJni.h create mode 100644 android/src/main/java/me/sectiontn/flume/OffsetModel.kt create mode 100644 android/src/test/java/me/sectiontn/flume/OffsetModelConfigTest.kt create mode 100644 cpp/core/ListOpsArrays.cpp create mode 100644 cpp/core/ListOpsArrays.h create mode 100644 cpp/tests/list_ops_arrays_test.cpp diff --git a/android/src/main/cpp/FlumeJni.cpp b/android/src/main/cpp/FlumeJni.cpp index 97d9675..c3a863b 100644 --- a/android/src/main/cpp/FlumeJni.cpp +++ b/android/src/main/cpp/FlumeJni.cpp @@ -8,6 +8,7 @@ #include "AndroidStoreListener.h" #include "AndroidTextMeasurer.h" +#include "FlumeModelJni.h" #include "bindings/Engine.h" #include "bindings/FlumeHost.h" #include "core/OpBatch.h" @@ -143,5 +144,8 @@ struct JFlumeJni : jni::JavaClass { } // namespace facebook::react JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) { - return facebook::jni::initialize(vm, [] { facebook::react::JFlumeJni::registerNatives(); }); + return facebook::jni::initialize(vm, [] { + facebook::react::JFlumeJni::registerNatives(); + flume::android::registerModelNatives(); + }); } diff --git a/android/src/main/cpp/FlumeModelJni.cpp b/android/src/main/cpp/FlumeModelJni.cpp new file mode 100644 index 0000000..d3e99ad --- /dev/null +++ b/android/src/main/cpp/FlumeModelJni.cpp @@ -0,0 +1,185 @@ +#include "FlumeModelJni.h" + +#include + +#include +#include +#include + +#include "bindings/Engine.h" +#include "core/ListLayout.h" +#include "core/ListOpsArrays.h" +#include "core/ListRows.h" + +namespace flume::android { + +namespace jni = facebook::jni; + +namespace { + +// One view's model in px: the layout, its geometry, and the rows and full-span positions it was last given. +struct ViewModel { + ListLayout layout; + ListLayoutConfig config; + ListRows rows; + std::vector fullSpan; +}; + +ViewModel& modelOf(jlong handle) { return *reinterpret_cast(handle); } + +std::vector intsOf(jni::alias_ref array) { + if (!array) return {}; + const auto size = array->size(); + auto region = array->getRegion(0, size); + return std::vector(region.get(), region.get() + size); +} + +std::vector floatsOf(jni::alias_ref array) { + if (!array) return {}; + const auto size = array->size(); + auto region = array->getRegion(0, size); + return std::vector(region.get(), region.get() + size); +} + +// The header, footer and empty row positions follow the rows, which change with the data. +void setRows(ViewModel& model, const ListRows& rows) { + model.rows = rows; + model.config.dataStart = rows.headerCount(); + model.config.afterData = rows.footer ? 1 : 0; + model.config.emptyIndex = rows.hasEmptyRow() ? rows.headerCount() : -1; +} + +// The same ids as FlumeAdapter.getItemViewType: the store's type for a data row, the list component's own for the rest. +ListLayout::TypeOf typeOf(int storeId, const ListRows& rows) { + return [storeId, rows](int position) { + switch (rows.kind(position)) { + case RowKind::Header: + return kHeaderViewType; + case RowKind::Data: + return Engine::instance().viewType(storeId, rows.dataIndex(position)); + case RowKind::Empty: + return kEmptyViewType; + case RowKind::Footer: + return kFooterViewType; + } + return kFooterViewType; + }; +} + +struct JFlumeModel : jni::JavaClass { + static constexpr auto kJavaDescriptor = "Lme/sectiontn/flume/FlumeJni;"; + + static jlong modelCreate(jni::alias_ref) { return reinterpret_cast(new ViewModel()); } + + static void modelDrop(jni::alias_ref, jlong handle) { delete reinterpret_cast(handle); } + + // The order of OffsetModelConfig.floats and ints. + static void modelConfigure(jni::alias_ref, jlong handle, jni::alias_ref floats, + jni::alias_ref ints, jni::alias_ref fullSpan) { + ViewModel& model = modelOf(handle); + const std::vector f = floatsOf(floats); + const std::vector i = intsOf(ints); + if (f.size() < 12 || i.size() < 6) return; + ListLayoutConfig& config = model.config; + config.paddingStart = f[0]; + config.paddingEnd = f[1]; + config.paddingCrossStart = f[2]; + config.paddingCrossEnd = f[3]; + config.estimatedItemLength = f[4]; + config.estimateFloor = f[5]; + config.separator = f[6]; + config.mainGap = f[7]; + config.gridRowGap = f[8]; + config.crossGap = f[9]; + config.laneInsetStart = f[10]; + config.laneInsetEnd = f[11]; + config.horizontal = i[0] != 0; + config.inverted = i[1] != 0; + config.lanes = i[2]; + config.stackFromEnd = i[3] != 0; + config.justify = static_cast(std::clamp(i[4], 0, 2)); + config.growFooter = i[5] != 0; + model.fullSpan = intsOf(fullSpan); + model.layout.configure(config, model.fullSpan); + } + + static void modelReset(jni::alias_ref, jlong handle, jint storeId, jboolean header, jint dataCount, + jboolean emptyComponent, jboolean footer) { + ViewModel& model = modelOf(handle); + setRows(model, ListRows{header != 0, dataCount, emptyComponent != 0, footer != 0}); + model.layout.configure(model.config, model.fullSpan); + model.layout.reset(model.rows.itemCount(), typeOf(storeId, model.rows)); + } + + static void modelApply(jni::alias_ref, jlong handle, jint storeId, jboolean header, jint dataCount, + jboolean emptyComponent, jboolean footer, jboolean reset, jni::alias_ref removed, + jni::alias_ref inserted, jni::alias_ref moved, + jni::alias_ref changed, jni::alias_ref fullSpan) { + ViewModel& model = modelOf(handle); + setRows(model, ListRows{header != 0, dataCount, emptyComponent != 0, footer != 0}); + model.fullSpan = intsOf(fullSpan); + model.layout.configure(model.config, model.fullSpan); + const ListOps dataOps = listOpsFromArrays(reset != 0, intsOf(removed), intsOf(inserted), intsOf(moved), intsOf(changed)); + model.layout.apply(model.rows.positions(dataOps), model.rows.itemCount(), typeOf(storeId, model.rows), model.fullSpan); + } + + static jint modelPush(jni::alias_ref, jlong handle, jni::alias_ref pairs, jint count) { + const std::vector values = intsOf(pairs); + const int pairCount = std::min(static_cast(count), static_cast(values.size() / 2)); + return modelOf(handle).layout.setLengths(values.data(), pairCount); + } + + static void modelInvalidate(jni::alias_ref, jlong handle) { modelOf(handle).layout.invalidateLengths(); } + + static jdouble modelStart(jni::alias_ref, jlong handle, jint position, jint viewportPx) { + ViewModel& model = modelOf(handle); + const ItemRect rect = model.layout.rectOf(position, 0, static_cast(viewportPx)); + return model.config.horizontal ? rect.x : rect.y; + } + + static jint modelItemLength(jni::alias_ref, jlong handle, jint position) { + const ListLayout& layout = modelOf(handle).layout; + if (position < 0 || position >= layout.count()) return 0; + return static_cast(std::lround(layout.itemLength(position))); + } + + static jdouble modelContentLength(jni::alias_ref, jlong handle, jint viewportPx) { + return modelOf(handle).layout.contentLength(static_cast(viewportPx)); + } + + static jint modelEstimate(jni::alias_ref, jlong handle, jint viewType) { + return static_cast(std::lround(modelOf(handle).layout.estimate(viewType))); + } + + static jni::local_ref modelLanding(jni::alias_ref, jlong handle, jdouble contentOffsetPx, + jint viewportPx) { + const OffsetLanding landing = modelOf(handle).layout.landingAt(contentOffsetPx, static_cast(viewportPx)); + auto out = jni::JArrayDouble::newArray(2); + const jdouble values[] = {static_cast(landing.index), landing.remainder}; + out->setRegion(0, 2, values); + return out; + } + + static void registerNatives() { + javaClassStatic()->registerNatives({ + makeNativeMethod("modelCreate", JFlumeModel::modelCreate), + makeNativeMethod("modelDrop", JFlumeModel::modelDrop), + makeNativeMethod("modelConfigure", JFlumeModel::modelConfigure), + makeNativeMethod("modelReset", JFlumeModel::modelReset), + makeNativeMethod("modelApply", JFlumeModel::modelApply), + makeNativeMethod("modelPush", JFlumeModel::modelPush), + makeNativeMethod("modelInvalidate", JFlumeModel::modelInvalidate), + makeNativeMethod("modelStart", JFlumeModel::modelStart), + makeNativeMethod("modelItemLength", JFlumeModel::modelItemLength), + makeNativeMethod("modelContentLength", JFlumeModel::modelContentLength), + makeNativeMethod("modelEstimate", JFlumeModel::modelEstimate), + makeNativeMethod("modelLanding", JFlumeModel::modelLanding), + }); + } +}; + +} // namespace + +void registerModelNatives() { JFlumeModel::registerNatives(); } + +} // namespace flume::android diff --git a/android/src/main/cpp/FlumeModelJni.h b/android/src/main/cpp/FlumeModelJni.h new file mode 100644 index 0000000..59c806e --- /dev/null +++ b/android/src/main/cpp/FlumeModelJni.h @@ -0,0 +1,8 @@ +#pragma once + +namespace flume::android { + +// Registers the offset model natives of me.sectiontn.flume.FlumeJni. +void registerModelNatives(); + +} // namespace flume::android diff --git a/android/src/main/java/me/sectiontn/flume/FlumeJni.kt b/android/src/main/java/me/sectiontn/flume/FlumeJni.kt index 6c290df..5cf69fe 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeJni.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeJni.kt @@ -52,4 +52,45 @@ object FlumeJni { // Binds read the clock and return stats only while this is on. @JvmStatic external fun setStatsEnabled(enabled: Boolean) + + // The offset model of one view, in FlumeModelJni.cpp. The handle lives until modelDrop. + @JvmStatic external fun modelCreate(): Long + + @JvmStatic external fun modelDrop(handle: Long) + + @JvmStatic external fun modelConfigure(handle: Long, floats: FloatArray, ints: IntArray, fullSpan: IntArray) + + @JvmStatic + external fun modelReset(handle: Long, storeId: Int, header: Boolean, dataCount: Int, emptyComponent: Boolean, footer: Boolean) + + @JvmStatic + external fun modelApply( + handle: Long, + storeId: Int, + header: Boolean, + dataCount: Int, + emptyComponent: Boolean, + footer: Boolean, + reset: Boolean, + removed: IntArray, + inserted: IntArray, + moved: IntArray, + changed: IntArray, + fullSpan: IntArray, + ) + + @JvmStatic external fun modelPush(handle: Long, pairs: IntArray, count: Int): Int + + @JvmStatic external fun modelInvalidate(handle: Long) + + @JvmStatic external fun modelStart(handle: Long, position: Int, viewportPx: Int): Double + + @JvmStatic external fun modelItemLength(handle: Long, position: Int): Int + + @JvmStatic external fun modelContentLength(handle: Long, viewportPx: Int): Double + + @JvmStatic external fun modelEstimate(handle: Long, viewType: Int): Int + + // The landing's position and remainder. + @JvmStatic external fun modelLanding(handle: Long, contentOffsetPx: Double, viewportPx: Int): DoubleArray } diff --git a/android/src/main/java/me/sectiontn/flume/OffsetModel.kt b/android/src/main/java/me/sectiontn/flume/OffsetModel.kt new file mode 100644 index 0000000..e28e6c5 --- /dev/null +++ b/android/src/main/java/me/sectiontn/flume/OffsetModel.kt @@ -0,0 +1,132 @@ +package me.sectiontn.flume + +// The list's geometry in px for the offset model. Padding runs along the axis, at the visual top or left first. +data class OffsetModelConfig( + val horizontal: Boolean = false, + val inverted: Boolean = false, + val lanes: Int = 1, + val paddingStartPx: Int = 0, + val paddingEndPx: Int = 0, + val paddingCrossStartPx: Int = 0, + val paddingCrossEndPx: Int = 0, + val estimatedPx: Int = -1, + val floorPx: Int = 0, + val separatorPx: Int = 0, + val mainGapPx: Int = 0, + val gridRowGapPx: Int = 0, + val crossGapPx: Int = 0, + val laneInsetStartPx: Int = 0, + val laneInsetEndPx: Int = 0, + val stackFromEnd: Boolean = false, + val justify: ContentJustify = ContentJustify.Start, + val growFooter: Boolean = false, +) { + // Padding start, end, cross start, cross end, estimate, floor, separator, main gap, grid row gap, column gap, lane insets. + fun floats(): FloatArray = floatArrayOf( + paddingStartPx.toFloat(), paddingEndPx.toFloat(), paddingCrossStartPx.toFloat(), paddingCrossEndPx.toFloat(), + estimatedPx.toFloat(), floorPx.toFloat(), separatorPx.toFloat(), mainGapPx.toFloat(), gridRowGapPx.toFloat(), + crossGapPx.toFloat(), laneInsetStartPx.toFloat(), laneInsetEndPx.toFloat(), + ) + + // Horizontal, inverted, lanes, stack from end, justify as 0 start, 1 center or 2 end, growing footer. + fun ints(): IntArray = intArrayOf( + if (horizontal) 1 else 0, if (inverted) 1 else 0, lanes, if (stackFromEnd) 1 else 0, justify.ordinal, if (growFooter) 1 else 0, + ) +} + +// The first item of the row that holds a content-start offset, and how far past that row's start the offset is. +data class OffsetLanding(val position: Int, val remainderPx: Double) + +// The C++ list model that mirrors what RecyclerView lays out, in px, one per FlumeView. +interface OffsetModel { + fun configure(config: OffsetModelConfig, fullSpanPositions: IntArray) + + fun reset(storeId: Int, rows: ListRows) + + fun apply( + storeId: Int, + rows: ListRows, + reset: Boolean, + removed: IntArray, + inserted: IntArray, + moved: IntArray, + changed: IntArray, + fullSpanPositions: IntArray, + ) + + // Position and length pairs, count pairs long. Returns how many lengths changed. + fun push(pairs: IntArray, count: Int): Int + + fun invalidateLengths() + + // The position's start along the axis in the model's visual frame. + fun startPx(position: Int, viewportPx: Int): Double + + // The item's own length, measured or the estimate the model made it with. + fun itemLengthPx(position: Int): Int + + fun contentLengthPx(viewportPx: Int): Double + + fun estimatePx(viewType: Int): Int + + // An offset counted from the content start in content order. + fun landing(contentOffsetPx: Double, viewportPx: Int): OffsetLanding + + fun drop() +} + +class NativeOffsetModel : OffsetModel { + private var handle = FlumeJni.modelCreate() + + override fun configure(config: OffsetModelConfig, fullSpanPositions: IntArray) { + if (handle != 0L) FlumeJni.modelConfigure(handle, config.floats(), config.ints(), fullSpanPositions) + } + + override fun reset(storeId: Int, rows: ListRows) { + if (handle != 0L) FlumeJni.modelReset(handle, storeId, rows.header, rows.dataCount, rows.emptyComponent, rows.footer) + } + + override fun apply( + storeId: Int, + rows: ListRows, + reset: Boolean, + removed: IntArray, + inserted: IntArray, + moved: IntArray, + changed: IntArray, + fullSpanPositions: IntArray, + ) { + if (handle == 0L) return + FlumeJni.modelApply( + handle, storeId, rows.header, rows.dataCount, rows.emptyComponent, rows.footer, reset, removed, inserted, moved, changed, + fullSpanPositions, + ) + } + + override fun push(pairs: IntArray, count: Int): Int = if (handle == 0L) 0 else FlumeJni.modelPush(handle, pairs, count) + + override fun invalidateLengths() { + if (handle != 0L) FlumeJni.modelInvalidate(handle) + } + + override fun startPx(position: Int, viewportPx: Int): Double = + if (handle == 0L) 0.0 else FlumeJni.modelStart(handle, position, viewportPx) + + override fun itemLengthPx(position: Int): Int = if (handle == 0L) 0 else FlumeJni.modelItemLength(handle, position) + + override fun contentLengthPx(viewportPx: Int): Double = if (handle == 0L) 0.0 else FlumeJni.modelContentLength(handle, viewportPx) + + override fun estimatePx(viewType: Int): Int = if (handle == 0L) 0 else FlumeJni.modelEstimate(handle, viewType) + + override fun landing(contentOffsetPx: Double, viewportPx: Int): OffsetLanding { + if (handle == 0L) return OffsetLanding(0, 0.0) + val out = FlumeJni.modelLanding(handle, contentOffsetPx, viewportPx) + return OffsetLanding(out[0].toInt(), out[1]) + } + + override fun drop() { + if (handle == 0L) return + FlumeJni.modelDrop(handle) + handle = 0L + } +} diff --git a/android/src/test/java/me/sectiontn/flume/OffsetModelConfigTest.kt b/android/src/test/java/me/sectiontn/flume/OffsetModelConfigTest.kt new file mode 100644 index 0000000..dd299ec --- /dev/null +++ b/android/src/test/java/me/sectiontn/flume/OffsetModelConfigTest.kt @@ -0,0 +1,18 @@ +package me.sectiontn.flume + +import org.junit.Assert.assertArrayEquals +import org.junit.Test + +class OffsetModelConfigTest { + @Test + fun packsTheLengthsInTheOrderTheNativeReads() { + val config = OffsetModelConfig(paddingStartPx = 16, separatorPx = 3, justify = ContentJustify.Center) + assertArrayEquals(floatArrayOf(16f, 0f, 0f, 0f, -1f, 0f, 3f, 0f, 0f, 0f, 0f, 0f), config.floats(), 0f) + } + + @Test + fun packsTheFlagsInTheOrderTheNativeReads() { + val config = OffsetModelConfig(horizontal = true, inverted = true, lanes = 2, justify = ContentJustify.Center, growFooter = true) + assertArrayEquals(intArrayOf(1, 1, 2, 0, 1, 1), config.ints()) + } +} diff --git a/cpp/core/ListLayout.cpp b/cpp/core/ListLayout.cpp index e2145de..479ef05 100644 --- a/cpp/core/ListLayout.cpp +++ b/cpp/core/ListLayout.cpp @@ -84,7 +84,14 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi const auto edge = [&] { return anchorEdge(rowOf_[anchor], viewportLength) - (recenters ? slack(viewportLength) : 0); }; const double before = hasAnchor ? edge() : 0; const double contentBefore = contentLength(viewportLength); + record(index, length); + const double moved = hasAnchor ? edge() - before : 0; + return LengthChange{moved, contentLength(viewportLength) - contentBefore}; +} + +bool ListLayout::record(int index, float length) { Item& item = items_[index]; + if (item.measured && item.length == length) return false; Mean& mean = means_[item.type]; if (item.measured) { mean.sum -= item.length; @@ -95,8 +102,33 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi item.length = length; item.measured = true; updateRow(rowOf_[index]); - const double moved = hasAnchor ? edge() - before : 0; - return LengthChange{moved, contentLength(viewportLength) - contentBefore}; + return true; +} + +int ListLayout::setLengths(const int* pairs, int pairCount) { + int changed = 0; + for (int i = 0; i < pairCount; i++) { + const int index = pairs[2 * i]; + if (index >= 0 && index < count() && record(index, static_cast(pairs[2 * i + 1]))) changed++; + } + return changed; +} + +double ListLayout::contentStartOf(int index, float viewportLength) const { + if (index < 0 || index >= count()) return 0; + const int row = rowOf_[index]; + return config_.inverted ? config_.paddingEnd + rowStart(row) : startOf(row, viewportLength); +} + +OffsetLanding ListLayout::landingAt(double contentOffset, float viewportLength) const { + if (rows_.empty()) return OffsetLanding{-1, 0}; + const int rows = static_cast(rows_.size()); + rowStart(rows); + // Inverted content starts at the visual bottom, after the bottom padding, with no slack. + const double lead = config_.inverted ? config_.paddingEnd : config_.paddingStart + slack(viewportLength); + auto it = std::upper_bound(starts_.begin(), starts_.begin() + rows, contentOffset - lead); + const int row = std::max(0, static_cast(it - starts_.begin()) - 1); + return OffsetLanding{rows_[row].first, contentOffset - lead - starts_[row]}; } void ListLayout::invalidateLengths() { diff --git a/cpp/core/ListLayout.h b/cpp/core/ListLayout.h index 561cdfc..b9254e0 100644 --- a/cpp/core/ListLayout.h +++ b/cpp/core/ListLayout.h @@ -62,6 +62,12 @@ struct LengthChange { double contentMoved; // how far the content length moved }; +struct OffsetLanding { + int index; // the first item of the row that holds the offset, -1 with no rows + double remainder; // how far the offset is past that row's start in content order, negative inside the leading padding + bool operator==(const OffsetLanding&) const = default; +}; + // Item positions along the scroll axis from measured lengths and per-type estimates, in the platform units: points on iOS, px on Android. class ListLayout { public: @@ -73,6 +79,11 @@ class ListLayout { std::vector apply(const ListOps& ops, int count, const TypeOf& typeOf, std::vector fullSpan); // The anchor's move is its top edge's, or its bottom edge's when inverted. LengthChange setLength(int index, float length, int anchor, float viewportLength = 0); + // Position and length pairs, with no anchor. Skips a position out of range and an unchanged measured length, returns how many changed. + int setLengths(const int* pairs, int pairCount); + // An offset counted from the content start in content order, which is the visual bottom when inverted. + OffsetLanding landingAt(double contentOffset, float viewportLength) const; + double contentStartOf(int index, float viewportLength) const; // in content order from the content start void invalidateLengths(); int count() const { return static_cast(items_.size()); } int rowCount() const { return static_cast(rows_.size()); } @@ -121,6 +132,7 @@ class ListLayout { }; Item make(int index, const TypeOf& typeOf) const; + bool record(int index, float length); // a measured length and its type's mean, false when nothing changed bool fullSpan(int index) const; void rebuildRows(); void updateRow(int row); diff --git a/cpp/core/ListOpsArrays.cpp b/cpp/core/ListOpsArrays.cpp new file mode 100644 index 0000000..4739aef --- /dev/null +++ b/cpp/core/ListOpsArrays.cpp @@ -0,0 +1,18 @@ +#include "core/ListOpsArrays.h" + +#include + +namespace flume { + +ListOps listOpsFromArrays(bool reset, const std::vector& removed, const std::vector& inserted, + const std::vector& moved, const std::vector& changed) { + ListOps ops; + ops.reset = reset; + for (size_t i = 0; i + 1 < removed.size(); i += 2) ops.removed.push_back({removed[i], removed[i + 1]}); + for (size_t i = 0; i + 1 < inserted.size(); i += 2) ops.inserted.push_back({inserted[i], inserted[i + 1]}); + for (size_t i = 0; i + 1 < moved.size(); i += 2) ops.moved.emplace_back(moved[i], moved[i + 1]); + ops.changed = changed; + return ops; +} + +} // namespace flume diff --git a/cpp/core/ListOpsArrays.h b/cpp/core/ListOpsArrays.h new file mode 100644 index 0000000..7ceee90 --- /dev/null +++ b/cpp/core/ListOpsArrays.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +#include "core/KeyDiff.h" + +namespace flume { + +// The store's ops from the arrays the Android listener sends: index and count pairs, from and to pairs, changed indices. +ListOps listOpsFromArrays(bool reset, const std::vector& removed, const std::vector& inserted, + const std::vector& moved, const std::vector& changed); + +} // namespace flume diff --git a/cpp/tests/list_layout_test.cpp b/cpp/tests/list_layout_test.cpp index accc200..2f63b6b 100644 --- a/cpp/tests/list_layout_test.cpp +++ b/cpp/tests/list_layout_test.cpp @@ -710,3 +710,50 @@ TEST_CASE("positions stay exact past 2^24") { CHECK(layout.contentLength(800) == 20200000.0); CHECK(layout.indexAt(16777413.0 + 50, 800) == 166113); } + +TEST_CASE("setLengths skips unchanged lengths") { + ListLayout layout = list(10); + const int twice[] = {3, 80, 3, 80}; + CHECK(layout.setLengths(twice, 2) == 1); + const int same[] = {3, 80}; + CHECK(layout.setLengths(same, 1) == 0); + const int outside[] = {3, 90, 42, 10}; + CHECK(layout.setLengths(outside, 2) == 1); + CHECK(layout.itemLength(3) == 90); +} + +namespace { + +ListLayoutConfig padded(bool inverted) { + ListLayoutConfig config; + config.estimatedItemLength = 100; + config.paddingStart = 16; + config.paddingEnd = 16; + config.inverted = inverted; + return config; +} + +} // namespace + +TEST_CASE("landing upright with padding") { + ListLayout layout = list(10, padded(false)); + CHECK(layout.landingAt(0, 400) == OffsetLanding{0, -16}); + CHECK(layout.landingAt(16, 400) == OffsetLanding{0, 0}); + CHECK(layout.landingAt(250, 400) == OffsetLanding{2, 34}); + CHECK(layout.landingAt(116, 400) == OffsetLanding{1, 0}); +} + +TEST_CASE("landing inverted with padding") { + ListLayout layout = list(10, padded(true)); + CHECK(layout.landingAt(0, 400) == OffsetLanding{0, -16}); + CHECK(layout.landingAt(250, 400) == OffsetLanding{2, 34}); + CHECK(layout.contentStartOf(2, 400) == 216); +} + +TEST_CASE("landing in a grid") { + ListLayoutConfig config; + config.estimatedItemLength = 100; + config.lanes = 2; + ListLayout layout = list(10, config); + CHECK(layout.landingAt(150, 400) == OffsetLanding{2, 50}); +} diff --git a/cpp/tests/list_ops_arrays_test.cpp b/cpp/tests/list_ops_arrays_test.cpp new file mode 100644 index 0000000..fc2bd31 --- /dev/null +++ b/cpp/tests/list_ops_arrays_test.cpp @@ -0,0 +1,18 @@ +#include "doctest.h" + +#include "core/ListOpsArrays.h" + +using namespace flume; + +TEST_CASE("ops from the arrays the Android listener sends") { + const ListOps ops = listOpsFromArrays(false, {5, 2}, {0, 1}, {3, 1}, {4}); + CHECK_FALSE(ops.reset); + CHECK(ops.removed == std::vector{{5, 2}}); + CHECK(ops.inserted == std::vector{{0, 1}}); + CHECK(ops.moved == std::vector>{{3, 1}}); + CHECK(ops.changed == std::vector{4}); +} + +TEST_CASE("a reset passes through") { + CHECK(listOpsFromArrays(true, {}, {}, {}, {}).reset); +}