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_TIME_PICKER_TIME_PICKER_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TIME_PICKER_TIME_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 TimeChangeEvent = std::function<void(const BaseEventInfo* info)>; 25 using DialogEvent = std::function<void(const std::string&)>; 26 using DialogCancelEvent = std::function<void()>; 27 using DialogGestureEvent = std::function<void(const GestureEvent& info)>; 28 using TimeChangeEventForDatePicker = std::function<void()>; 29 30 class TimePickerEventHub : public EventHub { 31 DECLARE_ACE_TYPE(TimePickerEventHub, EventHub) 32 33 public: 34 TimePickerEventHub() = default; 35 ~TimePickerEventHub() override = default; 36 SetOnChange(TimeChangeEvent && changeEvent)37 void SetOnChange(TimeChangeEvent&& changeEvent) 38 { 39 changeEvent_ = std::move(changeEvent); 40 } 41 SetOnChangeForDatePicker(TimeChangeEventForDatePicker && changeEvent)42 void SetOnChangeForDatePicker(TimeChangeEventForDatePicker&& changeEvent) 43 { 44 changeEventForDatePicker_ = std::move(changeEvent); 45 } 46 FireChangeEvent(const BaseEventInfo * info)47 void FireChangeEvent(const BaseEventInfo* info) const 48 { 49 if (selectedTimeChangeEvent_) { 50 selectedTimeChangeEvent_(info); 51 } 52 if (changeEvent_) { 53 changeEvent_(info); 54 } 55 if (changeEventForDatePicker_) { 56 changeEventForDatePicker_(); 57 } 58 } 59 SetDialogChange(DialogEvent && onChange)60 void SetDialogChange(DialogEvent&& onChange) 61 { 62 DialogChangeEvent_ = std::move(onChange); 63 } 64 FireDialogChangeEvent(const std::string & info)65 void FireDialogChangeEvent(const std::string& info) const 66 { 67 if (DialogChangeEvent_) { 68 DialogChangeEvent_(info); 69 } 70 } 71 SetDialogAcceptEvent(DialogEvent && onChange)72 void SetDialogAcceptEvent(DialogEvent&& onChange) 73 { 74 DialogAcceptEvent_ = std::move(onChange); 75 } 76 FireDialogAcceptEvent(const std::string & info)77 void FireDialogAcceptEvent(const std::string& info) const 78 { 79 if (DialogAcceptEvent_) { 80 DialogAcceptEvent_(info); 81 } 82 } 83 SetChangeEvent(TimeChangeEvent && changeEvent)84 void SetChangeEvent(TimeChangeEvent&& changeEvent) 85 { 86 selectedTimeChangeEvent_ = std::move(changeEvent); 87 } 88 89 private: 90 TimeChangeEvent changeEvent_; 91 DialogEvent DialogChangeEvent_; 92 DialogEvent DialogAcceptEvent_; 93 TimeChangeEvent selectedTimeChangeEvent_; 94 TimeChangeEventForDatePicker changeEventForDatePicker_; 95 96 ACE_DISALLOW_COPY_AND_MOVE(TimePickerEventHub); 97 }; 98 99 } // namespace OHOS::Ace::NG 100 101 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TIME_PICKER_TIME_PICKER_EVENT_HUB_H 102