using System; using System.Collections.ObjectModel; using System.Linq; using System.Windows.Input; using ChineseChess.GUI.Contracts.Services; using ChineseChess.GUI.Properties; using MahApps.Metro.Controls; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; namespace ChineseChess.GUI.ViewModels { public class ShellViewModel : ObservableObject { private readonly INavigationService _navigationService; private HamburgerMenuItem _selectedMenuItem; private HamburgerMenuItem _selectedOptionsMenuItem; private RelayCommand _goBackCommand; private ICommand _menuItemInvokedCommand; private ICommand _optionsMenuItemInvokedCommand; private ICommand _loadedCommand; private ICommand _unloadedCommand; public HamburgerMenuItem SelectedMenuItem { get { return _selectedMenuItem; } set { SetProperty(ref _selectedMenuItem, value); } } public HamburgerMenuItem SelectedOptionsMenuItem { get { return _selectedOptionsMenuItem; } set { SetProperty(ref _selectedOptionsMenuItem, value); } } // TODO WTS: Change the icons and titles for all HamburgerMenuItems here. public ObservableCollection MenuItems { get; } = new ObservableCollection() { new HamburgerMenuGlyphItem() { Label = Resources.ShellMainPage, Glyph = "\uE8A5", TargetPageType = typeof(MainViewModel) }, }; public ObservableCollection OptionMenuItems { get; } = new ObservableCollection() { new HamburgerMenuGlyphItem() { Label = Resources.ShellSettingsPage, Glyph = "\uE713", TargetPageType = typeof(SettingsViewModel) } }; public RelayCommand GoBackCommand => _goBackCommand ??= new RelayCommand(OnGoBack, CanGoBack); public ICommand MenuItemInvokedCommand => _menuItemInvokedCommand ??= new RelayCommand(OnMenuItemInvoked); public ICommand OptionsMenuItemInvokedCommand => _optionsMenuItemInvokedCommand ??= new RelayCommand(OnOptionsMenuItemInvoked); public ICommand LoadedCommand => _loadedCommand ??= new RelayCommand(OnLoaded); public ICommand UnloadedCommand => _unloadedCommand ??= new RelayCommand(OnUnloaded); public ShellViewModel(INavigationService navigationService) { _navigationService = navigationService; } private void OnLoaded() { _navigationService.Navigated += OnNavigated; } private void OnUnloaded() { _navigationService.Navigated -= OnNavigated; } private bool CanGoBack() => _navigationService.CanGoBack; private void OnGoBack() => _navigationService.GoBack(); private void OnMenuItemInvoked() => NavigateTo(SelectedMenuItem.TargetPageType); private void OnOptionsMenuItemInvoked() => NavigateTo(SelectedOptionsMenuItem.TargetPageType); private void NavigateTo(Type targetViewModel) { if (targetViewModel != null) { _navigationService.NavigateTo(targetViewModel.FullName); } } private void OnNavigated(object sender, string viewModelName) { var item = MenuItems .OfType() .FirstOrDefault(i => viewModelName == i.TargetPageType?.FullName); if (item != null) { SelectedMenuItem = item; } else { SelectedOptionsMenuItem = OptionMenuItems .OfType() .FirstOrDefault(i => viewModelName == i.TargetPageType?.FullName); } GoBackCommand.NotifyCanExecuteChanged(); } } }