Skip to content

Commit

Permalink
Adjust PagerView's call function (#109)
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

* Modify: PagerView call function

* modify PagerView's Call function
  • Loading branch information
wangz2023 authored Jun 14, 2024
1 parent d00c715 commit af3f43b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +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 &SetWidthPercent(float percent);
virtual ArkUINode &SetHeightPercent(float percent);
virtual ArkUINode &SetVisibility(bool visibility);
virtual ArkUINode &SetBackgroundColor(uint32_t color);
virtual ArkUINode &SetOpacity(float opacity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ ArkUINode &ArkUINode::SetSizePercent(const HRSize &size) {
return *this;
}

ArkUINode &ArkUINode::SetPercentWidth(float percent) {
ArkUINode &ArkUINode::SetWidthPercent(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) {
ArkUINode &ArkUINode::SetHeightPercent(float percent) {
ArkUI_NumberValue value[] = {{.f32 = percent}};
ArkUI_AttributeItem item = {value, 1, nullptr, nullptr};
MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_HEIGHT_PERCENT, &item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ bool PagerView::SetProp(const std::string &propKey, const HippyValue &propValue)
index_ = initialPage_;
GetLocalRootArkUINode().SetSwiperIndex(index_);
return true;
} else if (propKey == "pagescroll") {
propValue.ToInt32(initialPage_);
index_ = initialPage_;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
return true;
} else if (propKey == "scrollEnabled") {
bool enable;
propValue.ToBoolean(enable);
Expand Down Expand Up @@ -163,30 +158,44 @@ void PagerView::OnNodeTouchEvent(const ArkUI_UIInputEvent *inputEvent) {

void PagerView::Call(const std::string &method, const std::vector<HippyValue> params,
std::function<void(const HippyValue &result)> callback) {
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;
if (method == "setPage") {
if (params.empty()) {
return;
}
index_ = HRValueUtils::GetInt32(params[0]);
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
} else if (method == "setPageWithoutAnimation") {
if (params.empty()) {
return;
}
index_ = HRValueUtils::GetInt32(params[0]);
GetLocalRootArkUINode().SetSwiperIndex(index_);
} else if (method == "next") {
int32_t total = static_cast<int32_t>(GetLocalRootArkUINode().GetTotalChildCount());
if (total < 1) {
return;
}
if (index_ < total - 1) {
++index_;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
GetLocalRootArkUINode().SetSwiperSwipeToIndex(++index_, 1);
}
} else if (method == "prev") {
if (index_ > 0) {
--index_;
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, 1);
GetLocalRootArkUINode().SetSwiperSwipeToIndex(--index_, 1);
}
} else if (method == "setIndex") {
HippyValueObjectType map;
if (params.empty() || !params[0].IsObject()) {
FOOTSTONE_DLOG(INFO) << "Unknown params";
return;
}
bool r = params[0].ToObject(map);
if (r && map.size() > 0) {
index_ = HRValueUtils::GetInt32(map["index"]);
int32_t animated = HRValueUtils::GetBool(map["animated"], 0);
GetLocalRootArkUINode().SetSwiperSwipeToIndex(index_, animated);
}
} else {
FOOTSTONE_DLOG(INFO) << "Unknown function name: " << method;
}
}
} // namespace native
Expand Down

0 comments on commit af3f43b

Please sign in to comment.