From 1b9523c7999e7eb87e61b5d2807bfc82f60deeb5 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 9 Oct 2023 00:03:02 -0400 Subject: [PATCH] Remove some message boxes from MainViewModel --- CHANGELIST.md | 1 + MPF.UI.Core/ViewModels/MainViewModel.cs | 127 +++++++++++------------- MPF.UI.Core/Windows/MainWindow.xaml.cs | 45 +++++++-- 3 files changed, 96 insertions(+), 77 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 042d39d5..fef40f90 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -45,6 +45,7 @@ - Remove MainWindow from MainViewModel - Use callback for logging, fix Options window - Remove WinForms from MainViewModel +- Remove some message boxes from MainViewModel ### 2.6.6 (2023-10-04) diff --git a/MPF.UI.Core/ViewModels/MainViewModel.cs b/MPF.UI.Core/ViewModels/MainViewModel.cs index 8acb69bc..8197f80e 100644 --- a/MPF.UI.Core/ViewModels/MainViewModel.cs +++ b/MPF.UI.Core/ViewModels/MainViewModel.cs @@ -12,6 +12,7 @@ using MPF.Core.Data; using MPF.Core.Utilities; using MPF.Core.UI.ComboBoxItems; using SabreTools.RedumpLib.Data; +using System.Threading.Tasks; using WPFCustomMessageBox; namespace MPF.UI.Core.ViewModels @@ -506,10 +507,6 @@ namespace MPF.UI.Core.ViewModels // Finish initializing the rest of the values InitializeUIValues(removeEventHandlers: false, rescanDrives: true); - - // Check for updates, if necessary - if (Options.CheckForUpdatesOnStartup) - CheckForUpdates(showIfSame: false); } #region Property Updates @@ -700,25 +697,38 @@ namespace MPF.UI.Core.ViewModels /// /// Check for available updates /// - /// True to show the box even if it's the same, false to only show if it's different - public void CheckForUpdates(bool showIfSame) + public (bool, string, string) CheckForUpdates() { (bool different, string message, string url) = Tools.CheckForNewVersion(); - // If we have a new version, put it in the clipboard - if (different) - Clipboard.SetText(url); - SecretLogLn(message); if (url == null) message = "An exception occurred while checking for versions, please try again later. See the log window for more details."; - if (showIfSame || different) - CustomMessageBox.Show(message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information); + return (different, message, url); } /// - /// Build a dummy SubmissionInfo and display it for testing + /// Build the about text + /// + /// + public string CreateAboutText() + { + string aboutText = $"Media Preservation Frontend (MPF)" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}A community preservation frontend developed in C#." + + $"{Environment.NewLine}Supports Redumper, Aaru, and DiscImageCreator." + + $"{Environment.NewLine}Originally created to help the Redump project." + + $"{Environment.NewLine}" + + $"{Environment.NewLine}Thanks to everyone who has supported this project!" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}Version {Tools.GetCurrentVersion()}"; + SecretLogLn(aboutText); + return aboutText; + } + + /// + /// Build a dummy SubmissionInfo /// public SubmissionInfo CreateDebugSubmissionInfo() { @@ -870,25 +880,6 @@ namespace MPF.UI.Core.ViewModels /// public static void ExitApplication() => Application.Current.Shutdown(); - /// - /// Show the About text popup - /// - public void ShowAboutText() - { - string aboutText = $"Media Preservation Frontend (MPF)" - + $"{Environment.NewLine}" - + $"{Environment.NewLine}A community preservation frontend developed in C#." - + $"{Environment.NewLine}Supports Redumper, Aaru, and DiscImageCreator." - + $"{Environment.NewLine}Originally created to help the Redump project." - + $"{Environment.NewLine}" - + $"{Environment.NewLine}Thanks to everyone who has supported this project!" - + $"{Environment.NewLine}" - + $"{Environment.NewLine}Version {Tools.GetCurrentVersion()}"; - - SecretLogLn(aboutText); - CustomMessageBox.Show(aboutText, "About", MessageBoxButton.OK, MessageBoxImage.Information); - } - /// /// Toggle the Start/Stop button /// @@ -1402,56 +1393,50 @@ namespace MPF.UI.Core.ViewModels /// /// Scan and show copy protection for the current disc /// - public async void ScanAndShowProtection() + public async Task<(string, string)> ScanAndShowProtection() { // Determine current environment, just in case if (_environment == null) _environment = DetermineEnvironment(); - // Pull the drive letter from the UI directly, just in case - if (this.CurrentDrive != null && this.CurrentDrive.Letter != default(char)) - { - VerboseLogLn($"Scanning for copy protection in {this.CurrentDrive.Letter}"); + // If we don't have a valid drive + if (this.CurrentDrive == null || this.CurrentDrive.Letter == default(char)) + return (null, "No valid drive found!"); - var tempContent = this.Status; - this.Status = "Scanning for copy protection... this might take a while!"; - this.StartStopButtonEnabled = false; - this.MediaScanButtonEnabled = false; - this.UpdateVolumeLabelEnabled = false; - this.CopyProtectScanButtonEnabled = false; + VerboseLogLn($"Scanning for copy protection in {this.CurrentDrive.Letter}"); - var progress = new Progress(); - progress.ProgressChanged += ProgressUpdated; - (var protections, string error) = await Protection.RunProtectionScanOnPath(this.CurrentDrive.Letter + ":\\", this.Options, progress); - string output = Protection.FormatProtections(protections); + var tempContent = this.Status; + this.Status = "Scanning for copy protection... this might take a while!"; + this.StartStopButtonEnabled = false; + this.MediaScanButtonEnabled = false; + this.UpdateVolumeLabelEnabled = false; + this.CopyProtectScanButtonEnabled = false; - // If SmartE is detected on the current disc, remove `/sf` from the flags for DIC only -- Disabled until further notice - //if (Env.InternalProgram == InternalProgram.DiscImageCreator && output.Contains("SmartE")) - //{ - // ((Modules.DiscImageCreator.Parameters)Env.Parameters)[Modules.DiscImageCreator.FlagStrings.ScanFileProtect] = false; - // if (this.Options.VerboseLogging) - // this.Logger.VerboseLogLn($"SmartE detected, removing {Modules.DiscImageCreator.FlagStrings.ScanFileProtect} from parameters"); - //} + var progress = new Progress(); + progress.ProgressChanged += ProgressUpdated; + (var protections, string error) = await Protection.RunProtectionScanOnPath(this.CurrentDrive.Letter + ":\\", this.Options, progress); + string output = Protection.FormatProtections(protections); - if (!this.LogPanelExpanded) - { - if (string.IsNullOrEmpty(error)) - CustomMessageBox.Show(output, "Detected Protection(s)", MessageBoxButton.OK, MessageBoxImage.Information); - else - CustomMessageBox.Show("An exception occurred, see the log for details", "Error!", MessageBoxButton.OK, MessageBoxImage.Error); - } + // If SmartE is detected on the current disc, remove `/sf` from the flags for DIC only -- Disabled until further notice + //if (Env.InternalProgram == InternalProgram.DiscImageCreator && output.Contains("SmartE")) + //{ + // ((Modules.DiscImageCreator.Parameters)Env.Parameters)[Modules.DiscImageCreator.FlagStrings.ScanFileProtect] = false; + // if (this.Options.VerboseLogging) + // this.Logger.VerboseLogLn($"SmartE detected, removing {Modules.DiscImageCreator.FlagStrings.ScanFileProtect} from parameters"); + //} - if (string.IsNullOrEmpty(error)) - LogLn($"Detected the following protections in {this.CurrentDrive.Letter}:\r\n\r\n{output}"); - else - ErrorLogLn($"Path could not be scanned! Exception information:\r\n\r\n{error}"); + if (string.IsNullOrEmpty(error)) + LogLn($"Detected the following protections in {this.CurrentDrive.Letter}:\r\n\r\n{output}"); + else + ErrorLogLn($"Path could not be scanned! Exception information:\r\n\r\n{error}"); - this.Status = tempContent; - this.StartStopButtonEnabled = ShouldEnableDumpingButton(); - this.MediaScanButtonEnabled = true; - this.UpdateVolumeLabelEnabled = true; - this.CopyProtectScanButtonEnabled = true; - } + this.Status = tempContent; + this.StartStopButtonEnabled = ShouldEnableDumpingButton(); + this.MediaScanButtonEnabled = true; + this.UpdateVolumeLabelEnabled = true; + this.CopyProtectScanButtonEnabled = true; + + return (output, error); } /// diff --git a/MPF.UI.Core/Windows/MainWindow.xaml.cs b/MPF.UI.Core/Windows/MainWindow.xaml.cs index 83c9091d..4d3983ca 100644 --- a/MPF.UI.Core/Windows/MainWindow.xaml.cs +++ b/MPF.UI.Core/Windows/MainWindow.xaml.cs @@ -43,6 +43,10 @@ namespace MPF.UI.Core.Windows DebugViewMenuItem.Visibility = Visibility.Visible; MainViewModel.Init(LogOutput.EnqueueLog, ShowDiscInformationWindow); + + // Check for updates, if necessary + if (MainViewModel.Options.CheckForUpdatesOnStartup) + CheckForUpdates(showIfSame: false); } #region UI Functionality @@ -112,6 +116,22 @@ namespace MPF.UI.Core.Windows } } + /// + /// Check for available updates + /// + /// True to show the box even if it's the same, false to only show if it's different + public void CheckForUpdates(bool showIfSame) + { + (bool different, string message, string url) = MainViewModel.CheckForUpdates(); + + // If we have a new version, put it in the clipboard + if (different) + Clipboard.SetText(url); + + if (showIfSame || different) + CustomMessageBox.Show(message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information); + } + /// /// Build a dummy SubmissionInfo and display it for testing /// @@ -185,8 +205,11 @@ namespace MPF.UI.Core.Windows /// /// Handler for AboutMenuItem Click event /// - public void AboutClick(object sender, RoutedEventArgs e) => - MainViewModel.ShowAboutText(); + public void AboutClick(object sender, RoutedEventArgs e) + { + string aboutText = MainViewModel.CreateAboutText(); + CustomMessageBox.Show(aboutText, "About", MessageBoxButton.OK, MessageBoxImage.Information); + } /// /// Handler for AppExitMenuItem Click event @@ -197,8 +220,8 @@ namespace MPF.UI.Core.Windows /// /// Handler for CheckForUpdatesMenuItem Click event /// - public void CheckForUpdatesClick(object sender, RoutedEventArgs e) => - MainViewModel.CheckForUpdates(showIfSame: true); + public void CheckForUpdatesClick(object sender, RoutedEventArgs e) + => CheckForUpdates(showIfSame: true); /// /// Handler for DebugViewMenuItem Click event @@ -219,8 +242,18 @@ namespace MPF.UI.Core.Windows /// /// Handler for CopyProtectScanButton Click event /// - public void CopyProtectScanButtonClick(object sender, RoutedEventArgs e) => - MainViewModel.ScanAndShowProtection(); + public async void CopyProtectScanButtonClick(object sender, RoutedEventArgs e) + { + (string output, string error) = await MainViewModel.ScanAndShowProtection(); + + if (!MainViewModel.LogPanelExpanded) + { + if (string.IsNullOrEmpty(error)) + CustomMessageBox.Show(output, "Detected Protection(s)", MessageBoxButton.OK, MessageBoxImage.Information); + else + CustomMessageBox.Show("An exception occurred, see the log for details", "Error!", MessageBoxButton.OK, MessageBoxImage.Error); + } + } /// /// Handler for DriveLetterComboBox SelectionChanged event