public NavBarViewModel(NavigationStateViewModel navigationState, RoutingState router, WalletManagerViewModel walletManager, AddWalletPageViewModel addWalletPage)
        {
            Router         = router;
            _walletManager = walletManager;
            _topItems      = new ObservableCollection <NavBarItemViewModel>();
            _bottomItems   = new ObservableCollection <NavBarItemViewModel>();

            var homePage     = new HomePageViewModel(navigationState, walletManager, addWalletPage);
            var settingsPage = new SettingsPageViewModel(navigationState);
            var searchPage   = new SearchPageViewModel(navigationState, walletManager, addWalletPage, settingsPage, homePage);

            _selectedItem = homePage;

            _topItems.Add(SelectedItem);
            _bottomItems.Add(searchPage);
            _bottomItems.Add(settingsPage);
            _bottomItems.Add(addWalletPage);

            Router.CurrentViewModel
            .OfType <NavBarItemViewModel>()
            .Subscribe(
                x =>
            {
                if (walletManager.Items.Contains(x) || _topItems.Contains(x) || _bottomItems.Contains(x))
                {
                    if (!_isNavigating)
                    {
                        _isNavigating = true;
                        SelectedItem  = x;
                        _isNavigating = false;
                    }
                }
            });

            this.WhenAnyValue(x => x.SelectedItem)
            .OfType <NavBarItemViewModel>()
            .Subscribe(
                x =>
            {
                if (!_isNavigating)
                {
                    _isNavigating = true;
                    x.NavigateToSelfAndReset(x.CurrentTarget);
                    CollapseOnClickAction?.Invoke();

                    _isNavigating = false;
                }
            });

            Observable.FromEventPattern(Router.NavigationStack, nameof(Router.NavigationStack.CollectionChanged))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => IsBackButtonVisible = Router.NavigationStack.Count > 1);

            this.WhenAnyValue(x => x.IsOpen)
            .Subscribe(x => SelectedItem.IsExpanded = x);
        }
Esempio n. 2
0
        public NavBarViewModel(IScreen screen, RoutingState router, WalletManager walletManager, UiConfig uiConfig)
        {
            _screen      = screen;
            Router       = router;
            _topItems    = new ObservableCollection <NavBarItemViewModel>();
            _items       = new ObservableCollection <WalletViewModelBase>();
            _bottomItems = new ObservableCollection <NavBarItemViewModel>();

            _walletDictionary = new Dictionary <Wallet, WalletViewModelBase>();

            SelectedItem = new HomePageViewModel(screen);
            _topItems.Add(_selectedItem);
            _bottomItems.Add(new AddWalletPageViewModel(screen));
            _bottomItems.Add(new SettingsPageViewModel(screen));

            Router.CurrentViewModel
            .OfType <NavBarItemViewModel>()
            .Subscribe(x =>
            {
                if (_items.Contains(x) || _topItems.Contains(x) || _bottomItems.Contains(x))
                {
                    if (!_isNavigating)
                    {
                        _isNavigating = true;
                        SelectedItem  = x;
                        _isNavigating = false;
                    }
                }
            });

            this.WhenAnyValue(x => x.SelectedItem)
            .OfType <IRoutableViewModel>()
            .Subscribe(x =>
            {
                if (!_isNavigating)
                {
                    _isNavigating = true;
                    Router.NavigateAndReset.Execute(x);
                    CollapseOnClickAction?.Invoke();

                    _isNavigating = false;
                }
            });

            Observable.FromEventPattern(Router.NavigationStack, nameof(Router.NavigationStack.CollectionChanged))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => IsBackButtonVisible = Router.NavigationStack.Count > 1);

            Observable
            .FromEventPattern <WalletState>(walletManager, nameof(WalletManager.WalletStateChanged))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(x =>
            {
                var wallet = x.Sender as Wallet;

                if (wallet is { } && _walletDictionary.ContainsKey(wallet))
                {
                    if (wallet.State == WalletState.Stopping)
                    {
                        RemoveWallet(_walletDictionary[wallet]);
                    }
                    else if (_walletDictionary[wallet] is ClosedWalletViewModel cwvm && wallet.State == WalletState.Started)
                    {
                        OpenClosedWallet(walletManager, uiConfig, cwvm);
                    }
                }

                AnyWalletStarted = Items.OfType <WalletViewModelBase>().Any(x => x.WalletState == WalletState.Started);
            });