Skip to content

Commit

Permalink
chore: scrollToIndex and scrollToOffset adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
azimgd committed Nov 25, 2024
1 parent 6bc02f2 commit 0728534
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
16 changes: 16 additions & 0 deletions android/src/main/java/com/shadowlist/SLContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,20 @@ public void run() {
}
}, 16);
}

public void scrollToIndex(int index, boolean animated) {
MapBuffer stateMapBuffer = mStateWrapper.getStateDataMapBuffer();
int headerFooter = 1;
int offset = mChildrenMeasurements.adjustVisibleStartIndex(
(int) mChildrenMeasurements.sum(index + headerFooter),
stateMapBuffer.getInt(SLContainerManager.SLCONTAINER_STATE_CHILDREN_MEASUREMENTS_TREE_SIZE)
);
float[] scrollPosition = mScrollable.getScrollPositionFromOffset(offset);
mScrollContainerVertical.scrollTo((int)PixelUtil.toPixelFromDIP(scrollPosition[0]), (int)PixelUtil.toPixelFromDIP(scrollPosition[1]));
}

public void scrollToOffset(int offset, boolean animated) {
float[] scrollPosition = mScrollable.getScrollPositionFromOffset(offset);
mScrollContainerVertical.scrollTo((int)PixelUtil.toPixelFromDIP(scrollPosition[0]), (int)PixelUtil.toPixelFromDIP(scrollPosition[1]));
}
}
4 changes: 2 additions & 2 deletions android/src/main/java/com/shadowlist/SLContainerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ public void receiveCommand(@NonNull SLContainer view, String commandId, @Nullabl

@Override
public void scrollToIndex(SLContainer view, int index, boolean animated) {

view.scrollToIndex(index, animated);
}

@Override
public void scrollToOffset(SLContainer view, int offset, boolean animated) {

view.scrollToOffset(offset, animated);
}

public static final String NAME = "SLContainer";
Expand Down
4 changes: 4 additions & 0 deletions android/src/main/java/com/shadowlist/SLScrollable.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ public float getVisibleStartOffset(float[] scrollPosition) {
public float getVisibleEndOffset(float[] scrollPosition) {
return getScrollPosition(scrollPosition) + mScrollContainerHeight;
}

public float[] getScrollPositionFromOffset(float scrollOffset) {
return mHorizontal ? new float[]{scrollOffset, 0} : new float[]{0, scrollOffset};
}
}
10 changes: 9 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useCallback } from 'react';
import { useRef, useCallback, useEffect } from 'react';
import { SafeAreaView, StyleSheet, Text, View } from 'react-native';
import type { DirectEventHandler } from 'react-native/Libraries/Types/CodegenTypes';
import {
Expand All @@ -16,6 +16,7 @@ const ITEMS_COUNT = 50;
const IS_INVERTED = false;
const IS_HORIZONTAL = false;
const INITIAL_SCROLL_INDEX = 0;
const FINAL_SCROLL_INDEX = 0;

const ListHeaderComponent = () => (
<View style={styles.static}>
Expand Down Expand Up @@ -43,6 +44,13 @@ export default function App() {
const data = useData({ length: ITEMS_COUNT, inverted: IS_INVERTED });
const ref = useRef<SLContainerRef>(null);

useEffect(() => {
if (!FINAL_SCROLL_INDEX) return;
setTimeout(() => {
ref.current?.scrollToIndex({ index: FINAL_SCROLL_INDEX });
}, 1000);
}, []);

const onStartReached = useCallback<DirectEventHandler<OnStartReached>>(
(event) => {
!IS_INVERTED ? data.loadPrepend() : data.loadAppend();
Expand Down
14 changes: 7 additions & 7 deletions ios/SLContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
- (void)scrollToIndexNativeCommand:(int)index animated:(BOOL)animated
{
auto headerFooter = 1;
auto stateData = _state->getData();
stateData.scrollPosition = stateData.calculateScrollPositionOffset(
stateData.childrenMeasurementsTree.sum(index + headerFooter)
auto nextStateData = self->_state->getData();
auto offset = adjustVisibleStartIndex(
nextStateData.childrenMeasurementsTree.sum(index + headerFooter),
nextStateData.childrenMeasurementsTree.size()
);
self->_state->updateState(std::move(stateData));

[self->_scrollContent setContentOffset:[self->_scrollable getScrollPositionFromOffset:offset] animated:animated];
}

- (void)scrollToOffsetNativeCommand:(int)offset animated:(BOOL)animated
{
auto stateData = _state->getData();
stateData.scrollPosition = stateData.calculateScrollPositionOffset(offset);
self->_state->updateState(std::move(stateData));
[self->_scrollContent setContentOffset:[self->_scrollable getScrollPositionFromOffset:offset] animated:animated];
}

- (void)handleRefresh {
Expand Down
1 change: 1 addition & 0 deletions ios/SLScrollable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
- (float)getScrollPosition:(CGPoint)scrollPosition;
- (float)getVisibleStartOffset:(CGPoint)scrollPosition;
- (float)getVisibleEndOffset:(CGPoint)scrollPosition;
- (CGPoint)getScrollPositionFromOffset:(float)scrollOffset;

@end
3 changes: 3 additions & 0 deletions ios/SLScrollable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ - (float)getVisibleEndOffset:(CGPoint)scrollPosition {
return [self getScrollPosition:scrollPosition] + self->_scrollContainerHeight;
}

- (CGPoint)getScrollPositionFromOffset:(float)scrollOffset {
return self->_horizontal ? CGPointMake(scrollOffset, 0) : CGPointMake(0, scrollOffset);
}
@end

0 comments on commit 0728534

Please sign in to comment.