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_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/common/recorder/event_recorder.h" 21 #include "core/components_ng/base/observer_handler.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/navrouter/navdestination_context.h" 25 26 namespace OHOS::Ace::NG { 27 using OnStateChangeEvent = std::function<void(bool)>; 28 using namespace Framework; 29 class NavDestinationEventHub : public EventHub { DECLARE_ACE_TYPE(NavDestinationEventHub,EventHub)30 DECLARE_ACE_TYPE(NavDestinationEventHub, EventHub) 31 public: 32 void SetOnStateChange(const OnStateChangeEvent& changeEvent) 33 { 34 onStateChangeEvent_ = changeEvent; 35 } 36 GetOnStateChange()37 const OnStateChangeEvent& GetOnStateChange() 38 { 39 return onStateChangeEvent_; 40 } 41 FireChangeEvent(bool isActivated)42 void FireChangeEvent(bool isActivated) 43 { 44 if (isActivated_ != isActivated) { 45 if (onStateChangeEvent_) { 46 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "navDestination state set to %{public}s.", 47 isActivated ? "Activated" : "Deactivated"); 48 onStateChangeEvent_(isActivated); 49 } 50 } 51 isActivated_ = isActivated; 52 } 53 SetOnShown(const std::function<void ()> & onShown)54 void SetOnShown(const std::function<void()>& onShown) 55 { 56 onShownEvent_ = onShown; 57 } 58 59 void FireOnShownEvent(const std::string& name, const std::string& param); 60 SetOnHidden(const std::function<void ()> & onHidden)61 void SetOnHidden(const std::function<void()>& onHidden) 62 { 63 onHiddenEvent_ = onHidden; 64 } 65 66 void FireOnHiddenEvent(const std::string& name); 67 SetOnBackPressed(const std::function<bool ()> & onBackPressed)68 void SetOnBackPressed(const std::function<bool()>& onBackPressed) 69 { 70 onBackPressedEvent_ = onBackPressed; 71 } 72 GetOnBackPressedEvent()73 std::function<bool()> GetOnBackPressedEvent() const 74 { 75 return onBackPressedEvent_; 76 } 77 78 bool FireOnBackPressedEvent(); 79 80 void FireOnAppear() override; 81 FireDisappearCallback()82 void FireDisappearCallback() 83 { 84 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "%{public}s lifecycle change to onDisappear state.", name_.c_str()); 85 EventHub::FireOnDisappear(); 86 } 87 88 void FireOnDisappear() override; 89 SetOnReady(const std::function<void (RefPtr<NavDestinationContext>)> & onReady)90 void SetOnReady(const std::function<void(RefPtr<NavDestinationContext>)>& onReady) 91 { 92 onReadyEvent_ = onReady; 93 } 94 GetOnReady()95 std::function<void(RefPtr<NavDestinationContext>)> GetOnReady() const 96 { 97 return onReadyEvent_; 98 } 99 FireOnReady(RefPtr<NavDestinationContext> context)100 void FireOnReady(RefPtr<NavDestinationContext> context) 101 { 102 if (onReadyEvent_) { 103 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "navDestination context is ready."); 104 onReadyEvent_(context); 105 } 106 } 107 AddOnHiddenChange(int32_t id,OnStateChangeEvent && onHiddenChange)108 void AddOnHiddenChange(int32_t id, OnStateChangeEvent&& onHiddenChange) 109 { 110 onHiddenChange_[id] = std::move(onHiddenChange); 111 } 112 FireOnHiddenChange(bool flag)113 void FireOnHiddenChange(bool flag) 114 { 115 for (auto& onHiddenChangeInfo : onHiddenChange_) { 116 if (onHiddenChangeInfo.second) { 117 auto onHiddenChange = onHiddenChangeInfo.second; 118 onHiddenChange(flag); 119 } 120 } 121 } 122 RemoveOnHiddenChange(int32_t id)123 void RemoveOnHiddenChange(int32_t id) 124 { 125 auto iter = onHiddenChange_.find(id); 126 if (iter != onHiddenChange_.end()) { 127 onHiddenChange_.erase(iter); 128 } 129 } 130 SetName(const std::string & name)131 void SetName(const std::string& name) 132 { 133 name_ = name; 134 } 135 SetOnWillAppear(std::function<void ()> & willAppear)136 void SetOnWillAppear(std::function<void()>& willAppear) 137 { 138 onWillAppear_ = std::move(willAppear); 139 } 140 141 void FireOnWillAppear(); 142 SetOnWillShow(std::function<void ()> & willShow)143 void SetOnWillShow(std::function<void()>& willShow) 144 { 145 onWillShow_ = std::move(willShow); 146 } 147 148 void FireOnWillShow(); 149 SetOnWillHide(std::function<void ()> & willHide)150 void SetOnWillHide(std::function<void()>& willHide) 151 { 152 onWillHide_ = std::move(willHide); 153 } 154 155 void FireOnWillHide(); 156 SetOnWillDisAppear(std::function<void ()> & willDisAppear)157 void SetOnWillDisAppear(std::function<void()>& willDisAppear) 158 { 159 onWillDisAppear_ = std::move(willDisAppear); 160 } 161 162 void FireOnWillDisAppear(); 163 GetState()164 NavDestinationState GetState() 165 { 166 return state_; 167 } 168 SetState(NavDestinationState state)169 void SetState(NavDestinationState state) 170 { 171 state_ = state; 172 } 173 174 private: GetNavDestinationPattern()175 WeakPtr<AceType> GetNavDestinationPattern() const 176 { 177 auto node = GetFrameNode(); 178 CHECK_NULL_RETURN(node, nullptr); 179 return node->GetPattern(); 180 } 181 182 void FireAutoSave(); 183 184 OnStateChangeEvent onStateChangeEvent_; 185 std::function<void()> onShownEvent_; 186 std::function<void()> onHiddenEvent_; 187 std::function<void()> onWillAppear_; 188 std::function<void()> onWillShow_; 189 std::function<void()> onWillHide_; 190 std::function<void()> onWillDisAppear_; 191 std::function<bool()> onBackPressedEvent_; 192 std::function<void(RefPtr<NavDestinationContext>)> onReadyEvent_; 193 std::unordered_map<int32_t, OnStateChangeEvent> onHiddenChange_; 194 std::string name_; 195 bool isActivated_ = false; 196 NavDestinationState state_; 197 }; 198 } // namespace OHOS::Ace::NG 199 200 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 201