[NUI] Apply Tizen 7.0 UX to Navigator - Page animation
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Navigator.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23
24 namespace Tizen.NUI.Components
25 {
26     /// <summary>
27     /// PoppedEventArgs is a class to record <see cref="Navigator.Popped"/> event arguments which will be sent to user.
28     /// </summary>
29     /// <since_tizen> 9 </since_tizen>
30     public class PoppedEventArgs : EventArgs
31     {
32         /// <summary>
33         /// Page popped by Navigator.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public Page Page { get; internal set; }
37     }
38
39     /// <summary>
40     /// The Navigator is a class which navigates pages with stack methods such as Push and Pop.
41     /// </summary>
42     /// <remarks>
43     /// With Transition class, Navigator supports smooth transition of View pair between two Pages
44     /// by using <see cref="PushWithTransition(Page)"/> and <see cref="PopWithTransition()"/> methods.
45     /// If current top Page and next top Page have <see cref="View"/>s those have same TransitionTag,
46     /// Navigator creates smooth transition motion for them.
47     /// Navigator.Transition property can be used to set properties of the Transition such as TimePeriod and AlphaFunction.
48     /// When all transitions are finished, Navigator calls a callback methods those connected on the "TransitionFinished" event.
49     /// </remarks>
50     /// <example>
51     /// <code>
52     /// Navigator navigator = new Navigator()
53     /// {
54     ///     TimePeriod = new TimePeriod(500),
55     ///     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOutSine)
56     /// };
57     ///
58     /// View view = new View()
59     /// {
60     ///     TransitionOptions = new TransitionOptions()
61     ///     {
62     ///         /* Set properties for the transition of this View */
63     ///     }
64     /// };
65     ///
66     /// ContentPage newPage = new ContentPage()
67     /// {
68     ///     Content = view,
69     /// };
70     ///
71     /// Navigator.PushWithTransition(newPage);
72     /// </code>
73     /// </example>
74     /// <since_tizen> 9 </since_tizen>
75     public class Navigator : Control
76     {
77         /// <summary>
78         /// TransitionProperty
79         /// </summary>
80         [EditorBrowsable(EditorBrowsableState.Never)]
81         public static readonly BindableProperty TransitionProperty = BindableProperty.Create(nameof(Transition), typeof(Transition), typeof(Navigator), null, propertyChanged: (bindable, oldValue, newValue) =>
82         {
83             var instance = (Navigator)bindable;
84             if (newValue != null)
85             {
86                 instance.InternalTransition = newValue as Transition;
87             }
88         },
89         defaultValueCreator: (bindable) =>
90         {
91             var instance = (Navigator)bindable;
92             return instance.InternalTransition;
93         });
94
95         private const int DefaultTransitionDuration = 300;
96
97         //This will be replaced with view transition class instance.
98         private Animation curAnimation = null;
99
100         //This will be replaced with view transition class instance.
101         private Animation newAnimation = null;
102
103         private TransitionSet transitionSet = null;
104
105         private Transition transition = new Transition()
106         {
107             TimePeriod = new TimePeriod(DefaultTransitionDuration),
108             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
109         };
110
111         private bool transitionFinished = true;
112
113         //TODO: Needs to consider how to remove disposed window from dictionary.
114         //Two dictionaries are required to remove disposed navigator from dictionary.
115         private static Dictionary<Window, Navigator> windowNavigator = new Dictionary<Window, Navigator>();
116         private static Dictionary<Navigator, Window> navigatorWindow = new Dictionary<Navigator, Window>();
117
118         private List<Page> navigationPages = new List<Page>();
119
120         /// <summary>
121         /// Creates a new instance of a Navigator.
122         /// </summary>
123         /// <since_tizen> 9 </since_tizen>
124         public Navigator() : base()
125         {
126             Layout = new AbsoluteLayout();
127         }
128
129         /// <inheritdoc/>
130         [EditorBrowsable(EditorBrowsableState.Never)]
131         public override void OnInitialize()
132         {
133             base.OnInitialize();
134
135             AccessibilityRole = Role.PageTabList;
136         }
137
138         /// <summary>
139         /// An event fired when Transition has been finished.
140         /// </summary>
141         /// <since_tizen> 9 </since_tizen>
142         public event EventHandler<EventArgs> TransitionFinished;
143
144         /// <summary>
145         /// An event fired when Pop of a page has been finished.
146         /// </summary>
147         /// <remarks>
148         /// When you free resources in the Popped event handler, please make sure if the popped page is the page you find.
149         /// </remarks>
150         /// <since_tizen> 9 </since_tizen>
151         public event EventHandler<PoppedEventArgs> Popped;
152
153         /// <summary>
154         /// Returns the count of pages in Navigator.
155         /// </summary>
156         /// <since_tizen> 9 </since_tizen>
157         public int PageCount => navigationPages.Count;
158
159         /// <summary>
160         /// Transition properties for the transition of View pair having same transition tag.
161         /// </summary>
162         /// <since_tizen> 9 </since_tizen>
163         public Transition Transition
164         {
165             get
166             {
167                 return GetValue(TransitionProperty) as Transition;
168             }
169             set
170             {
171                 SetValue(TransitionProperty, value);
172                 NotifyPropertyChanged();
173             }
174         }
175         private Transition InternalTransition
176         {
177             set
178             {
179                 transition = value;
180             }
181             get
182             {
183                 return transition;
184             }
185         }
186
187         /// <summary>
188         /// Pushes a page to Navigator.
189         /// If the page is already in Navigator, then it is not pushed.
190         /// </summary>
191         /// <param name="page">The page to push to Navigator.</param>
192         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
193         /// <since_tizen> 9 </since_tizen>
194         public void PushWithTransition(Page page)
195         {
196             if (!transitionFinished)
197             {
198                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
199                 return;
200             }
201
202             if (page == null)
203             {
204                 throw new ArgumentNullException(nameof(page), "page should not be null.");
205             }
206
207             //Duplicate page is not pushed.
208             if (navigationPages.Contains(page)) return;
209
210             var topPage = Peek();
211
212             if (!topPage)
213             {
214                 Insert(0, page);
215                 return;
216             }
217
218             navigationPages.Add(page);
219             Add(page);
220             page.Navigator = this;
221
222             //Invoke Page events
223             page.InvokeAppearing();
224             topPage.InvokeDisappearing();
225
226             transitionSet = CreateTransitions(topPage, page, true);
227             transitionSet.Finished += (object sender, EventArgs e) =>
228             {
229                 if (page is DialogPage == false)
230                 {
231                     topPage.SetVisible(false);
232                 }
233
234                 // Need to update Content of the new page
235                 ShowContentOfPage(page);
236
237                 //Invoke Page events
238                 page.InvokeAppeared();
239                 topPage.InvokeDisappeared();
240                 NotifyAccessibilityStatesChangeOfPages(topPage, page);
241             };
242             transitionFinished = false;
243         }
244
245         /// <summary>
246         /// Pops the top page from Navigator.
247         /// </summary>
248         /// <returns>The popped page.</returns>
249         /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
250         /// <since_tizen> 9 </since_tizen>
251         public Page PopWithTransition()
252         {
253             if (!transitionFinished)
254             {
255                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
256                 return null;
257             }
258
259             if (navigationPages.Count == 0)
260             {
261                 throw new InvalidOperationException("There is no page in Navigator.");
262             }
263
264             var topPage = Peek();
265
266             if (navigationPages.Count == 1)
267             {
268                 Remove(topPage);
269
270                 //Invoke Popped event
271                 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
272
273                 return topPage;
274             }
275             var newTopPage = navigationPages[navigationPages.Count - 2];
276
277             //Invoke Page events
278             newTopPage.InvokeAppearing();
279             topPage.InvokeDisappearing();
280
281             transitionSet = CreateTransitions(topPage, newTopPage, false);
282             transitionSet.Finished += (object sender, EventArgs e) =>
283             {
284                 Remove(topPage);
285                 topPage.SetVisible(true);
286
287                 // Need to update Content of the new page
288                 ShowContentOfPage(newTopPage);
289
290                 //Invoke Page events
291                 newTopPage.InvokeAppeared();
292                 topPage.InvokeDisappeared();
293
294                 //Invoke Popped event
295                 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
296             };
297             transitionFinished = false;
298
299             return topPage;
300         }
301
302         /// <summary>
303         /// Pushes a page to Navigator.
304         /// If the page is already in Navigator, then it is not pushed.
305         /// </summary>
306         /// <param name="page">The page to push to Navigator.</param>
307         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
308         /// <since_tizen> 9 </since_tizen>
309         public void Push(Page page)
310         {
311             if (!transitionFinished)
312             {
313                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
314                 return;
315             }
316
317             if (page == null)
318             {
319                 throw new ArgumentNullException(nameof(page), "page should not be null.");
320             }
321
322             //Duplicate page is not pushed.
323             if (navigationPages.Contains(page)) return;
324
325             var curTop = Peek();
326
327             if (!curTop)
328             {
329                 Insert(0, page);
330                 return;
331             }
332
333             navigationPages.Add(page);
334             Add(page);
335             page.Navigator = this;
336
337             //Invoke Page events
338             page.InvokeAppearing();
339             curTop.InvokeDisappearing();
340
341             curTop.SaveKeyFocus();
342
343             //TODO: The following transition codes will be replaced with view transition.
344             InitializeAnimation();
345
346             if (page is DialogPage == false)
347             {
348                 curAnimation = new Animation(DefaultTransitionDuration);
349                 curAnimation.AnimateTo(curTop, "PositionX", 0.0f, 0, DefaultTransitionDuration);
350                 curAnimation.EndAction = Animation.EndActions.StopFinal;
351                 curAnimation.Finished += (object sender, EventArgs args) =>
352                 {
353                     curTop.SetVisible(false);
354
355                     //Invoke Page events
356                     curTop.InvokeDisappeared();
357                 };
358                 curAnimation.Play();
359
360                 page.PositionX = SizeWidth;
361                 page.SetVisible(true);
362                 // Set Content visible because it was hidden by HideContentOfPage.
363                 (page as ContentPage).Content?.SetVisible(true);
364
365                 newAnimation = new Animation(DefaultTransitionDuration);
366                 newAnimation.AnimateTo(page, "PositionX", 0.0f, 0, DefaultTransitionDuration);
367                 newAnimation.EndAction = Animation.EndActions.StopFinal;
368                 newAnimation.Finished += (object sender, EventArgs e) =>
369                 {
370                     // Need to update Content of the new page
371                     ShowContentOfPage(page);
372
373                     //Invoke Page events
374                     page.InvokeAppeared();
375                     NotifyAccessibilityStatesChangeOfPages(curTop, page);
376
377                     page.RestoreKeyFocus();
378                 };
379                 newAnimation.Play();
380             }
381             else
382             {
383                 ShowContentOfPage(page);
384                 page.RestoreKeyFocus();
385             }
386         }
387
388         /// <summary>
389         /// Pops the top page from Navigator.
390         /// </summary>
391         /// <returns>The popped page.</returns>
392         /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
393         /// <since_tizen> 9 </since_tizen>
394         public Page Pop()
395         {
396             if (!transitionFinished)
397             {
398                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
399                 return null;
400             }
401
402             if (navigationPages.Count == 0)
403             {
404                 throw new InvalidOperationException("There is no page in Navigator.");
405             }
406
407             var curTop = Peek();
408
409             if (navigationPages.Count == 1)
410             {
411                 Remove(curTop);
412
413                 //Invoke Popped event
414                 Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
415
416                 return curTop;
417             }
418
419             var newTop = navigationPages[navigationPages.Count - 2];
420
421             //Invoke Page events
422             newTop.InvokeAppearing();
423             curTop.InvokeDisappearing();
424             curTop.SaveKeyFocus();
425
426             //TODO: The following transition codes will be replaced with view transition.
427             InitializeAnimation();
428
429             if (curTop is DialogPage == false)
430             {
431                 curAnimation = new Animation(DefaultTransitionDuration);
432                 curAnimation.AnimateTo(curTop, "PositionX", SizeWidth, 0, DefaultTransitionDuration);
433                 curAnimation.EndAction = Animation.EndActions.StopFinal;
434                 curAnimation.Finished += (object sender, EventArgs e) =>
435                 {
436                     //Removes the current top page after transition is finished.
437                     Remove(curTop);
438                     curTop.PositionX = 0.0f;
439
440                     //Invoke Page events
441                     curTop.InvokeDisappeared();
442
443                     //Invoke Popped event
444                     Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
445                 };
446                 curAnimation.Play();
447
448                 newTop.SetVisible(true);
449                 // Set Content visible because it was hidden by HideContentOfPage.
450                 (newTop as ContentPage).Content?.SetVisible(true);
451
452                 newAnimation = new Animation(DefaultTransitionDuration);
453                 newAnimation.AnimateTo(newTop, "PositionX", 0.0f, 0, DefaultTransitionDuration);
454                 newAnimation.EndAction = Animation.EndActions.StopFinal;
455                 newAnimation.Finished += (object sender, EventArgs e) =>
456                 {
457                     // Need to update Content of the new page
458                     ShowContentOfPage(newTop);
459
460                     //Invoke Page events
461                     newTop.InvokeAppeared();
462
463                     newTop.RestoreKeyFocus();
464                 };
465                 newAnimation.Play();
466             }
467             else
468             {
469                 Remove(curTop);
470             }
471
472             return curTop;
473         }
474
475         /// <summary>
476         /// Returns the page of the given index in Navigator.
477         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
478         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
479         /// </summary>
480         /// <param name="index">The index of a page in Navigator.</param>
481         /// <returns>The page of the given index in Navigator.</returns>
482         /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
483         public Page GetPage(int index)
484         {
485             if ((index < 0) || (index > navigationPages.Count))
486             {
487                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
488             }
489
490             return navigationPages[index];
491         }
492
493         /// <summary>
494         /// Returns the current index of the given page in Navigator.
495         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
496         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
497         /// </summary>
498         /// <param name="page">The page in Navigator.</param>
499         /// <returns>The index of the given page in Navigator. If the given page is not in the Navigator, then -1 is returned.</returns>
500         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
501         /// <since_tizen> 9 </since_tizen>
502         public int IndexOf(Page page)
503         {
504             if (page == null)
505             {
506                 throw new ArgumentNullException(nameof(page), "page should not be null.");
507             }
508
509             for (int i = 0; i < navigationPages.Count; i++)
510             {
511                 if (navigationPages[i] == page)
512                 {
513                     return i;
514                 }
515             }
516
517             return -1;
518         }
519
520         /// <summary>
521         /// Inserts a page at the specified index of Navigator.
522         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
523         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
524         /// To find the current index of a page in Navigator, please use IndexOf(page).
525         /// If the page is already in Navigator, then it is not inserted.
526         /// </summary>
527         /// <param name="index">The index of a page in Navigator where the page will be inserted.</param>
528         /// <param name="page">The page to insert to Navigator.</param>
529         /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
530         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
531         /// <since_tizen> 9 </since_tizen>
532         public void Insert(int index, Page page)
533         {
534             if ((index < 0) || (index > navigationPages.Count))
535             {
536                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
537             }
538
539             if (page == null)
540             {
541                 throw new ArgumentNullException(nameof(page), "page should not be null.");
542             }
543
544             //Duplicate page is not pushed.
545             if (navigationPages.Contains(page)) return;
546
547             //TODO: The following transition codes will be replaced with view transition.
548             InitializeAnimation();
549
550             ShowContentOfPage(page);
551
552             if (index == PageCount)
553             {
554                 page.SetVisible(true);
555             }
556             else
557             {
558                 page.SetVisible(false);
559             }
560
561             navigationPages.Insert(index, page);
562             Add(page);
563             page.Navigator = this;
564             if (index == PageCount - 1)
565             {
566                 if (PageCount > 1)
567                 {
568                     NotifyAccessibilityStatesChangeOfPages(navigationPages[PageCount - 2], page);
569                 }
570                 else
571                 {
572                     NotifyAccessibilityStatesChangeOfPages(null, page);
573                 }
574             }
575         }
576
577         /// <summary>
578         /// Inserts a page to Navigator before an existing page.
579         /// If the page is already in Navigator, then it is not inserted.
580         /// </summary>
581         /// <param name="before">The existing page, before which a page will be inserted.</param>
582         /// <param name="page">The page to insert to Navigator.</param>
583         /// <exception cref="ArgumentNullException">Thrown when the argument before is null.</exception>
584         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
585         /// <exception cref="ArgumentException">Thrown when the argument before does not exist in Navigator.</exception>
586         /// <since_tizen> 9 </since_tizen>
587         public void InsertBefore(Page before, Page page)
588         {
589             if (before == null)
590             {
591                 throw new ArgumentNullException(nameof(before), "before should not be null.");
592             }
593
594             if (page == null)
595             {
596                 throw new ArgumentNullException(nameof(page), "page should not be null.");
597             }
598
599             //Find the index of before page.
600             int beforeIndex = navigationPages.FindIndex(x => x == before);
601
602             //before does not exist in Navigator.
603             if (beforeIndex == -1)
604             {
605                 throw new ArgumentException("before does not exist in Navigator.", nameof(before));
606             }
607
608             Insert(beforeIndex, page);
609         }
610
611         /// <summary>
612         /// Removes a page from Navigator.
613         /// </summary>
614         /// <param name="page">The page to remove from Navigator.</param>
615         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
616         /// <since_tizen> 9 </since_tizen>
617         public void Remove(Page page)
618         {
619             if (page == null)
620             {
621                 throw new ArgumentNullException(nameof(page), "page should not be null.");
622             }
623
624             //TODO: The following transition codes will be replaced with view transition.
625             InitializeAnimation();
626
627             HideContentOfPage(page);
628
629             if (page == Peek())
630             {
631                 if (PageCount >= 2)
632                 {
633                     navigationPages[PageCount - 2].SetVisible(true);
634                     NotifyAccessibilityStatesChangeOfPages(page, navigationPages[PageCount - 2]);
635                 }
636                 else if (PageCount == 1)
637                 {
638                     NotifyAccessibilityStatesChangeOfPages(page, null);
639                 }
640             }
641             page.Navigator = null;
642             navigationPages.Remove(page);
643             base.Remove(page);
644         }
645
646         /// <summary>
647         /// Removes a page at the specified index of Navigator.
648         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
649         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
650         /// To find the current index of a page in Navigator, please use IndexOf(page).
651         /// </summary>
652         /// <param name="index">The index of a page in Navigator where the page will be removed.</param>
653         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of pages.</exception>
654         /// <since_tizen> 9 </since_tizen>
655         public void RemoveAt(int index)
656         {
657             if ((index < 0) || (index >= navigationPages.Count))
658             {
659                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than the number of pages.");
660             }
661
662             Remove(navigationPages[index]);
663         }
664
665         /// <summary>
666         /// Returns the page at the top of Navigator.
667         /// </summary>
668         /// <returns>The page at the top of Navigator.</returns>
669         /// <since_tizen> 9 </since_tizen>
670         public Page Peek()
671         {
672             if (navigationPages.Count == 0) return null;
673
674             return navigationPages[navigationPages.Count - 1];
675         }
676
677         /// <summary>
678         /// Disposes Navigator and all children on it.
679         /// </summary>
680         /// <param name="type">Dispose type.</param>
681         [EditorBrowsable(EditorBrowsableState.Never)]
682         protected override void Dispose(DisposeTypes type)
683         {
684             if (disposed)
685             {
686                 return;
687             }
688
689             if (type == DisposeTypes.Explicit)
690             {
691                 foreach (Page page in navigationPages)
692                 {
693                     Utility.Dispose(page);
694                 }
695                 navigationPages.Clear();
696
697                 Window window;
698
699                 if (navigatorWindow.TryGetValue(this, out window) == true)
700                 {
701                     navigatorWindow.Remove(this);
702                     windowNavigator.Remove(window);
703                 }
704             }
705
706             base.Dispose(type);
707         }
708
709         /// <summary>
710         /// Returns the default navigator of the given window.
711         /// </summary>
712         /// <returns>The default navigator of the given window.</returns>
713         /// <exception cref="ArgumentNullException">Thrown when the argument window is null.</exception>
714         /// <since_tizen> 9 </since_tizen>
715         public static Navigator GetDefaultNavigator(Window window)
716         {
717             if (window == null)
718             {
719                 throw new ArgumentNullException(nameof(window), "window should not be null.");
720             }
721
722             if (windowNavigator.ContainsKey(window) == true)
723             {
724                 return windowNavigator[window];
725             }
726
727             var defaultNavigator = new Navigator();
728             defaultNavigator.WidthResizePolicy = ResizePolicyType.FillToParent;
729             defaultNavigator.HeightResizePolicy = ResizePolicyType.FillToParent;
730             window.Add(defaultNavigator);
731             windowNavigator.Add(window, defaultNavigator);
732             navigatorWindow.Add(defaultNavigator, window);
733
734             return defaultNavigator;
735         }
736
737         /// <summary>
738         /// Create Transitions between currentTopPage and newTopPage
739         /// </summary>
740         /// <param name="currentTopPage">The top page of Navigator.</param>
741         /// <param name="newTopPage">The new top page after transition.</param>
742         /// <param name="pushTransition">True if this transition is for push new page</param>
743         private TransitionSet CreateTransitions(Page currentTopPage, Page newTopPage, bool pushTransition)
744         {
745             currentTopPage.SetVisible(true);
746             // Set Content visible because it was hidden by HideContentOfPage.
747             (currentTopPage as ContentPage).Content?.SetVisible(true);
748
749             newTopPage.SetVisible(true);
750             // Set Content visible because it was hidden by HideContentOfPage.
751             (newTopPage as ContentPage).Content?.SetVisible(true);
752
753             List<View> taggedViewsInNewTopPage = new List<View>();
754             RetrieveTaggedViews(taggedViewsInNewTopPage, newTopPage, true);
755             List<View> taggedViewsInCurrentTopPage = new List<View>();
756             RetrieveTaggedViews(taggedViewsInCurrentTopPage, currentTopPage, true);
757
758             List<KeyValuePair<View, View>> sameTaggedViewPair = new List<KeyValuePair<View, View>>();
759             foreach (View currentTopPageView in taggedViewsInCurrentTopPage)
760             {
761                 bool findPair = false;
762                 foreach (View newTopPageView in taggedViewsInNewTopPage)
763                 {
764                     if ((currentTopPageView.TransitionOptions != null) && (newTopPageView.TransitionOptions != null) &&
765                         currentTopPageView.TransitionOptions?.TransitionTag == newTopPageView.TransitionOptions?.TransitionTag)
766                     {
767                         sameTaggedViewPair.Add(new KeyValuePair<View, View>(currentTopPageView, newTopPageView));
768                         findPair = true;
769                         break;
770                     }
771                 }
772                 if (findPair)
773                 {
774                     taggedViewsInNewTopPage.Remove(sameTaggedViewPair[sameTaggedViewPair.Count - 1].Value);
775                 }
776             }
777             foreach (KeyValuePair<View, View> pair in sameTaggedViewPair)
778             {
779                 taggedViewsInCurrentTopPage.Remove(pair.Key);
780             }
781
782             TransitionSet newTransitionSet = new TransitionSet();
783             foreach (KeyValuePair<View, View> pair in sameTaggedViewPair)
784             {
785                 TransitionItem pairTransition = transition.CreateTransition(pair.Key, pair.Value, pushTransition);
786                 if (pair.Value.TransitionOptions?.TransitionWithChild ?? false)
787                 {
788                     pairTransition.TransitionWithChild = true;
789                 }
790                 newTransitionSet.AddTransition(pairTransition);
791             }
792
793             newTransitionSet.Finished += (object sender, EventArgs e) =>
794             {
795                 if (newTopPage.Layout != null)
796                 {
797                     newTopPage.Layout.RequestLayout();
798                 }
799                 if (currentTopPage.Layout != null)
800                 {
801                     currentTopPage.Layout.RequestLayout();
802                 }
803                 transitionFinished = true;
804                 InvokeTransitionFinished();
805                 transitionSet.Dispose();
806             };
807
808             if (!pushTransition || newTopPage is DialogPage == false)
809             {
810                 View transitionView = (currentTopPage is ContentPage) ? (currentTopPage as ContentPage).Content : (currentTopPage as DialogPage).Content;
811                 if (currentTopPage.DisappearingTransition != null && transitionView != null)
812                 {
813                     TransitionItemBase disappearingTransition = currentTopPage.DisappearingTransition.CreateTransition(transitionView, false);
814                     disappearingTransition.TransitionWithChild = true;
815                     newTransitionSet.AddTransition(disappearingTransition);
816                 }
817                 else
818                 {
819                     currentTopPage.SetVisible(false);
820                 }
821             }
822             if (pushTransition || currentTopPage is DialogPage == false)
823             {
824                 View transitionView = (newTopPage is ContentPage) ? (newTopPage as ContentPage).Content : (newTopPage as DialogPage).Content;
825                 if (newTopPage.AppearingTransition != null && transitionView != null)
826                 {
827                     TransitionItemBase appearingTransition = newTopPage.AppearingTransition.CreateTransition(transitionView, true);
828                     appearingTransition.TransitionWithChild = true;
829                     newTransitionSet.AddTransition(appearingTransition);
830                 }
831             }
832
833             newTransitionSet.Play();
834
835             return newTransitionSet;
836         }
837
838         /// <summary>
839         /// Retrieve Tagged Views in the view tree.
840         /// </summary>
841         /// <param name="taggedViews">Returned tagged view list..</param>
842         /// <param name="view">Root View to get tagged child View.</param>
843         /// <param name="isRoot">Flag to check current View is page or not</param>
844         private void RetrieveTaggedViews(List<View> taggedViews, View view, bool isRoot)
845         {
846             if (!isRoot && view.TransitionOptions != null)
847             {
848                 if (!string.IsNullOrEmpty(view.TransitionOptions?.TransitionTag))
849                 {
850                     taggedViews.Add((view as View));
851                     if (view.TransitionOptions.TransitionWithChild)
852                     {
853                         return;
854                     }
855                 }
856
857             }
858
859             foreach (View child in view.Children)
860             {
861                 RetrieveTaggedViews(taggedViews, child, false);
862             }
863         }
864
865         /// <summary>
866         /// Notify accessibility states change of pages.
867         /// </summary>
868         /// <param name="disappearedPage">Disappeared page</param>
869         /// <param name="appearedPage">Appeared page</param>
870         private void NotifyAccessibilityStatesChangeOfPages(Page disappearedPage, Page appearedPage)
871         {
872             if (disappearedPage != null)
873             {
874                 disappearedPage.UnregisterDefaultLabel();
875                 //We can call disappearedPage.NotifyAccessibilityStatesChange
876                 //To reduce accessibility events, we are using currently highlighted view instead
877                 View curHighlightedView = Accessibility.Accessibility.GetCurrentlyHighlightedView();
878                 if (curHighlightedView != null)
879                 {
880                     curHighlightedView.NotifyAccessibilityStatesChange(new AccessibilityStates(AccessibilityState.Visible, AccessibilityState.Showing), AccessibilityStatesNotifyMode.Single);
881                 }
882             }
883
884             if (appearedPage != null)
885             {
886                 appearedPage.RegisterDefaultLabel();
887                 appearedPage.NotifyAccessibilityStatesChange(new AccessibilityStates(AccessibilityState.Visible, AccessibilityState.Showing), AccessibilityStatesNotifyMode.Single);
888             }
889         }
890
891         internal void InvokeTransitionFinished()
892         {
893             TransitionFinished?.Invoke(this, new EventArgs());
894         }
895
896         //TODO: The following transition codes will be replaced with view transition.
897         private void InitializeAnimation()
898         {
899             if (curAnimation != null)
900             {
901                 curAnimation.Stop();
902                 curAnimation.Clear();
903                 curAnimation = null;
904             }
905
906             if (newAnimation != null)
907             {
908                 newAnimation.Stop();
909                 newAnimation.Clear();
910                 newAnimation = null;
911             }
912         }
913
914         // Show and Register Content of Page to Accessibility bridge
915         private void ShowContentOfPage(Page page)
916         {
917             View content = (page is DialogPage) ? (page as DialogPage)?.Content : (page as ContentPage)?.Content;
918             if (content != null)
919             {
920                 content.Show(); // Calls RegisterDefaultLabel()
921             }
922         }
923
924         // Hide and Remove Content of Page from Accessibility bridge
925         private void HideContentOfPage(Page page)
926         {
927             View content = (page is DialogPage) ? (page as DialogPage)?.Content : (page as ContentPage)?.Content;
928             if (content != null)
929             {
930                 content.Hide(); // Calls UnregisterDefaultLabel()
931             }
932         }
933     }
934 }