• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "core/components_ng/base/observer_handler.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
20 
21 namespace OHOS::Ace::NG {
22 namespace {
GetNavigationId(const RefPtr<NavDestinationPattern> & pattern)23 std::string GetNavigationId(const RefPtr<NavDestinationPattern>& pattern)
24 {
25     CHECK_NULL_RETURN(pattern, "");
26     auto navigationNode = pattern->GetNavigationNode();
27     std::string navigationId;
28     if (navigationNode) {
29         navigationId = navigationNode->GetInspectorId().value_or("");
30     }
31     return navigationId;
32 }
33 } // namespace
34 
GetInstance()35 UIObserverHandler& UIObserverHandler::GetInstance()
36 {
37     static UIObserverHandler instance;
38     return instance;
39 }
40 
NotifyNavigationStateChange(const WeakPtr<AceType> & weakPattern,NavDestinationState state)41 void UIObserverHandler::NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state)
42 {
43     auto ref = weakPattern.Upgrade();
44     CHECK_NULL_VOID(ref);
45     auto pattern = AceType::DynamicCast<NavDestinationPattern>(ref);
46     CHECK_NULL_VOID(pattern);
47     std::string navigationId = GetNavigationId(pattern);
48     std::string navDestinationName = pattern->GetName();
49     CHECK_NULL_VOID(navigationHandleFunc_);
50     navigationHandleFunc_(navigationId, navDestinationName, state);
51 }
52 
NotifyRouterPageStateChange(const RefPtr<PageInfo> & pageInfo,RouterPageState state)53 void UIObserverHandler::NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state)
54 {
55     CHECK_NULL_VOID(pageInfo);
56     CHECK_NULL_VOID(routerPageHandleFunc_);
57     napi_value context = GetUIContextValue();
58     AbilityContextInfo info = {
59         AceApplicationInfo::GetInstance().GetAbilityName(),
60         AceApplicationInfo::GetInstance().GetProcessName(),
61         Container::Current()->GetModuleName()
62     };
63     int32_t index = pageInfo->GetPageIndex();
64     std::string name = pageInfo->GetPageUrl();
65     std::string path = pageInfo->GetPagePath();
66     routerPageHandleFunc_(info, context, index, name, path, state);
67 }
68 
GetNavigationState(const RefPtr<AceType> & node)69 std::shared_ptr<NavDestinationInfo> UIObserverHandler::GetNavigationState(const RefPtr<AceType>& node)
70 {
71     CHECK_NULL_RETURN(node, nullptr);
72     auto current = AceType::DynamicCast<UINode>(node);
73     while (current) {
74         if (current->GetTag() == V2::NAVDESTINATION_VIEW_ETS_TAG) {
75             break;
76         }
77         current = current->GetParent();
78     }
79     CHECK_NULL_RETURN(current, nullptr);
80     auto nav = AceType::DynamicCast<FrameNode>(current);
81     CHECK_NULL_RETURN(nav, nullptr);
82     auto pattern = nav->GetPattern<NavDestinationPattern>();
83     CHECK_NULL_RETURN(pattern, nullptr);
84     return std::make_shared<NavDestinationInfo>(
85         GetNavigationId(pattern),
86         pattern->GetName(),
87         pattern->GetIsOnShow() ? NavDestinationState::ON_SHOWN : NavDestinationState::ON_HIDDEN);
88 }
89 
GetRouterPageState(const RefPtr<AceType> & node)90 std::shared_ptr<RouterPageInfoNG> UIObserverHandler::GetRouterPageState(const RefPtr<AceType>& node)
91 {
92     CHECK_NULL_RETURN(node, nullptr);
93     auto current = AceType::DynamicCast<UINode>(node);
94     while (current) {
95         if (current->GetTag() == V2::PAGE_ETS_TAG) {
96             break;
97         }
98         current = current->GetParent();
99     }
100     CHECK_NULL_RETURN(current, nullptr);
101     auto routerPage = AceType::DynamicCast<FrameNode>(current);
102     CHECK_NULL_RETURN(routerPage, nullptr);
103     auto pattern = routerPage->GetPattern<PagePattern>();
104     CHECK_NULL_RETURN(pattern, nullptr);
105     auto pageInfo = pattern->GetPageInfo();
106     int32_t index = pageInfo->GetPageIndex();
107     std::string name = pageInfo->GetPageUrl();
108     std::string path = pageInfo->GetPagePath();
109     return std::make_shared<RouterPageInfoNG>(
110         GetUIContextValue(),
111         index,
112         name,
113         path,
114         RouterPageState(pattern->GetPageState()));
115 }
116 
SetHandleNavigationChangeFunc(NavigationHandleFunc func)117 void UIObserverHandler::SetHandleNavigationChangeFunc(NavigationHandleFunc func)
118 {
119     navigationHandleFunc_ = func;
120 }
121 
SetHandleRouterPageChangeFunc(RouterPageHandleFunc func)122 void UIObserverHandler::SetHandleRouterPageChangeFunc(RouterPageHandleFunc func)
123 {
124     routerPageHandleFunc_ = func;
125 }
126 
GetUIContextValue()127 napi_value UIObserverHandler::GetUIContextValue()
128 {
129     auto container = Container::Current();
130     CHECK_NULL_RETURN(container, nullptr);
131     auto frontend = container->GetFrontend();
132     CHECK_NULL_RETURN(frontend, nullptr);
133     return frontend->GetContextValue();
134 }
135 } // namespace OHOS::Ace::NG
136