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_PATTERN_CALENDAR_CALENDAR_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CALENDAR_CALENDAR_EVENT_HUB_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/components_ng/event/event_hub.h"
23 #include "core/components_ng/event/gesture_event_hub.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 using DialogEvent = std::function<void(const std::string&)>;
28 using DialogCancelEvent = std::function<void()>;
29 using DialogGestureEvent = std::function<void(const GestureEvent& info)>;
30 using SelectedChangeEvent = std::function<void(const std::string&)>;
31 using RequestDataEvent = std::function<void(const std::string&)>;
32 
33 class CalendarEventHub : public EventHub {
34     DECLARE_ACE_TYPE(CalendarEventHub, EventHub)
35 
36 public:
37     CalendarEventHub() = default;
38     ~CalendarEventHub() override = default;
39 
SetSelectedChangeEvent(const SelectedChangeEvent & selectedChangeEvent)40     void SetSelectedChangeEvent(const SelectedChangeEvent& selectedChangeEvent)
41     {
42         changeEvent_ = selectedChangeEvent;
43     }
44 
UpdateSelectedChangeEvent(const std::string & info)45     void UpdateSelectedChangeEvent(const std::string& info) const
46     {
47         if (changeEvent_) {
48             changeEvent_(info);
49         }
50     }
SetOnRequestDataEvent(RequestDataEvent && requestDataEvent)51     void SetOnRequestDataEvent(RequestDataEvent&& requestDataEvent)
52     {
53         requestDataEvent_ = std::move(requestDataEvent);
54     }
55 
GetOnRequestDataEvent()56     const RequestDataEvent& GetOnRequestDataEvent()
57     {
58         return requestDataEvent_;
59     }
60 
UpdateRequestDataEvent(const std::string & info)61     void UpdateRequestDataEvent(const std::string& info) const
62     {
63         if (requestDataEvent_) {
64             requestDataEvent_(info);
65         }
66     }
67 
SetDialogChange(DialogEvent && onChange)68     void SetDialogChange(DialogEvent&& 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             TAG_LOGD(AceLogTag::ACE_DIALOG, "start calendar dialog change event");
77             dialogChangeEvent_(info);
78         }
79     }
80 
SetDialogAcceptEvent(DialogEvent && onAccept)81     void SetDialogAcceptEvent(DialogEvent&& onAccept)
82     {
83         dialogAcceptEvent_ = std::move(onAccept);
84     }
85 
FireDialogAcceptEvent(const std::string & info)86     void FireDialogAcceptEvent(const std::string& info) const
87     {
88         if (dialogAcceptEvent_) {
89             dialogAcceptEvent_(info);
90         }
91     }
92 
93 private:
94     SelectedChangeEvent changeEvent_;
95     RequestDataEvent requestDataEvent_;
96     DialogEvent dialogChangeEvent_;
97     DialogEvent dialogAcceptEvent_;
98 
99     ACE_DISALLOW_COPY_AND_MOVE(CalendarEventHub);
100 };
101 
102 } // namespace OHOS::Ace::NG
103 
104 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CALENDAR_CALENDAR_EVENT_HUB_H