From 439850b5b7bde417030f90f2e028bf3cb8e3ea90 Mon Sep 17 00:00:00 2001 From: Natlinux <97396587+Natlinux81@users.noreply.github.com> Date: Sat, 26 Nov 2022 20:40:45 +0100 Subject: [PATCH] impliment NavigateService --- Apollon.WPF/Commands/AddTournamentCommand.cs | 17 +++++++---- Apollon.WPF/Commands/NavigateCommand.cs | 15 +++++----- Apollon.WPF/Services/NavigationService.cs | 28 +++++++++++++++++++ .../ViewModels/AddTournametViewModel.cs | 3 +- .../ViewModels/OverviewDetailsViewModel.cs | 4 ++- Apollon.WPF/ViewModels/OverviewViewModel.cs | 3 +- .../ViewModels/PreparationViewModel.cs | 4 ++- 7 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 Apollon.WPF/Services/NavigationService.cs diff --git a/Apollon.WPF/Commands/AddTournamentCommand.cs b/Apollon.WPF/Commands/AddTournamentCommand.cs index c9b1b56..3790b5c 100644 --- a/Apollon.WPF/Commands/AddTournamentCommand.cs +++ b/Apollon.WPF/Commands/AddTournamentCommand.cs @@ -1,4 +1,5 @@ using Apollon.Domain.Models; +using Apollon.WPF.Services; using Apollon.WPF.Stores; using Apollon.WPF.ViewModels; using System; @@ -12,19 +13,23 @@ namespace Apollon.WPF.Commands public class AddTournamentCommand : AsyncCommandBase { private readonly TournamentsStore _tournamentStore; - private readonly NavigationStore _navigationStore; + private readonly NavigationService _navigationService; private readonly ModalNavigationStore _modalNavigationStore; private readonly SelectedTournamentsStore _selectedTournamentsStore; private AddTournamentViewModel _addTournamentViewModel; - - public AddTournamentCommand(AddTournamentViewModel addTournamentViewModel, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore, NavigationStore navigationStore, SelectedTournamentsStore selectedTournamentsStore) + public AddTournamentCommand(AddTournamentViewModel addTournamentViewModel, NavigationService navigationService) + { + } + + public AddTournamentCommand(TournamentsStore tournamentStore, NavigationService navigationService, ModalNavigationStore modalNavigationStore, + SelectedTournamentsStore selectedTournamentsStore, AddTournamentViewModel addTournamentViewModel) { - _addTournamentViewModel = addTournamentViewModel; _tournamentStore = tournamentStore; + _navigationService = navigationService; _modalNavigationStore = modalNavigationStore; - _navigationStore = navigationStore; _selectedTournamentsStore = selectedTournamentsStore; + _addTournamentViewModel = addTournamentViewModel; } public override async Task ExecuteAsync(object parameter) @@ -52,7 +57,7 @@ namespace Apollon.WPF.Commands await _tournamentStore.Add(tournament); _modalNavigationStore.Close(); - _navigationStore.CurrentViewModel = new PreparationViewModel(_selectedTournamentsStore, _navigationStore, _modalNavigationStore, _tournamentStore); + _navigationService.Navigate(); } catch (Exception) diff --git a/Apollon.WPF/Commands/NavigateCommand.cs b/Apollon.WPF/Commands/NavigateCommand.cs index 30dc6ec..6a20234 100644 --- a/Apollon.WPF/Commands/NavigateCommand.cs +++ b/Apollon.WPF/Commands/NavigateCommand.cs @@ -1,4 +1,5 @@ -using Apollon.WPF.Stores; +using Apollon.WPF.Services; +using Apollon.WPF.Stores; using Apollon.WPF.ViewModels; using System; using System.Collections.Generic; @@ -11,18 +12,16 @@ namespace Apollon.WPF.Commands public class NavigateCommand : CommandBase where TViewModel : ViewModelBase { - private readonly NavigationStore _navigationStore; - private readonly Func _createViewModel; + private readonly NavigationService _navigationService; - public NavigateCommand(NavigationStore navigationStore, Func createViewModel) + public NavigateCommand(NavigationService navigationService) { - _navigationStore = navigationStore; - _createViewModel = createViewModel; + _navigationService = navigationService; } public override void Execute(object parameter) - { - _navigationStore.CurrentViewModel = _createViewModel(); + { + _navigationService.Navigate(); } } } diff --git a/Apollon.WPF/Services/NavigationService.cs b/Apollon.WPF/Services/NavigationService.cs new file mode 100644 index 0000000..8154cbd --- /dev/null +++ b/Apollon.WPF/Services/NavigationService.cs @@ -0,0 +1,28 @@ +using Apollon.WPF.Stores; +using Apollon.WPF.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apollon.WPF.Services +{ + public class NavigationService + where TViewModel : ViewModelBase + { + private readonly NavigationStore _navigationStore; + private readonly Func _createViewModel; + + public NavigationService(NavigationStore navigationStore, Func createViewModel) + { + _navigationStore = navigationStore; + _createViewModel = createViewModel; + } + + public void Navigate() + { + _navigationStore.CurrentViewModel = _createViewModel(); + } + } +} diff --git a/Apollon.WPF/ViewModels/AddTournametViewModel.cs b/Apollon.WPF/ViewModels/AddTournametViewModel.cs index 1020230..7b79432 100644 --- a/Apollon.WPF/ViewModels/AddTournametViewModel.cs +++ b/Apollon.WPF/ViewModels/AddTournametViewModel.cs @@ -1,4 +1,5 @@ using Apollon.WPF.Commands; +using Apollon.WPF.Services; using Apollon.WPF.Stores; using System; using System.Collections.Generic; @@ -15,7 +16,7 @@ namespace Apollon.WPF.ViewModels public AddTournamentViewModel(TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore, NavigationStore navigationStore,SelectedTournamentsStore selectedTournamentsStore) { - ICommand submitCommand = new AddTournamentCommand(this, tournamentStore,modalNavigationStore,navigationStore, selectedTournamentsStore); + ICommand submitCommand = new AddTournamentCommand(this, new NavigationService(navigationStore, () => new PreparationViewModel( selectedTournamentsStore, navigationStore, modalNavigationStore, tournamentStore))); ICommand cancelCommand = new CloseModalCommand(modalNavigationStore); AddEditDetailsViewModel = new AddEditDetailsViewModel(submitCommand, cancelCommand); } diff --git a/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs b/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs index 9e94e47..603a2dd 100644 --- a/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs @@ -1,5 +1,6 @@ using Apollon.Domain.Models; using Apollon.WPF.Commands; +using Apollon.WPF.Services; using Apollon.WPF.Stores; using System; using System.Collections.Generic; @@ -35,7 +36,8 @@ namespace Apollon.WPF.ViewModels _selectedTournamentStore.SelectedTournamentChanged += SelectedTournamentStore_SelectedTournamentChanged; - NavigateTournamentDetailsCommand = new NavigateCommand(navigationStore, () => new PreparationViewModel(_selectedTournamentStore,navigationStore, modalNavigationStore, tournamentsStore)); + NavigateTournamentDetailsCommand = new NavigateCommand(new NavigationService(navigationStore,() => new PreparationViewModel( + selectedTournamentStore,navigationStore,modalNavigationStore, tournamentsStore))); } protected override void Dispose() diff --git a/Apollon.WPF/ViewModels/OverviewViewModel.cs b/Apollon.WPF/ViewModels/OverviewViewModel.cs index 7374a5e..3a2d278 100644 --- a/Apollon.WPF/ViewModels/OverviewViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewViewModel.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; +using Apollon.WPF.Services; namespace Apollon.WPF.ViewModels { @@ -58,7 +59,7 @@ namespace Apollon.WPF.ViewModels LoadTournamentsCommand = new LoadTournamentsCommand(this, tournamentStore); AddTournamentCommand = new OpenAddTournamentCommand(tournamentStore, modalNavigationStore, navigationStore, selectedTournamentStore); - NavigateNameListCommand = new NavigateCommand(navigationStore, () => new NameListViewModel()); + NavigateNameListCommand = new NavigateCommand(new NavigationService(navigationStore, () => new NameListViewModel())); } diff --git a/Apollon.WPF/ViewModels/PreparationViewModel.cs b/Apollon.WPF/ViewModels/PreparationViewModel.cs index 5a9ac41..d01831c 100644 --- a/Apollon.WPF/ViewModels/PreparationViewModel.cs +++ b/Apollon.WPF/ViewModels/PreparationViewModel.cs @@ -1,5 +1,6 @@ using Apollon.Domain.Models; using Apollon.WPF.Commands; +using Apollon.WPF.Services; using Apollon.WPF.Stores; using System; using System.Collections.Generic; @@ -21,7 +22,8 @@ namespace Apollon.WPF.ViewModels { TournamentDetailsViewModel = new TournamentDetailsViewModel(selectedTournamentStore); - NavigateOverviewCommand = new NavigateCommand(navigationStore, () => OverviewViewModel.LoadViewModel(selectedTournamentStore, modalNavigationStore, tournamentsStore, navigationStore)); + NavigateOverviewCommand = new NavigateCommand(new NavigationService(navigationStore, () => OverviewViewModel.LoadViewModel(selectedTournamentStore, modalNavigationStore, tournamentsStore, navigationStore))); + } }