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 #include "bridge/declarative_frontend/jsview/models/view_partial_update_model_impl.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/utils/utils.h"
20 #include "bridge/declarative_frontend/view_stack_processor.h"
21 #include "core/components/grid_layout/grid_layout_item_element.h"
22 #include "core/components/ifelse/if_else_element.h"
23 #include "core/components_ng/base/view_full_update_model.h"
24 #include "core/components_v2/common/element_proxy.h"
25 #include "core/pipeline/base/component.h"
26 
27 namespace OHOS::Ace::Framework {
28 
CreateNode(NodeInfoPU && info)29 RefPtr<AceType> ViewPartialUpdateModelImpl::CreateNode(NodeInfoPU&& info)
30 {
31     ACE_SCOPED_TRACE("JSViewPartialUpdate::CreateSpecializedComponent");
32     // create component, return new something, need to set proper ID
33 
34     const auto reservedElementId = ViewStackProcessor::GetInstance()->ClaimElementId();
35     const std::string key = std::to_string(reservedElementId);
36     auto composedComponent = AceType::MakeRefPtr<ComposedComponent>(key, "view");
37     composedComponent->SetElementId(reservedElementId);
38     auto isStatic = info.isStatic;
39     if (info.updateViewIdFunc) {
40         info.updateViewIdFunc(key);
41     }
42 
43     auto renderFunction = [renderFunc = std::move(info.renderFunc), updateFunc = std::move(info.updateFunc)](
44                               const RefPtr<Component>& component) -> RefPtr<Component> {
45         auto node = renderFunc ? renderFunc() : nullptr;
46         if (updateFunc) {
47             updateFunc();
48         }
49         return AceType::DynamicCast<Component>(node);
50     };
51 
52     auto elementFunction = [renderFunction = std::move(renderFunction), nodeInfo = std::move(info)](
53                                const RefPtr<ComposedElement>& element) mutable {
54         if (nodeInfo.appearFunc) {
55             nodeInfo.appearFunc();
56         }
57         if (nodeInfo.updateNodeFunc) {
58             nodeInfo.updateNodeFunc(element);
59         }
60 
61         // add render function callback to element. when the element rebuilds due
62         // to state update it will call this callback to get the new child component.
63         if (element) {
64             element->SetRenderFunction(std::move(renderFunction));
65             element->SetRemoveFunction(std::move(nodeInfo.removeFunc));
66             if (nodeInfo.pageTransitionFunc) {
67                 auto pageTransitionFunction = [transitionFunc =
68                                                       std::move(nodeInfo.pageTransitionFunc)]() -> RefPtr<Component> {
69                     transitionFunc();
70                     auto pageTransitionComponent = ViewStackProcessor::GetInstance()->GetPageTransitionComponent();
71                     ViewStackProcessor::GetInstance()->ClearPageTransitionComponent();
72                     return pageTransitionComponent;
73                 };
74                 element->SetPageTransitionFunction(std::move(pageTransitionFunction));
75             }
76         }
77     };
78 
79     composedComponent->SetElementFunction(std::move(elementFunction));
80 
81     if (isStatic) {
82         composedComponent->SetStatic();
83     }
84 
85     return composedComponent;
86 }
87 
MarkNeedUpdate(const WeakPtr<AceType> & node)88 bool ViewPartialUpdateModelImpl::MarkNeedUpdate(const WeakPtr<AceType>& node)
89 {
90     ACE_SCOPED_TRACE("JSView::MarkNeedUpdate");
91     auto weakElement = AceType::DynamicCast<ComposedElement>(node);
92     if (weakElement.Invalid()) {
93         LOGE("Invalid Element weak ref, internal error");
94         return false;
95     }
96     auto element = weakElement.Upgrade();
97     if (element) {
98         element->MarkDirty();
99     }
100     return true;
101 }
102 
FlushUpdateTask(const UpdateTask & task)103 void ViewPartialUpdateModelImpl::FlushUpdateTask(const UpdateTask& task)
104 {
105     const int32_t elmtId = std::get<0>(task);
106     const RefPtr<Component> outmostWrappingComponent = AceType::DynamicCast<Component>(std::get<1>(task)); // stop at
107     const RefPtr<Component> mainComponent = AceType::DynamicCast<Component>(std::get<2>(task));            // has elmtId
108 
109     ACE_DCHECK(mainComponent != nullptr);
110     ACE_DCHECK(elmtId != ElementRegister::UndefinedElementId);
111 
112     RefPtr<Element> element = ElementRegister::GetInstance()->GetElementById(elmtId);
113     if (element != nullptr) {
114         // special case, because new IfElement will be created
115         if (AceType::DynamicCast<IfElseElement>(element) != nullptr) {
116             IfElseElement::ComponentToElementLocalizedUpdate(mainComponent, element);
117         } else if (AceType::DynamicCast<GridLayoutItemElement>(element) != nullptr) {
118             // VSP::Finish returns swapped compared to reg
119             AceType::DynamicCast<SoleChildElement>(element)->LocalizedUpdateWithItemComponent(
120                 mainComponent, outmostWrappingComponent);
121         } else {
122             element->LocalizedUpdateWithComponent(mainComponent, outmostWrappingComponent);
123         }
124         return;
125     }
126 
127     auto elementProxy = ElementRegister::GetInstance()->GetElementProxyById(elmtId);
128     if (elementProxy != nullptr) {
129         elementProxy->LocalizedUpdate(mainComponent, outmostWrappingComponent);
130         return;
131     }
132 
133     LOGE("No suitable Element/ElementProxy with elmtId %{public}d found to update from %{public}s,"
134          " elmtId exists in ElementRegister "
135          "'%{public}s'.",
136         elmtId, (mainComponent ? AceType::TypeName(mainComponent) : "no Component error"),
137         (ElementRegister::GetInstance()->Exists(elmtId) ? "exists" : "missing"));
138 }
139 
FinishUpdate(const WeakPtr<AceType> & viewNode,int32_t id,std::function<void (const UpdateTask &)> && emplaceTaskFunc)140 void ViewPartialUpdateModelImpl::FinishUpdate(
141     const WeakPtr<AceType>& viewNode, int32_t id, std::function<void(const UpdateTask&)>&& emplaceTaskFunc)
142 {
143     auto componentsPair = ViewStackProcessor::GetInstance()->FinishReturnMain();
144     if ((componentsPair.first == nullptr) || (componentsPair.second == nullptr)) {
145         LOGE("outmost wrapping component is null");
146         return;
147     }
148     // chk main component componentsPair.second elmtId
149     ACE_DCHECK(componentsPair.second->GetElementId() == id);
150     // push the result of the update function with elmtId added on the list of pending updates, triple:
151     // 0: elmtId
152     // 1: outmost wrapping Component (most keep reference until localized updates done to avoid smart pointer auto
153     // deletion!) 2: main Component
154     if (emplaceTaskFunc) {
155         emplaceTaskFunc(std::make_tuple(id, componentsPair.first, componentsPair.second));
156     }
157 
158     // FlushBuild on UI thread side
159     // will call MakeElementUpdatesToCompleteRerender
160     auto element = AceType::DynamicCast<ComposedElement>(viewNode.Upgrade());
161     if (element) {
162         element->MarkDirty();
163     } else {
164         LOGE("Internal error, element is is null");
165     }
166 }
167 
168 } // namespace OHOS::Ace::Framework
169