Skip to content

Commit 8e878e0

Browse files
authored
fix:Fix the problem of sliding not following hands (#5)
1 parent 9aac948 commit 8e878e0

File tree

5 files changed

+69
-162
lines changed

5 files changed

+69
-162
lines changed

harmony/spring_scrollview/src/main/cpp/ShadowNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace facebook {
2929
namespace react {
30-
extern const char RNCSpringScrollViewComponentName[] = "RNCSpringScrollView";
30+
extern const char RNCSpringScrollViewComponentName[] = "SpringScrollView";
3131

3232
void RNCSpringScrollViewShadowNode::updateStateIfNeeded() {
3333
ensureUnsealed();

harmony/spring_scrollview/src/main/cpp/SpringScrollViewNode.cpp

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SpringScrollViewNode::SpringScrollViewNode()
3434
: ArkUINode(NativeNodeApi::getInstance()->createNode(ArkUI_NodeType::ARKUI_NODE_STACK)),
3535
m_stackArkUINodeHandle(nullptr) {}
3636
SpringScrollViewNode::~SpringScrollViewNode() {
37+
this->isDestory = true;
38+
m_scrollNodeDelegate->callArkTSAnimationCancel();
3739
this->isInitialContentOffset = false;
3840
this->recordEventModel = std::make_shared<SpringScrollViewEvent>(5);
3941
this->recordEventModel->setRefreshStatus("waiting");
@@ -112,27 +114,30 @@ void SpringScrollViewNode::registerPanGesture() {
112114
float y = OH_ArkUI_PanGesture_GetOffsetY(event);
113115

114116
if (actionType == GESTURE_EVENT_ACTION_ACCEPT) {
115-
DLOG(INFO) << "SpringScrollViewNode GESTURE_EVENT_ACTION_ACCEPT x:" << x << " y:" << y;
116-
springScrollViewNode->downX = x;
117-
springScrollViewNode->downY = y;
118-
springScrollViewNode->onDown(event);
117+
DLOG(INFO) << "SpringScrollViewNode GESTURE_EVENT_ACTION_ACCEPT x:" << x << " y:" << y;
118+
springScrollViewNode->downX = x;
119+
springScrollViewNode->downY = y;
120+
springScrollViewNode->onDown(event);
119121
} else if (actionType == GESTURE_EVENT_ACTION_UPDATE) {
120-
DLOG(INFO) << "SpringScrollViewNode GESTURE_EVENT_ACTION_UPDATE x:" << x << " y:" << y;
121-
springScrollViewNode->moveX = x;
122-
springScrollViewNode->moveY = y;
123-
if ((springScrollViewNode->moveX - springScrollViewNode->downX > 1.5) ||
124-
(springScrollViewNode->moveX - springScrollViewNode->downX < -1.5)) {
125-
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(true);
126-
}
127-
if ((springScrollViewNode->moveY - springScrollViewNode->downY > 1.5) ||
128-
(springScrollViewNode->moveY - springScrollViewNode->downY < -1.5)) {
129-
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(true);
130-
}
131-
springScrollViewNode->onMove(event);
122+
DLOG(INFO) << "SpringScrollViewNode GESTURE_EVENT_ACTION_UPDATE x:" << x << " y:" << y;
123+
springScrollViewNode->moveX = x;
124+
springScrollViewNode->moveY = y;
125+
if ((springScrollViewNode->moveX - springScrollViewNode->downX > 1.5) ||
126+
(springScrollViewNode->moveX - springScrollViewNode->downX < -1.5)) {
127+
springScrollViewNode->isMove = true;
128+
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(true);
129+
}
130+
if ((springScrollViewNode->moveY - springScrollViewNode->downY > 1.5) ||
131+
(springScrollViewNode->moveY - springScrollViewNode->downY < -1.5)) {
132+
springScrollViewNode->isMove = true;
133+
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(true);
134+
}
135+
springScrollViewNode->onMove(event);
132136
} else if (actionType == GESTURE_EVENT_ACTION_END) {
133-
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(false);
134-
DLOG(INFO) << "SpringScrollViewNode onUp GESTURE_EVENT_ACTION_END";
135-
springScrollViewNode->onUp(event);
137+
springScrollViewNode->m_scrollNodeDelegate->setSwiperStatus(false);
138+
DLOG(INFO) << "SpringScrollViewNode onUp GESTURE_EVENT_ACTION_END";
139+
springScrollViewNode->onUp(event);
140+
springScrollViewNode->isMove = false;
136141
}
137142
};
138143
this->panGestureApi->setGestureEventTarget(
@@ -142,8 +147,8 @@ void SpringScrollViewNode::registerPanGesture() {
142147
}
143148

144149
void SpringScrollViewNode::onMove(ArkUI_GestureEvent *evt) {
145-
if (!this->scrollEnabled) {
146-
return;
150+
if (!this->scrollEnabled || !isMove) {
151+
return;
147152
}
148153
float x = OH_ArkUI_PanGesture_GetOffsetX(evt);
149154
float y = OH_ArkUI_PanGesture_GetOffsetY(evt);
@@ -153,6 +158,9 @@ void SpringScrollViewNode::onMove(ArkUI_GestureEvent *evt) {
153158
}
154159

155160
void SpringScrollViewNode::onDown(ArkUI_GestureEvent *evt) {
161+
auto recordEvent =
162+
std::static_pointer_cast<SpringScrollViewEvent>(EventBus::EventBus::getInstance()->getEvent());
163+
this->contentOffset = recordEvent->getEventContentOffset();
156164
refreshStatus = "waiting";
157165
loadingStatus = "waiting";
158166
float x = OH_ArkUI_PanGesture_GetOffsetX(evt);
@@ -191,6 +199,7 @@ void SpringScrollViewNode::onDown(ArkUI_GestureEvent *evt) {
191199
}
192200

193201
void SpringScrollViewNode::onUp(ArkUI_GestureEvent *evt) {
202+
if(!isMove) return;
194203
this->onMove(evt);
195204
dragging = false;
196205
float vy = 0.020;
@@ -538,6 +547,7 @@ float SpringScrollViewNode::getXDampingCoefficient() {
538547
}
539548

540549
void SpringScrollViewNode ::setContentOffset(float x, float y) {
550+
if(this->isDestory) return;
541551
this->contentOffset.x = x;
542552
this->contentOffset.y = y;
543553
this->recordEventModel = std::make_shared<SpringScrollViewEvent>(5);
@@ -550,13 +560,13 @@ void SpringScrollViewNode ::setContentOffset(float x, float y) {
550560
auto baseEvent = std::static_pointer_cast<EventBus::Event>(this->recordEventModel);
551561
EventBus::EventBus::getInstance()->setEvent(baseEvent);
552562
std::array<ArkUI_NumberValue, 3> translateValue = {
553-
ArkUI_NumberValue{.f32 = -this->contentOffset.x}, {.f32 = -this->contentOffset.y}, {.f32 = 0}};
554-
ArkUI_AttributeItem translateItem = {translateValue.data(), translateValue.size()};
563+
ArkUI_NumberValue{.f32 = -this->contentOffset.x}, {.f32 = -this->contentOffset.y}, {.f32 = 0}};
564+
ArkUI_AttributeItem translateItem = {translateValue.data(), translateValue.size()};
555565
NativeNodeApi::getInstance()->setAttribute(m_stackArkUINodeHandle, NODE_TRANSLATE, &translateItem);
556566
facebook::react::RNCSpringScrollViewEventEmitter::OnScroll onScroll = {
557567
{contentOffset.x / 2, contentOffset.y / 2}, refreshStatus, loadingStatus};
558568
m_scrollNodeDelegate->onScroll(onScroll);
559-
DLOG(INFO) << "SpringScrollViewNode registerPanGesture setContentOffset loadingStatus:" << loadingStatus
569+
DLOG(INFO) << "SpringScrollViewNode setContentOffset loadingStatus:" << loadingStatus
560570
<< " refreshStatus:" << refreshStatus << " contentOffset.y " << contentOffset.y << " contentOffset.x "
561571
<< contentOffset.x;
562572
}
@@ -642,21 +652,34 @@ void SpringScrollViewNode ::endRefresh() {
642652
contentOffset.x = 0;
643653
}
644654

645-
void SpringScrollViewNode ::scrollTo(float x, float y, bool animated) {
646-
DLOG(INFO) << "SpringScrollViewNode scrollTo x:" << x << " y:" << y << " animated:" << animated
647-
<< " contentOffset.x:" << contentOffset.x;
648-
cancelAllAnimations();
649-
if (!animated) {
650-
moveToOffset(x, y);
651-
return;
652-
}
653-
m_scrollNodeDelegate->callArkTSScrollYStart(contentOffset.y, y, 500);
654-
if (x != contentOffset.x) {
655-
m_scrollNodeDelegate->callArkTSScrollXStart(contentOffset.x, x, 500);
655+
void SpringScrollViewNode ::scrollTo(float x, float y, bool animated) {
656+
if( x == DISMINATE_KEYBOARD_SHOW_HIDE && this->recordKeyBoardShow) return;
657+
if(y == DISMINATE_KEYBOARD_SHOW_HIDE){
658+
this->recordKeyBoardShow = false;
659+
y = this->recordKeyBoardPositioY;
660+
this->keyboardHideLastTime = std::chrono::high_resolution_clock::now();
661+
}
662+
else if(x == DISMINATE_KEYBOARD_SHOW_HIDE){
663+
auto keyboardShowCurrentTime = std::chrono::high_resolution_clock::now();
664+
std::chrono::duration<double> duration = keyboardShowCurrentTime - this->keyboardHideLastTime;
665+
this->recordKeyBoardShow = true;
666+
if(duration.count() > 1.5) {
667+
this->recordKeyBoardPositioY = contentOffset.y;
668+
}
669+
}
670+
cancelAllAnimations();
671+
if (!animated) {
672+
moveToOffset(x, y);
673+
return;
674+
}
675+
m_scrollNodeDelegate->callArkTSScrollYStart(contentOffset.y, y, 500);
676+
if (x != contentOffset.x && x!=DISMINATE_KEYBOARD_SHOW_HIDE) {
677+
m_scrollNodeDelegate->callArkTSScrollXStart(contentOffset.x, x, 500);
678+
}
656679
}
657-
}
658680

659681
void SpringScrollViewNode::onEvent(std::shared_ptr<SpringScrollViewEvent> &event) {
682+
if(this->isDestory) return;
660683
auto recordEvent = std::static_pointer_cast<SpringScrollViewEvent>(EventBus::EventBus::getInstance()->getEvent());
661684
this->m_stackArkUINodeHandle = recordEvent->getNodeHandle();
662685
this->m_scrollNodeDelegate = (SpringScrollViewNodeDelegate *)recordEvent->getEventSpringScrollViewNodeDelegate();

harmony/spring_scrollview/src/main/cpp/SpringScrollViewNode.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ class SpringScrollViewNode : public ArkUINode, public EventBus::EventHandler<Spr
137137
float moveX = 0;
138138
float contentHeight;
139139
bool isInitialContentOffset = false;
140-
140+
bool isMove = false;
141+
bool isDestory = false;
142+
float recordKeyBoardPositioY = 0;
143+
bool recordKeyBoardShow = false;
144+
std::chrono::high_resolution_clock::time_point keyboardHideLastTime;
145+
const int DISMINATE_KEYBOARD_SHOW_HIDE = 99999;
141146
bool cancelAllAnimations();
142147
void onMove(ArkUI_GestureEvent *evt);
143148
void onDown(ArkUI_GestureEvent *evt);

harmony/spring_scrollview/src/main/cpp/SpringScrollViewNodeEvent.h

Lines changed: 0 additions & 121 deletions
This file was deleted.

harmony/spring_scrollview/src/main/cpp/SpringScrollViewPackage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SpringScrollViewPackageComponentInstanceFactoryDelegate : public Component
4040
using ComponentInstanceFactoryDelegate::ComponentInstanceFactoryDelegate;
4141

4242
ComponentInstance::Shared create(ComponentInstance::Context ctx) override {
43-
if (ctx.componentName == "RNCSpringScrollView") {
43+
if (ctx.componentName == "SpringScrollView") {
4444
return std::make_shared<SpringScrollViewComponentInstance>(std::move(ctx));
4545
}
4646
return nullptr;
@@ -72,13 +72,13 @@ class SpringScrollViewPackage : public Package {
7272

7373
ComponentJSIBinderByString createComponentJSIBinderByName() override {
7474
return {
75-
{"RNCSpringScrollView", std::make_shared<SpringScrollViewJSIBinder>()},
75+
{"SpringScrollView", std::make_shared<SpringScrollViewJSIBinder>()},
7676
};
7777
}
7878

7979
ComponentNapiBinderByString createComponentNapiBinderByName() override {
8080
return {
81-
{"RNCSpringScrollView", std::make_shared<SpringScrollViewNapiBinder>()},
81+
{"SpringScrollView", std::make_shared<SpringScrollViewNapiBinder>()},
8282
};
8383
}
8484

0 commit comments

Comments
 (0)