dev: updater
This commit is contained in:
@@ -13,7 +13,7 @@ namespace CarManagerV3.Forms
|
||||
public partial class SettingsForm : Form
|
||||
{
|
||||
// Settings map (Maps settings to controls and default values + optional change event handler function that takes previous and new value)
|
||||
private Dictionary<string, (Control control, string defaultValue, Action<string, string> onChange)> settingsMap = new Dictionary<string, (Control control, string defaultValue, Action<string, string> onChange)>();
|
||||
private readonly Dictionary<string, ISettingBinding> settingsMap = new();
|
||||
|
||||
public SettingsForm()
|
||||
{
|
||||
@@ -24,11 +24,23 @@ namespace CarManagerV3.Forms
|
||||
private void initializeSettingsMap()
|
||||
{
|
||||
// Initialize the settings map with setting keys, associated controls, default values, and optional change event handlers
|
||||
settingsMap["DataLocation"] = (tbxDataLocation, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CarManagerV3", (string before, string after) =>
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
);
|
||||
settingsMap["DataLocation"] =
|
||||
new SettingBinding<string>(
|
||||
control: tbxDataLocation,
|
||||
defaultValue: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CarManagerV3",
|
||||
read: () => Properties.Settings.Default.DataLocation, // strongly typed
|
||||
write: v => Properties.Settings.Default.DataLocation = v, // strongly typed
|
||||
onChange: (before, after) =>
|
||||
{
|
||||
// TODO: handle path change if needed
|
||||
});
|
||||
|
||||
settingsMap["AllowPrerelease"] =
|
||||
new SettingBinding<bool>(
|
||||
control: cbxPreRelease,
|
||||
defaultValue: false,
|
||||
read: () => Properties.Settings.Default.AllowPrerelease,
|
||||
write: v => Properties.Settings.Default.AllowPrerelease = v);
|
||||
|
||||
}
|
||||
|
||||
@@ -39,51 +51,32 @@ namespace CarManagerV3.Forms
|
||||
|
||||
private void loadSettings()
|
||||
{
|
||||
// with settings map
|
||||
foreach (var setting in settingsMap)
|
||||
{
|
||||
string key = setting.Key;
|
||||
Control control = setting.Value.control;
|
||||
string value = Properties.Settings.Default[key]?.ToString() ?? string.Empty;
|
||||
control.Text = value;
|
||||
}
|
||||
foreach (var kvp in settingsMap)
|
||||
kvp.Value.Load();
|
||||
}
|
||||
|
||||
private void saveSettings()
|
||||
{
|
||||
// Save settings using the settings map
|
||||
foreach (var setting in settingsMap)
|
||||
{
|
||||
string key = setting.Key;
|
||||
Control control = setting.Value.control;
|
||||
string value = control.Text;
|
||||
string oldValue = Properties.Settings.Default[key]?.ToString() ?? string.Empty;
|
||||
// Save the value to application settings
|
||||
Properties.Settings.Default[key] = value;
|
||||
// Invoke the change event handler if it exists and the value has changed
|
||||
if (setting.Value.onChange != null && oldValue != value)
|
||||
{
|
||||
setting.Value.onChange(oldValue, value);
|
||||
}
|
||||
}
|
||||
foreach (var kvp in settingsMap)
|
||||
kvp.Value.Save();
|
||||
|
||||
Properties.Settings.Default.Save();
|
||||
}
|
||||
|
||||
private void resetSettings()
|
||||
{
|
||||
DialogResult confirmReset = MessageBox.Show("Are you sure you want to reset all settings to their default values? This action cannot be undone.", "Confirm Reset", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
|
||||
if(confirmReset != DialogResult.Yes)
|
||||
{
|
||||
DialogResult confirmReset = MessageBox.Show(
|
||||
"Are you sure you want to reset all settings to their default values? This action cannot be undone.",
|
||||
"Confirm Reset",
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Warning);
|
||||
|
||||
if (confirmReset != DialogResult.Yes)
|
||||
return;
|
||||
}
|
||||
// Reset settings to default values using the settings map
|
||||
foreach (var setting in settingsMap)
|
||||
{
|
||||
string key = setting.Key;
|
||||
string defaultValue = setting.Value.defaultValue;
|
||||
// Reset the value to default in application settings
|
||||
Properties.Settings.Default[key] = defaultValue;
|
||||
}
|
||||
|
||||
foreach (var kvp in settingsMap)
|
||||
kvp.Value.Reset();
|
||||
|
||||
Properties.Settings.Default.Save();
|
||||
loadSettings();
|
||||
}
|
||||
@@ -104,4 +97,77 @@ namespace CarManagerV3.Forms
|
||||
resetSettings();
|
||||
}
|
||||
}
|
||||
|
||||
internal interface ISettingBinding
|
||||
{
|
||||
void Load();
|
||||
void Save();
|
||||
void Reset();
|
||||
}
|
||||
|
||||
internal sealed class SettingBinding<T> : ISettingBinding
|
||||
{
|
||||
private readonly Control control;
|
||||
private readonly T defaultValue;
|
||||
private readonly Func<T> read;
|
||||
private readonly Action<T> write;
|
||||
private readonly Action<T, T>? onChange;
|
||||
|
||||
public SettingBinding(Control control, T defaultValue, Func<T> read, Action<T> write, Action<T, T>? onChange = null)
|
||||
{
|
||||
this.control = control;
|
||||
this.defaultValue = defaultValue;
|
||||
this.read = read;
|
||||
this.write = write;
|
||||
this.onChange = onChange;
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
T value = read();
|
||||
ApplyToControl(value);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
T before = read();
|
||||
T after = ReadFromControl();
|
||||
|
||||
write(after);
|
||||
|
||||
if (onChange != null && !EqualityComparer<T>.Default.Equals(before, after))
|
||||
onChange(before, after);
|
||||
}
|
||||
|
||||
public void Reset() => write(defaultValue);
|
||||
|
||||
private void ApplyToControl(T value)
|
||||
{
|
||||
switch (control)
|
||||
{
|
||||
case TextBox tb:
|
||||
tb.Text = value?.ToString() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case CheckBox cb:
|
||||
cb.Checked = value is bool b ? b : Convert.ToBoolean(value);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"Control type '{control.GetType().Name}' not supported for {typeof(T).Name}.");
|
||||
}
|
||||
}
|
||||
|
||||
private T ReadFromControl()
|
||||
{
|
||||
object result = control switch
|
||||
{
|
||||
TextBox tb when typeof(T) == typeof(string) => tb.Text,
|
||||
CheckBox cb when typeof(T) == typeof(bool) => cb.Checked,
|
||||
_ => throw new NotSupportedException($"Cannot read {typeof(T).Name} from control type '{control.GetType().Name}'.")
|
||||
};
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user