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
ComponentToElementLocalizedUpdate(const RefPtr<Component> & component,RefPtr<Element> & element)40 void IfElseElement::ComponentToElementLocalizedUpdate(const RefPtr<Component>& component, RefPtr<Element>& element)
41 {
42 RefPtr<IfElseElement> ifElseElement = AceType::DynamicCast<IfElseElement>(element);
43 if (!ifElseElement) {
44 LOGE("%{public}s is not a IfElseElement. Internal Error!", AceType::TypeName(element));
45 return;
46 }
47
48 RefPtr<IfElseComponent> ifElseComponent = AceType::DynamicCast<IfElseComponent>(component);
49 if (!ifElseComponent) {
50 LOGE("%{public}s is not a IfElseComponent. Internal Error!", AceType::TypeName(component));
51 return;
52 }
53
54 if (ifElseComponent->GetBranchId() == ifElseElement->GetBranchId()) {
55 return;
56 }
57
58 // even though the IfElement will be deleted, do not put to list of deleted elements
59 // because new Element with same elmtId will be created
60 ElementRegister::GetInstance()->RemoveItemSilently(ifElseElement->GetElementId());
61 ifElseElement->UnregisterChildrenForPartialUpdates();
62
63 auto ifElseParentElement = ifElseElement->GetElementParent().Upgrade();
64
65 ifElseParentElement->UpdateChildWithSlot(
66 ifElseElement, ifElseComponent, ifElseElement->GetSlot(), ifElseElement->GetRenderSlot());
67
68 ifElseElement->branchId_ = ifElseComponent->GetBranchId();
69 }
70 } // namespace OHOS::Ace
71