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_PATTERNS_CHECKBOX_CHECKBOX_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_EVENT_HUB_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/event/event_hub.h"
21 #include "core/common/recorder/event_recorder.h"
22 #include "core/common/recorder/node_data_cache.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/event/gesture_event_hub.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 using ChangeEvent = std::function<void(const bool)>;
29 
30 class CheckBoxEventHub : public EventHub {
31     DECLARE_ACE_TYPE(CheckBoxEventHub, EventHub)
32 
33 public:
34     CheckBoxEventHub() = default;
35 
36     ~CheckBoxEventHub() override = default;
37 
SetOnChange(ChangeEvent && changeEvent)38     void SetOnChange(ChangeEvent&& changeEvent)
39     {
40         changeEvent_ = std::move(changeEvent);
41     }
42 
UpdateChangeEvent(bool select)43     void UpdateChangeEvent(bool select) const
44     {
45         if (selectChangeEvent_) {
46             selectChangeEvent_(select);
47         }
48         if (changeEvent_) {
49             changeEvent_(select);
50         }
51         if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) {
52             Recorder::EventParamsBuilder builder;
53             auto host = GetFrameNode();
54             if (host) {
55                 auto id = host->GetInspectorIdValue("");
56                 builder.SetId(id).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue(""));
57                 if (!id.empty()) {
58                     Recorder::NodeDataCache::Get().PutMultiple(host, id, name_, select);
59                 }
60             }
61             builder.SetChecked(select).SetText(name_);
62             Recorder::EventRecorder::Get().OnChange(std::move(builder));
63         }
64     }
65 
GetName()66     const std::string& GetName()
67     {
68         return name_;
69     }
70 
GetGroupName()71     const std::string& GetGroupName()
72     {
73         return groupname_;
74     }
75 
SetName(const std::string & name)76     void SetName(const std::string& name)
77     {
78         name_ = name;
79     }
80 
SetGroupName(const std::string & groupname)81     void SetGroupName(const std::string& groupname)
82     {
83         groupname_ = groupname;
84     }
85 
SetChangeEvent(ChangeEvent && changeEvent)86     void SetChangeEvent(ChangeEvent&& changeEvent)
87     {
88         selectChangeEvent_ = std::move(changeEvent);
89     }
90 
91 private:
92     ChangeEvent changeEvent_;
93     std::string name_;
94     std::string groupname_;
95     ChangeEvent selectChangeEvent_;
96 
97     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxEventHub);
98 };
99 
100 } // namespace OHOS::Ace::NG
101 
102 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_EVENT_HUB_H
103