Save settings to JSON file

This commit is contained in:
deagahelio
2021-03-24 17:34:04 -03:00
parent 8074fe703e
commit 17c5ed7a6f
6 changed files with 133 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ namespace RedBookPlayer
{ {
public class App : Application public class App : Application
{ {
public static string CurrentTheme = "default"; public static Settings Settings;
static App() static App()
{ {
@@ -32,6 +32,8 @@ namespace RedBookPlayer
{ {
desktop.MainWindow = new MainWindow(); desktop.MainWindow = new MainWindow();
desktop.ShutdownMode = ShutdownMode.OnMainWindowClose; desktop.ShutdownMode = ShutdownMode.OnMainWindowClose;
Settings = Settings.Load("settings.json");
} }
base.OnFrameworkInitializationCompleted(); base.OnFrameworkInitializationCompleted();

View File

@@ -1,6 +1,8 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Input; using Avalonia.Input;
using System;
using System.IO;
namespace RedBookPlayer namespace RedBookPlayer
{ {
@@ -16,11 +18,50 @@ namespace RedBookPlayer
InitializeComponent(); InitializeComponent();
} }
public static void ApplyTheme(string theme)
{
if ((theme ?? "") == "")
{
return;
}
if (theme == "default")
{
MainWindow.Instance.ContentControl.Content = new PlayerView();
}
else
{
string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + theme;
string xamlPath = themeDirectory + "/view.xaml";
if (!File.Exists(xamlPath))
{
Console.WriteLine($"Warning: specified theme doesn't exist, reverting to default");
return;
}
try
{
MainWindow.Instance.ContentControl.Content = new PlayerView(
File.ReadAllText(xamlPath).Replace("Source=\"", $"Source=\"file://{themeDirectory}/")
);
}
catch (System.Xml.XmlException ex)
{
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
MainWindow.Instance.ContentControl.Content = new PlayerView();
}
}
MainWindow.Instance.Width = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width;
MainWindow.Instance.Height = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height;
}
public void OnKeyDown(object sender, KeyEventArgs e) public void OnKeyDown(object sender, KeyEventArgs e)
{ {
if (e.Key == Key.F1) if (e.Key == Key.F1)
{ {
settingsWindow = new SettingsWindow(); settingsWindow = new SettingsWindow(App.Settings);
settingsWindow.Show(); settingsWindow.Show();
} }
} }

View File

@@ -168,14 +168,14 @@ namespace RedBookPlayer
private Bitmap GetBitmap(char character) private Bitmap GetBitmap(char character)
{ {
if (App.CurrentTheme == "default") if (App.Settings.SelectedTheme == "default")
{ {
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>(); IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png"))); return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
} }
else else
{ {
string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.CurrentTheme; string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.Settings.SelectedTheme;
Bitmap bitmap; Bitmap bitmap;
using (FileStream stream = File.Open(themeDirectory + $"/{character}.png", FileMode.Open)) using (FileStream stream = File.Open(themeDirectory + $"/{character}.png", FileMode.Open))
{ {

57
RedBookPlayer/Settings.cs Normal file
View File

@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text.Json;
namespace RedBookPlayer
{
public class Settings
{
public bool AutoPlay { get; set; } = false;
public string SelectedTheme { get; set; } = "default";
string filePath;
public Settings() { }
public Settings(string filePath)
{
this.filePath = filePath;
}
public static Settings Load(string filePath)
{
if (File.Exists(filePath))
{
try
{
Settings settings = JsonSerializer.Deserialize<Settings>(File.ReadAllText(filePath));
settings.filePath = filePath;
Console.WriteLine(settings.AutoPlay);
MainWindow.ApplyTheme(settings.SelectedTheme);
return settings;
}
catch (JsonException)
{
Console.WriteLine("Couldn't parse settings, reverting to default");
return new Settings(filePath);
}
}
else
{
return new Settings(filePath);
}
}
public void Save()
{
JsonSerializerOptions options = new JsonSerializerOptions()
{
WriteIndented = true
};
string json = JsonSerializer.Serialize(this, options);
File.WriteAllText(filePath, json);
}
}
}

View File

@@ -8,7 +8,13 @@
Width="450" Height="600"> Width="450" Height="600">
<DockPanel Margin="16"> <DockPanel Margin="16">
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,4">Themes</TextBlock> <TextBlock DockPanel.Dock="Top" Margin="0,0,0,4">Themes</TextBlock>
<Button DockPanel.Dock="Bottom" Name="ApplyButton">Apply</Button> <StackPanel DockPanel.Dock="Bottom">
<WrapPanel Margin="0,0,0,16">
<CheckBox Name="AutoPlay" Margin="0,0,8,0"/>
<TextBlock VerticalAlignment="Center">Auto-play CD on load</TextBlock>
</WrapPanel>
<Button Name="ApplyButton">Apply</Button>
</StackPanel>
<ListBox Name="ThemeList" SelectionMode="Single" Margin="0,0,0,16"/> <ListBox Name="ThemeList" SelectionMode="Single" Margin="0,0,0,16"/>
</DockPanel> </DockPanel>
</Window> </Window>

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.Json;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
@@ -9,11 +10,16 @@ namespace RedBookPlayer
{ {
public class SettingsWindow : Window public class SettingsWindow : Window
{ {
Settings settings;
ListBox themeList; ListBox themeList;
string selectedTheme; string selectedTheme;
CheckBox autoPlay;
public SettingsWindow() public SettingsWindow() { }
public SettingsWindow(Settings settings)
{ {
this.settings = settings;
InitializeComponent(); InitializeComponent();
} }
@@ -27,39 +33,21 @@ namespace RedBookPlayer
selectedTheme = (string)e.AddedItems[0]; selectedTheme = (string)e.AddedItems[0];
} }
public void ApplyTheme(object sender, RoutedEventArgs e) public void LoadSettings()
{ {
if (selectedTheme == "" || selectedTheme == null) autoPlay.IsChecked = settings.AutoPlay;
}
public void ApplySettings(object sender, RoutedEventArgs e)
{
if ((selectedTheme ?? "") != "")
{ {
return; settings.SelectedTheme = selectedTheme;
MainWindow.ApplyTheme(selectedTheme);
} }
if (selectedTheme == "default") settings.AutoPlay = autoPlay.IsChecked ?? false;
{ settings.Save();
MainWindow.Instance.ContentControl.Content = new PlayerView();
}
else
{
string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + selectedTheme;
string xamlPath = themeDirectory + "/view.xaml";
try
{
MainWindow.Instance.ContentControl.Content = new PlayerView(
File.ReadAllText(xamlPath).Replace("Source=\"", $"Source=\"file://{themeDirectory}/")
);
}
catch (System.Xml.XmlException ex)
{
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
MainWindow.Instance.ContentControl.Content = new PlayerView();
}
}
MainWindow.Instance.Width = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width;
MainWindow.Instance.Height = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height;
App.CurrentTheme = selectedTheme;
} }
private void InitializeComponent() private void InitializeComponent()
@@ -89,7 +77,10 @@ namespace RedBookPlayer
themeList.Items = items; themeList.Items = items;
this.FindControl<Button>("ApplyButton").Click += ApplyTheme; autoPlay = this.FindControl<CheckBox>("AutoPlay");
LoadSettings();
this.FindControl<Button>("ApplyButton").Click += ApplySettings;
} }
} }
} }