1 
2 /*
3  * Copyright (c) 2023 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H
18 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H
19 
20 #include <functional>
21 #include <optional>
22 
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/base/ui_node.h"
25 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
26 #include "core/components_ng/pattern/navrouter/navdestination_context.h"
27 
28 namespace OHOS::Ace::NG {
29 using NavPathList = std::vector<std::pair<std::string, RefPtr<UINode>>>;
30 class NavDestinationContext;
31 class RouteInfo : public virtual AceType {
32     DECLARE_ACE_TYPE(NG::RouteInfo, AceType)
33 public:
34     RouteInfo() = default;
35     virtual ~RouteInfo() = default;
36 
GetName()37     virtual std::string GetName()
38     {
39         return "";
40     }
41 };
42 
43 class NavigationStack : public virtual AceType {
44     DECLARE_ACE_TYPE(NG::NavigationStack, AceType)
45 public:
46     NavigationStack() = default;
47     ~NavigationStack() override = default;
48 
UpdateStackInfo(const RefPtr<NavigationStack> & newStack)49     virtual void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) {}
50 
GetAllNavDestinationNodes()51     NavPathList& GetAllNavDestinationNodes()
52     {
53         return navPathList_;
54     }
55 
SetOnStateChangedCallback(std::function<void ()> callback)56     virtual void SetOnStateChangedCallback(std::function<void()> callback) {}
57 
SavePreNavList()58     void SavePreNavList()
59     {
60         // same navdestination nodes before poped
61         navPathListBeforePoped_.clear();
62         for (auto iter: preNavPathList_) {
63             navPathListBeforePoped_.emplace_back(std::make_pair(iter.first, WeakPtr<UINode>(iter.second)));
64         }
65     }
66 
SetNavPathList(const NavPathList & navPathList)67     void SetNavPathList(const NavPathList& navPathList)
68     {
69         // save pre nav path list when poped
70         SavePreNavList();
71         preNavPathList_ = navPathList;
72         //copy nav path
73         navPathList_ = navPathList;
74     }
75 
GetAllNavDestinationNodesPrev()76     std::vector<std::pair<std::string, WeakPtr<UINode>>>& GetAllNavDestinationNodesPrev()
77     {
78         return navPathListBeforePoped_;
79     }
80 
Empty()81     bool Empty() const
82     {
83         return navPathList_.empty();
84     }
85 
Size()86     int32_t Size() const
87     {
88         return static_cast<int32_t>(navPathList_.size());
89     }
90 
PreSize()91     int32_t PreSize() const
92     {
93         return static_cast<int32_t>(preNavPathList_.size());
94     }
95 
GetTopNavPath()96     std::optional<std::pair<std::string, RefPtr<UINode>>> GetTopNavPath() const
97     {
98         if (navPathList_.empty()) {
99             return std::nullopt;
100         }
101         return navPathList_.back();
102     }
103 
GetPreTopNavPath()104     std::optional<std::pair<std::string, RefPtr<UINode>>> GetPreTopNavPath() const
105     {
106         if (preNavPathList_.empty()) {
107             return std::nullopt;
108         }
109         return preNavPathList_.back();
110     }
111 
RemoveStack()112     void RemoveStack()
113     {
114         navPathList_.clear();
115     }
116 
117     NavPathList GetAllCacheNodes();
118     void AddCacheNode(const std::string& name, const RefPtr<UINode>& uiNode);
119     RefPtr<UINode> GetFromCacheNode(NavPathList& cacheNodes, const std::string& name);
120     RefPtr<UINode> GetFromCacheNode(const std::string& name);
121     std::optional<std::pair<std::string, RefPtr<UINode>>> GetFromCacheNode(int32_t handle);
122     void RemoveCacheNode(
123         NavPathList& cacheNodes, const std::string& name, const RefPtr<UINode>& navDestinationNode);
124     void RemoveCacheNode(int32_t handle);
125     void ReOrderCache(const std::string& name, const RefPtr<UINode>& navDestinationNode);
126 
127     void Remove();
128     void Remove(const std::string& name);
129     void Remove(const std::string& name, const RefPtr<UINode>& navDestinationNode);
130     int32_t RemoveInNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode);
131     int32_t RemoveInPreNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode);
132     void RemoveAll();
133     void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode,
134         const RefPtr<RouteInfo>& routeInfo = nullptr);
135     void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode,
136         const RefPtr<RouteInfo>& routeInfo = nullptr);
137     RefPtr<UINode> Get();
138     RefPtr<UINode> Get(int32_t index);
139     std::string GetNavDesNameByIndex(int32_t index);
140     bool Get(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index);
141     bool GetFromPreBackup(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index);
142     RefPtr<UINode> GetPre(const std::string& name, const RefPtr<UINode>& navDestinationNode);
143     virtual bool IsEmpty();
144     virtual std::vector<std::string> GetAllPathName();
145     virtual std::vector<int32_t> GetAllPathIndex();
InitNavPathIndex(const std::vector<std::string> & pathNames)146     virtual void InitNavPathIndex(const std::vector<std::string>& pathNames) {}
SetDestinationIdToJsStack(int32_t index,const std::string & navDestinationId)147     virtual void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) {}
148     virtual void Pop();
149     virtual void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr);
150     virtual void Push(const std::string& name, int32_t index);
151     virtual void RemoveName(const std::string& name);
152     virtual void RemoveIndex(int32_t index);
153     virtual void Clear();
154     virtual void UpdateReplaceValue(int32_t replaceValue) const;
155     virtual int32_t GetReplaceValue() const;
156     virtual RefPtr<UINode> CreateNodeByIndex(int32_t index, const WeakPtr<UINode>& customNode);
157     virtual RefPtr<UINode> CreateNodeByRouteInfo(const RefPtr<RouteInfo>& routeInfo, const WeakPtr<UINode>& node);
GetDisableAnimation()158     virtual bool GetDisableAnimation() const
159     {
160         return false;
161     }
GetAnimatedValue()162     virtual bool GetAnimatedValue() const
163     {
164         return animated_;
165     }
UpdateAnimatedValue(bool animated)166     virtual void UpdateAnimatedValue(bool animated)
167     {
168         animated_ = animated;
169     }
170     int32_t FindIndex(const std::string& name, const RefPtr<UINode>& navDestinationNode, bool isNavPathList);
GetRouteParam()171     virtual std::string GetRouteParam() const
172     {
173         return "";
174     }
175 
FireNavigationInterception(bool isBefore,const RefPtr<NG::NavDestinationContext> & from,const RefPtr<NG::NavDestinationContext> & to,NavigationOperation operation,bool isAnimated)176     virtual void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from,
177         const RefPtr<NG::NavDestinationContext>& to, NavigationOperation operation, bool isAnimated) {}
178 
FireNavigationModeChange(NavigationMode mode)179     virtual void FireNavigationModeChange(NavigationMode mode) {}
180 
OnAttachToParent(RefPtr<NavigationStack> parent)181     virtual void OnAttachToParent(RefPtr<NavigationStack> parent) {}
OnDetachFromParent()182     virtual void OnDetachFromParent() {}
ClearPreBuildNodeList()183     virtual void ClearPreBuildNodeList() {}
184 
185     virtual std::vector<std::string> DumpStackInfo() const;
186 
GetJsIndexFromNativeIndex(int32_t index)187     virtual int32_t GetJsIndexFromNativeIndex(int32_t index) { return -1; }
MoveIndexToTop(int32_t index)188     virtual void MoveIndexToTop(int32_t index) {}
189 
GetStringifyParamByIndex(int32_t index)190     virtual std::string GetStringifyParamByIndex(int32_t index) const { return ""; }
SetPathArray(const std::vector<NavdestinationRecoveryInfo> & navdestinationsInfo)191     virtual void SetPathArray(const std::vector<NavdestinationRecoveryInfo>& navdestinationsInfo) {}
SetFromRecovery(int32_t index,bool fromRecovery)192     virtual void SetFromRecovery(int32_t index, bool fromRecovery) {}
IsFromRecovery(int32_t index)193     virtual bool IsFromRecovery(int32_t index) { return false; }
GetRecoveredDestinationMode(int32_t index)194     virtual int32_t GetRecoveredDestinationMode(int32_t index) { return false; }
195 
GetNavigationNode()196     const WeakPtr<UINode>& GetNavigationNode()
197     {
198         return navigationNode_;
199     }
200 
SetNavigationNode(const WeakPtr<UINode> & navigationNode)201     void SetNavigationNode(const WeakPtr<UINode>& navigationNode)
202     {
203         navigationNode_ = navigationNode;
204     }
205 
206 #if defined(ENABLE_NAV_SPLIT_MODE)
SetLastNavPathList(const NavPathList & navPathList)207     void SetLastNavPathList(const NavPathList& navPathList)
208     {
209         lastNavPathList_ = navPathList;
210     }
211     bool isLastListContains(const std::string& name, const RefPtr<UINode>& navDestinationNode);
212 #endif
213 
UpdatePathInfoIfNeeded(RefPtr<UINode> & uiNode,int32_t index)214     virtual void UpdatePathInfoIfNeeded(RefPtr<UINode>& uiNode, int32_t index) {}
RecoveryNavigationStack()215     virtual void RecoveryNavigationStack() {}
NeedBuildNewInstance(int32_t index)216     virtual bool NeedBuildNewInstance(int32_t index) { return false; }
SetNeedBuildNewInstance(int32_t index,bool need)217     virtual void SetNeedBuildNewInstance(int32_t index, bool need) {}
218 
UpdateRecoveryList()219     void UpdateRecoveryList()
220     {
221         recoveryList_ = navPathList_;
222     }
223 
ClearRecoveryList()224     void ClearRecoveryList()
225     {
226         recoveryList_.clear();
227     }
228 
GetRecoveryList()229     NavPathList GetRecoveryList()
230     {
231         return recoveryList_;
232     }
233 
SetIsEntryByIndex(int32_t index,bool isEntry)234     virtual void SetIsEntryByIndex(int32_t index, bool isEntry) {}
235 
236 protected:
237     void MoveToTop(const std::string& name, const RefPtr<UINode>& navDestinationNode);
238     void AddForDefault(const std::string& name, const RefPtr<UINode>& navDestinationNode,
239         const RefPtr<RouteInfo>& routeInfo = nullptr);
240     void AddForReplace(const std::string& name, const RefPtr<UINode>& navDestinationNode,
241         const RefPtr<RouteInfo>& routeInfo = nullptr);
242 
243     NavPathList navPathList_;
244     // prev backup NavPathList
245     NavPathList preNavPathList_;
246 #if defined(ENABLE_NAV_SPLIT_MODE)
247     // backup NavPathList before push or pop
248     NavPathList lastNavPathList_;
249 #endif
250     // recovery NavPathList
251     NavPathList recoveryList_;
252     NavPathList cacheNodes_;
253     bool animated_ = true;
254     WeakPtr<UINode> navigationNode_;
255     std::vector<std::pair<std::string, WeakPtr<UINode>>> navPathListBeforePoped_;
256 };
257 } // namespace OHOS::Ace::NG
258 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H
259