diff --git a/MPF.Core/UI/ComboBoxItems/Element.cs b/MPF.Core/UI/ComboBoxItems/Element.cs index 8f266045..b4568767 100644 --- a/MPF.Core/UI/ComboBoxItems/Element.cs +++ b/MPF.Core/UI/ComboBoxItems/Element.cs @@ -9,7 +9,7 @@ namespace MPF.Core.UI.ComboBoxItems /// A generic combo box element /// /// Enum type representing the possible values - public class Element : IElement where T : struct, Enum + public class Element : IEquatable>, IElement where T : struct, Enum { private readonly T Data; @@ -47,5 +47,18 @@ namespace MPF.Core.UI.ComboBoxItems .OfType() .Select(e => new Element(e)); } + + /// +#if NET48 + public bool Equals(Element other) +#else + public bool Equals(Element? other) +#endif + { + if (other == null) + return false; + + return Name == other.Name; + } } } diff --git a/MPF.Core/UI/ComboBoxItems/RedumpSystemComboBoxItem.cs b/MPF.Core/UI/ComboBoxItems/RedumpSystemComboBoxItem.cs index 4c6ea28f..d6ba20af 100644 --- a/MPF.Core/UI/ComboBoxItems/RedumpSystemComboBoxItem.cs +++ b/MPF.Core/UI/ComboBoxItems/RedumpSystemComboBoxItem.cs @@ -8,7 +8,7 @@ namespace MPF.Core.UI.ComboBoxItems /// /// Represents a single item in the System combo box /// - public class RedumpSystemComboBoxItem : IElement + public class RedumpSystemComboBoxItem : IEquatable, IElement { #if NET48 private readonly object Data; @@ -83,5 +83,18 @@ namespace MPF.Core.UI.ComboBoxItems return systemsValues; } + + /// +#if NET48 + public bool Equals(RedumpSystemComboBoxItem other) +#else + public bool Equals(RedumpSystemComboBoxItem? other) +#endif + { + if (other == null) + return false; + + return Value == other.Value; + } } } diff --git a/MPF.UI.Core/ElementConverter.cs b/MPF.UI.Core/ElementConverter.cs new file mode 100644 index 00000000..e106be6e --- /dev/null +++ b/MPF.UI.Core/ElementConverter.cs @@ -0,0 +1,45 @@ +using System; +using System.Globalization; +using System.Windows.Data; +using MPF.Core.Data; +using MPF.Core.UI.ComboBoxItems; +using SabreTools.RedumpLib.Data; + +namespace MPF.UI.Core +{ + internal class ElementConverter: IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + switch (value) + { + case InternalProgram internalProgram: + return new Element(internalProgram); + case RedumpSystem redumpSystem: + return new RedumpSystemComboBoxItem(redumpSystem); + + // Null values are treated as a system value + default: + return new RedumpSystemComboBoxItem((RedumpSystem?)null); + } + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + // If it's an IElement but ends up null + var element = value as IElement; + if (element == null) + return null; + + switch (element) + { + case Element ipElement: + return ipElement.Value; + case RedumpSystemComboBoxItem rsElement: + return rsElement.Value; + + default: return null; + } + } + } +} diff --git a/MPF.UI.Core/ViewModels/OptionsViewModel.cs b/MPF.UI.Core/ViewModels/OptionsViewModel.cs index beed4a29..38d2067c 100644 --- a/MPF.UI.Core/ViewModels/OptionsViewModel.cs +++ b/MPF.UI.Core/ViewModels/OptionsViewModel.cs @@ -7,7 +7,6 @@ using System.Windows; using System.Windows.Forms; using MPF.Core.Data; using MPF.Core.UI.ComboBoxItems; -using MPF.UI.Core.Windows; using SabreTools.RedumpLib.Web; using WPFCustomMessageBox; @@ -25,7 +24,7 @@ namespace MPF.UI.Core.ViewModels /// /// Flag for if settings were saved or not /// - public bool SavedSettings { get; private set; } + public bool SavedSettings { get; internal set; } #endregion @@ -51,34 +50,6 @@ namespace MPF.UI.Core.ViewModels Options = new Options(baseOptions); } - #region Load and Save - - /// - /// Load any options-related elements - /// - /// TODO: Convert selected list item to binding - internal void Load(OptionsWindow parent) - { - parent.InternalProgramComboBox.SelectedIndex = InternalPrograms.FindIndex(r => r == Options.InternalProgram); - parent.DefaultSystemComboBox.SelectedIndex = Systems.FindIndex(r => r == Options.DefaultSystem); - } - - /// - /// Save any options-related elements - /// - /// TODO: Convert selected list item to binding - internal void Save(OptionsWindow parent) - { - var selectedInternalProgram = parent.InternalProgramComboBox.SelectedItem as Element; - Options.InternalProgram = selectedInternalProgram?.Value ?? InternalProgram.DiscImageCreator; - var selectedDefaultSystem = parent.DefaultSystemComboBox.SelectedItem as RedumpSystemComboBoxItem; - Options.DefaultSystem = selectedDefaultSystem?.Value ?? null; - - SavedSettings = true; - } - - #endregion - #region Population /// @@ -158,24 +129,16 @@ namespace MPF.UI.Core.ViewModels /// Test Redump login credentials /// #if NET48 - public bool? TestRedumpLogin(Window parent, string username, string password) + public (bool?, string) TestRedumpLogin(Window parent, string username, string password) #else - public async Task TestRedumpLogin(Window parent, string username, string password) + public async Task<(bool?, string)> TestRedumpLogin(Window parent, string username, string password) #endif { #if NET48 - (bool? success, string message) = RedumpWebClient.ValidateCredentials(username, password); + return RedumpWebClient.ValidateCredentials(username, password); #else - (bool? success, string message) = await RedumpHttpClient.ValidateCredentials(username, password); + return await RedumpHttpClient.ValidateCredentials(username, password); #endif - if (success == true) - CustomMessageBox.Show(parent, message, "Success", MessageBoxButton.OK, MessageBoxImage.Information); - else if (success == false) - CustomMessageBox.Show(parent, message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error); - else - CustomMessageBox.Show(parent, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - - return success; } #endregion diff --git a/MPF.UI.Core/Windows/OptionsWindow.xaml b/MPF.UI.Core/Windows/OptionsWindow.xaml index feb7e323..1fe3626c 100644 --- a/MPF.UI.Core/Windows/OptionsWindow.xaml +++ b/MPF.UI.Core/Windows/OptionsWindow.xaml @@ -10,6 +10,9 @@ WindowStartupLocation="CenterOwner" ResizeMode="CanMinimize" SizeToContent="Height" BorderBrush="DarkGray" BorderThickness="2"> + + + @@ -99,26 +102,31 @@