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