Files
Nebula-Auth/NebulaAuth/Model/Settings.cs
T
2024-02-01 01:21:56 +02:00

70 lines
2.1 KiB
C#

using NebulaAuth.Core;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
namespace NebulaAuth.Model;
public partial class Settings : ObservableObject
{
#region Properties
[ObservableProperty] private bool _disableTimersOnChange = true;
[ObservableProperty] private BackgroundMode _backgroundMode = BackgroundMode.Default;
[ObservableProperty] private bool _hideToTray;
[ObservableProperty] private int _timerSeconds = 60;
[ObservableProperty] private Color? _backgroundColor;
[ObservableProperty] private Color? _iconColor;
[ObservableProperty] private bool _isPasswordSet;
[ObservableProperty] private LocalizationLanguage _language = LocalizationLanguage.English;
[ObservableProperty] private bool _legacyMode = true;
[ObservableProperty] private bool _allowAutoUpdate;
#endregion
public static Settings Instance { get; }
static Settings()
{
if (File.Exists("settings.json") == false)
{
Instance = new();
Instance.PropertyChanged += SettingsOnPropertyChanged;
return;
}
try
{
var json = File.ReadAllText("settings.json");
var settings = JsonConvert.DeserializeObject<Settings>(json) ?? throw new NullReferenceException();
Instance = settings;
}
catch (Exception ex)
{
SnackbarController.SendSnackbar("Ошибка при загрузке настроек. Настройки были сброшены");
SnackbarController.SendSnackbar(ex.Message);
Instance = new();
}
Instance.PropertyChanged += SettingsOnPropertyChanged;
}
private static void SettingsOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
Save();
}
public static void Save()
{
var json = JsonConvert.SerializeObject(Instance, Formatting.Indented);
File.WriteAllText("settings.json", json);
}
}
public enum BackgroundMode
{
Default, Custom, Color
}