Skip to content

Commit

Permalink
Start of the new settings implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed Apr 24, 2023
1 parent d76ef5a commit fb6a230
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 10 deletions.
4 changes: 2 additions & 2 deletions OpenLyricsClient/Backend/Cache/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void ReadCache()
string id = ifo.FileInfo.Name.Replace(CACHE_EXTENSION, string.Empty);

JsonCacheData jsonLyricData =
new JsonDeserializer<JsonCacheData>().Deserialize(FileUtils.ReadFileString(ifo.FileInfo));
new JsonDeserializer().Deserialize<JsonCacheData>(FileUtils.ReadFileString(ifo.FileInfo));

if (this._cache.Length + 1 > this._maxCapacity)
continue;
Expand Down Expand Up @@ -224,7 +224,7 @@ private CacheData TryGetDataFromDisk(SongRequestObject songRequestObject)
return null;

JsonCacheData jsonLyricData =
new JsonDeserializer<JsonCacheData>().Deserialize(data);
new JsonDeserializer().Deserialize<JsonCacheData>(data);

GCHandle.Alloc(data).Free();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private async Task<NetEaseLyricResponse> GetLyricsFromEndpoint(int songId)

if (responseData.StatusCode == HttpStatusCode.OK)
{
return new JsonDeserializer<NetEaseLyricResponse>().Deserialize(responseData.GetContentAsString());
return new JsonDeserializer().Deserialize<NetEaseLyricResponse>(responseData.GetContentAsString());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private async Task<NetEaseV2LyricResponse> GetLyricsFromEndpoint(int songId)

if (responseData.StatusCode == HttpStatusCode.OK)
{
return new JsonDeserializer<NetEaseV2LyricResponse>().Deserialize(responseData.GetContentAsString());
return new JsonDeserializer().Deserialize<NetEaseV2LyricResponse>(responseData.GetContentAsString());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<TextylLyricReponse[]> FetchLyrics(SongRequestObject songReques
return null;

TextylLyricReponse[] reponse =
new JsonDeserializer<TextylLyricReponse[]>().Deserialize(content);
new JsonDeserializer().Deserialize<TextylLyricReponse[]>(content);

if (reponse != null)
return reponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private async Task<NetEaseSearchResponse> SearchTrack(SongRequestObject songRequ

if (responseData.StatusCode == HttpStatusCode.OK)
{
return new JsonDeserializer<NetEaseSearchResponse>().Deserialize(responseData.GetContentAsString());
return new JsonDeserializer().Deserialize<NetEaseSearchResponse>(responseData.GetContentAsString());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private async Task<NetEaseV2SearchResponse> SearchTrack(SongRequestObject songRe

if (responseData.StatusCode == HttpStatusCode.OK)
{
return new JsonDeserializer<NetEaseV2SearchResponse>().Deserialize(responseData.GetContentAsString());
return new JsonDeserializer().Deserialize<NetEaseV2SearchResponse>(responseData.GetContentAsString());
}

return null;
Expand Down
8 changes: 7 additions & 1 deletion OpenLyricsClient/Backend/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using OpenLyricsClient.Backend.Handler.Song;
using OpenLyricsClient.Backend.Helper;
using OpenLyricsClient.Backend.Settings;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Backend.Structure.Enum;
using TaskRegister = OpenLyricsClient.Backend.Overwrite.TaskRegister;

Expand All @@ -33,6 +34,7 @@ class Core
private Debugger<Core> _debugger;

private SettingManager _settingManager;
private SettingsHandler _settingsHandler;

private ServiceHandler _serviceHandler;
private SongHandler _songHandler;
Expand Down Expand Up @@ -83,7 +85,11 @@ public Core()

this._settingManager = new SettingManager(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) +
string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar));


this._settingsHandler = new SettingsHandler(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) +
string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar));

this._cacheManager = new CacheManager(10, TimeSpan.FromMinutes(5).Milliseconds);

this._tokenCollector = new TokenCollector();
Expand Down
13 changes: 13 additions & 0 deletions OpenLyricsClient/Backend/Settings/ISettingSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace OpenLyricsClient.Backend.Settings;

public interface ISettingSection
{
Task WriteToDisk();
Task ReadFromDisk();
T GetValue<T>(string field);
Task SetValue<T>(string field, T value);
JObject Defaults();
}
70 changes: 70 additions & 0 deletions OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.IO;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using DevBase.Api.Serializer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenLyricsClient.Backend.Structure.Enum;
using OpenLyricsClient.Backend.Utils;

namespace OpenLyricsClient.Backend.Settings.Sections.Lyrics;

public class LyricsSection : ISettingSection
{
private FileInfo _file;
private JObject _data;

public LyricsSection(string filePath)
{
this._file = new FileInfo(filePath);
}

public async Task WriteToDisk()
{
await using FileStream stream = this._file.OpenWrite();
await using StreamWriter writer = new StreamWriter(stream);

await writer.WriteAsync(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 T GetValue<T>(string field)
{
return this._data[field].Value<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
{
Selection = EnumLyricsDisplayMode.KARAOKE
};

return new JsonDeserializer().Serialize(structure);
}
}
10 changes: 10 additions & 0 deletions OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;
using OpenLyricsClient.Backend.Structure.Enum;

namespace OpenLyricsClient.Backend.Settings.Sections.Lyrics;

public class Structure
{
[JsonProperty("Selection")]
public EnumLyricsDisplayMode Selection { get; set; }
}
32 changes: 32 additions & 0 deletions OpenLyricsClient/Backend/Settings/SettingsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;
using DevBase.Generics;
using OpenLyricsClient.Backend.Settings.Sections;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Backend.Structure.Enum;

namespace OpenLyricsClient.Backend.Settings;

public class SettingsHandler
{
private AList<ISettingSection> _sections;

public SettingsHandler(string workingDirectory)
{
this._sections = new AList<ISettingSection>();

this._sections.Add(new LyricsSection(string.Format("{0}{1}",
workingDirectory, "lyrics-preferences.json")));

Task.Factory.StartNew(Initialize).GetAwaiter().GetResult();
}

public async Task Initialize()
{
this._sections.ForEach(async t=> await t.ReadFromDisk());
}

public T? Settings<T>() where T : ISettingSection
{
return (T)this._sections.GetAsList().Find(s => s is T);
}
}
13 changes: 11 additions & 2 deletions OpenLyricsClient/Backend/Utils/JsonDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using DevBase.Generics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace OpenLyricsClient.Backend.Utils
{
public class JsonDeserializer<T>
public class JsonDeserializer
{
private JsonSerializerSettings _serializerSettings;
private AList<string> _errorList;
Expand All @@ -23,11 +24,19 @@ public JsonDeserializer()
};
}

public T Deserialize(string input)
public T Deserialize<T>(string input)
{
return JsonConvert.DeserializeObject<T>(input, this._serializerSettings);
}

public JObject Serialize(object structure)
{
string serialized = JsonConvert.SerializeObject(structure, this._serializerSettings);
return JObject.Parse(serialized);
}

public JObject ToJObject<T>(T data) => JObject.FromObject(data);

public AList<string> ErrorList
{
get => _errorList;
Expand Down

0 comments on commit fb6a230

Please sign in to comment.