• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 onStateChangeEvent_(isActivated);
47             }
48         }
49         isActivated_ = isActivated;
50     }
51 
SetOnShown(const std::function<void ()> & onShown)52     void SetOnShown(const std::function<void()>& onShown)
53     {
54         onShownEvent_ = onShown;
55     }
56 
FireOnShownEvent(const std::string & name,const std::string & param)57     void FireOnShownEvent(const std::string& name, const std::string& param) const
58     {
59         UIObserverHandler::GetInstance().NotifyNavigationStateChange(GetNavDestinationPattern(),
60                                                                      NavDestinationState::ON_SHOWN);
61         if (onShownEvent_) {
62             auto onShownEvent = onShownEvent_;
63             onShownEvent();
64         }
65         if (Recorder::EventRecorder::Get().IsPageRecordEnable()) {
66             auto host = GetFrameNode();
67             CHECK_NULL_VOID(host);
68             auto id = host->GetInspectorIdValue("");
69             Recorder::EventParamsBuilder builder;
70             builder.SetId(id)
71                 .SetText(name)
72                 .SetExtra(Recorder::KEY_PAGE_PARAM, param)
73                 .SetDescription(host->GetAutoEventParamValue(""));
74             Recorder::EventRecorder::Get().OnNavDstShow(std::move(builder));
75         }
76     }
77 
SetOnHidden(const std::function<void ()> & onHidden)78     void SetOnHidden(const std::function<void()>& onHidden)
79     {
80         onHiddenEvent_ = onHidden;
81     }
82 
FireOnHiddenEvent(const std::string & name)83     void FireOnHiddenEvent(const std::string& name) const
84     {
85         UIObserverHandler::GetInstance().NotifyNavigationStateChange(GetNavDestinationPattern(),
86                                                                      NavDestinationState::ON_HIDDEN);
87         if (onHiddenEvent_) {
88             onHiddenEvent_();
89         }
90         if (Recorder::EventRecorder::Get().IsPageRecordEnable()) {
91             auto host = GetFrameNode();
92             CHECK_NULL_VOID(host);
93             auto id = host->GetInspectorIdValue("");
94             Recorder::EventParamsBuilder builder;
95             builder.SetId(id).SetText(name).SetDescription(host->GetAutoEventParamValue(""));
96             Recorder::EventRecorder::Get().OnNavDstHide(std::move(builder));
97         }
98     }
99 
SetOnBackPressed(const std::function<bool ()> & onBackPressed)100     void SetOnBackPressed(const std::function<bool()>& onBackPressed)
101     {
102         onBackPressedEvent_ = onBackPressed;
103     }
104 
GetOnBackPressedEvent()105     std::function<bool()> GetOnBackPressedEvent() const
106     {
107         return onBackPressedEvent_;
108     }
109 
FireOnBackPressedEvent()110     bool FireOnBackPressedEvent()
111     {
112         if (onBackPressedEvent_) {
113             return onBackPressedEvent_();
114         }
115         return false;
116     }
117 
SetOnReady(const std::function<void (RefPtr<NavDestinationContext>)> & onReady)118     void SetOnReady(const std::function<void(RefPtr<NavDestinationContext>)>& onReady)
119     {
120         onReadyEvent_ = onReady;
121     }
122 
GetOnReady()123     std::function<void(RefPtr<NavDestinationContext>)> GetOnReady() const
124     {
125         return onReadyEvent_;
126     }
127 
FireOnReady(RefPtr<NavDestinationContext> context)128     void FireOnReady(RefPtr<NavDestinationContext> context)
129     {
130         if (onReadyEvent_) {
131             onReadyEvent_(context);
132         }
133     }
134 
135 private:
GetNavDestinationPattern()136     WeakPtr<AceType> GetNavDestinationPattern() const
137     {
138         auto node = GetFrameNode();
139         CHECK_NULL_RETURN(node, nullptr);
140         return node->GetPattern();
141     }
142 
143     OnStateChangeEvent onStateChangeEvent_;
144     std::function<void()> onShownEvent_;
145     std::function<void()> onHiddenEvent_;
146     std::function<bool()> onBackPressedEvent_;
147     std::function<void(RefPtr<NavDestinationContext>)> onReadyEvent_;
148 
149     bool isActivated_ = false;
150 };
151 } // namespace OHOS::Ace::NG
152 
153 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H
154