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_NAVIGATION_NAVIGATION_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_EVENT_HUB_H
18 
19 #include <optional>
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 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 using OnTitleModeChangeEvent = std::function<void(const BaseEventInfo* eventInfo)>;
29 using OnNavBarStateChangeEvent = std::function<void(bool)>;
30 using OnNavigationModeChangeEvent = std::function<void(NavigationMode mode)>;
31 
32 class NavigationEventHub : public EventHub {
DECLARE_ACE_TYPE(NavigationEventHub,EventHub)33     DECLARE_ACE_TYPE(NavigationEventHub, EventHub)
34 public:
35     void SetOnTitleModeChange(OnTitleModeChangeEvent&& changeEvent)
36     {
37         onTitleModeChangeEvent_ = changeEvent;
38     }
39 
FireChangeEvent(const BaseEventInfo * eventInfo)40     void FireChangeEvent(const BaseEventInfo* eventInfo) const
41     {
42         if (onTitleModeChangeEvent_) {
43             TAG_LOGI(AceLogTag::ACE_NAVIGATION, "change titleMode eventInfo");
44             onTitleModeChangeEvent_(eventInfo);
45         }
46     }
47 
SetOnNavBarStateChange(OnNavBarStateChangeEvent && changeEvent)48     void SetOnNavBarStateChange(OnNavBarStateChangeEvent&& changeEvent)
49     {
50         onNavBarStateChangeEvent_ = changeEvent;
51     }
52 
FireNavBarStateChangeEvent(bool isVisible)53     void FireNavBarStateChangeEvent(bool isVisible)
54     {
55         if (isVisible_.has_value()) {
56             if (isVisible_.value() != isVisible) {
57                 if (onNavBarStateChangeEvent_) {
58                     TAG_LOGI(AceLogTag::ACE_NAVIGATION, "NavBar Visible State Change %{public}s",
59                         isVisible ? "false -> true" : "true -> false");
60                     onNavBarStateChangeEvent_(isVisible);
61                 }
62             }
63         } else {
64             if (onNavBarStateChangeEvent_) {
65                 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "Set NavBar Visible State as %{public}s",
66                     isVisible ? "true" : "false");
67                 onNavBarStateChangeEvent_(isVisible);
68             }
69         }
70         isVisible_ = isVisible;
71     }
72 
SetOnNavigationModeChange(OnNavigationModeChangeEvent && modeChange)73     void SetOnNavigationModeChange(OnNavigationModeChangeEvent&& modeChange)
74     {
75         onNavigationModeChangeEvent_ = modeChange;
76     }
77 
FireNavigationModeChangeEvent(NavigationMode mode)78     void FireNavigationModeChangeEvent(NavigationMode mode)
79     {
80         if (onNavigationModeChangeEvent_) {
81             TAG_LOGI(AceLogTag::ACE_NAVIGATION, "Chanage NavigationMode as %{public}d", mode);
82             onNavigationModeChangeEvent_(mode);
83         }
84     }
85 
86     void FireOnAppear() override;
87 
88 private:
89     OnTitleModeChangeEvent onTitleModeChangeEvent_;
90     OnNavBarStateChangeEvent onNavBarStateChangeEvent_;
91     OnNavigationModeChangeEvent onNavigationModeChangeEvent_;
92 
93     std::optional<bool> isVisible_;
94 };
95 
96 } // namespace OHOS::Ace::NG
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_EVENT_HUB_H
99