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_RADIO_RADIO_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RADIO_RADIO_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_ng/base/frame_node.h"
23 #include "core/components_ng/event/event_hub.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 using ChangeEvent = std::function<void(const bool)>;
28 
29 class RadioEventHub : public EventHub {
30     DECLARE_ACE_TYPE(RadioEventHub, EventHub)
31 
32 public:
33     RadioEventHub() = default;
34     ~RadioEventHub() override = default;
35 
SetOnChange(ChangeEvent && changeEvent)36     void SetOnChange(ChangeEvent&& changeEvent)
37     {
38         changeEvent_ = std::move(changeEvent);
39     }
40 
UpdateChangeEvent(bool check)41     void UpdateChangeEvent(bool check) const
42     {
43         if (checkedChangeEvent_) {
44             checkedChangeEvent_(check);
45         }
46         if (changeEvent_) {
47             changeEvent_(check);
48         }
49         if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) {
50             Recorder::EventParamsBuilder builder;
51             auto host = GetFrameNode();
52             if (host) {
53                 auto id = host->GetInspectorIdValue("");
54                 builder.SetId(id).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue(""));
55                 if (!id.empty()) {
56                     Recorder::NodeDataCache::Get().PutMultiple(host, id, value_, check);
57                 }
58             }
59             builder.SetChecked(check).SetText(value_);
60             Recorder::EventRecorder::Get().OnChange(std::move(builder));
61         }
62     }
63 
GetValue()64     const std::string& GetValue()
65     {
66         return value_;
67     }
68 
GetGroup()69     const std::string& GetGroup()
70     {
71         return group_;
72     }
73 
SetValue(const std::string & value)74     void SetValue(const std::string& value)
75     {
76         value_ = value;
77     }
78 
SetGroup(const std::string & group)79     void SetGroup(const std::string& group)
80     {
81         group_ = group;
82     }
83 
SetOnChangeEvent(ChangeEvent && checkedChangeEvent)84     void SetOnChangeEvent(ChangeEvent&& checkedChangeEvent)
85     {
86         checkedChangeEvent_ = std::move(checkedChangeEvent);
87     }
88 
89 private:
90     ChangeEvent changeEvent_;
91     std::string value_;
92     std::string group_;
93     ChangeEvent checkedChangeEvent_;
94 };
95 
96 } // namespace OHOS::Ace::NG
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RADIO_RADIO_EVENT_HUB_H
99