Skip to content

Commit

Permalink
Implemented the new setting-system
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed Apr 25, 2023
1 parent f885a7c commit 08b779e
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 285 deletions.
4 changes: 2 additions & 2 deletions OpenLyricsClient/Backend/Cache/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class CacheManager

private Debugger<CacheManager> _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<CacheManager>(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public async Task<LyricData> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,12 +27,12 @@ public async Task<SongResponseObject> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,16 +33,15 @@ public MusixmatchTokenCollector()

public async Task CollectToken()
{
bool settingsChanged = false;

return;
List<MusixMatchToken> tokens = Core.INSTANCE.SettingsHandler.Settings<TokenSection>()
.GetValue<List<MusixMatchToken>>("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();
Expand All @@ -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<TokenSection>()!.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<TokenSection>()!.RemoveToken(token);
}
}
if (settingsChanged)
Core.INSTANCE.SettingManager.WriteSettings(false);*/
}

public MusixMatchToken GetToken()
public async Task<MusixMatchToken> GetToken()
{
/*List<MusixMatchToken> tokens = Core.INSTANCE.SettingManager.Settings.MusixMatchToken;
List<MusixMatchToken> tokens = Core.INSTANCE.SettingsHandler.Settings<TokenSection>()
.GetValue<List<MusixMatchToken>>("Tokens");

if (tokens.Count == 0)
return null;
Expand All @@ -92,10 +87,10 @@ public MusixMatchToken GetToken()

if (token.Usage <= 0)
{
Core.INSTANCE.SettingManager.Settings.MusixMatchToken.Remove(token);
}*/
await Core.INSTANCE.SettingsHandler.Settings<TokenSection>()!.RemoveToken(token);
}

return null;
return token;
}
}
}
17 changes: 5 additions & 12 deletions OpenLyricsClient/Backend/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Core

private Debugger<Core> _debugger;

private SettingManager _settingManager;
private SettingsHandler _settingsHandler;

private ServiceHandler _serviceHandler;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -155,11 +153,6 @@ protected virtual void SlowTickEvent()
slowTickEventHandler?.Invoke(this);
}

public SettingManager SettingManager
{
get => _settingManager;
}

public SettingsHandler SettingsHandler
{
get => this._settingsHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ private async Task RefreshToken()

public bool IsConnected()
{
return Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<bool>("IsSpotifyConnected")
&& Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<string>("AccessToken") != null;
return Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<bool>("IsSpotifyConnected") == true;
}

public async Task<bool> TestConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -172,7 +174,7 @@ public async Task<Structure.Song.Song> UpdateCurrentPlaybackTrack()

private SpotifyClient GetPlayerApi()
{
return new SpotifyClient(Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify").GetAccessToken());
return new SpotifyClient(this._service.GetAccessToken());
}

public void Dispose()
Expand Down
16 changes: 10 additions & 6 deletions OpenLyricsClient/Backend/Romanization/Romanization.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,10 +36,13 @@ public async Task<string> Romanize(string text)
if (!DataValidator.ValidateData(text))
return text;

//if (Core.INSTANCE.SettingManager.Settings.RomanizeSelection.Count == 0)
// return text;
List<RomanizeSelection> selections = Core.INSTANCE.SettingsHandler.Settings<RomanizationSection>()
.GetValue<List<RomanizeSelection>>("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))
{
Expand All @@ -46,7 +51,7 @@ public async Task<string> 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))
{
Expand All @@ -55,14 +60,13 @@ public async Task<string> 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;
}
Expand Down
7 changes: 0 additions & 7 deletions OpenLyricsClient/Backend/Settings/EnumSetting.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<RomanizeSelection> tokens = GetValue<List<RomanizeSelection>>("Selections");
tokens.Add(selection);

await SetValue("Selections", tokens);
}

public async Task RemoveRomanization(RomanizeSelection selection)
{
List<RomanizeSelection> tokens = GetValue<List<RomanizeSelection>>("Selections");
tokens.Remove(selection);

await SetValue("Selections", tokens);
}

public bool ContainsdRomanization(RomanizeSelection selection)
{
List<RomanizeSelection> tokens = GetValue<List<RomanizeSelection>>("Selections");
return tokens.Contains(selection);
}

public T GetValue<T>(string field)
{
return (T)this._data[field].ToObject<T>();
}

public async Task SetValue<T>(string field, T value)
{
this._data[field] = JToken.FromObject(value);
await WriteToDisk();
}

public JObject Defaults()
{
Structure structure = new Structure
{
Selections = new List<RomanizeSelection>(
new RomanizeSelection[]
{
RomanizeSelection.RUSSIA_TO_LATIN,
RomanizeSelection.KOREAN_TO_ROMANJI,
RomanizeSelection.JAPANESE_TO_ROMANJI
})
};

return new JsonDeserializer().Serialize(structure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using OpenLyricsClient.Backend.Romanization;

namespace OpenLyricsClient.Backend.Settings.Sections.Romanization;

public class Structure
{
public List<RomanizeSelection> Selections { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using OpenLyricsClient.Backend.Structure;

namespace OpenLyricsClient.Backend.Settings.Sections.Tokens;

public class Structure
{
public List<MusixMatchToken> Tokens { get; set; }
}
Loading

0 comments on commit 08b779e

Please sign in to comment.