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
6 changes: 5 additions & 1 deletion android/src/main/cpp/FlumeJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -143,5 +144,8 @@ struct JFlumeJni : jni::JavaClass<JFlumeJni> {
} // 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();
});
}
185 changes: 185 additions & 0 deletions android/src/main/cpp/FlumeModelJni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include "FlumeModelJni.h"

#include <fbjni/fbjni.h>

#include <algorithm>
#include <cmath>
#include <vector>

#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<int> fullSpan;
};

ViewModel& modelOf(jlong handle) { return *reinterpret_cast<ViewModel*>(handle); }

std::vector<int> intsOf(jni::alias_ref<jni::JArrayInt> array) {
if (!array) return {};
const auto size = array->size();
auto region = array->getRegion(0, size);
return std::vector<int>(region.get(), region.get() + size);
}

std::vector<float> floatsOf(jni::alias_ref<jni::JArrayFloat> array) {
if (!array) return {};
const auto size = array->size();
auto region = array->getRegion(0, size);
return std::vector<float>(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<JFlumeModel> {
static constexpr auto kJavaDescriptor = "Lme/sectiontn/flume/FlumeJni;";

static jlong modelCreate(jni::alias_ref<jclass>) { return reinterpret_cast<jlong>(new ViewModel()); }

static void modelDrop(jni::alias_ref<jclass>, jlong handle) { delete reinterpret_cast<ViewModel*>(handle); }

// The order of OffsetModelConfig.floats and ints.
static void modelConfigure(jni::alias_ref<jclass>, jlong handle, jni::alias_ref<jni::JArrayFloat> floats,
jni::alias_ref<jni::JArrayInt> ints, jni::alias_ref<jni::JArrayInt> fullSpan) {
ViewModel& model = modelOf(handle);
const std::vector<float> f = floatsOf(floats);
const std::vector<int> 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<ListLayoutConfig::Justify>(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<jclass>, 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<jclass>, jlong handle, jint storeId, jboolean header, jint dataCount,
jboolean emptyComponent, jboolean footer, jboolean reset, jni::alias_ref<jni::JArrayInt> removed,
jni::alias_ref<jni::JArrayInt> inserted, jni::alias_ref<jni::JArrayInt> moved,
jni::alias_ref<jni::JArrayInt> changed, jni::alias_ref<jni::JArrayInt> 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<jclass>, jlong handle, jni::alias_ref<jni::JArrayInt> pairs, jint count) {
const std::vector<int> values = intsOf(pairs);
const int pairCount = std::min(static_cast<int>(count), static_cast<int>(values.size() / 2));
return modelOf(handle).layout.setLengths(values.data(), pairCount);
}

static void modelInvalidate(jni::alias_ref<jclass>, jlong handle) { modelOf(handle).layout.invalidateLengths(); }

static jdouble modelStart(jni::alias_ref<jclass>, jlong handle, jint position, jint viewportPx) {
ViewModel& model = modelOf(handle);
const ItemRect rect = model.layout.rectOf(position, 0, static_cast<float>(viewportPx));
return model.config.horizontal ? rect.x : rect.y;
}

static jint modelItemLength(jni::alias_ref<jclass>, jlong handle, jint position) {
const ListLayout& layout = modelOf(handle).layout;
if (position < 0 || position >= layout.count()) return 0;
return static_cast<jint>(std::lround(layout.itemLength(position)));
}

static jdouble modelContentLength(jni::alias_ref<jclass>, jlong handle, jint viewportPx) {
return modelOf(handle).layout.contentLength(static_cast<float>(viewportPx));
}

static jint modelEstimate(jni::alias_ref<jclass>, jlong handle, jint viewType) {
return static_cast<jint>(std::lround(modelOf(handle).layout.estimate(viewType)));
}

static jni::local_ref<jni::JArrayDouble> modelLanding(jni::alias_ref<jclass>, jlong handle, jdouble contentOffsetPx,
jint viewportPx) {
const OffsetLanding landing = modelOf(handle).layout.landingAt(contentOffsetPx, static_cast<float>(viewportPx));
auto out = jni::JArrayDouble::newArray(2);
const jdouble values[] = {static_cast<jdouble>(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
8 changes: 8 additions & 0 deletions android/src/main/cpp/FlumeModelJni.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace flume::android {

// Registers the offset model natives of me.sectiontn.flume.FlumeJni.
void registerModelNatives();

} // namespace flume::android
41 changes: 41 additions & 0 deletions android/src/main/java/me/sectiontn/flume/FlumeJni.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
132 changes: 132 additions & 0 deletions android/src/main/java/me/sectiontn/flume/OffsetModel.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading