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_BRIDGE_DECLARATIVE_FRONTEND_COMPONENTS_FOR_EACH_ELEMENT_PART_UPD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_COMPONENTS_FOR_EACH_ELEMENT_PART_UPD_H
18 
19 #include <list>
20 #include <string>
21 
22 #include "base/utils/macros.h"
23 #include "frameworks/core/pipeline/base/multi_composed_element.h"
24 
25 namespace OHOS::Ace::PartUpd {
26 
27 // ComposedElement just maintain a child element may have render node.
28 class ACE_EXPORT ForEachElement : public MultiComposedElement {
29     DECLARE_ACE_TYPE(PartUpd::ForEachElement, MultiComposedElement);
30 
31 using ComponentFunction = std::function<RefPtr<Component>(int32_t index)>;
32 
33 public:
ForEachElement(const ComposeId & id)34     explicit ForEachElement(const ComposeId& id) : MultiComposedElement(id) {};
35     ~ForEachElement() override = default;
36     bool CanUpdate(const RefPtr<Component>& newComponent) override;
37 
38     // array of id created during render
39     // update from ForEachComponent on sync
40     // purpose: memorize what array items (their IDs) have been
41     // used for creating child components during previous render
42     // and in which order (slots)
43     // use to determin chich child Elements to keep and possibly
44     // move, which ones to delete
45     // e.g. old [ 1, 2, 3], new [ 0, 1, 2]
46     //   new child from Component place to index 0
47     //   old child moves from index 0 to 1
48     //   old child moves from index 1 to 2
49     //   old child at index 2 delete
SetIdArray(const std::list<std::string> & newIdArray)50     void SetIdArray(const std::list<std::string>& newIdArray)
51     {
52         idArray_ = newIdArray;
53     }
54 
GetIdArray()55     const std::list<std::string>& GetIdArray() const
56     {
57         return idArray_;
58     }
59 
60     /**
61      * Compare current ID array with given new ID array
62      * find no longer used IDs, the corresponding earlier created child Element for each such ID
63      * remove these children from the ElementRegister.
64      * This function is called from
65      * TS View.forEachUpdateFunction -> ForEach.setIdArray ->
66      * C++ JSForEach::SetIdArray
67      * This function reads from the ForEachElement but it does not modify it.
68      */
69     void RemoveUnusedChildElementsFromRegistery(const std::list<std::string>& newIds) const;
70 
71    // caution added for partial update
72     virtual void Update() override;
73 
74     /**
75      * Update Element with given ForEachComponent
76      */
77     virtual void LocalizedUpdate() override;
78 
79     /**
80      * helper function that updates the slot of given Element
81      * if the Element is a ComposedElement it
82      * recursively sets the slot of of the single child until
83      * it get to the first RenderElement
84      */
85     void UpdateSlot(const RefPtr<Element>& element, int32_t slot, int32_t renderSlot);
86 
87 protected:
88     static bool CompareSlots(const RefPtr<Element>& first, const RefPtr<Element>& second);
89     // adds elements from given list into a Map with key Element.GetSlot
90     static void MakeElementByIdMap(const std::list<RefPtr<Element>>& elmts,
91         const std::list<std::string>& ids, std::map<std::string, Ace::RefPtr<Element>>& result);
92 
93 private:
94     std::list<std::string> idArray_;
95 };
96 
97 } // namespace OHOS::Ace::PartUpd
98 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_COMPONENTS_FOR_EACH_ELEMENT_H
99