-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start of the new settings implementation
- Loading branch information
AlexanderDotH
committed
Apr 24, 2023
1 parent
d76ef5a
commit fb6a230
Showing
12 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters