1 /*
2  * Copyright (c) 2022-2023 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_TEXT_PICKER_TEXT_PICKER_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_PICKER_TEXT_PICKER_EVENT_HUB_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/event/event_hub.h"
21 
22 namespace OHOS::Ace::NG {
23 
24 using TextChangeEvent = std::function<void(const std::vector<std::string>&, const std::vector<double>&)>;
25 using TextValueChangeEvent = std::function<void(const std::vector<std::string>&)>;
26 using TextSelectedChangeEvent = std::function<void(const std::vector<double>&)>;
27 using DialogTextEvent = std::function<void(const std::string&)>;
28 using DialogCancelEvent = std::function<void()>;
29 using DialogGestureEvent = std::function<void(const GestureEvent& info)>;
30 
31 class TextPickerEventHub : public EventHub {
32     DECLARE_ACE_TYPE(TextPickerEventHub, EventHub)
33 
34 public:
35     TextPickerEventHub() = default;
36     ~TextPickerEventHub() override = default;
37 
SetOnChange(TextChangeEvent && TextChangeEvent)38     void SetOnChange(TextChangeEvent&& TextChangeEvent)
39     {
40         TextChangeEvent_ = std::move(TextChangeEvent);
41     }
42 
FireChangeEvent(const std::vector<std::string> & value,const std::vector<double> & index)43     void FireChangeEvent(const std::vector<std::string>& value, const std::vector<double>& index) const
44     {
45         if (onValueChangeEvent_) {
46             onValueChangeEvent_(value);
47         }
48         if (onSelectedChangeEvent_) {
49             onSelectedChangeEvent_(index);
50         }
51         if (TextChangeEvent_) {
52             TextChangeEvent_(value, index);
53         }
54     }
55 
SetOnScrollStop(TextChangeEvent && onScrollStopEvent)56     void SetOnScrollStop(TextChangeEvent&& onScrollStopEvent)
57     {
58         onScrollStopEvent_ = std::move(onScrollStopEvent);
59     }
60 
FireScrollStopEvent(const std::vector<std::string> & value,const std::vector<double> & index)61     void FireScrollStopEvent(const std::vector<std::string>& value, const std::vector<double>& index) const
62     {
63         if (onScrollStopEvent_) {
64             onScrollStopEvent_(value, index);
65         }
66     }
67 
SetDialogChange(DialogTextEvent && onChange)68     void SetDialogChange(DialogTextEvent&& onChange)
69     {
70         DialogChangeEvent_ = std::move(onChange);
71     }
72 
FireDialogChangeEvent(const std::string & info)73     void FireDialogChangeEvent(const std::string& info) const
74     {
75         if (DialogChangeEvent_) {
76             DialogChangeEvent_(info);
77         }
78     }
79 
SetDialogScrollStop(DialogTextEvent && onScrollStopEvent)80     void SetDialogScrollStop(DialogTextEvent&& onScrollStopEvent)
81     {
82         dialogScrollStopEvent_ = std::move(onScrollStopEvent);
83     }
84 
FireDialogScrollStopEvent(const std::string & info)85     void FireDialogScrollStopEvent(const std::string& info) const
86     {
87         if (dialogScrollStopEvent_) {
88             dialogScrollStopEvent_(info);
89         }
90     }
91 
SetDialogAcceptEvent(DialogTextEvent && onChange)92     void SetDialogAcceptEvent(DialogTextEvent&& onChange)
93     {
94         DialogAcceptEvent_ = std::move(onChange);
95     }
96 
FireDialogAcceptEvent(const std::string & info)97     void FireDialogAcceptEvent(const std::string& info) const
98     {
99         if (DialogAcceptEvent_) {
100             DialogAcceptEvent_(info);
101         }
102     }
103 
SetOnValueChangeEvent(TextValueChangeEvent && onValueChangeEvent)104     void SetOnValueChangeEvent(TextValueChangeEvent&& onValueChangeEvent)
105     {
106         onValueChangeEvent_ = std::move(onValueChangeEvent);
107     }
108 
SetOnSelectedChangeEvent(TextSelectedChangeEvent && onSelectedChangeEvent)109     void SetOnSelectedChangeEvent(TextSelectedChangeEvent&& onSelectedChangeEvent)
110     {
111         onSelectedChangeEvent_ = std::move(onSelectedChangeEvent);
112     }
113 private:
114     TextChangeEvent TextChangeEvent_;
115     TextChangeEvent onScrollStopEvent_;
116     DialogTextEvent DialogChangeEvent_;
117     DialogTextEvent dialogScrollStopEvent_;
118     DialogTextEvent DialogAcceptEvent_;
119     TextValueChangeEvent onValueChangeEvent_;
120     TextSelectedChangeEvent onSelectedChangeEvent_;
121 
122     ACE_DISALLOW_COPY_AND_MOVE(TextPickerEventHub);
123 };
124 
125 } // namespace OHOS::Ace::NG
126 
127 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_PICKER_TEXT_PICKER_EVENT_HUB_H
128