From af90c40d0cc1d9a92e4261bfcc5279e7f33bd304 Mon Sep 17 00:00:00 2001 From: lizhan Date: Wed, 6 Sep 2023 16:33:53 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=8C=91=E5=8D=950815=E3=80=91lazy=5F?= =?UTF-8?q?for=5Feach=20onDataMoved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lizhan Change-Id: I1b56a779587a6cb52a989c8456bb8e486d5b3725 --- .../syntax/lazy_for_each_builder.h | 26 ++++++++++++++++--- .../syntax/lazy_for_each_node.cpp | 7 ++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_builder.h b/frameworks/core/components_ng/syntax/lazy_for_each_builder.h index 4a50886211e..72a81c006dd 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_builder.h +++ b/frameworks/core/components_ng/syntax/lazy_for_each_builder.h @@ -162,6 +162,22 @@ public: bool OnDataMoved(size_t from, size_t to) { + if (from == to) { + return false; + } + auto fromIter = cachedItems_.find(from); + auto toIter = cachedItems_.find(to); + if (fromIter != cachedItems_.end() && toIter != cachedItems_.end()) { + std::swap(fromIter->second, toIter->second); + } else if (fromIter != cachedItems_.end()) { + expiringItem_.try_emplace( + fromIter->second.first, LazyForEachCacheChild(to, std::move(fromIter->second.second))); + cachedItems_.erase(fromIter); + } else if (toIter != cachedItems_.end()) { + expiringItem_.try_emplace( + toIter->second.first, LazyForEachCacheChild(from, std::move(toIter->second.second))); + cachedItems_.erase(toIter); + } return true; } @@ -261,13 +277,15 @@ public: std::unordered_set idleIndexes; if (startIndex_ != -1 && endIndex_ != -1) { for (int32_t i = 1; i <= cacheCount_; i++) { - if (startIndex_ >= i) { - idleIndexes.emplace(startIndex_ - i); - } if (endIndex_ + i < count) { idleIndexes.emplace(endIndex_ + i); } } + for (int32_t i = 1; i <= cacheCount_; i++) { + if (startIndex_ >= i) { + idleIndexes.emplace(startIndex_ - i); + } + } } for (auto& [key, node] : expiringItem_) { @@ -345,4 +363,4 @@ private: }; } // namespace OHOS::Ace::NG -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H \ No newline at end of file +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp b/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp index 2cb4f51276a..3ef4073168f 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp +++ b/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp @@ -153,7 +153,12 @@ void LazyForEachNode::OnDataChanged(size_t index) void LazyForEachNode::OnDataMoved(size_t from, size_t to) { - // TODO: data move + if (builder_) { + builder_->OnDataMoved(from, to); + } + children_.clear(); + MarkNeedSyncRenderTree(true); + MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT); } void LazyForEachNode::NotifyDataCountChanged(int32_t index) -- Gitee