Add form validation

This commit is contained in:
mashed-potatoes 2020-06-28 18:58:58 +05:00
parent 1e1c140035
commit ab65086cfe
6 changed files with 152 additions and 37 deletions

View file

@ -0,0 +1,36 @@
<UserControl x:Class="PotatoNV_next.Controls.NVForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PotatoNV_next.Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Style="{x:Null}">
<StackPanel>
<Label Content="Target device" />
<ComboBox Name="deviceList" />
</StackPanel>
<StackPanel>
<Label Content="Bootloader" />
<ComboBox Name="deviceBootloader" />
</StackPanel>
<StackPanel>
<Label Content="Serial number (optional)" />
<TextBox x:Name="nvSerialNumber" />
</StackPanel>
<StackPanel>
<Label Content="Board ID (optional)" />
<TextBox x:Name="nvBidNumber" />
</StackPanel>
<StackPanel>
<Label Content="Unlock code" />
<TextBox x:Name="nvUnlockCode" />
</StackPanel>
<StackPanel>
<CheckBox x:Name="disableFBLOCK" Content="Disable FBLOCK" IsChecked="True" />
<Button x:Name="startButton" Content="Start!" Height="40" Margin="0,10,0,0" Click="StartButton_Click" />
</StackPanel>
</StackPanel>
</UserControl>

View file

@ -0,0 +1,107 @@
using PotatoNV_next.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.Navigation;
using System.Windows.Shapes;
namespace PotatoNV_next.Controls
{
public partial class NVForm : UserControl
{
private UsbController usbController;
private Regex nvRegex = new Regex("^[a-zA-Z0-9]{16}$");
public NVForm()
{
InitializeComponent();
usbController = new UsbController();
usbController.AddListener(HandleDevices);
usbController.StartWorker();
}
private void Assert(bool result, string message)
{
if (result)
{
return;
}
Log.Debug("Form check failed");
Log.Error(message);
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
throw new Exception(message);
}
private void HandleDevices(UsbController.Device[] devices)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => HandleDevices(devices));
return;
}
deviceList.Items.Clear();
foreach (var device in devices)
{
deviceList.Items.Add(device.Mode == UsbController.Device.DMode.DownloadVCOM
? device.Description
: $"Fastboot: {device.Description}");
Log.Debug($"{device.Mode} mode: {device.Description}");
}
if (deviceList.SelectedIndex == -1 && devices.Length > 0)
{
deviceList.SelectedIndex = 0;
}
}
private bool VerifyNVValue(string value, bool required = false)
{
if (value == string.Empty && !required)
{
return true;
}
return nvRegex.IsMatch(value);
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
try
{
Assert(deviceList.SelectedIndex != -1, "No connected devices!\n\r" +
"Check connection and required drivers.");
Assert(deviceBootloader.SelectedIndex != -1, "Couldn't find any valid bootloader!");
Assert(VerifyNVValue(nvSerialNumber.Text), "Serial number is not valid.");
Assert(VerifyNVValue(nvBidNumber.Text), "BoardID is not valid.");
Assert(VerifyNVValue(nvUnlockCode.Text, true), "Unlock code is not valid.");
}
catch
{
return;
}
Log.Success("Form is valid, starting");
}
}
}

View file

@ -30,32 +30,7 @@
<UniformGrid Columns="2"> <UniformGrid Columns="2">
<TabControl Margin="3"> <TabControl Margin="3">
<TabItem Header="NVME Update"> <TabItem Header="NVME Update">
<StackPanel Style="{x:Null}"> <controls:NVForm />
<StackPanel>
<Label Content="Target device" />
<ComboBox Name="deviceList" />
</StackPanel>
<StackPanel>
<Label Content="Bootloader" />
<ComboBox Name="deviceBootloader" />
</StackPanel>
<StackPanel>
<Label Content="Serial number (optional)" />
<TextBox x:Name="nvSerialNumber" />
</StackPanel>
<StackPanel>
<Label Content="Board ID (optional)" />
<TextBox x:Name="bidNumber" />
</StackPanel>
<StackPanel>
<Label Content="Unlock code" />
<TextBox x:Name="unlockCode" />
</StackPanel>
<StackPanel>
<CheckBox x:Name="disableFBLOCK" Content="Disable FBLOCK" IsChecked="True" />
<Button Content="Start!" Height="40" Margin="0,10,0,0" />
</StackPanel>
</StackPanel>
</TabItem> </TabItem>
<TabItem Header="About program"> <TabItem Header="About program">
<controls:AboutTab /> <controls:AboutTab />

View file

@ -19,22 +19,11 @@ namespace PotatoNV_next
{ {
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private UsbController usbController = new UsbController();
public MainWindow() public MainWindow()
{ {
Icon = MediaConverter.ImageSourceFromBitmap(Properties.Resources.Fire.ToBitmap()); Icon = MediaConverter.ImageSourceFromBitmap(Properties.Resources.Fire.ToBitmap());
InitializeComponent(); InitializeComponent();
usbController.AddListener(HandleDevices);
usbController.StartWorker();
}
private void HandleDevices(UsbController.Device[] devices)
{
foreach (var device in devices)
{
Log.Debug($"{device.Mode} - {device.Description}");
}
} }
} }
} }

View file

@ -129,6 +129,9 @@
<Compile Include="Controls\LogBox.xaml.cs"> <Compile Include="Controls\LogBox.xaml.cs">
<DependentUpon>LogBox.xaml</DependentUpon> <DependentUpon>LogBox.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Controls\NVForm.xaml.cs">
<DependentUpon>NVForm.xaml</DependentUpon>
</Compile>
<Compile Include="Utils\Log.cs" /> <Compile Include="Utils\Log.cs" />
<Compile Include="Utils\MediaConverter.cs" /> <Compile Include="Utils\MediaConverter.cs" />
<Compile Include="Utils\UsbController.cs" /> <Compile Include="Utils\UsbController.cs" />
@ -144,6 +147,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Controls\NVForm.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>

View file

@ -9,6 +9,7 @@ namespace PotatoNV_next.Utils
public class Log public class Log
{ {
public static bool PrintDebug { get; set; } = false; public static bool PrintDebug { get; set; } = false;
private static List<Action<LogEventArgs>> actions = new List<Action<LogEventArgs>>(); private static List<Action<LogEventArgs>> actions = new List<Action<LogEventArgs>>();
public enum Status public enum Status