Allow flag JSON importing to be done by copy-paste

This commit is contained in:
pizzaboxer 2023-08-23 13:25:37 +01:00
parent 517546087c
commit 5f8e1401c7
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
4 changed files with 144 additions and 42 deletions

View File

@ -33,10 +33,10 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" MinWidth="100" Text="Flag name" Margin="0,0,0,12" />
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" MinWidth="100" Text="Name" Margin="0,0,0,12" />
<TextBox Grid.Row="0" Grid.Column="1" Name="FlagNameTextBox" Margin="0,0,0,12" />
<TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" MinWidth="100" Text="Flag value" />
<TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" MinWidth="100" Text="Value" />
<TextBox Grid.Row="1" Grid.Column="1" Name="FlagValueTextBox" />
</Grid>

View File

@ -0,0 +1,60 @@
<ui:UiWindow x:Class="Bloxstrap.UI.Elements.Dialogs.BulkAddFastFlagDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:local="clr-namespace:Bloxstrap.UI.Elements.Dialogs"
mc:Ignorable="d"
Title="Import JSON"
MinHeight="0"
Width="480"
SizeToContent="Height"
ResizeMode="NoResize"
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
ExtendsContentIntoTitleBar="True"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ui:TitleBar Grid.Row="0" Grid.ColumnSpan="2" Padding="8" Title="Import JSON" ShowMinimize="False" ShowMaximize="False" CanMaximize="False" KeyboardNavigation.TabNavigation="None" />
<Grid Grid.Row="1" Margin="8,4,8,4">
<TextBox x:Name="JsonTextBox" Margin="5" AcceptsTab="True" AcceptsReturn="True" MinHeight="80" MaxHeight="480" />
<TextBlock IsHitTestVisible="False" Margin="18,14,0,0" Foreground="DarkGray">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=JsonTextBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
Paste in your JSON here...
</TextBlock>
</Grid>
<Border Grid.Row="3" Margin="0,10,0,0" Padding="15" Background="{ui:ThemeResource SolidBackgroundFillColorSecondaryBrush}">
<StackPanel Orientation="Horizontal" FlowDirection="LeftToRight" HorizontalAlignment="Right">
<Button MinWidth="100" Content="OK" Click="OKButton_Click">
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=JsonTextBox, Path=Text.Length}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button MinWidth="100" Margin="12,0,0,0" Content="Cancel" IsCancel="True" />
</StackPanel>
</Border>
</Grid>
</ui:UiWindow>

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Bloxstrap.UI.Elements.Dialogs
{
/// <summary>
/// Interaction logic for BulkAddFastFlagDialog.xaml
/// </summary>
public partial class BulkAddFastFlagDialog
{
public MessageBoxResult Result = MessageBoxResult.Cancel;
public BulkAddFastFlagDialog()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
Result = MessageBoxResult.OK;
Close();
}
}
}

View File

@ -215,58 +215,65 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
private void ImportJSONButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog
string json = "";
Dictionary<string, object>? list = null;
while (list is null)
{
Filter = "JSON files|*.json|All files|*.*"
};
var dialog = new BulkAddFastFlagDialog();
dialog.JsonTextBox.Text = json;
dialog.ShowDialog();
if (dialog.ShowDialog() != true)
return;
if (dialog.Result != MessageBoxResult.OK)
return;
try
{
var list = JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(dialog.FileName));
json = dialog.JsonTextBox.Text;
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())
try
{
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" +
list = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
if (list is null)
throw new Exception("JSON deserialization returned null");
}
catch (Exception ex)
{
Controls.ShowMessageBox(
"The JSON you've entered does not appear to be valid. Please double check it and try again.\n" +
"\n" +
"Conflicting flags:\n" +
String.Join(", ", conflictingFlags),
MessageBoxImage.Question,
MessageBoxButton.YesNo
"More information:\n" +
$"{ex.Message}",
MessageBoxImage.Error
);
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);
}
ClearSearch();
}
catch (Exception ex)
var conflictingFlags = App.FastFlags.Prop.Where(x => list.ContainsKey(x.Key)).Select(x => x.Key);
bool overwriteConflicting = false;
if (conflictingFlags.Any())
{
Controls.ShowMessageBox(
"The file you've selected does not appear to be valid JSON. Please double check the file contents and try again.\n" +
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" +
"More information:\n" +
$"{ex.Message}",
MessageBoxImage.Error
"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);
}
ClearSearch();
}
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)