Skip to content

Commit

Permalink
chore: refactor adjustVisibleIndex input arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
azimgd committed Nov 25, 2024
1 parent 9a0b53e commit 6bc02f2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 52 deletions.
49 changes: 21 additions & 28 deletions android/src/main/java/com/shadowlist/SLContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,22 @@ public void updateVirtualization() {

MapBuffer stateMapBuffer = mStateWrapper.getStateDataMapBuffer();

float[] scrollPosition = new float[]{
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollX()),
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollY())};

int visibleStartIndex = mChildrenMeasurements.adjustVisibleStartIndex(
mChildrenMeasurements.lowerBound(
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollY())
),
mChildrenMeasurements.lowerBound(mScrollable.getVisibleStartOffset(scrollPosition)),
stateMapBuffer.getInt(SLContainerManager.SLCONTAINER_STATE_CHILDREN_MEASUREMENTS_TREE_SIZE)
);
int visibleEndIndex = mChildrenMeasurements.adjustVisibleEndIndex(
mChildrenMeasurements.lowerBound(
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollY()) + (float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_HEIGHT)
),
mChildrenMeasurements.lowerBound(mScrollable.getVisibleEndOffset(scrollPosition)),
stateMapBuffer.getInt(SLContainerManager.SLCONTAINER_STATE_CHILDREN_MEASUREMENTS_TREE_SIZE)
);
mContainerChildrenManager.mount(
visibleStartIndex,
visibleEndIndex
);
float[] scrollPosition = new float[]{
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollY()),
PixelUtil.toDIPFromPixel(this.mScrollContainerVertical.getScrollY())};

updateObservers(scrollPosition, visibleStartIndex, visibleEndIndex);
}
Expand Down Expand Up @@ -181,6 +178,19 @@ public void setStateWrapper(

mChildrenMeasurements = new SLFenwickTree(childrenMeasurements);

mScrollable.updateState(
stateMapBuffer.getBoolean(SLContainerManager.SLCONTAINER_STATE_HORIZONTAL),
false,
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_WIDTH),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_HEIGHT),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTENT_WIDTH),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTENT_HEIGHT)
);

float[] scrollPosition = new float[]{
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_LEFT) + PixelUtil.toDIPFromPixel(mScrollContainerVertical.getScrollX()),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_TOP) + PixelUtil.toDIPFromPixel(mScrollContainerVertical.getScrollY())};

this.setScrollContainerLayout(
(int)stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_WIDTH),
(int)stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_HEIGHT)
Expand All @@ -196,25 +206,12 @@ public void setStateWrapper(
(int)stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_TOP)
);

mScrollable.updateState(
stateMapBuffer.getBoolean(SLContainerManager.SLCONTAINER_STATE_HORIZONTAL),
false,
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_WIDTH),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_HEIGHT),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTENT_WIDTH),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTENT_HEIGHT)
);

int visibleStartIndex = mChildrenMeasurements.adjustVisibleStartIndex(
mChildrenMeasurements.lowerBound(
(int)stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_TOP)
),
mChildrenMeasurements.lowerBound(mScrollable.getVisibleStartOffset(scrollPosition)),
stateMapBuffer.getInt(SLContainerManager.SLCONTAINER_STATE_CHILDREN_MEASUREMENTS_TREE_SIZE)
);
int visibleEndIndex = mChildrenMeasurements.adjustVisibleEndIndex(
mChildrenMeasurements.lowerBound(
(int)stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_TOP) + (float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_CONTAINER_HEIGHT)
),
mChildrenMeasurements.lowerBound(mScrollable.getVisibleEndOffset(scrollPosition)),
stateMapBuffer.getInt(SLContainerManager.SLCONTAINER_STATE_CHILDREN_MEASUREMENTS_TREE_SIZE)
);

Expand All @@ -223,10 +220,6 @@ public void setStateWrapper(
visibleEndIndex
);

float[] scrollPosition = new float[]{
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_LEFT) + PixelUtil.toDIPFromPixel(mScrollContainerVertical.getScrollX()),
(float) stateMapBuffer.getDouble(SLContainerManager.SLCONTAINER_STATE_SCROLL_POSITION_TOP) + PixelUtil.toDIPFromPixel(mScrollContainerVertical.getScrollY())};

mOnStartReachedHandler = onStartReachedHandler;
mOnEndReachedHandler = onEndReachedHandler;
mOnVisibleChangeHandler = onVisibleChangeHandler;
Expand Down
12 changes: 12 additions & 0 deletions android/src/main/java/com/shadowlist/SLScrollable.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,16 @@ public float[] calculateVelocity() {

return new float[]{0.0f, 0.0f};
}

public float getScrollPosition(float[] scrollPosition) {
return mHorizontal ? scrollPosition[0] : scrollPosition[1];
}

public float getVisibleStartOffset(float[] scrollPosition) {
return getScrollPosition(scrollPosition);
}

public float getVisibleEndOffset(float[] scrollPosition) {
return getScrollPosition(scrollPosition) + mScrollContainerHeight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ float SLContainerState::calculateContentSize() const {
return childrenMeasurementsTree.sum(childrenMeasurementsTree.size());
}

float SLContainerState::getScrollPosition(const Point& scrollPosition) const {
return horizontal ? scrollPosition.x : scrollPosition.y;
}

#ifdef ANDROID
folly::dynamic SLContainerState::childrenMeasurementsTreeToDynamic(SLFenwickTree childrenMeasurementsTree) const {
folly::dynamic childrenMeasurementsNext = folly::dynamic::array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class SLContainerState {

Point calculateScrollPositionOffset(const float visibleStartOffset) const;
float calculateContentSize() const;
float getScrollPosition(const Point& scrollPosition) const;

#ifdef ANDROID
folly::dynamic childrenMeasurementsTreeToDynamic(SLFenwickTree childrenMeasurementsTree) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">

<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
Expand Down
30 changes: 12 additions & 18 deletions ios/SLContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &
const auto &nextStateData = self->_state->getData();
const auto &nextViewProps = *std::static_pointer_cast<SLContainerProps const>(self->_props);

[self->_scrollable updateState:nextStateData.horizontal
inverted:nextViewProps.inverted
scrollContainerWidth:nextStateData.scrollContainer.width
scrollContainerHeight:nextStateData.scrollContainer.height
scrollContentWidth:nextStateData.scrollContent.width
scrollContentHeight:nextStateData.scrollContent.height];

CGPoint scrollPositionCGPoint = CGPointMake(
nextStateData.scrollPosition.x + self->_scrollContent.contentOffset.x,
nextStateData.scrollPosition.y + self->_scrollContent.contentOffset.y
Expand All @@ -89,30 +96,19 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &

CGSize scrollContent = RCTCGSizeFromSize(nextStateData.scrollContent);
int visibleStartIndex = adjustVisibleStartIndex(
nextStateData.childrenMeasurementsTree.lower_bound(
nextStateData.getScrollPosition(scrollPositionPoint)),
nextStateData.childrenMeasurementsTree.lower_bound([self->_scrollable getVisibleStartOffset:scrollPositionCGPoint]),
nextStateData.childrenMeasurementsTree.size()
);
int visibleEndIndex = adjustVisibleEndIndex(
nextStateData.childrenMeasurementsTree.lower_bound(
nextStateData.getScrollPosition(scrollPositionPoint) + nextStateData.scrollContainer.height),
nextStateData.childrenMeasurementsTree.lower_bound([self->_scrollable getVisibleEndOffset:scrollPositionCGPoint]),
nextStateData.childrenMeasurementsTree.size()
);

[self->_containerChildrenManager
mount:visibleStartIndex
visibleEndIndex:visibleEndIndex];
[self->_containerChildrenManager mount:visibleStartIndex visibleEndIndex:visibleEndIndex];

[self->_scrollContent setContentSize:scrollContent];
[self->_scrollContent setContentOffset:scrollPositionCGPoint];

[self->_scrollable updateState:nextStateData.horizontal
inverted:nextViewProps.inverted
scrollContainerWidth:nextStateData.scrollContainer.width
scrollContainerHeight:nextStateData.scrollContainer.height
scrollContentWidth:nextStateData.scrollContent.width
scrollContentHeight:nextStateData.scrollContent.height];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(16 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
[self updateVirtualization];
});
Expand All @@ -133,13 +129,11 @@ - (void)updateVirtualization
facebook::react::Point scrollPositionPoint = RCTPointFromCGPoint(scrollPositionCGPoint);

int visibleStartIndex = adjustVisibleStartIndex(
nextStateData.childrenMeasurementsTree.lower_bound(
nextStateData.getScrollPosition(scrollPositionPoint)),
nextStateData.childrenMeasurementsTree.lower_bound([self->_scrollable getVisibleStartOffset:scrollPositionCGPoint]),
nextStateData.childrenMeasurementsTree.size()
);
int visibleEndIndex = adjustVisibleEndIndex(
nextStateData.childrenMeasurementsTree.lower_bound(
nextStateData.getScrollPosition(scrollPositionPoint) + nextStateData.scrollContainer.height),
nextStateData.childrenMeasurementsTree.lower_bound([self->_scrollable getVisibleEndOffset:scrollPositionCGPoint]),
nextStateData.childrenMeasurementsTree.size()
);

Expand Down
3 changes: 3 additions & 0 deletions ios/SLScrollable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
- (int)shouldNotifyEnd:(CGPoint)contentOffset;
- (int)scrollDirectionHorizontal:(CGPoint)contentOffset;
- (int)scrollDirectionVertical:(CGPoint)contentOffset;
- (float)getScrollPosition:(CGPoint)scrollPosition;
- (float)getVisibleStartOffset:(CGPoint)scrollPosition;
- (float)getVisibleEndOffset:(CGPoint)scrollPosition;

@end
12 changes: 12 additions & 0 deletions ios/SLScrollable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ - (CGPoint)calculateVelocity {
return CGPointZero;
}

- (float)getScrollPosition:(CGPoint)scrollPosition {
return self->_horizontal ? scrollPosition.x : scrollPosition.y;
}

- (float)getVisibleStartOffset:(CGPoint)scrollPosition {
return [self getScrollPosition:scrollPosition];
}

- (float)getVisibleEndOffset:(CGPoint)scrollPosition {
return [self getScrollPosition:scrollPosition] + self->_scrollContainerHeight;
}

@end

0 comments on commit 6bc02f2

Please sign in to comment.