From 08b779eceb98be102c143cc838999748761cf609 Mon Sep 17 00:00:00 2001 From: AlexanderDotH Date: Tue, 25 Apr 2023 17:58:13 +0200 Subject: [PATCH] Implemented the new setting-system --- .../Backend/Cache/CacheManager.cs | 4 +- .../Musixmatch/MusixmatchCollector.cs | 4 +- .../Musixmatch/MusixmatchCollector.cs | 5 +- .../Musixmatch/MusixmatchTokenCollector.cs | 41 ++-- OpenLyricsClient/Backend/Core.cs | 17 +- .../Services/Spotify/SpotifyService.cs | 3 +- .../Spotify/SpotifySongProvider.cs | 4 +- .../Backend/Romanization/Romanization.cs | 16 +- .../Backend/Settings/EnumSetting.cs | 7 - .../Romanization/RomanizationSection.cs | 100 +++++++++ .../Sections/Romanization/Structure.cs | 9 + .../Settings/Sections/Tokens/Structure.cs | 9 + .../Settings/Sections/Tokens/TokenSection.cs | 87 ++++++++ .../Backend/Settings/SettingManager.cs | 195 ------------------ OpenLyricsClient/Backend/Settings/Settings.cs | 17 -- .../Backend/Settings/SettingsHandler.cs | 8 + .../Settings/SettingsRomanizationViewModel.cs | 34 +-- 17 files changed, 275 insertions(+), 285 deletions(-) delete mode 100644 OpenLyricsClient/Backend/Settings/EnumSetting.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Romanization/RomanizationSection.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Romanization/Structure.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Tokens/Structure.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs delete mode 100644 OpenLyricsClient/Backend/Settings/SettingManager.cs delete mode 100644 OpenLyricsClient/Backend/Settings/Settings.cs diff --git a/OpenLyricsClient/Backend/Cache/CacheManager.cs b/OpenLyricsClient/Backend/Cache/CacheManager.cs index c375d7c..70dbc9e 100644 --- a/OpenLyricsClient/Backend/Cache/CacheManager.cs +++ b/OpenLyricsClient/Backend/Cache/CacheManager.cs @@ -31,9 +31,9 @@ public class CacheManager private Debugger _debugger; - public CacheManager(int maxCapacity, int expirationMs) + public CacheManager(string workingDirectory, int maxCapacity, int expirationMs) { - CACHE_PATH = string.Format("{1}{2}{0}", Path.DirectorySeparatorChar, Core.INSTANCE.SettingManager.WorkingDirectory, + CACHE_PATH = string.Format("{1}{2}{0}", Path.DirectorySeparatorChar, workingDirectory, CACHE_FOLDER_NAME); this._debugger = new Debugger(this); diff --git a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Musixmatch/MusixmatchCollector.cs b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Musixmatch/MusixmatchCollector.cs index ac66965..e5d2935 100644 --- a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Musixmatch/MusixmatchCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Musixmatch/MusixmatchCollector.cs @@ -40,12 +40,12 @@ public async Task GetLyrics(SongResponseObject songResponseObject) if (!songResponseObject.CollectorName.Equals(this.CollectorName())) return new LyricData(); - string token = MusixmatchTokenCollector.Instance.GetToken().Token; + MusixMatchToken token = await MusixmatchTokenCollector.Instance.GetToken(); if (!DataValidator.ValidateData(token)) return new LyricData(); - MusixmatchClient musixmatchClient = new MusixmatchClient(token); + MusixmatchClient musixmatchClient = new MusixmatchClient(token.Token); if (!DataValidator.ValidateData(musixmatchClient)) return new LyricData(); diff --git a/OpenLyricsClient/Backend/Collector/Song/Providers/Musixmatch/MusixmatchCollector.cs b/OpenLyricsClient/Backend/Collector/Song/Providers/Musixmatch/MusixmatchCollector.cs index fcafc05..ceb5de9 100644 --- a/OpenLyricsClient/Backend/Collector/Song/Providers/Musixmatch/MusixmatchCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Song/Providers/Musixmatch/MusixmatchCollector.cs @@ -6,6 +6,7 @@ using MusixmatchClientLib.Types; using OpenLyricsClient.Backend.Collector.Token.Provider.Musixmatch; using OpenLyricsClient.Backend.Debugger; +using OpenLyricsClient.Backend.Structure; using OpenLyricsClient.Backend.Structure.Enum; using OpenLyricsClient.Backend.Structure.Song; using OpenLyricsClient.Backend.Utils; @@ -26,12 +27,12 @@ public async Task GetSong(SongRequestObject songRequestObjec if (!DataValidator.ValidateData(songRequestObject)) return null; - string token = MusixmatchTokenCollector.Instance.GetToken().Token; + MusixMatchToken token = await MusixmatchTokenCollector.Instance.GetToken(); if (!DataValidator.ValidateData(token)) return null; - MusixmatchClient musixmatchClient = new MusixmatchClient(token); + MusixmatchClient musixmatchClient = new MusixmatchClient(token.Token); if (!DataValidator.ValidateData(musixmatchClient)) return null; diff --git a/OpenLyricsClient/Backend/Collector/Token/Provider/Musixmatch/MusixmatchTokenCollector.cs b/OpenLyricsClient/Backend/Collector/Token/Provider/Musixmatch/MusixmatchTokenCollector.cs index 4fb10ec..35eeafb 100644 --- a/OpenLyricsClient/Backend/Collector/Token/Provider/Musixmatch/MusixmatchTokenCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Token/Provider/Musixmatch/MusixmatchTokenCollector.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading.Tasks; using MusixmatchClientLib.Auth; +using OpenLyricsClient.Backend.Settings.Sections.Tokens; using OpenLyricsClient.Backend.Structure; namespace OpenLyricsClient.Backend.Collector.Token.Provider.Musixmatch @@ -32,16 +33,15 @@ public MusixmatchTokenCollector() public async Task CollectToken() { - bool settingsChanged = false; - - return; + List tokens = Core.INSTANCE.SettingsHandler.Settings() + .GetValue>("Tokens"); - /*try + try { - if (!DataValidator.ValidateData(Core.INSTANCE.SettingManager.Settings.MusixMatchToken)) + if (!DataValidator.ValidateData(tokens)) return; - if (Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Count > this._tokenLimit) + if (tokens.Count > this._tokenLimit) return; string token = await new MusixmatchToken("").IssueNewTokenAsync(); @@ -52,37 +52,32 @@ public async Task CollectToken() mxmToken.ExpirationDate = expiresIn; mxmToken.Usage = 5; - Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Add(mxmToken); - - _debugger.Write("Requested new musixmatch token", DebugType.INFO); + await Core.INSTANCE.SettingsHandler.Settings()!.AddToken(mxmToken); - settingsChanged = true; + this._debugger.Write("Requested new musixmatch token", DebugType.INFO); } catch (Exception e) { - _debugger.Write(e); + this._debugger.Write(e); } //Check expiration date - for (int i = 0; i < Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Count; i++) + for (int i = 0; i < tokens.Count; i++) { - MusixMatchToken token = Core.INSTANCE.SettingManager.Settings.MusixMatchToken[i]; + MusixMatchToken token = tokens[i]; if (DateTimeOffset.Now.ToUnixTimeMilliseconds() > token.ExpirationDate) { - Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Remove(token); - settingsChanged = true; + await Core.INSTANCE.SettingsHandler.Settings()!.RemoveToken(token); } } - - if (settingsChanged) - Core.INSTANCE.SettingManager.WriteSettings(false);*/ } - public MusixMatchToken GetToken() + public async Task GetToken() { - /*List tokens = Core.INSTANCE.SettingManager.Settings.MusixMatchToken; + List tokens = Core.INSTANCE.SettingsHandler.Settings() + .GetValue>("Tokens"); if (tokens.Count == 0) return null; @@ -92,10 +87,10 @@ public MusixMatchToken GetToken() if (token.Usage <= 0) { - Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Remove(token); - }*/ + await Core.INSTANCE.SettingsHandler.Settings()!.RemoveToken(token); + } - return null; + return token; } } } diff --git a/OpenLyricsClient/Backend/Core.cs b/OpenLyricsClient/Backend/Core.cs index cc07965..8ef7181 100644 --- a/OpenLyricsClient/Backend/Core.cs +++ b/OpenLyricsClient/Backend/Core.cs @@ -34,7 +34,6 @@ class Core private Debugger _debugger; - private SettingManager _settingManager; private SettingsHandler _settingsHandler; private ServiceHandler _serviceHandler; @@ -84,14 +83,13 @@ public Core() this._windowLogger = new WindowLogger(); - this._settingManager = new SettingManager(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + - string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar)); - - this._settingsHandler = new SettingsHandler( + string workingDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + - string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar)); + string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar); + + this._settingsHandler = new SettingsHandler(workingDirectory); - this._cacheManager = new CacheManager(10, TimeSpan.FromMinutes(5).Milliseconds); + this._cacheManager = new CacheManager(workingDirectory, 10, TimeSpan.FromMinutes(5).Milliseconds); this._tokenCollector = new TokenCollector(); @@ -155,11 +153,6 @@ protected virtual void SlowTickEvent() slowTickEventHandler?.Invoke(this); } - public SettingManager SettingManager - { - get => _settingManager; - } - public SettingsHandler SettingsHandler { get => this._settingsHandler; diff --git a/OpenLyricsClient/Backend/Handler/Services/Services/Spotify/SpotifyService.cs b/OpenLyricsClient/Backend/Handler/Services/Services/Spotify/SpotifyService.cs index 7f21c36..57e7bf8 100644 --- a/OpenLyricsClient/Backend/Handler/Services/Services/Spotify/SpotifyService.cs +++ b/OpenLyricsClient/Backend/Handler/Services/Services/Spotify/SpotifyService.cs @@ -65,8 +65,7 @@ private async Task RefreshToken() public bool IsConnected() { - return Core.INSTANCE.SettingsHandler.Settings().GetValue("IsSpotifyConnected") - && Core.INSTANCE.SettingsHandler.Settings().GetValue("AccessToken") != null; + return Core.INSTANCE.SettingsHandler.Settings().GetValue("IsSpotifyConnected") == true; } public async Task TestConnection() diff --git a/OpenLyricsClient/Backend/Handler/Song/SongProvider/Spotify/SpotifySongProvider.cs b/OpenLyricsClient/Backend/Handler/Song/SongProvider/Spotify/SpotifySongProvider.cs index 004df2c..1211434 100644 --- a/OpenLyricsClient/Backend/Handler/Song/SongProvider/Spotify/SpotifySongProvider.cs +++ b/OpenLyricsClient/Backend/Handler/Song/SongProvider/Spotify/SpotifySongProvider.cs @@ -32,6 +32,8 @@ public SpotifySongProvider() this._service = Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify"); + this._spotifyClient = GetPlayerApi(); + Core.INSTANCE.TaskRegister.Register( out _updateSongDataSuspensionToken, new Task(async () => await UpdateSongDataTask(), Core.INSTANCE.CancellationTokenSource.Token, TaskCreationOptions.LongRunning), @@ -172,7 +174,7 @@ private async Task UpdateSongDataTask() private SpotifyClient GetPlayerApi() { - return new SpotifyClient(Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify").GetAccessToken()); + return new SpotifyClient(this._service.GetAccessToken()); } public void Dispose() diff --git a/OpenLyricsClient/Backend/Romanization/Romanization.cs b/OpenLyricsClient/Backend/Romanization/Romanization.cs index 92fb54d..6cee0ff 100644 --- a/OpenLyricsClient/Backend/Romanization/Romanization.cs +++ b/OpenLyricsClient/Backend/Romanization/Romanization.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using HangeulRomanizer; using Kawazu; +using OpenLyricsClient.Backend.Settings.Sections.Romanization; using OpenLyricsClient.Backend.Structure.Lyrics; using OpenLyricsClient.Backend.Utils; using Romanization; @@ -34,10 +36,13 @@ public async Task Romanize(string text) if (!DataValidator.ValidateData(text)) return text; - //if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Count == 0) - // return text; + List selections = Core.INSTANCE.SettingsHandler.Settings() + .GetValue>("Selections"); - /*if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Contains(RomanizeSelection.JAPANESE_TO_ROMANJI) && DataValidator.ValidateData(this._kawazuConverter)) + if (selections.Count == 0) + return text; + + if (selections.Contains(RomanizeSelection.JAPANESE_TO_ROMANJI) && DataValidator.ValidateData(this._kawazuConverter)) { if (Utilities.HasJapanese(text)) { @@ -46,7 +51,7 @@ public async Task Romanize(string text) } } - if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Contains(RomanizeSelection.KOREAN_TO_ROMANJI) && DataValidator.ValidateData(this._koreanConverter)) + if (selections.Contains(RomanizeSelection.KOREAN_TO_ROMANJI) && DataValidator.ValidateData(this._koreanConverter)) { if (LanguageUtils.IsKorean(text)) { @@ -55,14 +60,13 @@ public async Task Romanize(string text) } } - if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Contains(RomanizeSelection.RUSSIA_TO_LATIN) && DataValidator.ValidateData(this._russiaConverter)) + if (selections.Contains(RomanizeSelection.RUSSIA_TO_LATIN) && DataValidator.ValidateData(this._russiaConverter)) { if (this._russiaConverter.IsPartOfCulture(text)) { return this._russiaConverter.Process(text); } } - */ return text; } diff --git a/OpenLyricsClient/Backend/Settings/EnumSetting.cs b/OpenLyricsClient/Backend/Settings/EnumSetting.cs deleted file mode 100644 index 038b038..0000000 --- a/OpenLyricsClient/Backend/Settings/EnumSetting.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace OpenLyricsClient.Backend.Settings -{ - public enum EnumSetting - { - SPOTIFY, TIDAL, ROMANIZATION, MUSIXMATCH_TOKENS, DISPLAY_PREFERENCES - } -} diff --git a/OpenLyricsClient/Backend/Settings/Sections/Romanization/RomanizationSection.cs b/OpenLyricsClient/Backend/Settings/Sections/Romanization/RomanizationSection.cs new file mode 100644 index 0000000..bd34028 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Romanization/RomanizationSection.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using DevBase.Api.Serializer; +using DevBase.Generics; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OpenLyricsClient.Backend.Romanization; +using OpenLyricsClient.Backend.Structure; +using OpenLyricsClient.Backend.Structure.Enum; +using OpenLyricsClient.Backend.Utils; + +namespace OpenLyricsClient.Backend.Settings.Sections.Romanization; + +public class RomanizationSection : ISettingSection +{ + private FileInfo _file; + private JObject _data; + + public RomanizationSection(string filePath) + { + this._file = new FileInfo(filePath); + } + + public async Task WriteToDisk() + { + await File.WriteAllTextAsync(this._file.FullName, this._data.ToString()); + } + + public async Task ReadFromDisk() + { + if (!this._file.Exists) + { + this._data = Defaults(); + await WriteToDisk(); + return; + } + + await using FileStream stream = this._file.OpenRead(); + using StreamReader reader = new StreamReader(stream); + + this._data = JObject.Parse(reader.ReadToEnd()); + + await stream.FlushAsync(); + + stream.Close(); + reader.Close(); + } + + public async Task AddRomanization(RomanizeSelection selection) + { + List tokens = GetValue>("Selections"); + tokens.Add(selection); + + await SetValue("Selections", tokens); + } + + public async Task RemoveRomanization(RomanizeSelection selection) + { + List tokens = GetValue>("Selections"); + tokens.Remove(selection); + + await SetValue("Selections", tokens); + } + + public bool ContainsdRomanization(RomanizeSelection selection) + { + List tokens = GetValue>("Selections"); + return tokens.Contains(selection); + } + + public T GetValue(string field) + { + return (T)this._data[field].ToObject(); + } + + public async Task SetValue(string field, T value) + { + this._data[field] = JToken.FromObject(value); + await WriteToDisk(); + } + + public JObject Defaults() + { + Structure structure = new Structure + { + Selections = new List( + new RomanizeSelection[] + { + RomanizeSelection.RUSSIA_TO_LATIN, + RomanizeSelection.KOREAN_TO_ROMANJI, + RomanizeSelection.JAPANESE_TO_ROMANJI + }) + }; + + return new JsonDeserializer().Serialize(structure); + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Romanization/Structure.cs b/OpenLyricsClient/Backend/Settings/Sections/Romanization/Structure.cs new file mode 100644 index 0000000..306e27f --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Romanization/Structure.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; +using OpenLyricsClient.Backend.Romanization; + +namespace OpenLyricsClient.Backend.Settings.Sections.Romanization; + +public class Structure +{ + public List Selections { get; set; } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Tokens/Structure.cs b/OpenLyricsClient/Backend/Settings/Sections/Tokens/Structure.cs new file mode 100644 index 0000000..708ef09 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Tokens/Structure.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; +using OpenLyricsClient.Backend.Structure; + +namespace OpenLyricsClient.Backend.Settings.Sections.Tokens; + +public class Structure +{ + public List Tokens { get; set; } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs b/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs new file mode 100644 index 0000000..914a4bd --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using DevBase.Api.Serializer; +using DevBase.Generics; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OpenLyricsClient.Backend.Structure; +using OpenLyricsClient.Backend.Structure.Enum; +using OpenLyricsClient.Backend.Utils; + +namespace OpenLyricsClient.Backend.Settings.Sections.Tokens; + +public class TokenSection : ISettingSection +{ + private FileInfo _file; + private JObject _data; + + public TokenSection(string filePath) + { + this._file = new FileInfo(filePath); + } + + public async Task WriteToDisk() + { + await File.WriteAllTextAsync(this._file.FullName, this._data.ToString()); + } + + public async Task ReadFromDisk() + { + if (!this._file.Exists) + { + this._data = Defaults(); + await WriteToDisk(); + return; + } + + await using FileStream stream = this._file.OpenRead(); + using StreamReader reader = new StreamReader(stream); + + this._data = JObject.Parse(reader.ReadToEnd()); + + await stream.FlushAsync(); + + stream.Close(); + reader.Close(); + } + + public async Task AddToken(MusixMatchToken token) + { + List tokens = GetValue>("Tokens"); + tokens.Add(token); + + await SetValue("Tokens", tokens); + } + + public async Task RemoveToken(MusixMatchToken token) + { + List tokens = GetValue>("Tokens"); + tokens.Remove(token); + + await SetValue("Tokens", tokens); + } + + public T GetValue(string field) + { + return (T)this._data[field].ToObject(); + } + + public async Task SetValue(string field, T value) + { + this._data[field] = JToken.FromObject(value); + await WriteToDisk(); + } + + public JObject Defaults() + { + Structure structure = new Structure + { + Tokens = new List() + }; + + return new JsonDeserializer().Serialize(structure); + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/SettingManager.cs b/OpenLyricsClient/Backend/Settings/SettingManager.cs deleted file mode 100644 index aa74d6f..0000000 --- a/OpenLyricsClient/Backend/Settings/SettingManager.cs +++ /dev/null @@ -1,195 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Runtime.CompilerServices; -using DevBase.IO; -using Newtonsoft.Json; -using OpenLyricsClient.Backend.Collector.Lyrics; -using OpenLyricsClient.Backend.Events.EventArgs; -using OpenLyricsClient.Backend.Events.EventHandler; -using OpenLyricsClient.Backend.Romanization; -using OpenLyricsClient.Backend.Structure; -using OpenLyricsClient.Backend.Structure.Enum; -using OpenLyricsClient.Backend.Structure.Other; -using SharpDX.Text; -using SpotifyAPI.Web; -using SimpleArtist = OpenLyricsClient.Backend.Structure.Other.SimpleArtist; -using SimpleTrack = OpenLyricsClient.Backend.Structure.Other.SimpleTrack; - -namespace OpenLyricsClient.Backend.Settings -{ - public class SettingManager - { - private Settings _settings; - private AFileObject _settingsFilePath; - private string _workingDirectory; - - private const string SETTING_FILE_NAME = "settings.json"; - - //public event SettingsChangedEventHandler SettingsChanged; - - public SettingManager(string workingFolder) - { - this._workingDirectory = workingFolder; - - this._settingsFilePath = new AFileObject(new FileInfo(string.Format("{0}{1}", - this._workingDirectory, SETTING_FILE_NAME))); - - Setup(); - } - - public void Setup() - { - if (!Directory.Exists(this._workingDirectory)) - Directory.CreateDirectory(this._workingDirectory); - - if (this._settingsFilePath.FileInfo.Exists) - { - Settings settings = ReadSettings(); - - if (settings != null) - { - this._settings = settings; - } - else - { - this._settings = GenerateAndWriteSettings(); - } - } - else - { - this._settings = GenerateAndWriteSettings(); - } - } - - private Settings GenerateAndWriteSettings() - { - Settings settings = new Settings(); - - settings.TidalAccess = (TidalAccess)DefaultSetting(EnumSetting.TIDAL); - settings.SpotifyAccess = (SpotifyAccess)DefaultSetting(EnumSetting.SPOTIFY); - settings.RomanizeSelection = (List)DefaultSetting(EnumSetting.ROMANIZATION); - settings.DisplayPreferences = (LyricsDisplayPreferences)DefaultSetting(EnumSetting.DISPLAY_PREFERENCES); - settings.MusixMatchToken = (List)DefaultSetting(EnumSetting.MUSIXMATCH_TOKENS); - - WriteSettings(settings); - - return settings; - } - - public object DefaultSetting(EnumSetting enumSetting) - { - switch (enumSetting) - { - case EnumSetting.SPOTIFY: - { - SpotifyAccess spotifyAccess = new SpotifyAccess(); - - spotifyAccess.AccessToken = "null"; - spotifyAccess.IsSpotifyConnected = false; - spotifyAccess.RefreshToken = string.Empty; - spotifyAccess.SpotifyExpireTime = (int)DateTimeOffset.Now.ToUnixTimeMilliseconds(); - - PrivateUser privateUser = new PrivateUser(); - privateUser.Country = ""; - privateUser.Email = ""; - privateUser.Followers = null; - privateUser.Href = ""; - privateUser.Id = ""; - privateUser.Images = new List(); - privateUser.Product = ""; - privateUser.Type = ""; - privateUser.Uri = ""; - privateUser.DisplayName = ""; - - SpotifyStatistics statistics = new SpotifyStatistics(); - statistics.TopArtists = Array.Empty(); - statistics.TopTracks = Array.Empty(); - - spotifyAccess.UserData = privateUser; - spotifyAccess.Statistics = statistics; - - return spotifyAccess; - } - case EnumSetting.TIDAL: - { - TidalAccess tidalAccess = new TidalAccess(); - tidalAccess.IsTidalConnected = false; - tidalAccess.AccessToken = "null"; - tidalAccess.RefreshToken = "null"; - tidalAccess.ExpirationDate = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - tidalAccess.UserID = 0; - - return tidalAccess; - } - case EnumSetting.ROMANIZATION: - { - List romanizeSelections = new List(); - romanizeSelections.Add(RomanizeSelection.KOREAN_TO_ROMANJI); - romanizeSelections.Add(RomanizeSelection.JAPANESE_TO_ROMANJI); - romanizeSelections.Add(RomanizeSelection.RUSSIA_TO_LATIN); - return romanizeSelections; - } - case EnumSetting.MUSIXMATCH_TOKENS: - { - return new List(); - } - case EnumSetting.DISPLAY_PREFERENCES: - { - LyricsDisplayPreferences lyricsDisplayPreferences = new LyricsDisplayPreferences(); - lyricsDisplayPreferences.DisplayMode = EnumLyricsDisplayMode.KARAOKE; - lyricsDisplayPreferences.ArtworkBackground = false; - lyricsDisplayPreferences.LyricsBlur = false; - return lyricsDisplayPreferences; - } - } - - return null; - } - - public void WriteSettings(Settings settings, bool fireEvent = true) - { - string json = JsonConvert.SerializeObject(settings, Formatting.Indented); - - File.WriteAllText(this._settingsFilePath.FileInfo.FullName, json); - } - - public void WriteSettings(bool fireEvent = true) - { - if (!Core.IsLoaded()) - return; - - WriteSettings(this._settings, fireEvent); - } - - private Settings ReadSettings() - { - if (!this._settingsFilePath.FileInfo.Exists) - return null; - - Stream file = File.Open(this._settingsFilePath.FileInfo.FullName, FileMode.Open, FileAccess.Read); - StreamReader reader = new StreamReader(file); - - string content = reader.ReadToEnd(); - - reader.Close(); - file.Close(); - - return JsonConvert.DeserializeObject(content); - } - - /*public Settings Settings - { - get => _settings; - set => _settings = value; - }*/ - - public string WorkingDirectory - { - get => _workingDirectory; - } - - - } -} diff --git a/OpenLyricsClient/Backend/Settings/Settings.cs b/OpenLyricsClient/Backend/Settings/Settings.cs deleted file mode 100644 index 807450c..0000000 --- a/OpenLyricsClient/Backend/Settings/Settings.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using OpenLyricsClient.Backend.Romanization; -using OpenLyricsClient.Backend.Structure; -using OpenLyricsClient.Backend.Structure.Enum; - -namespace OpenLyricsClient.Backend.Settings -{ - public class Settings - { - public SpotifyAccess SpotifyAccess { get; set; } - public TidalAccess TidalAccess { get; set; } - public List RomanizeSelection { get; set; } - public LyricsDisplayPreferences DisplayPreferences { get; set; } - public List MusixMatchToken { get; set; } - - } -} diff --git a/OpenLyricsClient/Backend/Settings/SettingsHandler.cs b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs index b59e18b..e0ccd08 100644 --- a/OpenLyricsClient/Backend/Settings/SettingsHandler.cs +++ b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs @@ -6,6 +6,8 @@ using OpenLyricsClient.Backend.Settings.Sections; using OpenLyricsClient.Backend.Settings.Sections.Connection.Spotify; using OpenLyricsClient.Backend.Settings.Sections.Lyrics; +using OpenLyricsClient.Backend.Settings.Sections.Romanization; +using OpenLyricsClient.Backend.Settings.Sections.Tokens; using OpenLyricsClient.Backend.Structure.Enum; namespace OpenLyricsClient.Backend.Settings; @@ -25,6 +27,12 @@ public SettingsHandler(string workingDirectory) this._sections.Add(new SpotifySection(string.Format("{0}{1}", workingDirectory, "Spotify Access.json"))); + + this._sections.Add(new TokenSection(string.Format("{0}{1}", + workingDirectory, "Tokens.json"))); + + this._sections.Add(new RomanizationSection(string.Format("{0}{1}", + workingDirectory, "Romanization.json"))); Task.Factory.StartNew(Initialize).GetAwaiter().GetResult(); } diff --git a/OpenLyricsClient/Frontend/Models/Pages/Settings/SettingsRomanizationViewModel.cs b/OpenLyricsClient/Frontend/Models/Pages/Settings/SettingsRomanizationViewModel.cs index 8614f74..e1a2214 100644 --- a/OpenLyricsClient/Frontend/Models/Pages/Settings/SettingsRomanizationViewModel.cs +++ b/OpenLyricsClient/Frontend/Models/Pages/Settings/SettingsRomanizationViewModel.cs @@ -3,8 +3,10 @@ using System.Linq; using System.Reactive; using System.Runtime.CompilerServices; +using System.Threading.Tasks; using OpenLyricsClient.Backend; using OpenLyricsClient.Backend.Romanization; +using OpenLyricsClient.Backend.Settings.Sections.Romanization; using ReactiveUI; namespace OpenLyricsClient.Frontend.Models.Pages.Settings; @@ -19,41 +21,41 @@ public class SettingsRomanizationViewModel : ViewModelBase, INotifyPropertyChang public SettingsRomanizationViewModel() { - JapaneseCommand = ReactiveCommand.Create(Japanese); - KoreanCommand = ReactiveCommand.Create(Korean); - RussianCommand = ReactiveCommand.Create(Russian); + JapaneseCommand = ReactiveCommand.CreateFromTask(Japanese); + KoreanCommand = ReactiveCommand.CreateFromTask(Korean); + RussianCommand = ReactiveCommand.CreateFromTask(Russian); } - private void CheckOrUncheckAndWrite(RomanizeSelection selection) + private async Task CheckOrUncheckAndWrite(RomanizeSelection selection) { - /*if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Contains(selection)) + if (Core.INSTANCE.SettingsHandler.Settings()!.ContainsdRomanization(selection)) { - Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Remove(selection); + await Core.INSTANCE.SettingsHandler.Settings()?.RemoveRomanization(selection); } else { - Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Add(selection); + await Core.INSTANCE.SettingsHandler.Settings()?.AddRomanization(selection); } - - Core.INSTANCE.SettingManager.WriteSettings();*/ + + await Core.INSTANCE.SettingsHandler?.TriggerEvent(typeof(RomanizationSection), "Selections"); } private bool IsAvailable(RomanizeSelection selection) => - true; + Core.INSTANCE.SettingsHandler.Settings()!.ContainsdRomanization(selection); - private void Japanese() + private async Task Japanese() { - CheckOrUncheckAndWrite(RomanizeSelection.JAPANESE_TO_ROMANJI); + await CheckOrUncheckAndWrite(RomanizeSelection.JAPANESE_TO_ROMANJI); } - private void Korean() + private async Task Korean() { - CheckOrUncheckAndWrite(RomanizeSelection.KOREAN_TO_ROMANJI); + await CheckOrUncheckAndWrite(RomanizeSelection.KOREAN_TO_ROMANJI); } - private void Russian() + private async Task Russian() { - CheckOrUncheckAndWrite(RomanizeSelection.RUSSIA_TO_LATIN); + await CheckOrUncheckAndWrite(RomanizeSelection.RUSSIA_TO_LATIN); } public bool IsJapaneseEnabled