Skip to content

Commit fb4587b

Browse files
committed
feat(ohos): list pull footer refactoring
1 parent 0e48248 commit fb4587b

File tree

5 files changed

+81
-18
lines changed

5 files changed

+81
-18
lines changed

framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/components/list_item_adapter.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ inline namespace native {
3434

3535
class ListItemAdapter {
3636
public:
37-
ListItemAdapter(std::vector<std::shared_ptr<BaseView>> &itemViews, uint32_t firstItemIndex)
38-
: handle_(OH_ArkUI_NodeAdapter_Create()), itemViews_(itemViews), firstItemIndex_(firstItemIndex) {
39-
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, static_cast<uint32_t>(itemViews.size()-firstItemIndex));
37+
ListItemAdapter(std::vector<std::shared_ptr<BaseView>> &itemViews, uint32_t firstItemIndex, uint32_t trimFooterItem)
38+
: handle_(OH_ArkUI_NodeAdapter_Create()), itemViews_(itemViews), firstItemIndex_(firstItemIndex), trimFooterItem_(trimFooterItem) {
39+
SetTotalNodeCount();
4040
OH_ArkUI_NodeAdapter_RegisterEventReceiver(handle_, this, OnStaticAdapterEvent);
4141
}
4242

@@ -48,15 +48,15 @@ class ListItemAdapter {
4848

4949
void ClearAll() {
5050
OH_ArkUI_NodeAdapter_UnregisterEventReceiver(handle_);
51-
OH_ArkUI_NodeAdapter_RemoveItem(handle_, 0, static_cast<uint32_t>(itemViews_.size())-firstItemIndex_);
52-
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, 0);
51+
OH_ArkUI_NodeAdapter_RemoveItem(handle_, 0, static_cast<uint32_t>(itemViews_.size())-firstItemIndex_-trimFooterItem_);
52+
ClearTotalNodeCount();
5353
cachedTypeRecycleViews_.clear();
5454
attachedHandleViewMap_.clear();
5555
}
5656

5757
void RestoreAll() {
5858
OH_ArkUI_NodeAdapter_RegisterEventReceiver(handle_, this, OnStaticAdapterEvent);
59-
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, static_cast<uint32_t>(itemViews_.size())-firstItemIndex_);
59+
SetTotalNodeCount();
6060
}
6161

6262
ArkUI_NodeAdapterHandle GetHandle() const { return handle_; }
@@ -68,7 +68,7 @@ class ListItemAdapter {
6868
// 如果index会导致可视区域元素发生可见性变化,则会回调NODE_ADAPTER_EVENT_ON_REMOVE_NODE_FROM_ADAPTER事件删除元素,
6969
// 根据是否有新增元素回调NODE_ADAPTER_EVENT_ON_GET_NODE_ID和NODE_ADAPTER_EVENT_ON_ADD_NODE_TO_ADAPTER事件
7070
OH_ArkUI_NodeAdapter_RemoveItem(handle_, static_cast<uint32_t>(index) - firstItemIndex_, 1);
71-
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, static_cast<uint32_t>(itemViews_.size()) - firstItemIndex_);
71+
SetTotalNodeCount();
7272
}
7373

7474
void InsertItem(int32_t index) {
@@ -78,14 +78,38 @@ class ListItemAdapter {
7878
// 如果index会导致可视区域元素发生可见性变化,则会回调NODE_ADAPTER_EVENT_ON_GET_NODE_ID和NODE_ADAPTER_EVENT_ON_ADD_NODE_TO_ADAPTER事件,
7979
// 根据是否有删除元素回调NODE_ADAPTER_EVENT_ON_REMOVE_NODE_FROM_ADAPTER事件
8080
OH_ArkUI_NodeAdapter_InsertItem(handle_, static_cast<uint32_t>(index) - firstItemIndex_, 1);
81-
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, static_cast<uint32_t>(itemViews_.size()) - firstItemIndex_);
81+
SetTotalNodeCount();
82+
}
83+
84+
void AddFooterItem() {
85+
trimFooterItem_ = 0;
86+
OH_ArkUI_NodeAdapter_InsertItem(handle_, static_cast<uint32_t>(itemViews_.size()) - 1 - firstItemIndex_, 1);
87+
SetTotalNodeCount();
88+
}
89+
90+
void RemoveFooterItem() {
91+
trimFooterItem_ = 1;
92+
OH_ArkUI_NodeAdapter_RemoveItem(handle_, static_cast<uint32_t>(itemViews_.size()) - 1 - firstItemIndex_, 1);
93+
SetTotalNodeCount();
94+
}
95+
96+
bool HasFooterItem() {
97+
return trimFooterItem_ ? false : true;
8298
}
8399

84100
private:
85101
static void OnStaticAdapterEvent(ArkUI_NodeAdapterEvent *event) {
86102
auto itemAdapter = reinterpret_cast<ListItemAdapter *>(OH_ArkUI_NodeAdapterEvent_GetUserData(event));
87103
itemAdapter->OnAdapterEvent(event);
88104
}
105+
106+
void SetTotalNodeCount() {
107+
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, static_cast<uint32_t>(itemViews_.size()) - firstItemIndex_ - trimFooterItem_);
108+
}
109+
110+
void ClearTotalNodeCount() {
111+
OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, 0);
112+
}
89113

90114
void OnAdapterEvent(ArkUI_NodeAdapterEvent *event) {
91115
auto type = OH_ArkUI_NodeAdapterEvent_GetType(event);
@@ -171,6 +195,7 @@ class ListItemAdapter {
171195

172196
std::vector<std::shared_ptr<BaseView>> &itemViews_;
173197
uint32_t firstItemIndex_ = 0;
198+
uint32_t trimFooterItem_ = 0;
174199

175200
std::unordered_map<std::string, std::stack<std::shared_ptr<RecycleView>>> cachedTypeRecycleViews_;
176201

framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/components/list_view.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ enum class ScrollAction : int32_t {
4343
ReleaseFooter
4444
};
4545

46-
class ListView : public BaseView, public ListNodeDelegate, public ListItemNodeDelegate, public RefreshNodeDelegate, public PullHeaderViewDelegate {
46+
class ListView : public BaseView, public ListNodeDelegate, public ListItemNodeDelegate
47+
, public RefreshNodeDelegate, public PullHeaderViewDelegate, public PullFooterViewDelegate {
4748
public:
4849
ListView(std::shared_ptr<NativeRenderContext> &ctx);
4950
~ListView();
@@ -88,6 +89,9 @@ class ListView : public BaseView, public ListNodeDelegate, public ListItemNodeDe
8889
// PullHeaderViewDelegate
8990
void OnPullHeaderViewSizeUpdated(const HRSize &size) override;
9091

92+
// PullFooterViewDelegate
93+
void OnCollapsePullFooter() override;
94+
9195
// pull head
9296
void OnHeadRefreshFinish(int32_t delay);
9397
void OnHeadRefresh();
@@ -173,6 +177,8 @@ class ListView : public BaseView, public ListNodeDelegate, public ListItemNodeDe
173177
bool isListZeroSize = false;
174178

175179
bool isInitListReadyNotified = false;
180+
181+
// bool isDesktopDevice_ = true;
176182
};
177183

178184
} // namespace native

framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/components/pull_footer_view.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ namespace hippy {
2929
inline namespace render {
3030
inline namespace native {
3131

32+
class PullFooterViewDelegate {
33+
public:
34+
virtual ~PullFooterViewDelegate() = default;
35+
virtual void OnCollapsePullFooter() {}
36+
};
37+
3238
class PullFooterView : public ListItemView {
3339
public:
3440
PullFooterView(std::shared_ptr<NativeRenderContext> &ctx);
@@ -45,10 +51,13 @@ class PullFooterView : public ListItemView {
4551
void UpdateRenderViewFrameImpl(const HRRect &frame, const HRPadding &padding) override;
4652
void Show(bool show);
4753

54+
void SetPullFooterViewDelegate(PullFooterViewDelegate *delegate) { viewDelegate_ = delegate; }
4855
private:
4956
bool isVisible_ = true;
5057

5158
bool sticky_ = false;
59+
60+
PullFooterViewDelegate *viewDelegate_ = nullptr;
5261
};
5362

5463
} // namespace native

framework/ohos/src/main/cpp/impl/renderer/native/src/components/list_view.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ ListView::~ListView() {
5858
headerView_->SetPullHeaderViewDelegate(nullptr);
5959
headerView_->DestroyArkUINode();
6060
}
61+
if (footerView_) {
62+
footerView_->SetPullFooterViewDelegate(nullptr);
63+
}
6164
}
6265

6366
void ListView::Init() {
@@ -341,7 +344,15 @@ void ListView::OnReachStart() {
341344

342345
void ListView::OnReachEnd() {
343346
FOOTSTONE_DLOG(INFO) << "ListView onReachEnd";
344-
SendOnReachedEvent();
347+
if (footerView_) {
348+
if (!adapter_->HasFooterItem()) {
349+
adapter_->AddFooterItem();
350+
} else {
351+
SendOnReachedEvent();
352+
}
353+
} else {
354+
SendOnReachedEvent();
355+
}
345356
}
346357

347358
void ListView::OnTouch(int32_t actionType, const HRPosition &screenPosition) {
@@ -409,6 +420,14 @@ void ListView::OnPullHeaderViewSizeUpdated(const HRSize &size) {
409420
}
410421
}
411422

423+
void ListView::OnCollapsePullFooter() {
424+
if (footerView_) {
425+
if (adapter_->HasFooterItem()) {
426+
adapter_->RemoveFooterItem();
427+
}
428+
}
429+
}
430+
412431
void ListView::OnHeadRefreshFinish(int32_t delay) {
413432
FOOTSTONE_DLOG(INFO) << __FUNCTION__ << " delay = " << delay;
414433
refreshNode_->SetRefreshRefreshing(false);
@@ -445,6 +464,7 @@ void ListView::HandleOnChildrenUpdated() {
445464
}
446465
if (children_[childrenCount - 1]->GetViewType() == PULL_FOOTER_VIEW_TYPE) {
447466
footerView_ = std::static_pointer_cast<PullFooterView>(children_[childrenCount - 1]);
467+
footerView_->SetPullFooterViewDelegate(this);
448468
footerView_->Show(false);
449469
}
450470

@@ -497,7 +517,7 @@ void ListView::CreateArkUINodeAfterHeaderCheck() {
497517
stackNode_->InsertChild(listNode_.get(), 0);
498518
}
499519
if (!adapter_) {
500-
adapter_ = std::make_shared<ListItemAdapter>(children_, hasPullHeader_ ? 1 : 0);
520+
adapter_ = std::make_shared<ListItemAdapter>(children_, hasPullHeader_ ? 1 : 0, footerView_ ? 1 : 0);
501521
listNode_->SetLazyAdapter(adapter_->GetHandle());
502522
}
503523
}

framework/ohos/src/main/cpp/impl/renderer/native/src/components/pull_footer_view.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,23 @@ void PullFooterView::CallImpl(const std::string &method, const std::vector<Hippy
7575
std::function<void(const HippyValue &result)> callback) {
7676
FOOTSTONE_DLOG(INFO)<<__FUNCTION__<<" method = "<<method;
7777
if (method == "collapsePullFooter") {
78+
if (viewDelegate_) {
79+
viewDelegate_->OnCollapsePullFooter();
80+
}
7881
Show(false);
7982
} else {
8083
BaseView::CallImpl(method, params, callback);
8184
}
8285
}
8386

8487
void PullFooterView::Show(bool show) {
85-
if (show != isVisible_) {
86-
auto node = GetLocalRootArkUINode();
87-
if (node) {
88-
isVisible_ = show;
89-
node->SetVisibility(show);
90-
}
91-
}
88+
// if (show != isVisible_) {
89+
// auto node = GetLocalRootArkUINode();
90+
// if (node) {
91+
// isVisible_ = show;
92+
// node->SetVisibility(show);
93+
// }
94+
// }
9295
}
9396

9497
void PullFooterView::UpdateRenderViewFrameImpl(const HRRect &frame, const HRPadding &padding){

0 commit comments

Comments
 (0)