Lynne
b26326baf5
- use var instead of specific type names where applicable - get user's home directory from Environment.SpecialFolder.UserProfile instead of reading the HOME environment variable, which isn't always set - use object initialisers where possible - remove redundant identifiers (e.g. "Gtk.Application.Invoke" becomes "Application.Invoke"), of which there were MANY - remove unused local variables - replace "variable as Class" with "(Class) variable" - many other miscellaneous improvements thanks rider 0u0
91 lines
No EOL
2.9 KiB
C#
91 lines
No EOL
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Gtk;
|
|
// ReSharper disable UnusedMember.Local
|
|
// ReSharper disable UnusedParameter.Local
|
|
|
|
namespace Buypeeb {
|
|
internal class SettingsWindow : Window {
|
|
private readonly List<Switch> generalSwitches = new List<Switch>();
|
|
private readonly List<Entry> updateIntervalEntries = new List<Entry>();
|
|
private readonly Settings settings;
|
|
private readonly Builder builder;
|
|
|
|
private readonly List<string> generalSwitchNames = new List<string> { "ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList" };
|
|
private readonly List<string> updateIntervalEntryNames = new List<string> { "UpdateInterval", "UpdateIntervalCritical", "FavouriteUpdateInterval", "FavouriteUpdateIntervalCritical" };
|
|
|
|
public SettingsWindow(Settings settings) : this(new Builder("settings.glade"), settings) { }
|
|
|
|
private SettingsWindow(Builder builder, Settings settings) : base(builder.GetObject("WindowSettings").Handle) {
|
|
Title = "Buypeeb - Settings";
|
|
this.settings = settings;
|
|
this.builder = builder;
|
|
builder.Autoconnect(this);
|
|
|
|
foreach (var name in generalSwitchNames) {
|
|
var s = (Switch)builder.GetObject($"Switch{name}");
|
|
generalSwitches.Add(s);
|
|
s.Active = GetSetting<bool>(PropertyName(name));
|
|
}
|
|
|
|
foreach (var name in updateIntervalEntryNames) {
|
|
var e = (Entry)builder.GetObject($"Entry{name}");
|
|
updateIntervalEntries.Add(e);
|
|
e.Text = GetSetting<int>(PropertyName(name)).ToString();
|
|
}
|
|
}
|
|
|
|
private T GetSetting<T>(string property) {
|
|
return (T)settings.GetType().GetProperty(property).GetValue(settings, null);
|
|
}
|
|
|
|
private void SetSetting<T>(string property, T value) {
|
|
settings.GetType().GetProperty(property).SetValue(settings, value);
|
|
}
|
|
|
|
private string PropertyName(string property) {
|
|
// replace "PropertyName" with "propertyName";
|
|
return property[0].ToString().ToLower() + property.Substring(1);
|
|
}
|
|
|
|
private void ButtonSaveClicked(object sender, EventArgs args) {
|
|
// first, validate all the intervals
|
|
var failed = false;
|
|
foreach (var name in updateIntervalEntryNames) {
|
|
var e = (Entry)builder.GetObject($"Entry{name}");
|
|
if (!int.TryParse(e.Text, out var result)) {
|
|
failed = true;
|
|
}
|
|
else {
|
|
if (result < 30 || result > 6000) {
|
|
failed = true;
|
|
}
|
|
}
|
|
|
|
if (!failed) {
|
|
continue;
|
|
}
|
|
|
|
var md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Update intervals must be a whole number between 30 and 6000.");
|
|
md.Run();
|
|
md.Dispose();
|
|
return;
|
|
}
|
|
|
|
// validation success!
|
|
|
|
foreach (var name in updateIntervalEntryNames) {
|
|
SetSetting(PropertyName(name), int.Parse(((Entry) builder.GetObject($"Entry{name}")).Text));
|
|
}
|
|
|
|
foreach (var name in generalSwitchNames) {
|
|
SetSetting(PropertyName(name), ((Switch) builder.GetObject($"Switch{name}")).Active);
|
|
}
|
|
Dispose();
|
|
}
|
|
|
|
private void ButtonCancelClicked(object sender, EventArgs args) {
|
|
Dispose();
|
|
}
|
|
}
|
|
} |