Skip to content

Commit

Permalink
add Pager call logic and coding standards (#106)
Browse files Browse the repository at this point in the history
* Enhance pager component functionality

* Update: Switch Pager API to use NodeSwiperIndex

* add Pager call logic and coding standards

* Add boundary check for index in Call function, improve code style
  • Loading branch information
wangz2023 authored Jun 13, 2024
1 parent c9198fa commit d00c715
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ArkUINode {
virtual ArkUINode &SetWidth(float width);
virtual ArkUINode &SetHeight(float height);
virtual ArkUINode &SetSizePercent(const HRSize &size);
virtual ArkUINode &SetPercentWidth(float percent);
virtual ArkUINode &SetPercentHeight(float percent);
virtual ArkUINode &SetVisibility(bool visibility);
virtual ArkUINode &SetBackgroundColor(uint32_t color);
virtual ArkUINode &SetOpacity(float opacity);
Expand All @@ -92,6 +94,7 @@ class ArkUINode {
virtual ArkUINode &SetBorderStyle(ArkUI_BorderStyle top, ArkUI_BorderStyle right, ArkUI_BorderStyle bottom, ArkUI_BorderStyle left);
virtual ArkUINode &SetShadow(const HRShadow &shadow);
virtual HRSize GetSize() const;
virtual uint32_t GetTotalChildCount() const;

virtual void OnNodeEvent(ArkUI_NodeEvent *event) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class SwiperNodeDelegate {


class SwiperNode : public ArkUINode {
protected:
protected:
SwiperNodeDelegate *swiperNodeDelegate_ = nullptr;

public:
public:
SwiperNode();
~SwiperNode();

Expand All @@ -62,13 +62,13 @@ class SwiperNode : public ArkUINode {
void InsertChild(ArkUINode &child, int32_t index);
void RemoveChild(ArkUINode &child);

void ShowIndicator(bool show);
void NodeSwiperIndex(int32_t index);
void NodeSwiperSwipeToIndex(int32_t index, int32_t animation);
void NodeSwiperVertical(int32_t direction);
void NodeSwiperPrevMargin(float fValue);
void NodeSwiperNextMargin(float fValue);
void NodeSwiperLoop(int32_t enable);
void SetShowIndicator(bool show);
void SetSwiperIndex(int32_t index);
void SetSwiperSwipeToIndex(int32_t index, int32_t animation);
void SetSwiperVertical(int32_t direction);
void SetSwiperPrevMargin(float fValue);
void SetSwiperNextMargin(float fValue);
void SetSwiperLoop(int32_t enable);
};

} // namespace native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ inline namespace render {
inline namespace native {

class PagerView : public BaseView, public SwiperNodeDelegate {
public:
public:
PagerView(std::shared_ptr<NativeRenderContext> &ctx);
~PagerView();

Expand All @@ -52,8 +52,6 @@ class PagerView : public BaseView, public SwiperNodeDelegate {
void OnTouchIntercept(const int32_t &eventEnum) override;
void OnNodeTouchEvent(const ArkUI_UIInputEvent *inputEvent) override;

void WangzCheck(const HippyValue &value);

void Call(const std::string &method, const std::vector<HippyValue> params,
std::function<void(const HippyValue &result)> callback) override;

Expand All @@ -64,7 +62,7 @@ class PagerView : public BaseView, public SwiperNodeDelegate {
bool disableSwipe_ = true;
bool vertical_ = false;

private:
private:
SwiperNode swiperNode_;
void SendScrollStateChangeEvent(const std::string &state);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ HRSize ArkUINode::GetSize() const {
return HRSize{width, height};
}

uint32_t ArkUINode::GetTotalChildCount() const {
return NativeNodeApi::GetInstance()->getTotalChildCount(nodeHandle_);
}

ArkUINode &ArkUINode::SetSizePercent(const HRSize &size) {
ArkUI_NumberValue widthValue[] = {{size.width}};
ArkUI_AttributeItem widthItem = {widthValue, sizeof(widthValue) / sizeof(ArkUI_NumberValue), nullptr, nullptr};
Expand All @@ -116,6 +120,20 @@ ArkUINode &ArkUINode::SetSizePercent(const HRSize &size) {
return *this;
}

ArkUINode &ArkUINode::SetPercentWidth(float percent) {
ArkUI_NumberValue value[] = {{.f32 = percent}};
ArkUI_AttributeItem item = {value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_WIDTH_PERCENT, &item));
return *this;
}

ArkUINode &ArkUINode::SetPercentHeight(float percent) {
ArkUI_NumberValue value[] = {{.f32 = percent}};
ArkUI_AttributeItem item = {value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_HEIGHT_PERCENT, &item));
return *this;
}

ArkUINode &ArkUINode::SetVisibility(bool visibility) {
ArkUI_NumberValue value[] = {{.i32 = visibility ? ARKUI_VISIBILITY_VISIBLE : ARKUI_VISIBILITY_HIDDEN}};
ArkUI_AttributeItem item = {value, sizeof(value) / sizeof(value), nullptr, nullptr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,47 +108,47 @@ void SwiperNode::RemoveChild(ArkUINode &child) {
MaybeThrow(NativeNodeApi::GetInstance()->removeChild(nodeHandle_, child.GetArkUINodeHandle()));
}

void SwiperNode::ShowIndicator(bool show) {
void SwiperNode::SetShowIndicator(bool show) {
ArkUI_NumberValue value = {.i32 = int32_t(show)};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(
NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_SHOW_INDICATOR, &item));
}

void SwiperNode::NodeSwiperIndex(int32_t index) {
void SwiperNode::SetSwiperIndex(int32_t index) {
ArkUI_NumberValue value = {.i32 = int32_t(index)};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_INDEX, &item));
}

void SwiperNode::NodeSwiperSwipeToIndex(int32_t index, int32_t animation) {
void SwiperNode::SetSwiperSwipeToIndex(int32_t index, int32_t animation) {
ArkUI_NumberValue value[] = {{.i32 = int32_t(index)}, {.i32 = int32_t(animation)}};
ArkUI_AttributeItem item = {value, sizeof(value) / sizeof(ArkUI_NumberValue), nullptr, nullptr};
MaybeThrow(
NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_SWIPE_TO_INDEX, &item));
}

void SwiperNode::NodeSwiperVertical(int32_t direction) {
void SwiperNode::SetSwiperVertical(int32_t direction) {
ArkUI_NumberValue value = {.i32 = int32_t(direction)};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_VERTICAL, &item));
}

void SwiperNode::NodeSwiperPrevMargin(float fValue) {
void SwiperNode::SetSwiperPrevMargin(float fValue) {
ArkUI_NumberValue value = {.f32 = fValue};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(
NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_PREV_MARGIN, &item));
}

void SwiperNode::NodeSwiperNextMargin(float fValue) {
void SwiperNode::SetSwiperNextMargin(float fValue) {
ArkUI_NumberValue value = {.f32 = fValue};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(
NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_NEXT_MARGIN, &item));
}

void SwiperNode::NodeSwiperLoop(int32_t enable) {
void SwiperNode::SetSwiperLoop(int32_t enable) {
ArkUI_NumberValue value = {.i32 = enable};
ArkUI_AttributeItem item = {&value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_SWIPER_LOOP, &item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ inline namespace native {

PagerView::PagerView(std::shared_ptr<NativeRenderContext> &ctx) : BaseView(ctx) {
swiperNode_.SetNodeDelegate(this);
GetLocalRootArkUINode().ShowIndicator(false);
GetLocalRootArkUINode().NodeSwiperLoop(0);
GetLocalRootArkUINode().SetShowIndicator(false);
GetLocalRootArkUINode().SetSwiperLoop(0);
FOOTSTONE_DLOG(INFO) << "PagerView initialized.";
}

Expand All @@ -44,12 +44,12 @@ bool PagerView::SetProp(const std::string &propKey, const HippyValue &propValue)
if (propKey == "initialPage") {
initialPage_ = HRValueUtils::GetInt32(propValue);
index_ = initialPage_;
GetLocalRootArkUINode().NodeSwiperIndex(index_);
GetLocalRootArkUINode().SetSwiperIndex(index_);
return true;
} else if (propKey == "pagescroll") {
propValue.ToInt32(initialPage_);
index_ = initialPage_;
GetLocalRootArkUINode().NodeSwiperIndex(index_);
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
return true;
} else if (propKey == "scrollEnabled") {
bool enable;
Expand All @@ -62,17 +62,17 @@ bool PagerView::SetProp(const std::string &propKey, const HippyValue &propValue)
propValue.ToString(directionVal);
if (directionVal == "vertical") {
vertical_ = true;
GetLocalRootArkUINode().NodeSwiperVertical(1);
GetLocalRootArkUINode().SetSwiperVertical(1);
}
return true;
} else if (propKey == "vertical") {
vertical_ = true;
GetLocalRootArkUINode().NodeSwiperVertical(1);
GetLocalRootArkUINode().SetSwiperVertical(1);
return true;
} else if (propKey == "pageMargin") {
prevMargin_ = nextMargin_ = HRValueUtils::GetFloat(propValue);
GetLocalRootArkUINode().NodeSwiperPrevMargin(prevMargin_);
GetLocalRootArkUINode().NodeSwiperNextMargin(nextMargin_);
GetLocalRootArkUINode().SetSwiperPrevMargin(prevMargin_);
GetLocalRootArkUINode().SetSwiperNextMargin(nextMargin_);
return true;
}
return BaseView::SetProp(propKey, propValue);
Expand All @@ -97,6 +97,7 @@ void PagerView::OnChange(const int32_t &index) {
std::shared_ptr<HippyValue> changedParams = std::make_shared<HippyValue>(changedPayload);
HREventUtils::SendComponentEvent(ctx_, tag_, HREventUtils::EVENT_PAGE_SCROLL_STATE_CHANGED,
changedParams);
index_ = index;
}

void PagerView::OnAnimationStart(const int32_t &currentIndex, const int32_t &targetIndex,
Expand Down Expand Up @@ -129,19 +130,18 @@ void PagerView::OnGestureSwipe(const int32_t &swiperPageIndex,
}

void PagerView::OnTouchIntercept(const int32_t &eventEnum) {
FOOTSTONE_DLOG(INFO) << "PagerView::OnTouchIntercept - eventEnum:" << eventEnum;
//No specific actions required
}

void PagerView::SendScrollStateChangeEvent(const std::string &state) {
HippyValueObjectType payload = {{"pageScrollState", HippyValue{state}}};
auto params = std::make_shared<HippyValue>(payload);
HREventUtils::SendComponentEvent(ctx_, tag_, HREventUtils::EVENT_PAGE_SCROLL_STATE_CHANGED, params);
HREventUtils::SendComponentEvent(ctx_, tag_, HREventUtils::EVENT_PAGE_SCROLL_STATE_CHANGED,
params);
}

void PagerView::OnNodeTouchEvent(const ArkUI_UIInputEvent *inputEvent) {
int32_t touchAction = OH_ArkUI_UIInputEvent_GetAction(inputEvent);
FOOTSTONE_DLOG(INFO) << "PagerView::OnNodeTouchEvent - Action: " << touchAction;

switch (touchAction) {
case UI_TOUCH_EVENT_ACTION_CANCEL:
// No specific action needed for cancel, logging suffices.
Expand All @@ -160,15 +160,33 @@ void PagerView::OnNodeTouchEvent(const ArkUI_UIInputEvent *inputEvent) {
break;
}
}

void PagerView::Call(const std::string &method, const std::vector<HippyValue> params,
std::function<void(const HippyValue &result)> callback) {
if (method == "setPage") {
index_ = HRValueUtils::GetInt32(params[0]);
GetLocalRootArkUINode().NodeSwiperSwipeToIndex(index_, 1);
} else if (method == "setPageWithoutAnimation") {
int32_t total = static_cast<int32_t>(GetLocalRootArkUINode().GetTotalChildCount());
if (total <= 0) {
return;
}
int32_t newIndex = 0;
if (!params.empty()) {
newIndex = HRValueUtils::GetInt32(params[0]);
}
if (method == "setPage" || method == "setPageWithoutAnimation") {
index_ = newIndex;
GetLocalRootArkUINode().SetSwiperIndex(index_);
} else if (method == "setIndex") {
index_ = newIndex + 1;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
} else if (method == "next") {
if (index_ < total - 1) {
++index_;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
}
} else if (method == "prev") {
} else if (method == "setIndex") {
if (index_ > 0) {
--index_;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
}
}
}
} // namespace native
Expand Down

0 comments on commit d00c715

Please sign in to comment.