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_full_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_ng/base/view_full_update_model.h"
22 
23 namespace OHOS::Ace::Framework {
24 
CreateNode(NodeInfo && info)25 RefPtr<AceType> ViewFullUpdateModelImpl::CreateNode(NodeInfo&& info)
26 {
27     ACE_SCOPED_TRACE("JSView::CreateSpecializedComponent");
28     // create component, return new something, need to set proper ID
29 
30     std::string key = ViewStackProcessor::GetInstance()->ProcessViewId(info.viewId);
31     auto composedComponent = AceType::MakeRefPtr<ComposedComponent>(key, "view");
32     auto isStatic = info.isStatic;
33 
34     auto elementFunction = [nodeInfo = std::move(info)](const RefPtr<ComposedElement>& element) mutable {
35         if (nodeInfo.appearFunc) {
36             nodeInfo.appearFunc();
37         }
38 
39         if (nodeInfo.updateNodeFunc) {
40             nodeInfo.updateNodeFunc(element);
41         }
42 
43         // add render function callback to element. when the element rebuilds due
44         // to state update it will call this callback to get the new child component.
45         if (element) {
46             auto renderFunction = [renderFunc = nodeInfo.renderFunc](
47                                       const RefPtr<Component>& component) -> RefPtr<Component> {
48                 if (!renderFunc) {
49                     return nullptr;
50                 }
51                 auto node = renderFunc();
52                 return AceType::DynamicCast<Component>(node);
53             };
54             element->SetRenderFunction(std::move(renderFunction));
55             auto removeFunc = nodeInfo.removeFunc;
56             element->SetRemoveFunction(std::move(removeFunc));
57             if (nodeInfo.pageTransitionFunc) {
58                 auto pageTransitionFunction = [transitionFunc = nodeInfo.pageTransitionFunc]() -> RefPtr<Component> {
59                     transitionFunc();
60                     auto pageTransitionComponent = ViewStackProcessor::GetInstance()->GetPageTransitionComponent();
61                     ViewStackProcessor::GetInstance()->ClearPageTransitionComponent();
62                     return pageTransitionComponent;
63                 };
64                 element->SetPageTransitionFunction(std::move(pageTransitionFunction));
65             }
66         }
67     };
68 
69     composedComponent->SetElementFunction(std::move(elementFunction));
70 
71     if (isStatic) {
72         composedComponent->SetStatic();
73     }
74     return composedComponent;
75 }
76 
MarkNeedUpdate(const WeakPtr<AceType> & node)77 bool ViewFullUpdateModelImpl::MarkNeedUpdate(const WeakPtr<AceType>& node)
78 {
79     ACE_SCOPED_TRACE("JSView::MarkNeedUpdate");
80     auto weakElement = AceType::DynamicCast<ComposedElement>(node);
81     if (weakElement.Invalid()) {
82         LOGE("Invalid Element weak ref, internal error");
83         return false;
84     }
85     auto element = weakElement.Upgrade();
86     if (element) {
87         element->MarkDirty();
88     }
89     return true;
90 }
91 
92 } // namespace OHOS::Ace::Framework
93