From f67f25d8e16e3fb8fd1ecc16e6e1394208c1e837 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Fri, 7 Dec 2018 22:04:42 +0100 Subject: [PATCH] Rework NavigationService. --- Application/FileConverter/Application.xaml.cs | 20 +- .../FileConverter/FileConverter.csproj | 1 + .../Services/INavigationService.cs | 8 +- .../Services/NavigationService.cs | 189 +++++++++++------- .../ViewModels/DiagnosticsViewModel.cs | 66 ++++++ .../FileConverter/ViewModels/HelpViewModel.cs | 16 +- .../FileConverter/ViewModels/MainViewModel.cs | 24 ++- .../ViewModels/SettingsViewModel.cs | 10 +- .../ViewModels/UpgradeViewModel.cs | 24 ++- .../ViewModels/ViewModelLocator.cs | 6 +- .../Views/DiagnosticsWindow.xaml | 10 +- .../FileConverter/Views/HelpWindow.xaml | 9 +- .../FileConverter/Views/MainWindow.xaml | 7 + .../FileConverter/Views/MainWindow.xaml.cs | 73 +------ .../FileConverter/Views/SettingsWindow.xaml | 12 +- .../Views/SettingsWindow.xaml.cs | 9 - .../FileConverter/Views/UpgradeWindow.xaml | 7 + 17 files changed, 300 insertions(+), 191 deletions(-) create mode 100644 Application/FileConverter/ViewModels/DiagnosticsViewModel.cs diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 997d56c..41776b7 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -72,13 +72,13 @@ namespace FileConverter if (this.showHelp) { - navigationService.NavigateTo(Pages.Help); + navigationService.Show(Pages.Help); return; } if (this.needToRunConversionThread) { - navigationService.NavigateTo(Pages.Main); + navigationService.Show(Pages.Main); IConversionService conversionService = SimpleIoc.Default.GetInstance(); conversionService.ConversionJobsTerminated += this.ConversionService_ConversionJobsTerminated; @@ -87,12 +87,12 @@ namespace FileConverter if (this.showSettings) { - navigationService.NavigateTo(Pages.Settings); + navigationService.Show(Pages.Settings); } if (this.verbose) { - navigationService.NavigateTo(Pages.Diagnostics); + navigationService.Show(Pages.Diagnostics); } } @@ -184,11 +184,11 @@ namespace FileConverter INavigationService navigationService = SimpleIoc.Default.GetInstance(); - navigationService.RegisterPage(Pages.Help, false); - navigationService.RegisterPage(Pages.Main, false); - navigationService.RegisterPage(Pages.Settings, true); - navigationService.RegisterPage(Pages.Diagnostics, true); - navigationService.RegisterPage(Pages.Upgrade, true); + navigationService.RegisterPage(Pages.Help, false, true); + navigationService.RegisterPage(Pages.Main, false, true); + navigationService.RegisterPage(Pages.Settings, true, true); + navigationService.RegisterPage(Pages.Diagnostics, true, false); + navigationService.RegisterPage(Pages.Upgrade, true, false); } private void Initialize() @@ -351,7 +351,7 @@ namespace FileConverter private void UpgradeService_NewVersionAvailable(object sender, UpgradeVersionDescription e) { - SimpleIoc.Default.GetInstance().NavigateTo(Pages.Upgrade); + SimpleIoc.Default.GetInstance().Show(Pages.Upgrade); IUpgradeService upgradeService = SimpleIoc.Default.GetInstance(); upgradeService.NewVersionAvailable -= this.UpgradeService_NewVersionAvailable; diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 1bdb67d..0892153 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -218,6 +218,7 @@ + diff --git a/Application/FileConverter/Services/INavigationService.cs b/Application/FileConverter/Services/INavigationService.cs index 59dccea..2fe0524 100644 --- a/Application/FileConverter/Services/INavigationService.cs +++ b/Application/FileConverter/Services/INavigationService.cs @@ -4,8 +4,12 @@ namespace FileConverter.Services { using System.Windows; - public interface INavigationService : GalaSoft.MvvmLight.Views.INavigationService + public interface INavigationService { - void RegisterPage(string pageKey, bool cancelAutoExit) where T : Window; + void RegisterPage(string pageKey, bool cancelAutoExit, bool mainWindow) where T : Window; + + void Show(string pageKey); + + void Close(string pageKey, bool alreadyClosing); } } \ No newline at end of file diff --git a/Application/FileConverter/Services/NavigationService.cs b/Application/FileConverter/Services/NavigationService.cs index e1260ab..b4e7a7a 100644 --- a/Application/FileConverter/Services/NavigationService.cs +++ b/Application/FileConverter/Services/NavigationService.cs @@ -9,6 +9,8 @@ namespace FileConverter.Services using System.Runtime.CompilerServices; using System.Windows; + using FileConverter.Annotations; + using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; @@ -17,76 +19,16 @@ namespace FileConverter.Services public class NavigationService : ObservableObject, INavigationService { private readonly Dictionary pageInfoByType; - private readonly List historic; - private string currentPageKey; + private int numberOfPageShowed = 0; public NavigationService() { this.pageInfoByType = new Dictionary(); - this.historic = new List(); SimpleIoc.Default.Register(() => this); } - - public string CurrentPageKey - { - get => this.currentPageKey; - - private set - { - if (this.currentPageKey == value) - { - return; - } - - this.currentPageKey = value; - this.RaisePropertyChanged(); - } - } - - public object Parameter { get; private set; } - public void GoBack() - { - lock (this.pageInfoByType) - { - if (this.historic.Count <= 0) - { - return; - } - - string pageKey = this.historic.Last(); - PageInfo pageInfo; - if (!this.pageInfoByType.TryGetValue(pageKey, out pageInfo)) - { - throw new ArgumentException($"No such page: {pageKey} ", nameof(pageKey)); - } - - this.historic.RemoveAt(this.historic.Count - 1); - this.CurrentPageKey = this.historic.LastOrDefault(); - - Diagnostics.Debug.Log($"Go back from {pageKey ?? "null"} to {this.CurrentPageKey ?? "null"}."); - - this.pageInfoByType[pageKey] = pageInfo; - - if (pageInfo.Instance.IsVisible) - { - pageInfo.Instance.Close(); - } - - if (string.IsNullOrEmpty(this.CurrentPageKey)) - { - Application.Current.Shutdown(); - } - } - } - - public void NavigateTo(string pageKey) - { - this.NavigateTo(pageKey, null); - } - - public virtual void NavigateTo(string pageKey, object parameter) + public void Show([NotNull] string pageKey) { lock (this.pageInfoByType) { @@ -96,23 +38,23 @@ namespace FileConverter.Services throw new ArgumentException($"No such page: {pageKey}.", nameof(pageKey)); } + if (pageInfo.Showed) + { + return; + } + + pageInfo.Showed = true; + if (pageInfo.Instance == null || !pageInfo.Instance.IsLoaded) { pageInfo.Instance = Activator.CreateInstance(pageInfo.Type) as Window; } - this.Parameter = parameter; - this.historic.Add(pageKey); - this.CurrentPageKey = pageKey; - - Diagnostics.Debug.Log($"Navigate to {this.CurrentPageKey ?? "null"}."); + Diagnostics.Debug.Log($"Show page {pageKey}."); this.pageInfoByType[pageKey] = pageInfo; - - if (pageInfo.Instance.IsVisible) - { - return; - } + + this.numberOfPageShowed++; if (pageInfo.CancelAutoExit) { @@ -124,7 +66,75 @@ namespace FileConverter.Services } } - public void RegisterPage(string pageKey, bool cancelAutoExit) where T : Window + public void Close([NotNull] string pageKey, bool alreadyClosing) + { + if (pageKey == null) + { + throw new ArgumentNullException(nameof(pageKey)); + } + + lock (this.pageInfoByType) + { + PageInfo pageInfo; + if (!this.pageInfoByType.TryGetValue(pageKey, out pageInfo)) + { + throw new ArgumentException($"No such page: {pageKey}.", nameof(pageKey)); + } + + if (!pageInfo.Showed) + { + return; + } + + pageInfo.Showed = false; + + Diagnostics.Debug.Log($"Close page {pageKey}."); + + this.pageInfoByType[pageKey] = pageInfo; + + if (pageInfo.MainWindow) + { + this.CloseSecondaryWindowsIfThereIsNoOtherMainWindowShowed(); + } + + if (!alreadyClosing) + { + pageInfo.Instance.Close(); + } + + this.numberOfPageShowed--; + + // If this is the last window. + if (this.numberOfPageShowed == 0) + { + IUpgradeService upgradeService = SimpleIoc.Default.GetInstance(); + bool upgradeInProgress = upgradeService.UpgradeVersionDescription != null && + upgradeService.UpgradeVersionDescription.NeedToUpgrade && + !upgradeService.UpgradeVersionDescription.InstallerDownloadDone; + + if (upgradeInProgress) + { + if (pageKey == Pages.Upgrade) + { + upgradeService.CancelUpgrade(); + Application.Current.Shutdown(); + } + else + { + INavigationService navigationService = SimpleIoc.Default.GetInstance(); + navigationService.Show(Pages.Upgrade); + Diagnostics.Debug.Log("There is an upgrade in progress, display the upgrade window."); + } + } + else + { + Application.Current.Shutdown(); + } + } + } + } + + public void RegisterPage(string pageKey, bool cancelAutoExit, bool mainWindow) where T : Window { if (string.IsNullOrEmpty(pageKey)) { @@ -135,7 +145,7 @@ namespace FileConverter.Services { if (!this.pageInfoByType.ContainsKey(pageKey)) { - this.pageInfoByType.Add(pageKey, new PageInfo(typeof(T), cancelAutoExit)); + this.pageInfoByType.Add(pageKey, new PageInfo(typeof(T), cancelAutoExit, mainWindow)); } else { @@ -144,17 +154,46 @@ namespace FileConverter.Services } } + private void CloseSecondaryWindowsIfThereIsNoOtherMainWindowShowed() + { + List windowsToClose = new List(); + foreach (KeyValuePair keyValuePair in this.pageInfoByType) + { + if (keyValuePair.Value.MainWindow && keyValuePair.Value.Showed) + { + // There is another main window showed. + return; + } + + if (keyValuePair.Value.Showed) + { + windowsToClose.Add(keyValuePair.Key); + } + } + + // Close all windows. + for (int index = 0; index < windowsToClose.Count; index++) + { + this.Close(windowsToClose[index], false); + } + } + private struct PageInfo { public Window Instance; public Type Type; public bool CancelAutoExit; + public bool MainWindow; - public PageInfo(Type type, bool cancelAutoExit) + public bool Showed; + + public PageInfo(Type type, bool cancelAutoExit, bool mainWindow) { this.Instance = null; this.Type = type; this.CancelAutoExit = cancelAutoExit; + this.MainWindow = mainWindow; + this.Showed = false; } } } diff --git a/Application/FileConverter/ViewModels/DiagnosticsViewModel.cs b/Application/FileConverter/ViewModels/DiagnosticsViewModel.cs new file mode 100644 index 0000000..30793bb --- /dev/null +++ b/Application/FileConverter/ViewModels/DiagnosticsViewModel.cs @@ -0,0 +1,66 @@ + +namespace FileConverter.ViewModels +{ + using System; + using System.Collections.ObjectModel; + using System.ComponentModel; + using System.Windows.Input; + + using FileConverter.Commands; + using FileConverter.ConversionJobs; + using FileConverter.Services; + + using GalaSoft.MvvmLight; + using GalaSoft.MvvmLight.Command; + using GalaSoft.MvvmLight.Ioc; + + /// + /// This class contains properties that the main View can data bind to. + /// + /// Use the mvvminpc snippet to add bindable properties to this ViewModel. + /// + /// + /// You can also use Blend to data bind with the tool's support. + /// + /// + /// See http://www.galasoft.ch/mvvm + /// + /// + public class DiagnosticsViewModel : ViewModelBase + { + private RelayCommand closeCommand; + + /// + /// Initializes a new instance of the HelpViewModel class. + /// + public DiagnosticsViewModel() + { + if (this.IsInDesignMode) + { + // Code runs in Blend --> create design time data. + } + else + { + } + } + + public ICommand CloseCommand + { + get + { + if (this.closeCommand == null) + { + this.closeCommand = new RelayCommand(this.Close); + } + + return this.closeCommand; + } + } + + private void Close(CancelEventArgs args) + { + INavigationService navigationService = SimpleIoc.Default.GetInstance(); + navigationService.Close(Pages.Diagnostics, args != null); + } + } +} diff --git a/Application/FileConverter/ViewModels/HelpViewModel.cs b/Application/FileConverter/ViewModels/HelpViewModel.cs index eb1445c..a82d65b 100644 --- a/Application/FileConverter/ViewModels/HelpViewModel.cs +++ b/Application/FileConverter/ViewModels/HelpViewModel.cs @@ -28,7 +28,7 @@ namespace FileConverter.ViewModels /// public class HelpViewModel : ViewModelBase { - private RelayCommand quitCommand; + private RelayCommand closeCommand; /// /// Initializes a new instance of the HelpViewModel class. @@ -44,17 +44,23 @@ namespace FileConverter.ViewModels } } - public ICommand QuitCommand + public ICommand CloseCommand { get { - if (this.quitCommand == null) + if (this.closeCommand == null) { - this.quitCommand = new RelayCommand(() => System.Windows.Application.Current.Shutdown()); + this.closeCommand = new RelayCommand(this.Close); } - return this.quitCommand; + return this.closeCommand; } } + + private void Close(CancelEventArgs args) + { + INavigationService navigationService = SimpleIoc.Default.GetInstance(); + navigationService.Close(Pages.Help, args != null); + } } } diff --git a/Application/FileConverter/ViewModels/MainViewModel.cs b/Application/FileConverter/ViewModels/MainViewModel.cs index 7b7b0e3..0e83df6 100644 --- a/Application/FileConverter/ViewModels/MainViewModel.cs +++ b/Application/FileConverter/ViewModels/MainViewModel.cs @@ -32,6 +32,7 @@ namespace FileConverter.ViewModels private RelayCommand showSettingsCommand; private RelayCommand showDiagnosticsCommand; + private RelayCommand closeCommand; /// /// Initializes a new instance of the MainViewModel class. @@ -92,7 +93,7 @@ namespace FileConverter.ViewModels { if (this.showSettingsCommand == null) { - this.showSettingsCommand = new RelayCommand(() => SimpleIoc.Default.GetInstance().NavigateTo(Pages.Settings)); + this.showSettingsCommand = new RelayCommand(() => SimpleIoc.Default.GetInstance().Show(Pages.Settings)); } return this.showSettingsCommand; @@ -105,13 +106,32 @@ namespace FileConverter.ViewModels { if (this.showDiagnosticsCommand == null) { - this.showDiagnosticsCommand = new RelayCommand(() => SimpleIoc.Default.GetInstance().NavigateTo(Pages.Diagnostics)); + this.showDiagnosticsCommand = new RelayCommand(() => SimpleIoc.Default.GetInstance().Show(Pages.Diagnostics)); } return this.showDiagnosticsCommand; } } + public ICommand CloseCommand + { + get + { + if (this.closeCommand == null) + { + this.closeCommand = new RelayCommand(this.Close); + } + + return this.closeCommand; + } + } + + private void Close(CancelEventArgs args) + { + INavigationService navigationService = SimpleIoc.Default.GetInstance(); + navigationService.Close(Pages.Main, args != null); + } + private void ConversionJob_PropertyChanged(object sender, PropertyChangedEventArgs eventArgs) { if (eventArgs.PropertyName != "State" && eventArgs.PropertyName != "Progress") diff --git a/Application/FileConverter/ViewModels/SettingsViewModel.cs b/Application/FileConverter/ViewModels/SettingsViewModel.cs index 570534f..5c5fdc4 100644 --- a/Application/FileConverter/ViewModels/SettingsViewModel.cs +++ b/Application/FileConverter/ViewModels/SettingsViewModel.cs @@ -51,7 +51,7 @@ namespace FileConverter.ViewModels private RelayCommand addNewPresetCommand; private RelayCommand removePresetCommand; private RelayCommand saveCommand; - private RelayCommand closeCommand; + private RelayCommand closeCommand; private ListCollectionView outputTypes; private CultureInfo[] supportedCultures; @@ -314,7 +314,7 @@ namespace FileConverter.ViewModels { if (this.closeCommand == null) { - this.closeCommand = new RelayCommand(this.CloseSettings); + this.closeCommand = new RelayCommand(this.CloseSettings); } return this.closeCommand; @@ -377,13 +377,13 @@ namespace FileConverter.ViewModels this.RaisePropertyChanged(nameof(this.InputCategories)); } - private void CloseSettings() + private void CloseSettings(CancelEventArgs args) { ISettingsService settingsService = SimpleIoc.Default.GetInstance(); settingsService.RevertSettings(); INavigationService navigationService = SimpleIoc.Default.GetInstance(); - navigationService.GoBack(); + navigationService.Close(Pages.Settings, args != null); } private bool CanSaveSettings() @@ -398,7 +398,7 @@ namespace FileConverter.ViewModels settingsService.Settings.Save(); INavigationService navigationService = SimpleIoc.Default.GetInstance(); - navigationService.GoBack(); + navigationService.Close(Pages.Settings, false); } private bool CanMoveSelectedPresetUp() diff --git a/Application/FileConverter/ViewModels/UpgradeViewModel.cs b/Application/FileConverter/ViewModels/UpgradeViewModel.cs index 04b3ad7..65b13a8 100644 --- a/Application/FileConverter/ViewModels/UpgradeViewModel.cs +++ b/Application/FileConverter/ViewModels/UpgradeViewModel.cs @@ -33,6 +33,7 @@ namespace FileConverter.ViewModels private RelayCommand downloadInstallerCommand; private RelayCommand launchInstallerCommand; + private RelayCommand closeCommand; /// /// Initializes a new instance of the UpgradeViewModel class. @@ -79,6 +80,19 @@ namespace FileConverter.ViewModels return this.launchInstallerCommand; } } + + public ICommand CloseCommand + { + get + { + if (this.closeCommand == null) + { + this.closeCommand = new RelayCommand(this.Close); + } + + return this.closeCommand; + } + } public UpgradeVersionDescription VersionDescription { @@ -131,13 +145,19 @@ namespace FileConverter.ViewModels upgradeService.StartUpgrade(); INavigationService navigationService = SimpleIoc.Default.GetInstance(); - navigationService.GoBack(); + navigationService.Close(Pages.Upgrade, false); } private void ExecuteLaunchInstallerCommand() { INavigationService navigationService = SimpleIoc.Default.GetInstance(); - navigationService.GoBack(); + navigationService.Close(Pages.Upgrade, false); + } + + private void Close(CancelEventArgs args) + { + INavigationService navigationService = SimpleIoc.Default.GetInstance(); + navigationService.Close(Pages.Upgrade, args != null); } } } diff --git a/Application/FileConverter/ViewModels/ViewModelLocator.cs b/Application/FileConverter/ViewModels/ViewModelLocator.cs index 7074098..ab3b7fe 100644 --- a/Application/FileConverter/ViewModels/ViewModelLocator.cs +++ b/Application/FileConverter/ViewModels/ViewModelLocator.cs @@ -51,6 +51,7 @@ namespace FileConverter.ViewModels SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); + SimpleIoc.Default.Register(); } public HelpViewModel Help => ServiceLocator.Current.GetInstance(); @@ -61,9 +62,6 @@ namespace FileConverter.ViewModels public SettingsViewModel Settings => ServiceLocator.Current.GetInstance(); - public static void Cleanup() - { - // TODO Clear the ViewModels - } + public DiagnosticsViewModel Diagnostics => ServiceLocator.Current.GetInstance(); } } \ No newline at end of file diff --git a/Application/FileConverter/Views/DiagnosticsWindow.xaml b/Application/FileConverter/Views/DiagnosticsWindow.xaml index a36bc13..8e9121f 100644 --- a/Application/FileConverter/Views/DiagnosticsWindow.xaml +++ b/Application/FileConverter/Views/DiagnosticsWindow.xaml @@ -6,8 +6,16 @@ xmlns:diagnostics="clr-namespace:FileConverter.Diagnostics" xmlns:project="clr-namespace:FileConverter.Properties" xmlns:local="clr-namespace:FileConverter" + xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" + xmlns:command="http://www.galasoft.ch/mvvmlight" mc:Ignorable="d" - Height="600" Width="800" Icon="/FileConverter;component/Resources/ApplicationIcon.ico"> + Height="600" Width="800" Icon="/FileConverter;component/Resources/ApplicationIcon.ico" + DataContext="{Binding Diagnostics, Source={StaticResource Locator}}"> + + + + + diff --git a/Application/FileConverter/Views/HelpWindow.xaml b/Application/FileConverter/Views/HelpWindow.xaml index 4d1f087..033ba66 100644 --- a/Application/FileConverter/Views/HelpWindow.xaml +++ b/Application/FileConverter/Views/HelpWindow.xaml @@ -6,9 +6,16 @@ xmlns:fileConverter="clr-namespace:FileConverter" xmlns:project="clr-namespace:FileConverter.Properties" xmlns:gif="http://wpfanimatedgif.codeplex.com" + xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" + xmlns:command="http://www.galasoft.ch/mvvmlight" mc:Ignorable="d" Icon="/FileConverter;component/Resources/ApplicationIcon.ico" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" ResizeMode="NoResize" Width="640" Height="545" DataContext="{Binding Help, Source={StaticResource Locator}}"> + + + + + @@ -21,6 +28,6 @@ -