Skip to content

feat: Add onScrollBeginDrag and BeginRefresh properties and native implementation #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
8 changes: 8 additions & 0 deletions harmony/spring_scrollview/src/main/cpp/EventEmitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,13 @@ void RNCSpringScrollViewEventEmitter::onContentSizeChange(OnContentSizeChange ev
return payload;
});
}

void RNCSpringScrollViewEventEmitter::onScrollBeginDrag() const {
dispatchEvent("scrollBeginDrag", [](jsi::Runtime &runtime) {
auto payload = jsi::Object(runtime);
return payload;
});
}

} // namespace react
} // namespace facebook
1 change: 1 addition & 0 deletions harmony/spring_scrollview/src/main/cpp/EventEmitters.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class JSI_EXPORT RNCSpringScrollViewEventEmitter : public ViewEventEmitter {
void onScroll(OnScroll value) const;
void onSizeChange(OnSizeChange value) const;
void onContentSizeChange(OnContentSizeChange value) const;
void onScrollBeginDrag() const;
};
} // namespace react
} // namespace facebook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,14 @@ void SpringScrollViewComponentInstance::handleCommand(std::string const &command
this->getLocalRootArkUINode().endLoading();
this->firstLoad = false;
} else if (commandName == "10002") {
if (args[0] == nullptr) {
this->getLocalRootArkUINode().scrollTo(0, args[1].asDouble(), args[2].asBool());
return;
}
this->getLocalRootArkUINode().scrollTo(args[0].asDouble(), args[1].asDouble(), args[2].asBool());
}
}

void SpringScrollViewComponentInstance::setLayout(facebook::react::LayoutMetrics layoutMetrics) {
CppComponentInstance::setLayout(layoutMetrics);
DLOG(INFO) << "SpringScrollViewComponentInstance setLayout w:" << layoutMetrics.frame.size.width
<< " h:" << layoutMetrics.frame.size.height;
}

facebook::react::Size SpringScrollViewComponentInstance::getLayoutSize() { return this->m_layoutMetrics.frame.size; }

Expand All @@ -315,4 +314,10 @@ void SpringScrollViewComponentInstance::finalizeUpdates() {
DLOG(INFO) << "SpringScrollViewComponentInstance finalizeUpdates";
this->getLocalRootArkUINode().init();
}

void SpringScrollViewComponentInstance::onScrollBeginDrag() {
DLOG(INFO) << "onScrollBeginDrag";
m_eventEmitter->onScrollBeginDrag();
};

} // namespace rnoh
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ class SpringScrollViewComponentInstance : public CppComponentInstance<facebook::
void callArkTSScrollYStart(float from, float to, long duration) override;
void callArkTSAnimationCancel() override;
void handleCommand(std::string const &commandName, folly::dynamic const &args) override;
void setLayout(facebook::react::LayoutMetrics layoutMetrics) override;
facebook::react::Size getLayoutSize() override;
void onStateChanged(SharedConcreteState const &state) override;
bool isHandlingTouches() const override;
void setSwiperStatus(bool swiperStatus) override;
void finalizeUpdates() override;
void onScrollBeginDrag() override;

};
} // namespace rnoh
#endif // HARMONY_SpringScrollViewCOMPONENTINSTANCE_H
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class SpringScrollViewJSIBinder : public ViewComponentJSIBinder {
events.setProperty(rt, "topMomentumScrollBegin", createDirectEvent(rt, "onMomentumScrollBegin"));
events.setProperty(rt, "topMomentumScrollEnd", createDirectEvent(rt, "onMomentumScrollEnd"));
events.setProperty(rt, "topScroll", createDirectEvent(rt, "onScroll"));
events.setProperty(rt, "topScrollBeginDrag", createDirectEvent(rt, "onScrollBeginDrag"));
return events;
}
};
Expand Down
63 changes: 62 additions & 1 deletion harmony/spring_scrollview/src/main/cpp/SpringScrollViewNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ void SpringScrollViewNode::onMove(ArkUI_GestureEvent *evt) {
drag(lastPoint.x - x, lastPoint.y - y);
lastPoint.x = x;
lastPoint.y = y;
if (this->m_scrollNodeDelegate && !this->scrollBeginDrag) {
this->scrollBeginDrag = true;
this->m_scrollNodeDelegate->onScrollBeginDrag();
}
}

void SpringScrollViewNode::onDown(ArkUI_GestureEvent *evt) {
Expand Down Expand Up @@ -199,6 +203,7 @@ void SpringScrollViewNode::onDown(ArkUI_GestureEvent *evt) {
}

void SpringScrollViewNode::onUp(ArkUI_GestureEvent *evt) {
this->scrollBeginDrag = false;
if(!isMove) return;
this->onMove(evt);
dragging = false;
Expand Down Expand Up @@ -547,7 +552,7 @@ float SpringScrollViewNode::getXDampingCoefficient() {
}

void SpringScrollViewNode ::setContentOffset(float x, float y) {
if(this->isDestory) return;
if(this->isDestory || !m_stackArkUINodeHandle || !m_scrollNodeDelegate) return;
this->contentOffset.x = x;
this->contentOffset.y = y;
this->recordEventModel = std::make_shared<SpringScrollViewEvent>(5);
Expand Down Expand Up @@ -653,6 +658,12 @@ void SpringScrollViewNode ::endRefresh() {
}

void SpringScrollViewNode ::scrollTo(float x, float y, bool animated) {
this->setRecordEventModel();
if(x == BEGIN_REFRESH){
m_scrollNodeDelegate->callArkTSScrollYStart(0, y, 500);
this->beginRefresh();
return;
}
if( x == DISMINATE_KEYBOARD_SHOW_HIDE && this->recordKeyBoardShow) return;
if(y == DISMINATE_KEYBOARD_SHOW_HIDE){
this->recordKeyBoardShow = false;
Expand Down Expand Up @@ -911,4 +922,54 @@ void SpringScrollViewNode::setChildWidth(float width) {

void SpringScrollViewNode::setContentHeight(float height) { this->contentHeight = height; }

void SpringScrollViewNode::beginRefresh() {
this->scrollBeginDrag = false;
dragging = false;
float vy = 0.020;
draggingDirection = "";
if (!momentumScrolling) {
momentumScrolling = true;
this->m_scrollNodeDelegate->onMomentumScrollBegin();
}
refreshStatus = "refreshing";
contentInsets.top = refreshHeaderHeight;
this->recordEventModel = std::make_shared<SpringScrollViewEvent>(5);
this->recordEventModel->setNodeHandle(m_stackArkUINodeHandle);
this->recordEventModel->setEventSpringScrollViewNodeDelegate(this->m_scrollNodeDelegate);
this->recordEventModel->setRefreshStatus(refreshStatus);
this->recordEventModel->setLoadingStatus(loadingStatus);
this->recordEventModel->setEventBounces(bounces);
this->recordEventModel->setEventContentOffset(contentOffset);
this->recordEventModel->setEventSize(size);
this->recordEventModel->setEventContentSize(contentSize);
this->recordEventModel->setEventContentInsets(contentInsets);
this->recordEventModel->setEventBeginPoint(beginPoint);
this->recordEventModel->setEventLastPoint(lastPoint);
this->recordEventModel->setEventMomentumScrolling(momentumScrolling);
auto baseEvent = std::static_pointer_cast<EventBus::Event>(this->recordEventModel);
EventBus::EventBus::getInstance()->setEvent(baseEvent);
this->beginOuterAnimation(vy);
}


void SpringScrollViewNode::setRecordEventModel() {
auto recordEvent = std::static_pointer_cast<SpringScrollViewEvent>(EventBus::EventBus::getInstance()->getEvent());
this->contentOffset = recordEvent->getEventContentOffset();
this->recordEventModel = std::make_shared<SpringScrollViewEvent>(5);
this->recordEventModel->setNodeHandle(m_stackArkUINodeHandle);
this->recordEventModel->setEventSpringScrollViewNodeDelegate(this->m_scrollNodeDelegate);
this->recordEventModel->setRefreshStatus(refreshStatus);
this->recordEventModel->setLoadingStatus(loadingStatus);
this->recordEventModel->setEventBounces(bounces);
this->recordEventModel->setEventContentOffset(contentOffset);
this->recordEventModel->setEventSize(size);
this->recordEventModel->setEventContentSize(contentSize);
this->recordEventModel->setEventContentInsets(contentInsets);
this->recordEventModel->setEventBeginPoint(beginPoint);
this->recordEventModel->setEventLastPoint(lastPoint);
this->recordEventModel->setEventMomentumScrolling(momentumScrolling);
auto baseEvent = std::static_pointer_cast<EventBus::Event>(this->recordEventModel);
EventBus::EventBus::getInstance()->setEvent(baseEvent);
}

} // namespace rnoh
5 changes: 5 additions & 0 deletions harmony/spring_scrollview/src/main/cpp/SpringScrollViewNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SpringScrollViewNodeDelegate {
virtual void callArkTSAnimationCancel(){};
virtual facebook::react::Size getLayoutSize(){};
virtual void setSwiperStatus(bool swiperStatus){};
virtual void onScrollBeginDrag(){};
};

class SpringScrollViewNode : public ArkUINode, public EventBus::EventHandler<SpringScrollViewEvent> {
Expand Down Expand Up @@ -143,6 +144,8 @@ class SpringScrollViewNode : public ArkUINode, public EventBus::EventHandler<Spr
bool recordKeyBoardShow = false;
std::chrono::high_resolution_clock::time_point keyboardHideLastTime;
const int DISMINATE_KEYBOARD_SHOW_HIDE = 99999;
const int BEGIN_REFRESH = 10000;
bool scrollBeginDrag = false;
bool cancelAllAnimations();
void onMove(ArkUI_GestureEvent *evt);
void onDown(ArkUI_GestureEvent *evt);
Expand Down Expand Up @@ -181,5 +184,7 @@ class SpringScrollViewNode : public ArkUINode, public EventBus::EventHandler<Spr
void beginInnerAnimation(float initialVelocity);
void beginReboundAnimation();
void beginHorizontalReboundAnimation();
void beginRefresh();
void setRecordEventModel();
};
} // namespace rnoh
26 changes: 20 additions & 6 deletions src/SpringScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
ViewStyle,
ScrollView
} from "react-native";
import * as TextInputState from "react-native/lib/TextInputState";
import { FooterStatus } from "./LoadingFooter";
import { NormalHeader } from "./NormalHeader";
import { NormalFooter } from "./NormalFooter";
Expand All @@ -48,6 +47,7 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
_loadingStatus: FooterStatus = "waiting";
_indicatorAnimation;
_nativeOffset;
_contentOffset: Offset = { x: 0, y: 0 };

constructor(props: SpringScrollViewPropType) {
super(props);
Expand Down Expand Up @@ -112,6 +112,7 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
onLayout={this._onWrapperLayoutChange}
onTouchBegin={(Platform.OS === "android" || Platform.OS === "harmony") && this._onTouchBegin}
onTouchStart={Platform.OS === "ios" && this._onTouchBegin}
onScrollBeginDrag={this._onScrollBeginDrag}
onMomentumScrollEnd={this._onMomentumScrollEnd}
scrollEventThrottle={1}
onNativeContentOffsetExtract={this._nativeOffset}
Expand Down Expand Up @@ -285,9 +286,6 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
input.current.measure((x, y, w, h, l, t) => {
this._keyboardHeight = t + h - evt.endCoordinates.screenY + this.props.inputToolBarHeight;
if (Platform.OS === "harmony"){
if(this._keyboardHeight > 450){
this._keyboardHeight = this._keyboardHeight - 50;
}
this._keyboardHeight > 0 && this.scroll({ x: 99999, y: this._keyboardHeight });
}
else {
Expand Down Expand Up @@ -330,6 +328,7 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
}

_onScroll = e => {
this._contentOffset = { x, y };
const {
contentOffset: { x, y },
refreshStatus,
Expand Down Expand Up @@ -516,8 +515,6 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
};

_onTouchBegin = () => {
if (TextInputState.currentlyFocusedField())
TextInputState.blurTextInput(TextInputState.currentlyFocusedField());
this.props.tapToHideKeyboard && Keyboard.dismiss();
this.props.onTouchBegin && this.props.onTouchBegin();
};
Expand All @@ -527,6 +524,22 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
this.props.onMomentumScrollEnd && this.props.onMomentumScrollEnd();
};

_onScrollBeginDrag = () => {
if (this.props.dragToHideKeyboard) Keyboard.dismiss();
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag();
};

beginRefresh() {
if (!this.props.loadingFooter || this.props.loadingFooter.height <= 0)
return Promise.reject(
"SpringScrollView: call beginRefresh without loadingFooter or loadingFooter height"
);
return this.scrollTo({
x: 10000,
y: -this.props.loadingFooter.height - 1,
});
}

static defaultProps = {
bounces: true,
scrollEnabled: true,
Expand All @@ -535,6 +548,7 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
textInputRefs: [],
inputToolBarHeight: 44,
tapToHideKeyboard: true,
dragToHideKeyboard: true,
initOffset: { x: 0, y: 0 },
keyboardShouldPersistTaps: "always",
showsVerticalScrollIndicator: true,
Expand Down
11 changes: 10 additions & 1 deletion src/Types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface Offset {
y: number
}

export interface Size {
width: number,
height: number
}

export interface NativeContentOffset {
x?: Animated.Value,
y?: Animated.Value
Expand Down Expand Up @@ -56,6 +61,9 @@ export interface SpringScrollViewPropType extends ViewProps {
textInputRefs?: any[],
inputToolBarHeight?: number,
tapToHideKeyboard?: boolean,
pagingEnabled?: boolean,
pageSize?: Size,
dragToHideKeyboard?: boolean,
onTouchBegin?: () => any,
onTouchEnd?: () => any,
onTouchFinish?: () => any,
Expand All @@ -66,5 +74,6 @@ export interface SpringScrollViewPropType extends ViewProps {
keyboardShouldPersistTaps?: "always"|"never"|"handled",
onNativeContentOffsetExtract?: NativeContentOffset,
onSizeChange?: ({ width: number, height: number }) => any,
onContentSizeChange?: ({ width: number, height: number }) => any
onContentSizeChange?: ({ width: number, height: number }) => any,
onScrollBeginDrag?: () => any
}
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ declare module "react-native-spring-scrollview" {
textInputRefs?: any[];
inputToolBarHeight?: number;
tapToHideKeyboard?: boolean;
pagingEnabled?: boolean;
pageSize?: Size;
dragToHideKeyboard?: boolean;
onTouchBegin?: () => any;
onTouchEnd?: () => any;
onTouchFinish?: () => any;
Expand All @@ -113,6 +116,7 @@ declare module "react-native-spring-scrollview" {
onNativeContentOffsetExtract?: NativeContentOffset;
onSizeChange?: (size:Size) => any;
onContentSizeChange?: (size: Size) => any;
onScrollBeginDrag?: () => any;
}
export class SpringScrollView extends React.PureComponent<SpringScrollViewPropType> {
scrollTo(offset: Offset, animated?: boolean): Promise<void>;
Expand All @@ -121,5 +125,6 @@ declare module "react-native-spring-scrollview" {
scrollToEnd(animated?: boolean): Promise<void>;
endRefresh(): void;
endLoading(): void;
beginRefresh(): Promise<any>;
}
}