hub manager, start page manager, adding to settings
This commit is contained in:
parent
95e1c66adf
commit
e986f32058
10
Selector.MAUI/Constants.cs
Normal file
10
Selector.MAUI/Constants.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
namespace Selector.MAUI
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const string JwtPrefKey = "last_jwt_key";
|
||||
public const string StartPagePrefKey = "start_page";
|
||||
}
|
||||
}
|
||||
|
21
Selector.MAUI/Extensions/ServiceExtensions.cs
Normal file
21
Selector.MAUI/Extensions/ServiceExtensions.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Selector.MAUI.Services;
|
||||
using Selector.SignalR;
|
||||
|
||||
namespace Selector.MAUI.Extensions;
|
||||
|
||||
public static class ServiceExtensions
|
||||
{
|
||||
public static IServiceCollection AddHubs(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<NowHubClient>()
|
||||
.AddSingleton<NowHubCache>();
|
||||
|
||||
services.AddSingleton<PastHubClient>();
|
||||
|
||||
services.AddSingleton<HubManager>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Selector.MAUI.Data;
|
||||
using Selector.MAUI.Services;
|
||||
using Selector.SignalR;
|
||||
using Selector.MAUI.Extensions;
|
||||
|
||||
namespace Selector.MAUI;
|
||||
|
||||
@ -31,12 +32,10 @@ public static class MauiProgram
|
||||
builder.Services.AddHttpClient()
|
||||
.AddTransient<ISelectorNetClient, SelectorNetClient>();
|
||||
|
||||
builder.Services.AddSingleton<SessionManager>();
|
||||
builder.Services.AddSingleton<SessionManager>()
|
||||
.AddTransient<StartPageManager>();
|
||||
|
||||
builder.Services.AddSingleton<NowHubClient>()
|
||||
.AddSingleton<NowHubCache>();
|
||||
|
||||
builder.Services.AddSingleton<PastHubClient>();
|
||||
builder.Services.AddHubs();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
@page "/app"
|
||||
@using Selector.SignalR
|
||||
@inject NowHubClient nowClient
|
||||
@inject NowHubCache nowCache
|
||||
@inject PastHubClient pastClient
|
||||
@inject HubManager hubManager
|
||||
@inject ILogger<Index> logger
|
||||
|
||||
<h1>run that</h1>
|
||||
@ -11,16 +9,6 @@
|
||||
|
||||
protected async override Task OnInitializedAsync()
|
||||
{
|
||||
|
||||
if (nowClient.State == Microsoft.AspNetCore.SignalR.Client.HubConnectionState.Disconnected)
|
||||
{
|
||||
logger.LogInformation("Starting now hub connection");
|
||||
|
||||
await nowClient.StartAsync();
|
||||
await pastClient.StartAsync();
|
||||
nowCache.BindClient();
|
||||
await nowClient.OnConnected();
|
||||
}
|
||||
|
||||
await hubManager.EnsureConnected();
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
@inject ILogger<Login> logger;
|
||||
@inject NavigationManager NavManager;
|
||||
@inject SessionManager sessionManager;
|
||||
@inject HubManager hubManager;
|
||||
@inject StartPageManager startManager;
|
||||
|
||||
<h1>Loading...</h1>
|
||||
|
||||
@ -15,8 +17,11 @@
|
||||
|
||||
if (sessionManager.IsLoggedIn)
|
||||
{
|
||||
await hubManager.EnsureConnected();
|
||||
|
||||
logger.LogInformation("User logged in, navigating to main app");
|
||||
NavManager.NavigateTo("/app");
|
||||
|
||||
startManager.NavigateToStartPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,7 +1,5 @@
|
||||
@page "/past"
|
||||
@using Selector.SignalR;
|
||||
@using System.Linq;
|
||||
@implements IDisposable
|
||||
|
||||
<h1>Past</h1>
|
||||
|
||||
|
@ -1,8 +1,23 @@
|
||||
@page "/settings"
|
||||
|
||||
<h1>Settings</h1>
|
||||
<div class="form-container">
|
||||
<h1>Settings</h1>
|
||||
|
||||
<button @onclick="@SignOut">Sign Out</button>
|
||||
<div class="row">
|
||||
<RadzenCard>
|
||||
<RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">Start Page</RadzenText>
|
||||
<RadzenDropDown AllowClear="true" TValue="string" Class="w-100"
|
||||
Data=@startManager.StartPages
|
||||
@bind-Value="currentStartPage"
|
||||
Change=@OnStartPageChange />
|
||||
</RadzenCard>
|
||||
</div>
|
||||
<div class="row">
|
||||
<RadzenButton Click=@(_ => SignOut()) Text="Sign Out" ButtonStyle="ButtonStyle.Danger" />
|
||||
</div>
|
||||
|
||||
<SignatureImage/>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
@ -10,10 +25,19 @@
|
||||
private SessionManager sessionManager { get; set; }
|
||||
[Inject]
|
||||
private NavigationManager navigationManager { get; set; }
|
||||
[Inject]
|
||||
private StartPageManager startManager { get; set; }
|
||||
|
||||
private string currentStartPage { get; set; }
|
||||
|
||||
protected async override Task OnInitializedAsync()
|
||||
{
|
||||
currentStartPage = startManager.GetStartPage();
|
||||
}
|
||||
|
||||
private void OnStartPageChange(object value)
|
||||
{
|
||||
startManager.SetStartPage((string) value);
|
||||
}
|
||||
|
||||
private void SignOut()
|
||||
|
20
Selector.MAUI/Pages/Settings.razor.css
Normal file
20
Selector.MAUI/Pages/Settings.razor.css
Normal file
@ -0,0 +1,20 @@
|
||||
body {
|
||||
}
|
||||
|
||||
.form-container {
|
||||
max-width: 300px;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.row {
|
||||
padding-top: 5px;
|
||||
padding-bottom: 5px;
|
||||
}
|
||||
|
||||
::deep h3 {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
h1 {
|
||||
padding-bottom: 20px;
|
||||
}
|
@ -98,10 +98,12 @@
|
||||
<None Remove="Microsoft.AspNetCore.SignalR.Client" />
|
||||
<None Remove="Microsoft.AspNetCore.Components.Forms" />
|
||||
<None Remove="Radzen.Blazor" />
|
||||
<None Remove="Extensions\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\" />
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Selector\Selector.csproj" />
|
||||
|
43
Selector.MAUI/Services/HubManager.cs
Normal file
43
Selector.MAUI/Services/HubManager.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Selector.SignalR;
|
||||
namespace Selector.MAUI.Services;
|
||||
|
||||
public class HubManager
|
||||
{
|
||||
private readonly NowHubClient nowClient;
|
||||
private readonly NowHubCache nowCache;
|
||||
private readonly PastHubClient pastClient;
|
||||
private readonly ILogger<HubManager> logger;
|
||||
|
||||
public HubManager(NowHubClient nowClient, NowHubCache nowCache, PastHubClient pastClient, ILogger<HubManager> logger)
|
||||
{
|
||||
this.nowClient = nowClient;
|
||||
this.nowCache = nowCache;
|
||||
this.pastClient = pastClient;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task EnsureConnected()
|
||||
{
|
||||
if (nowClient.State == HubConnectionState.Disconnected)
|
||||
{
|
||||
logger.LogInformation("Starting now hub connection");
|
||||
|
||||
await nowClient.StartAsync();
|
||||
nowCache.BindClient();
|
||||
await nowClient.OnConnected();
|
||||
}
|
||||
|
||||
if (pastClient.State == HubConnectionState.Disconnected)
|
||||
{
|
||||
logger.LogInformation("Starting past hub connection");
|
||||
|
||||
await pastClient.StartAsync();
|
||||
await pastClient.OnConnected();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,6 @@ namespace Selector.MAUI.Services;
|
||||
|
||||
public class SessionManager
|
||||
{
|
||||
private const string jwt_keychain_key = "last_jwt_key";
|
||||
|
||||
private string lastStoredKey;
|
||||
private DateTime lastRefresh;
|
||||
private readonly ISelectorNetClient _selectorNetClient;
|
||||
@ -23,7 +21,7 @@ public class SessionManager
|
||||
public async Task LoadUserFromDisk()
|
||||
{
|
||||
//var lastToken = await SecureStorage.Default.GetAsync(jwt_keychain_key);
|
||||
var lastToken = Preferences.Default.Get(jwt_keychain_key, string.Empty);
|
||||
var lastToken = Preferences.Default.Get(Constants.JwtPrefKey, string.Empty);
|
||||
|
||||
lastStoredKey = lastToken;
|
||||
|
||||
@ -62,7 +60,7 @@ public class SessionManager
|
||||
|
||||
//await SecureStorage.Default.SetAsync(jwt_keychain_key, lastStoredKey);
|
||||
// I know, but I can't get secure storage to work
|
||||
Preferences.Default.Set(jwt_keychain_key, lastStoredKey);
|
||||
Preferences.Default.Set(Constants.JwtPrefKey, lastStoredKey);
|
||||
|
||||
break;
|
||||
case SelectorNetClient.TokenResponseStatus.Malformed:
|
||||
|
62
Selector.MAUI/Services/StartPageManager.cs
Normal file
62
Selector.MAUI/Services/StartPageManager.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Selector.MAUI.Services;
|
||||
|
||||
public class StartPageManager
|
||||
{
|
||||
private readonly NavigationManager navManager;
|
||||
|
||||
public string[] StartPages { get; } = new[]
|
||||
{
|
||||
Home, Now, Past
|
||||
};
|
||||
|
||||
public const string Home = "Home";
|
||||
public const string Now = "Now";
|
||||
public const string Past = "Past";
|
||||
|
||||
public StartPageManager(NavigationManager navManager)
|
||||
{
|
||||
this.navManager = navManager;
|
||||
}
|
||||
|
||||
public string GetStartPage()
|
||||
{
|
||||
var savedStartPage = Preferences.Default.Get(Constants.StartPagePrefKey, string.Empty);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(savedStartPage))
|
||||
{
|
||||
return savedStartPage;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Home;
|
||||
}
|
||||
}
|
||||
|
||||
public void NavigateToStartPage()
|
||||
{
|
||||
var startPage = GetStartPage();
|
||||
|
||||
switch (startPage)
|
||||
{
|
||||
case Now:
|
||||
navManager.NavigateTo("/now");
|
||||
break;
|
||||
case Past:
|
||||
navManager.NavigateTo("/past");
|
||||
break;
|
||||
case Home:
|
||||
default:
|
||||
navManager.NavigateTo("/app");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetStartPage(string value)
|
||||
{
|
||||
Preferences.Default.Set(Constants.StartPagePrefKey, value);
|
||||
}
|
||||
}
|
||||
|
16
Selector.MAUI/Shared/SignatureImage.razor
Normal file
16
Selector.MAUI/Shared/SignatureImage.razor
Normal file
@ -0,0 +1,16 @@
|
||||
<div style="text-align: center">
|
||||
<a href="https://sarsoo.xyz/about/" style="display: inline-block">
|
||||
<img src="/andy.png"
|
||||
alt="AP"
|
||||
width="120px"
|
||||
style="display: block;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
padding: 8px">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
<link href="css/link.css" rel="stylesheet" />
|
||||
<link href="css/input.css" rel="stylesheet" />
|
||||
<link href="Selector.MAUI.styles.css" rel="stylesheet" />
|
||||
<link rel="stylesheet" href="_content/Radzen.Blazor/css/material-base.css">
|
||||
<link rel="stylesheet" href="_content/Radzen.Blazor/css/dark-base.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -13,6 +13,9 @@ public class NowHubCache
|
||||
public List<ICard> LastCards { get; private set; } = new();
|
||||
private readonly object updateLock = new();
|
||||
|
||||
private readonly object bindingLock = new();
|
||||
private bool isBound = false;
|
||||
|
||||
public PlayCount LastPlayCount { get; private set; }
|
||||
public CurrentlyPlayingDTO LastPlaying { get; private set; }
|
||||
|
||||
@ -28,6 +31,10 @@ public class NowHubCache
|
||||
}
|
||||
|
||||
public void BindClient()
|
||||
{
|
||||
lock(bindingLock)
|
||||
{
|
||||
if(!isBound)
|
||||
{
|
||||
_connection.OnNewAudioFeature(af =>
|
||||
{
|
||||
@ -41,7 +48,7 @@ public class NowHubCache
|
||||
|
||||
_connection.OnNewCard(c =>
|
||||
{
|
||||
lock(updateLock)
|
||||
lock (updateLock)
|
||||
{
|
||||
logger.LogInformation("New card received: {0}", c);
|
||||
LastCards.Add(c);
|
||||
@ -92,11 +99,16 @@ public class NowHubCache
|
||||
LastPlaying.Track.Album?.Artists.FirstOrDefault()?.Name
|
||||
);
|
||||
}
|
||||
}catch(Exception e)
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Error while handling new now playing");
|
||||
}
|
||||
});
|
||||
|
||||
isBound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user