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 SetDialogChange(DialogTextEvent && onChange)56 void SetDialogChange(DialogTextEvent&& onChange) 57 { 58 DialogChangeEvent_ = std::move(onChange); 59 } 60 FireDialogChangeEvent(const std::string & info)61 void FireDialogChangeEvent(const std::string& info) const 62 { 63 if (DialogChangeEvent_) { 64 DialogChangeEvent_(info); 65 } 66 } 67 SetDialogAcceptEvent(DialogTextEvent && onChange)68 void SetDialogAcceptEvent(DialogTextEvent&& onChange) 69 { 70 DialogAcceptEvent_ = std::move(onChange); 71 } 72 FireDialogAcceptEvent(const std::string & info)73 void FireDialogAcceptEvent(const std::string& info) const 74 { 75 if (DialogAcceptEvent_) { 76 DialogAcceptEvent_(info); 77 } 78 } 79 SetOnValueChangeEvent(TextValueChangeEvent && onValueChangeEvent)80 void SetOnValueChangeEvent(TextValueChangeEvent&& onValueChangeEvent) 81 { 82 onValueChangeEvent_ = std::move(onValueChangeEvent); 83 } 84 SetOnSelectedChangeEvent(TextSelectedChangeEvent && onSelectedChangeEvent)85 void SetOnSelectedChangeEvent(TextSelectedChangeEvent&& onSelectedChangeEvent) 86 { 87 onSelectedChangeEvent_ = std::move(onSelectedChangeEvent); 88 } 89 private: 90 TextChangeEvent TextChangeEvent_; 91 DialogTextEvent DialogChangeEvent_; 92 DialogTextEvent DialogAcceptEvent_; 93 TextValueChangeEvent onValueChangeEvent_; 94 TextSelectedChangeEvent onSelectedChangeEvent_; 95 96 ACE_DISALLOW_COPY_AND_MOVE(TextPickerEventHub); 97 }; 98 99 } // namespace OHOS::Ace::NG 100 101 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_PICKER_TEXT_PICKER_EVENT_HUB_H 102