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_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_FOR_EACH_MODEL_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_FOR_EACH_MODEL_NG_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/macros.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/syntax/lazy_for_each_builder.h"
23 #include "core/components_ng/syntax/lazy_for_each_model.h"
24 #include "core/components_ng/syntax/lazy_for_each_node.h"
25 
26 namespace OHOS::Ace::NG {
27 class ACE_EXPORT LazyForEachModelNG : public LazyForEachModel {
28 public:
Create(const RefPtr<LazyForEachActuator> & actuator)29     void Create(const RefPtr<LazyForEachActuator>& actuator) override
30     {
31         auto builder = AceType::DynamicCast<LazyForEachBuilder>(actuator);
32         Create(builder);
33     }
34 
OnMove(std::function<void (int32_t,int32_t)> && onMove)35     void OnMove(std::function<void(int32_t, int32_t)>&& onMove) override
36     {
37         auto* stack = ViewStackProcessor::GetInstance();
38         auto node = AceType::DynamicCast<LazyForEachNode>(stack->GetMainElementNode());
39         CHECK_NULL_VOID(node);
40         node->SetOnMove(std::move(onMove));
41     }
42 
43 private:
Create(const RefPtr<LazyForEachBuilder> & forEachBuilder)44     void Create(const RefPtr<LazyForEachBuilder>& forEachBuilder)
45     {
46         auto* stack = ViewStackProcessor::GetInstance();
47         auto nodeId = stack->ClaimNodeId();
48 
49         if (stack->GetMainFrameNode() && stack->GetMainFrameNode()->GetTag() == V2::TABS_ETS_TAG) {
50             forEachBuilder->ExpandChildrenOnInitial();
51             return;
52         }
53 
54         auto lazyForEach = LazyForEachNode::GetOrCreateLazyForEachNode(nodeId, forEachBuilder);
55         stack->Push(lazyForEach);
56     }
57 };
58 } // namespace OHOS::Ace::NG
59 
60 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_FOR_EACH_MODEL_NG_H
61