• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <optional>
22 #include <string>
23 #include <unordered_map>
24 #include <utility>
25 
26 #include "base/log/ace_trace.h"
27 #include "base/utils/noncopyable.h"
28 #include "core/components_ng/base/ui_node.h"
29 #include "core/components_v2/foreach/lazy_foreach_component.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 class ACE_EXPORT LazyForEachBuilder : public virtual AceType {
34     DECLARE_ACE_TYPE(NG::LazyForEachBuilder, AceType)
35 public:
36     LazyForEachBuilder() = default;
37     ~LazyForEachBuilder() override = default;
38 
GetTotalCount()39     int32_t GetTotalCount()
40     {
41         return OnGetTotalCount();
42     }
43 
CreateChildByIndex(int32_t index)44     std::pair<std::string, RefPtr<UINode>> CreateChildByIndex(int32_t index)
45     {
46         {
47             ACE_SCOPED_TRACE("Builder:BuildLazyItem [%d]", index);
48             auto itemInfo = OnGetChildByIndex(index, generatedItem_);
49             CHECK_NULL_RETURN(itemInfo.second, itemInfo);
50             auto result = generatedItem_.try_emplace(itemInfo.first, itemInfo.second);
51             if (!result.second) {
52                 LOGD("already has same key %{private}s child", itemInfo.first.c_str());
53             }
54             return *(result.first);
55         }
56     }
57 
GetChildByKey(const std::string & key)58     RefPtr<UINode> GetChildByKey(const std::string& key)
59     {
60         auto iter = generatedItem_.find(key);
61         if (iter != generatedItem_.end()) {
62             return iter->second;
63         }
64         return nullptr;
65     }
66 
UpdateCachedItems(const std::list<std::optional<std::string>> & nodeIds,std::unordered_map<int32_t,std::optional<std::string>> && cachedItems)67     void UpdateCachedItems(const std::list<std::optional<std::string>>& nodeIds,
68         std::unordered_map<int32_t, std::optional<std::string>>&& cachedItems)
69     {
70         // use active ids to update cached items.
71         std::unordered_map<std::string, RefPtr<UINode>> generatedItem;
72         std::swap(generatedItem, generatedItem_);
73         for (const auto& id : nodeIds) {
74             if (!id) {
75                 continue;
76             }
77             auto iter = generatedItem.find(*id);
78             if (iter != generatedItem.end()) {
79                 generatedItem_.try_emplace(iter->first, iter->second);
80                 generatedItem.erase(iter);
81             }
82         }
83         // store cached items.
84         for (auto& [index, id] : cachedItems) {
85             if (!id) {
86                 // get id from old cachedItems which stores the idle task generate result.
87                 auto iter = cachedItems_.find(index);
88                 if (iter == cachedItems_.end()) {
89                     continue;
90                 }
91                 id = iter->second;
92             }
93             if (id) {
94                 auto iter = generatedItem.find(*id);
95                 if (iter != generatedItem.end()) {
96                     iter->second->SetActive(false);
97                     generatedItem_.try_emplace(iter->first, iter->second);
98                 }
99             }
100         }
101         std::swap(cachedItems_, cachedItems);
102         LOGD("LazyForEach cached size : %{public}d", static_cast<int32_t>(generatedItem_.size()));
103     }
104 
SetCacheItemInfo(int32_t index,const std::string & info)105     void SetCacheItemInfo(int32_t index, const std::string& info)
106     {
107         cachedItems_[index] = info;
108     }
109 
GetCacheItemInfo(int32_t index)110     std::optional<std::string> GetCacheItemInfo(int32_t index) const
111     {
112         auto iter = cachedItems_.find(index);
113         if (iter != cachedItems_.end()) {
114             return iter->second;
115         }
116         return std::nullopt;
117     }
118 
Clean()119     void Clean()
120     {
121         generatedItem_.clear();
122     }
123 
RemoveChild(const std::string & id)124     void RemoveChild(const std::string& id)
125     {
126         generatedItem_.erase(id);
127     }
128 
ExpandChildrenOnInitial()129     void ExpandChildrenOnInitial()
130     {
131         OnExpandChildrenOnInitialInNG();
132     }
133 
134     virtual void ReleaseChildGroupById(const std::string& id) = 0;
135     virtual void RegisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) = 0;
136     virtual void UnregisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) = 0;
137 
138 protected:
139     virtual int32_t OnGetTotalCount() = 0;
140     virtual std::pair<std::string, RefPtr<UINode>> OnGetChildByIndex(
141         int32_t index, const std::unordered_map<std::string, RefPtr<UINode>>& cachedItems) = 0;
142     virtual void OnExpandChildrenOnInitialInNG() = 0;
143 
144 private:
145     // [key, UINode]
146     std::unordered_map<std::string, RefPtr<UINode>> generatedItem_;
147     // [index, key]
148     std::unordered_map<int32_t, std::optional<std::string>> cachedItems_;
149 
150     ACE_DISALLOW_COPY_AND_MOVE(LazyForEachBuilder);
151 };
152 } // namespace OHOS::Ace::NG
153 
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H
155