mirror of
https://github.com/aaru-dps/RedBookPlayer.git
synced 2025-12-16 19:24:41 +00:00
Save settings to JSON file
This commit is contained in:
@@ -9,7 +9,7 @@ namespace RedBookPlayer
|
||||
{
|
||||
public class App : Application
|
||||
{
|
||||
public static string CurrentTheme = "default";
|
||||
public static Settings Settings;
|
||||
|
||||
static App()
|
||||
{
|
||||
@@ -32,6 +32,8 @@ namespace RedBookPlayer
|
||||
{
|
||||
desktop.MainWindow = new MainWindow();
|
||||
desktop.ShutdownMode = ShutdownMode.OnMainWindowClose;
|
||||
|
||||
Settings = Settings.Load("settings.json");
|
||||
}
|
||||
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Input;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace RedBookPlayer
|
||||
{
|
||||
@@ -16,11 +18,50 @@ namespace RedBookPlayer
|
||||
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)
|
||||
{
|
||||
if (e.Key == Key.F1)
|
||||
{
|
||||
settingsWindow = new SettingsWindow();
|
||||
settingsWindow = new SettingsWindow(App.Settings);
|
||||
settingsWindow.Show();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,14 +168,14 @@ namespace RedBookPlayer
|
||||
|
||||
private Bitmap GetBitmap(char character)
|
||||
{
|
||||
if (App.CurrentTheme == "default")
|
||||
if (App.Settings.SelectedTheme == "default")
|
||||
{
|
||||
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
|
||||
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
|
||||
}
|
||||
else
|
||||
{
|
||||
string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.CurrentTheme;
|
||||
string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.Settings.SelectedTheme;
|
||||
Bitmap bitmap;
|
||||
using (FileStream stream = File.Open(themeDirectory + $"/{character}.png", FileMode.Open))
|
||||
{
|
||||
|
||||
57
RedBookPlayer/Settings.cs
Normal file
57
RedBookPlayer/Settings.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,13 @@
|
||||
Width="450" Height="600">
|
||||
<DockPanel Margin="16">
|
||||
<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"/>
|
||||
</DockPanel>
|
||||
</Window>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
@@ -9,11 +10,16 @@ namespace RedBookPlayer
|
||||
{
|
||||
public class SettingsWindow : Window
|
||||
{
|
||||
Settings settings;
|
||||
ListBox themeList;
|
||||
string selectedTheme;
|
||||
CheckBox autoPlay;
|
||||
|
||||
public SettingsWindow()
|
||||
public SettingsWindow() { }
|
||||
|
||||
public SettingsWindow(Settings settings)
|
||||
{
|
||||
this.settings = settings;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
@@ -27,39 +33,21 @@ namespace RedBookPlayer
|
||||
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")
|
||||
{
|
||||
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;
|
||||
settings.AutoPlay = autoPlay.IsChecked ?? false;
|
||||
settings.Save();
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
@@ -89,7 +77,10 @@ namespace RedBookPlayer
|
||||
|
||||
themeList.Items = items;
|
||||
|
||||
this.FindControl<Button>("ApplyButton").Click += ApplyTheme;
|
||||
autoPlay = this.FindControl<CheckBox>("AutoPlay");
|
||||
LoadSettings();
|
||||
|
||||
this.FindControl<Button>("ApplyButton").Click += ApplySettings;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user