Skip to content

Commit 39a6de2

Browse files
committed
feat:autoRefresh Indicates the automatic property refresh function
Signed-off-by: tyBrave <tianyong21@h-partners.com>
1 parent 23f24ba commit 39a6de2

File tree

7 files changed

+104
-11
lines changed

7 files changed

+104
-11
lines changed

harmony/smart_refresh_layout/src/main/cpp/Animation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ class Animation {
8888
state_ = Animation_State::ANIMATION_FREE;
8989
// 等待线程结束
9090
if (updateThread_ != nullptr) {
91-
updateThread_->join();
91+
if (updateThread_->joinable()) {
92+
updateThread_->join();
93+
}
9294
delete updateThread_; // 如果使用new分配,则需要delete释放
9395
updateThread_ = nullptr;
9496
}

harmony/smart_refresh_layout/src/main/cpp/PullToRefreshNode.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace rnoh {
1414
m_pullToRefreshNodeDelegate = pullToRefreshNodeDelegate;
1515
}
1616

17+
void PullToRefreshNode::onNodeEvent(ArkUI_NodeEventType eventType, EventArgs &eventArgs) {
18+
if (eventType == ArkUI_NodeEventType::NODE_EVENT_ON_APPEAR) {
19+
m_pullToRefreshNodeDelegate->onAppArea();
20+
}
21+
}
22+
1723
void PullToRefreshNode::insertChild(ArkUINode &child, std::size_t index) {
1824
if (index == 0) {
1925
m_headerArkUINodeHandle = child.getArkUINodeHandle();

harmony/smart_refresh_layout/src/main/cpp/PullToRefreshNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace rnoh {
1717
virtual void onHeaderReleased(){};
1818
virtual void onReleaseToRefresh(){};
1919
virtual bool isComponentTop(){};
20+
virtual void onAppArea(){};
2021
};
2122

2223
class PullToRefreshNode : public ArkUINode {
@@ -41,6 +42,7 @@ namespace rnoh {
4142
void setMaxTranslate(float maxHeight);
4243
void setHeaderBackgroundColor(facebook::react::SharedColor const &color);
4344
PullToRefreshConfigurator getPullToRefreshConfigurator() { return refreshConfigurator; }
45+
void onNodeEvent(ArkUI_NodeEventType eventType, EventArgs &eventArgs) override;
4446
};
4547

4648
} // namespace rnoh

harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutComponentInstance.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "SmartRefreshLayoutComponentInstance.h"
22
#include <folly/dynamic.h>
3+
#include <bits/alltypes.h>
34
#include "RNOH/arkui/ArkUINode.h"
45
#include "RNOHCorePackage/ComponentInstances/ScrollViewComponentInstance.h"
6+
#include "TaskProcessor.h"
57
#include "react/renderer/graphics/Color.h"
68
#include <arkui/native_interface.h>
79
#include <arkui/native_node.h>
@@ -25,8 +27,26 @@ namespace rnoh {
2527
ArkUI_AttributeItem clipItem = {clipValue, sizeof(clipValue) / sizeof(ArkUI_NumberValue)};
2628
NativeNodeApi::getInstance()->setAttribute(m_headerStackNode.getArkUINodeHandle(), NODE_CLIP, &clipItem);
2729
panGesture(m_pullToRefreshNode.getArkUINodeHandle());
30+
NativeNodeApi::getInstance()->registerNodeEvent(m_pullToRefreshNode.getArkUINodeHandle(), NODE_EVENT_ON_APPEAR,
31+
NODE_EVENT_ON_APPEAR, this);
2832
}
2933

34+
void SmartRefreshLayoutComponentInstance::onAppArea() {
35+
if (this->autoRefresh.refresh) {
36+
int32_t delayTime = (int32_t)autoRefresh.time;
37+
if (delayTime > 0) {
38+
TaskProcessor *taskProcessor = new TaskProcessor();
39+
taskProcessor->startDelayTask(static_cast<std::chrono::milliseconds>(delayTime), [this]() {
40+
this->trYTop = this->headerHeight * 2;
41+
this->onActionEnd();
42+
});
43+
} else {
44+
this->trYTop = this->headerHeight * 2;
45+
this->onActionEnd();
46+
}
47+
}
48+
};
49+
3050
void SmartRefreshLayoutComponentInstance::finalizeUpdates() {
3151
setHeaderChildSize();
3252
}
@@ -74,7 +94,7 @@ namespace rnoh {
7494
void SmartRefreshLayoutComponentInstance::panGesture(ArkUI_NodeHandle arkUI_NodeHandle) {
7595
auto anyGestureApi = OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1");
7696
auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>(anyGestureApi);
77-
auto panGesture = gestureApi->createPanGesture(1, GESTURE_DIRECTION_VERTICAL, 5);
97+
auto panGesture = gestureApi->createPanGesture(1, GESTURE_DIRECTION_VERTICAL, 0.1);
7898
auto onPanActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) {
7999
SmartRefreshLayoutComponentInstance *instance = (SmartRefreshLayoutComponentInstance *)extraParam;
80100
if (!instance->m_pullToRefreshNode.getPullToRefreshConfigurator().getHasRefresh()) {
@@ -90,8 +110,6 @@ namespace rnoh {
90110
instance->offsetY = OH_ArkUI_PanGesture_GetOffsetY(event) - instance->downY;
91111
if (instance->offsetY >= 0) {
92112
instance->onActionUpdate();
93-
} else {
94-
instance->onActionEnd();
95113
}
96114
} else if (actionType == GESTURE_EVENT_ACTION_END) {
97115
instance->onActionEnd();
@@ -129,8 +147,7 @@ namespace rnoh {
129147
// 可释放刷新时触发
130148
this->onReleaseToRefresh();
131149
}
132-
if (trY > 0) {
133-
this->onHeaderPulling(trYTop);
150+
if (trYTop >= 0) {
134151
this->changeStatus();
135152
}
136153
}
@@ -210,7 +227,6 @@ namespace rnoh {
210227
m_pullToRefreshNode.markDirty();
211228
this->changeStatus();
212229
}
213-
onHeaderReleasing(trYTop);
214230
});
215231
if (animation->GetAnimationStatus() == ANIMATION_FREE || animation->GetAnimationStatus() == ANIMATION_FINISH) {
216232
animation->Start();
@@ -276,7 +292,13 @@ namespace rnoh {
276292
} else {
277293
m_pullToRefreshNode.setHeaderHeight(h);
278294
}
279-
onHeaderMoving(h);
295+
// 下拉中
296+
if (oldHeaderTop < h) {
297+
onHeaderPulling(h);
298+
} else if (oldHeaderTop > h) {
299+
onHeaderReleasing(h);
300+
}
301+
oldHeaderTop = h;
280302
}
281303

282304
void SmartRefreshLayoutComponentInstance::onHeaderPulling(const float &displayedHeaderHeight) {

harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutComponentInstance.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace rnoh {
3737
int32_t state{IS_FREE};
3838
int32_t touchYOld{0};
3939
int32_t touchYNew{0};
40+
float oldHeaderTop{0.0};
4041
int32_t downY{0}; // first down touch on Y
4142
int32_t offsetY{0}; // pan offset on Y
4243
Animation *animation{nullptr};
@@ -74,7 +75,8 @@ namespace rnoh {
7475
void onPullDownToRefresh() override;
7576
void onReleaseToRefresh() override;
7677
void onHeaderReleased() override;
77-
78+
void onAppArea() override;
79+
7880
void handleCommand(std::string const &commandName, folly::dynamic const &args) override;
7981

8082

harmony/smart_refresh_layout/src/main/cpp/SmartRefreshState.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@
55
// please include "napi/native_api.h".
66

77
#include <string>
8-
enum { IS_FREE, IS_PULL_DOWN_1, IS_PULL_DOWN_2, IS_REFRESHING, IS_REFRESHED, IS_PULL_UP_1, IS_PULL_UP_2, IS_LOADING };
9-
const std::string MOMENTS[]={"中午", "凌晨", "早上", "下午", "晚上"};
8+
typedef enum {
9+
IS_FREE = 0x01,
10+
IS_PULL_DOWN_1 = 0x02,
11+
IS_PULL_DOWN_2 = 0x03,
12+
IS_REFRESHING = 0x04,
13+
IS_REFRESHED = 0x05,
14+
IS_PULL_UP_1 = 0x06,
15+
IS_PULL_UP_2 = 0x07,
16+
IS_LOADING = 0x08,
17+
} Smart_Status;
18+
const std::string MOMENTS[] = {"中午", "凌晨", "早上", "下午", "晚上"};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Created on 2024/4/3.
3+
//
4+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
5+
// please include "napi/native_api.h".
6+
7+
//
8+
// Created on 10/3/2024.
9+
//
10+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
11+
// please include "napi/native_api.h".
12+
13+
#include <thread>
14+
#include <atomic>
15+
#include <chrono>
16+
17+
class TaskProcessor {
18+
public:
19+
TaskProcessor() {}
20+
~TaskProcessor() { cancelTask(); }
21+
22+
void startDelayTask(std::chrono::milliseconds durationSeconds, std::function<void(void)> callback) {
23+
delayTime = durationSeconds;
24+
callback_ = callback;
25+
if (!callback_) {
26+
throw std::invalid_argument("Callback cannot be null");
27+
}
28+
updateThread_ = new std::thread(&TaskProcessor::task, this);
29+
}
30+
31+
private:
32+
void task() {
33+
std::this_thread::sleep_for(delayTime);
34+
callback_();
35+
}
36+
37+
private:
38+
std::chrono::milliseconds delayTime;
39+
std::thread *updateThread_;
40+
std::function<void(void)> callback_;
41+
void cancelTask() {
42+
if (updateThread_ != nullptr) {
43+
if (updateThread_->joinable()) {
44+
updateThread_->join();
45+
}
46+
delete updateThread_;
47+
updateThread_ = nullptr;
48+
}
49+
}
50+
};

0 commit comments

Comments
 (0)