/// <summary>
        /// Initializes a new instance of the <see cref="ViewStackService"/> class.
        /// </summary>
        /// <param name="viewShell">The view shell (platform specific).</param>
        /// <param name="pages">A list of pages to initialize the page stack with.</param>
        public ViewStackService(IViewShell viewShell, IList <IPageViewModel> pages = null)
        {
            _viewShell = viewShell ?? throw new NullReferenceException("The viewShell can't be null.");

            _currentPageStack = new BehaviorSubject <IImmutableList <IPageViewModel> >(ImmutableList <IPageViewModel> .Empty);
            _modalPageStack   = new BehaviorSubject <IImmutableList <IPageViewModel> >(ImmutableList <IPageViewModel> .Empty);
            _nullPageStack    = new BehaviorSubject <IImmutableList <IPageViewModel> >(null);

            var immutablePages = pages != null?pages.ToImmutableList() : ImmutableList <IPageViewModel> .Empty;

            _defaultNavigationStack = new BehaviorSubject <IImmutableList <IPageViewModel> >(immutablePages);
            for (int i = 0; i < immutablePages.Count; ++i)
            {
                ViewShell.InsertPage(i, pages[i], null);
            }

            _modalPageStack
            .Select(
                x =>
            {
                if (x.Count > 0)
                {
                    if (x[x.Count - 1] is INavigationPageViewModel navigationPage)
                    {
                        return(navigationPage.PageStack);
                    }
                    else
                    {
                        return(_nullPageStack);
                    }
                }
                else
                {
                    return(_defaultNavigationStack);
                }
            })
            .Subscribe(x => _currentPageStack = x);

            _viewShell
            .PagePopped
            .Do(
                _ =>
            {
                var removedPage = PopStackAndTick(_currentPageStack);
                this.Log().Debug("Removed page '{0}' from stack.", removedPage.Title);
            })
            .Subscribe();

            _viewShell
            .ModalPopped
            .Do(
                _ =>
            {
                var removedPage = PopStackAndTick(_modalPageStack);
                this.Log().Debug("Removed modal page '{0}' from stack.", removedPage.Title);
            })
            .Subscribe();
        }
Esempio n. 2
0
        public ViewStackServiceTests()
        {
            _viewShell   = Substitute.For <IViewShell>();
            _page        = Substitute.For <IPageViewModel>();
            _pagePopped  = new Subject <IPageViewModel>();
            _modalPopped = new Subject <Unit>();
            _viewShell.PagePopped.Returns(_pagePopped);
            _viewShell.ModalPopped.Returns(_modalPopped);

            _viewShell
            .When(x => x.PopPage(Arg.Any <bool>()))
            .Do(_ => _pagePopped.OnNext(_page));

            _viewShell
            .When(x => x.PopModal())
            .Do(_ => _modalPopped.OnNext(Unit.Default));
        }