From b651be7024c2ea1b9da6461598d6b527b4444a59 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Thu, 27 Jul 2023 22:56:11 +0100 Subject: [PATCH] Add ability to import FastFlags from JSON --- .../Menu/Pages/FastFlagEditorPage.xaml | 1 + .../Menu/Pages/FastFlagEditorPage.xaml.cs | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml b/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml index 6ea3bde..f66edba 100644 --- a/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml +++ b/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml @@ -34,6 +34,7 @@ + diff --git a/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml.cs b/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml.cs index e1217e8..4f77f3a 100644 --- a/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml.cs +++ b/Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml.cs @@ -3,6 +3,8 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; +using Microsoft.Win32; + using Wpf.Ui.Mvvm.Contracts; using Bloxstrap.UI.Elements.Dialogs; @@ -211,6 +213,63 @@ namespace Bloxstrap.UI.Elements.Menu.Pages ReloadList(); } + private void ImportJSONButton_Click(object sender, RoutedEventArgs e) + { + var dialog = new OpenFileDialog + { + Filter = "JSON files|*.json|All files|*.*" + }; + + if (dialog.ShowDialog() != true) + return; + + try + { + var list = JsonSerializer.Deserialize>(File.ReadAllText(dialog.FileName)); + + if (list is null) + throw new Exception("JSON deserialization returned null"); + + var conflictingFlags = App.FastFlags.Prop.Where(x => list.ContainsKey(x.Key)).Select(x => x.Key); + bool overwriteConflicting = false; + + if (conflictingFlags.Any()) + { + var result = Controls.ShowMessageBox( + "Some of the flags you are attempting to import already have set values. Would you like to overwrite their current values with the ones defined in the import?\n" + + "\n" + + "Conflicting flags:\n" + + String.Join(", ", conflictingFlags), + MessageBoxImage.Question, + MessageBoxButton.YesNo + ); + + overwriteConflicting = result == MessageBoxResult.Yes; + } + + foreach (var pair in list) + { + if (App.FastFlags.Prop.ContainsKey(pair.Key) && !overwriteConflicting) + continue; + + App.FastFlags.SetValue(pair.Key, pair.Value); + } + + _searchFilter = ""; + ReloadList(); + } + catch (Exception ex) + { + Controls.ShowMessageBox( + "The file you've selected does not appear to be valid JSON. Please double check the file contents and try again.\n" + + "\n" + + "More information:\n" + + $"{ex.Message}", + MessageBoxImage.Error + ); + } + } + private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e) { if (sender is not TextBox textbox)