diff --git a/src/MercuryConverter.csproj b/src/MercuryConverter.csproj index 684364a..04eddad 100644 --- a/src/MercuryConverter.csproj +++ b/src/MercuryConverter.csproj @@ -19,8 +19,10 @@ None All + + diff --git a/src/Program.cs b/src/Program.cs index 17e17b1..c10c255 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,11 +1,12 @@ namespace MercuryConverter; -using Avalonia; using System; -using MercuryConverter.UI; +using Avalonia; using Avalonia.Logging; +using MercuryConverter.UI; + class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any @@ -14,16 +15,15 @@ class Program [STAThread] public static void Main(string[] args) { - // BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); + new Settings(); + try { - // prepare and run your App here BuildAvaloniaApp() .StartWithClassicDesktopLifetime(args); } catch (Exception e) { - // here we can work with the exception, for example add it to our log file Console.WriteLine($"App exception!!\b{e}"); } } diff --git a/src/Settings.cs b/src/Settings.cs new file mode 100644 index 0000000..bd5b41a --- /dev/null +++ b/src/Settings.cs @@ -0,0 +1,94 @@ +using System; +using System.ComponentModel; +using System.IO; +using CommunityToolkit.Mvvm.ComponentModel; +using IniParser; +using IniParser.Model; + +namespace MercuryConverter; + +public enum Theme +{ + Light, Dark, System +} + +public partial class Settings : ObservableObject +{ + public static Settings? I; + + private string iniPath; + + public string AppDataPath => + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "muskit", "MercuryConverter"); + + [ObservableProperty] + private string dataPath = ""; + [ObservableProperty] + private Theme theme = Theme.System; + + protected override void OnPropertyChanged(PropertyChangedEventArgs e) + { + Console.Write($"Setting {e.PropertyName} changed to "); + switch (e.PropertyName) + { + case nameof(DataPath): + Console.WriteLine(DataPath); + break; + default: + Console.WriteLine("unknown variable"); + break; + } + SaveToIni(); + + base.OnPropertyChanged(e); + } + + public Settings() + { + I = this; + + Console.WriteLine($"Settings path: {AppDataPath}"); + iniPath = Path.Combine(AppDataPath, "settings.ini"); + + // Attempt to read settings; try to create new if unable to + try + { + LoadFromIni(); + } + catch (Exception e) + { + Console.WriteLine($"Couldn't read {iniPath}!\n{e.Message}"); + Console.WriteLine("Attempting to create new settings file."); + SaveToIni(); + } + } + + private void SaveToIni() + { + var data = new IniData(); + data["paths"]["data"] = DataPath; + data["ui"]["theme"] = Theme.ToString(); + + try + { + Directory.CreateDirectory(Path.GetDirectoryName(iniPath)!); + FileIniDataParser parser = new(); + parser.WriteFile(iniPath, data); + Console.WriteLine($"Settings saved to {iniPath}."); + } + catch (Exception e) + { + Console.WriteLine($"Could not save settings to {iniPath}!\n{e.Message}"); + } + } + + private void LoadFromIni() + { + FileIniDataParser parser = new(); + var iniData = parser.ReadFile(iniPath); + + DataPath = iniData["paths"]["data"]; + if (Enum.TryParse(iniData["ui"]["theme"], out Theme loadedTheme)) + Theme = loadedTheme; + } +} \ No newline at end of file diff --git a/src/UI/Dialogs/DataScanning.axaml b/src/UI/Dialogs/DataScanning.axaml index 39a12b2..8d77377 100644 --- a/src/UI/Dialogs/DataScanning.axaml +++ b/src/UI/Dialogs/DataScanning.axaml @@ -37,7 +37,7 @@ Margin="0,15,0,0"/>