1 /*
2 * Copyright (c) 2021 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 "frameworks/core/components/ifelse/if_else_element.h"
17
18 #include "frameworks/core/components/ifelse/if_else_component.h"
19
20 namespace OHOS::Ace {
21
CanUpdate(const RefPtr<Component> & newComponent)22 bool IfElseElement::CanUpdate(const RefPtr<Component>& newComponent)
23 {
24 auto ifElseComponent = AceType::DynamicCast<IfElseComponent>(newComponent);
25 return ifElseComponent ? branchId_ == ifElseComponent->GetBranchId() : false;
26 }
27
Update()28 void IfElseElement::Update()
29 {
30 auto ifElseComponent = AceType::DynamicCast<IfElseComponent>(component_);
31 if (!ifElseComponent) {
32 LOGW("IfElseElement: component MUST be instance of IfElseComponent");
33 return;
34 }
35
36 MultiComposedElement::Update();
37 branchId_ = ifElseComponent->GetBranchId();
38
39 LOGD("update, branchId: %d", branchId_);
40 }
41
ComponentToElementLocalizedUpdate(const RefPtr<Component> & component,RefPtr<Element> & element)42 void IfElseElement::ComponentToElementLocalizedUpdate(const RefPtr<Component>& component, RefPtr<Element>& element)
43 {
44 RefPtr<IfElseElement> ifElseElement = AceType::DynamicCast<IfElseElement>(element);
45 if (!ifElseElement) {
46 LOGE("%{public}s is not a IfElseElement. Internal Error!", AceType::TypeName(element));
47 return;
48 }
49
50 RefPtr<IfElseComponent> ifElseComponent = AceType::DynamicCast<IfElseComponent>(component);
51 if (!ifElseComponent) {
52 LOGE("%{public}s is not a IfElseComponent. Internal Error!", AceType::TypeName(component));
53 return;
54 }
55
56 LOGD("Component branch id %d, Element branch id %d",
57 ifElseComponent->GetBranchId(), ifElseElement->GetBranchId());
58
59 if (ifElseComponent->GetBranchId() == ifElseElement->GetBranchId()) {
60 LOGD("Unchanged branchId. No updates to be done.");
61 return;
62 }
63
64 // even though the IfElement will be deleted, do not put to list of deleted elements
65 // because new Element with same elmtId will be created
66 ElementRegister::GetInstance()->RemoveItemSilently(ifElseElement->GetElementId());
67 ifElseElement->UnregisterChildrenForPartialUpdates();
68
69 auto ifElseParentElement = ifElseElement->GetElementParent().Upgrade();
70 LOGD("Doing a deep update IfElseElement <- %{public}s ...", AceType::TypeName(ifElseComponent));
71 LOGD("IfElse element slot: %{public}d, renderSlot: %{public}d", ifElseElement->GetSlot(),
72 ifElseElement->GetRenderSlot());
73 LOGD(" IfElseElement parent Element is %{public}s", AceType::TypeName(ifElseParentElement));
74
75 ifElseParentElement->UpdateChildWithSlot(
76 ifElseElement, ifElseComponent, ifElseElement->GetSlot(), ifElseElement->GetRenderSlot());
77
78 ifElseElement->branchId_ = ifElseComponent->GetBranchId();
79
80 LOGD("Doing a deep update on IfElseElement - DONE");
81 }
82 } // namespace OHOS::Ace
83