Skip to content

Commit

Permalink
Implemented LicenseHandler.cs
Browse files Browse the repository at this point in the history
- Needs proper merging into the ecosystem
  • Loading branch information
AlexanderDotH committed Apr 27, 2023
1 parent 37402fd commit 783a991
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions OpenLyricsClient/Backend/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OpenLyricsClient.Backend.Debugger;
using OpenLyricsClient.Backend.Events.EventHandler;
using OpenLyricsClient.Backend.Handler.Artwork;
using OpenLyricsClient.Backend.Handler.License;
using OpenLyricsClient.Backend.Handler.Lyrics;
using OpenLyricsClient.Backend.Handler.Services;
using OpenLyricsClient.Backend.Handler.Song;
Expand Down Expand Up @@ -37,6 +38,7 @@ class Core
private SongHandler _songHandler;
private LyricHandler _lyricHandler;
private ArtworkHandler _artworkHandler;
private LicenseHandler _licenseHandler;

private CacheManager _cacheManager;

Expand Down Expand Up @@ -88,6 +90,8 @@ public Core()

this._settingsHandler = new SettingsHandler(workingDirectory);

this._licenseHandler = new LicenseHandler();

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

this._tokenCollector = new TokenCollector();
Expand Down
67 changes: 67 additions & 0 deletions OpenLyricsClient/Backend/Handler/License/LicenseHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Threading.Tasks;
using DevBase.Api.Apis.OpenLyricsClient.Structure.Enum;
using DevBase.Api.Apis.OpenLyricsClient.Structure.Json;
using DevBase.Async.Task;
using OpenLyricsClient.Backend.Structure.Enum;

namespace OpenLyricsClient.Backend.Handler.License;

public class LicenseHandler : IHandler
{
private TaskSuspensionToken _refreshSuspensionToken;

private bool _disposed;

private JsonOpenLyricsClientSubscriptionModel _license;
private DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient _openLyricsClientApi;

public LicenseHandler()
{
this._disposed = false;

this._openLyricsClientApi = new DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient(Core.INSTANCE.Sealing.ServerPublicKey);

JsonOpenLyricsClientSubscriptionModel model = new JsonOpenLyricsClientSubscriptionModel
{
Model = EnumSubscriptions.OPENLYRICSCLIENT_STANDARD
};
this._license = model;

Core.INSTANCE.TaskRegister.Register(
out _refreshSuspensionToken,
new Task(async () => await RefreshLicense(), Core.INSTANCE.CancellationTokenSource.Token, TaskCreationOptions.LongRunning),
EnumRegisterTypes.REFRESH_LICENSE);
}

public async Task RefreshLicense()
{
while (!this._disposed)
{
string userID = Core.INSTANCE.SettingsHandler.Settings<AccountSection>()!.GetValue<string>("UserID");
string userSecret = Core.INSTANCE.SettingsHandler.Settings<AccountSection>()!.GetValue<string>("UserSecret");

JsonOpenLyricsClientSubscription subscription = new JsonOpenLyricsClientSubscription
{
UserID = userID,
UserSecret = userSecret
};

JsonOpenLyricsClientSubscriptionModel model = await this._openLyricsClientApi.CheckSubscription(subscription);
this._license = model;

await Task.Delay((int)TimeSpan.FromMinutes(1).TotalMilliseconds);
}
}

public void Dispose()
{
this._disposed = true;
Core.INSTANCE.TaskRegister.Kill(EnumRegisterTypes.REFRESH_LICENSE);
}

public JsonOpenLyricsClientSubscriptionModel License
{
get => _license;
}
}
3 changes: 2 additions & 1 deletion OpenLyricsClient/Backend/Structure/Enum/EnumRegisterTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum EnumRegisterTypes
APPLY_ARTWORK_TO_SONG,
COLLECT_TOKENS,
SYNC_SCROLLER,
GLOBAL_TICK
GLOBAL_TICK,
REFRESH_LICENSE
}
}

0 comments on commit 783a991

Please sign in to comment.