Skip to content

feat:autoRefresh Indicates the automatic property refresh function #23

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
Jun 19, 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
4 changes: 3 additions & 1 deletion harmony/smart_refresh_layout/src/main/cpp/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class Animation {
state_ = Animation_State::ANIMATION_FREE;
// 等待线程结束
if (updateThread_ != nullptr) {
updateThread_->join();
if (updateThread_->joinable()) {
updateThread_->join();
}
delete updateThread_; // 如果使用new分配,则需要delete释放
updateThread_ = nullptr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace rnoh {
m_pullToRefreshNodeDelegate = pullToRefreshNodeDelegate;
}

void PullToRefreshNode::onNodeEvent(ArkUI_NodeEventType eventType, EventArgs &eventArgs) {
if (eventType == ArkUI_NodeEventType::NODE_EVENT_ON_APPEAR) {
m_pullToRefreshNodeDelegate->onAppArea();
}
}

void PullToRefreshNode::insertChild(ArkUINode &child, std::size_t index) {
if (index == 0) {
m_headerArkUINodeHandle = child.getArkUINodeHandle();
Expand Down
2 changes: 2 additions & 0 deletions harmony/smart_refresh_layout/src/main/cpp/PullToRefreshNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace rnoh {
virtual void onHeaderReleased(){};
virtual void onReleaseToRefresh(){};
virtual bool isComponentTop(){};
virtual void onAppArea(){};
};

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

} // namespace rnoh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "SmartRefreshLayoutComponentInstance.h"
#include <folly/dynamic.h>
#include <bits/alltypes.h>
#include "RNOH/arkui/ArkUINode.h"
#include "RNOHCorePackage/ComponentInstances/ScrollViewComponentInstance.h"
#include "TaskProcessor.h"
#include "react/renderer/graphics/Color.h"
#include <arkui/native_interface.h>
#include <arkui/native_node.h>
Expand All @@ -25,8 +27,26 @@ namespace rnoh {
ArkUI_AttributeItem clipItem = {clipValue, sizeof(clipValue) / sizeof(ArkUI_NumberValue)};
NativeNodeApi::getInstance()->setAttribute(m_headerStackNode.getArkUINodeHandle(), NODE_CLIP, &clipItem);
panGesture(m_pullToRefreshNode.getArkUINodeHandle());
NativeNodeApi::getInstance()->registerNodeEvent(m_pullToRefreshNode.getArkUINodeHandle(), NODE_EVENT_ON_APPEAR,
NODE_EVENT_ON_APPEAR, this);
}

void SmartRefreshLayoutComponentInstance::onAppArea() {
if (this->autoRefresh.refresh) {
int32_t delayTime = (int32_t)autoRefresh.time;
if (delayTime > 0) {
TaskProcessor *taskProcessor = new TaskProcessor();
taskProcessor->startDelayTask(static_cast<std::chrono::milliseconds>(delayTime), [this]() {
this->trYTop = this->headerHeight * 2;
this->onActionEnd();
});
} else {
this->trYTop = this->headerHeight * 2;
this->onActionEnd();
}
}
};

void SmartRefreshLayoutComponentInstance::finalizeUpdates() {
setHeaderChildSize();
}
Expand Down Expand Up @@ -74,7 +94,7 @@ namespace rnoh {
void SmartRefreshLayoutComponentInstance::panGesture(ArkUI_NodeHandle arkUI_NodeHandle) {
auto anyGestureApi = OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1");
auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>(anyGestureApi);
auto panGesture = gestureApi->createPanGesture(1, GESTURE_DIRECTION_VERTICAL, 5);
auto panGesture = gestureApi->createPanGesture(1, GESTURE_DIRECTION_VERTICAL, 0.1);
auto onPanActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) {
SmartRefreshLayoutComponentInstance *instance = (SmartRefreshLayoutComponentInstance *)extraParam;
if (!instance->m_pullToRefreshNode.getPullToRefreshConfigurator().getHasRefresh()) {
Expand All @@ -90,8 +110,6 @@ namespace rnoh {
instance->offsetY = OH_ArkUI_PanGesture_GetOffsetY(event) - instance->downY;
if (instance->offsetY >= 0) {
instance->onActionUpdate();
} else {
instance->onActionEnd();
}
} else if (actionType == GESTURE_EVENT_ACTION_END) {
instance->onActionEnd();
Expand Down Expand Up @@ -129,8 +147,7 @@ namespace rnoh {
// 可释放刷新时触发
this->onReleaseToRefresh();
}
if (trY > 0) {
this->onHeaderPulling(trYTop);
if (trYTop >= 0) {
this->changeStatus();
}
}
Expand Down Expand Up @@ -210,7 +227,6 @@ namespace rnoh {
m_pullToRefreshNode.markDirty();
this->changeStatus();
}
onHeaderReleasing(trYTop);
});
if (animation->GetAnimationStatus() == ANIMATION_FREE || animation->GetAnimationStatus() == ANIMATION_FINISH) {
animation->Start();
Expand Down Expand Up @@ -276,7 +292,13 @@ namespace rnoh {
} else {
m_pullToRefreshNode.setHeaderHeight(h);
}
onHeaderMoving(h);
// 下拉中
if (oldHeaderTop < h) {
onHeaderPulling(h);
} else if (oldHeaderTop > h) {
onHeaderReleasing(h);
}
oldHeaderTop = h;
}

void SmartRefreshLayoutComponentInstance::onHeaderPulling(const float &displayedHeaderHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace rnoh {
int32_t state{IS_FREE};
int32_t touchYOld{0};
int32_t touchYNew{0};
float oldHeaderTop{0.0};
int32_t downY{0}; // first down touch on Y
int32_t offsetY{0}; // pan offset on Y
Animation *animation{nullptr};
Expand Down Expand Up @@ -74,7 +75,8 @@ namespace rnoh {
void onPullDownToRefresh() override;
void onReleaseToRefresh() override;
void onHeaderReleased() override;

void onAppArea() override;

void handleCommand(std::string const &commandName, folly::dynamic const &args) override;


Expand Down
13 changes: 11 additions & 2 deletions harmony/smart_refresh_layout/src/main/cpp/SmartRefreshState.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
// please include "napi/native_api.h".

#include <string>
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 };
const std::string MOMENTS[]={"中午", "凌晨", "早上", "下午", "晚上"};
typedef enum {
IS_FREE = 0x01,
IS_PULL_DOWN_1 = 0x02,
IS_PULL_DOWN_2 = 0x03,
IS_REFRESHING = 0x04,
IS_REFRESHED = 0x05,
IS_PULL_UP_1 = 0x06,
IS_PULL_UP_2 = 0x07,
IS_LOADING = 0x08,
} Smart_Status;
const std::string MOMENTS[] = {"中午", "凌晨", "早上", "下午", "晚上"};
50 changes: 50 additions & 0 deletions harmony/smart_refresh_layout/src/main/cpp/TaskProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created on 2024/4/3.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

//
// Created on 10/3/2024.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <thread>
#include <atomic>
#include <chrono>

class TaskProcessor {
public:
TaskProcessor() {}
~TaskProcessor() { cancelTask(); }

void startDelayTask(std::chrono::milliseconds durationSeconds, std::function<void(void)> callback) {
delayTime = durationSeconds;
callback_ = callback;
if (!callback_) {
throw std::invalid_argument("Callback cannot be null");
}
updateThread_ = new std::thread(&TaskProcessor::task, this);
}

private:
void task() {
std::this_thread::sleep_for(delayTime);
callback_();
}

private:
std::chrono::milliseconds delayTime;
std::thread *updateThread_;
std::function<void(void)> callback_;
void cancelTask() {
if (updateThread_ != nullptr) {
if (updateThread_->joinable()) {
updateThread_->join();
}
delete updateThread_;
updateThread_ = nullptr;
}
}
};