Esempio n. 1
0
        public override void Show(MvvmCross.Core.ViewModels.MvxViewModelRequest request)
        {
            if (request.PresentationValues != null)
            {
                #region ClearStack
                if (request.PresentationValues.ContainsKey(PresentationBundleFlagKeys.ClearStack))
                {
                    //MasterNavigationController.PopToRootViewController(false);
                    var nextViewController = (UIViewController)ViewCreator.CreateView(request);
                    MasterNavigationController.PushViewController(nextViewController, true);
                    return;
                }
                #endregion

                #region ShowBack
                if (request.PresentationValues.ContainsKey(PresentationBundleFlagKeys.ShowBack))
                {
                    var  nextViewController    = (UIViewController)ViewCreator.CreateView(request);
                    var  currentViewController = MasterNavigationController.TopViewController;
                    bool animated = false;

                    while (!animated && currentViewController != null && currentViewController.GetType() != nextViewController.GetType())
                    {
                        if (MasterNavigationController.ViewControllers.Length > 1)
                        {
                            var backViewController = MasterNavigationController.ViewControllers[MasterNavigationController.ViewControllers.Length - 2];
                            if (backViewController.GetType() == nextViewController.GetType())
                            {
                                animated = true;
                            }
                        }
                        else
                        {
                            break;
                        }

                        MasterNavigationController.PopViewController(animated);

                        if (animated)
                        {
                            MemoryUtils.DelayReleaseObject(2000, currentViewController);
                        }
                        else
                        {
                            MemoryUtils.ReleaseObject(currentViewController);
                        }

                        currentViewController = MasterNavigationController.TopViewController;
                    }

                    if (MasterNavigationController.ViewControllers.Length <= 1)
                    {
                        MasterNavigationController.PushViewController(nextViewController, true);
                    }

                    return;
                }
                #endregion

                #region CloseCurrentAndShow
                if (request.PresentationValues.ContainsKey(PresentationBundleFlagKeys.CloseCurrentAndShow))
                {
                    // let make a async task to remove current from history to not make UI flash
                    System.Threading.Tasks.Task.Run(async() =>
                    {
                        await System.Threading.Tasks.Task.Delay(1000);

                        if (closeViewController == null)
                        {
                            return;
                        }
                        MasterNavigationController.InvokeOnMainThread(async() =>
                        {
                            //wait for until top view controller is showed
                            while (closeViewController != MasterNavigationController.TopViewController)
                            {
                                await System.Threading.Tasks.Task.Delay(100);
                            }

                            int countVc = MasterNavigationController.ViewControllers.Length;

                            if (countVc > 1)
                            {
                                UIViewController releaseViewController = MasterNavigationController.ViewControllers[countVc - 2];
                                UIViewController[] newHistory          = new UIViewController[countVc - 1];
                                for (int i = 0; i < countVc - 2; i++)
                                {
                                    newHistory[i] = MasterNavigationController.ViewControllers[i];
                                }
                                newHistory[countVc - 2] = MasterNavigationController.ViewControllers[countVc - 1];
                                MasterNavigationController.ViewControllers = newHistory;

                                MemoryUtils.ReleaseObject(releaseViewController);
                            }

                            closeViewController = null;
                        });
                    });
                }
                #endregion
            }
            base.Show(request);
        }