Last active
August 22, 2021 01:25
-
-
Save RupertAvery/c60c29dbf69624d56e9ac10da0601da5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WINKCrossViewX | |
{ | |
public class AppSettings | |
{ | |
public string Forges { get; set; } | |
public string APIUsername { get; set; } | |
public string APIPassword { get; set; } | |
public string RefreshInterval { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WINKCrossViewX.Settings" | |
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:local="clr-namespace:WINKCrossViewX" | |
mc:Ignorable="d" | |
d:DataContext="{d:DesignInstance local:SettingsModel, IsDesignTimeCreatable=True}" | |
Title="Settings" Height="450" Width="800"> | |
<Grid ShowGridLines="False"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="*"></ColumnDefinition> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="20"></RowDefinition> | |
<RowDefinition Height="*"></RowDefinition> | |
</Grid.RowDefinitions> | |
<TextBlock Grid.Row="0" Grid.Column="0" Text="Communication with use TCP (HTTPS) over ports 444 and 443" /> | |
<Border BorderThickness="0" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"> | |
<StackPanel> | |
<DockPanel> | |
<TextBlock Margin="10,10,10,10"> | |
WINK Forge's (IP or FQDN): | |
<LineBreak /> | |
<Italic>Seperate with CSV</Italic> | |
</TextBlock> | |
<TextBox Text="{Binding Forges}" Margin="10,0,7,10" Height="100" Width="150" TextWrapping="Wrap" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" /> | |
</DockPanel> | |
<DockPanel> | |
<TextBlock Text="API Username:" Margin="10,0,0,10" /> | |
<TextBox Text="{Binding APIUsername}" Margin="10,0,10,10" Height="20" Width="160" HorizontalAlignment="Right" /> | |
</DockPanel> | |
<DockPanel> | |
<TextBlock Text="API Password:" Margin="10,0,0,10"/> | |
<TextBox Text="{Binding APIPassword}" Margin="10,0,10,10" Height="20" Width="160" HorizontalAlignment="Right" /> | |
</DockPanel> | |
<DockPanel> | |
<TextBlock Text="Refresh Interval (Seconds):" Margin="10,0,0,10"/> | |
<TextBox Text="{Binding RefreshInterval}" Margin="10,0,10,10" PreviewTextInput="NumberValidationTextBox" Height="20" Width="160" HorizontalAlignment="Right" /> | |
</DockPanel> | |
<DockPanel> | |
<Button x:Name="Save" Click="Save_Click" Content="Save Settings" Margin="10,0,0,10" Width="140"/> | |
<Button x:Name="Reset" Click="Reset_Click" Content="Reset to Default" Margin="10,0,0,10" Width="140" /> | |
</DockPanel> | |
</StackPanel> | |
</Border> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
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 WINKCrossViewX | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class Settings : Window | |
{ | |
private readonly SettingsManager<AppSettings> _settingsManager; | |
private readonly SettingsModel _model; | |
public Settings() | |
{ | |
_settingsManager = new SettingsManager<AppSettings>("WINKCrossView.json"); | |
var appSettings = _settingsManager.LoadSettings(); | |
if (appSettings == null) | |
{ | |
appSettings = new AppSettings() | |
{ | |
Forges = "192.168.50.100,192.168.50.101", | |
APIUsername = "apiuser", | |
APIPassword = "apiuser", | |
RefreshInterval = "3" | |
}; | |
} | |
_model = new SettingsModel | |
{ | |
Forges = appSettings.Forges, | |
APIUsername = appSettings.APIUsername, | |
APIPassword = appSettings.APIPassword, | |
RefreshInterval = appSettings.RefreshInterval | |
}; | |
InitializeComponent(); | |
DataContext = _model; | |
} | |
private void Reset_Click(object sender, RoutedEventArgs e) | |
{ | |
_model.Forges = "192.168.50.100,192.168.50.101"; | |
_model.APIUsername = "user"; | |
_model.APIPassword = "pass"; | |
_model.RefreshInterval = "3"; | |
MessageBox.Show("Reset to Default Settings."); | |
} | |
private void Save_Click(object sender, RoutedEventArgs e) | |
{ | |
var appSettings = new AppSettings | |
{ | |
Forges = _model.Forges, | |
APIUsername = _model.APIUsername, | |
APIPassword = _model.APIPassword, | |
RefreshInterval = _model.RefreshInterval | |
}; | |
_settingsManager.SaveSettings(appSettings); | |
MessageBox.Show("Settings Saved."); | |
} | |
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e) | |
{ | |
Regex regex = new Regex("[^0-9]+"); | |
e.Handled = regex.IsMatch(e.Text); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
namespace WINKCrossViewX | |
{ | |
/// <summary> | |
/// Interaction logic for Settings.xaml | |
/// </summary> | |
/// | |
public class SettingsManager<T> where T : class | |
{ | |
private readonly string _filePath; | |
public SettingsManager(string fileName) | |
{ | |
_filePath = GetLocalFilePath(fileName); | |
} | |
private string GetLocalFilePath(string fileName) | |
{ | |
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | |
return System.IO.Path.Combine(appData, fileName); | |
} | |
public T LoadSettings() => | |
File.Exists(_filePath) ? | |
JsonConvert.DeserializeObject<T>(File.ReadAllText(_filePath)) : | |
null; | |
public void SaveSettings(T settings) | |
{ | |
string json = JsonConvert.SerializeObject(settings); | |
File.WriteAllText(_filePath, json); | |
//MessageBox.Show("Writing: " + _filePath + "\r\n" + json); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
namespace WINKCrossViewX | |
{ | |
public class SettingsModel: INotifyPropertyChanged | |
{ | |
private string _APIUsername; | |
private string _Forges; | |
private string _APIPassword; | |
private string _RefreshInterval; | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
public string Forges | |
{ | |
get { return _Forges; } | |
set | |
{ | |
if (value != _Forges) | |
{ | |
_Forges = value; | |
OnPropertyChanged(); | |
} | |
} | |
} | |
public string APIUsername | |
{ | |
get { return _APIUsername; } | |
set | |
{ | |
if (value != _APIUsername) | |
{ | |
_APIUsername = value; | |
OnPropertyChanged(); | |
} | |
} | |
} | |
public string APIPassword | |
{ | |
get { return _APIPassword; } | |
set | |
{ | |
if (value != _APIPassword) | |
{ | |
_APIPassword = value; | |
OnPropertyChanged(); | |
} | |
} | |
} | |
public string RefreshInterval | |
{ | |
get { return _RefreshInterval; } | |
set | |
{ | |
if (value != _RefreshInterval) | |
{ | |
_RefreshInterval = value; | |
OnPropertyChanged(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment