Skip to content

Commit

Permalink
Login custom server
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Apr 23, 2024
1 parent 933159c commit 8ac0577
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 22 deletions.
11 changes: 7 additions & 4 deletions ShockOsc/Platforms/Windows/WindowsTrayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ private static void OnMainClick(object? sender, EventArgs eventArgs)
var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
var windowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
var appWindow = AppWindow.GetFromWindowId(windowId);



if (appWindow.IsVisible) appWindow.Hide();
else appWindow.Show();
appWindow.Show();

if (appWindow.Presenter is OverlappedPresenter presenter)
{
presenter.IsAlwaysOnTop = true;
presenter.IsAlwaysOnTop = false;
}
}

private static void OnQuitClick(object? sender, EventArgs eventArgs)
Expand Down
5 changes: 4 additions & 1 deletion ShockOsc/Ui/Components/Tabs/DashboardTab.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<h3>DashboardTab</h3>
@using OpenShock.ShockOsc.Services
@inject StatusHandler StatusHandler

<h3>DashboardTab</h3>

@code {

Expand Down
21 changes: 6 additions & 15 deletions ShockOsc/Ui/Pages/Authentication/Authenticate.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using OpenShock.ShockOsc.Ui.Utils
@using OpenShock.SDK.CSharp.Live
@using OpenShock.ShockOsc.Backend
@using OpenShock.ShockOsc.Config
@using Microsoft.Extensions.Logging
Expand Down Expand Up @@ -39,10 +38,7 @@
@switch (_currentState)
{
case State.Login:
<MudTextField @bind-Value="ConfigManager.Config.OpenShock.Token" Label="API Token" Variant="Variant.Outlined"></MudTextField>
<br/>
<MudButton OnClick="Login" Variant="Variant.Filled" Color="Color.Primary" Style="float: right;">Continue</MudButton>
<br/>
<LoginPart ProceedAuthenticated="ProceedAuthenticated"/>
break;
case State.Loading:
<MudProgressCircular Color="Color.Primary" Style="height:125px;width:125px;" Indeterminate="true"/>
Expand All @@ -67,7 +63,7 @@
Authenticated,
Failed
}

private State _currentState = State.Login;

private bool Loading { get; set; }
Expand All @@ -84,17 +80,11 @@
_currentState = State.Login;
}

public async Task Login()
{
await ConfigManager.SaveAsync();
ApiClient.SetupApiClient();
await ProceedAuthenticated();
}

private async Task ProceedAuthenticated()
{
Loading = true;
_currentState = State.Loading;
await InvokeAsync(StateHasChanged);

try
{
Expand All @@ -107,15 +97,16 @@
await ApiClient.RefreshShockers();

await LiveControlManager.RefreshConnections();



_currentState = State.Authenticated;
await InvokeAsync(StateHasChanged);

NavigationManager.NavigateTo("main");
}
catch (Exception e)
{
_currentState = State.Failed;
await InvokeAsync(StateHasChanged);
Snackbar.Add("Failed to authenticate", Severity.Error);
}
}
Expand Down
149 changes: 149 additions & 0 deletions ShockOsc/Ui/Pages/Authentication/LoginPart.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
@using OneOf.Types
@using OpenShock.ShockOsc.Backend
@using OpenShock.ShockOsc.Config

@inject ConfigManager ConfigManager
@inject OpenShockApi ApiClient

<MudTextField @bind-Value="ConfigManager.Config.OpenShock.Token" Label="API Token" Variant="Variant.Outlined"></MudTextField>
<br/>
<MudButton OnClick="Login" Variant="Variant.Filled" Color="Color.Primary">Continue</MudButton>
<br/>
<br/>
<br/>
<MudPaper Outlined="true" Class="rounded-lg mud-paper-padding d-flex" Style="flex-direction: column">
<div>
@if (_advancedSettingsExpanded)
{
<MudButton OnClick="OnAdvancedSettingsClick" EndIcon="@Icons.Material.Filled.KeyboardArrowUp">Advanced Settings</MudButton>
}
else
{
<MudButton OnClick="OnAdvancedSettingsClick" EndIcon="@Icons.Material.Filled.KeyboardArrowDown">Advanced Settings</MudButton>
}
</div>

<MudCollapse Expanded="_advancedSettingsExpanded">
@if (!_useCustomServerDialog)
{
<MudSelect T="BackendServer" Label="Server" @bind-Value="Server" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter">
<MudSelectItem T="BackendServer" Value="BackendServer.Production"><span class="server-url-backdrop">https://api.shocklink.net/</span> (Production)</MudSelectItem>
<MudSelectItem T="BackendServer" Value="BackendServer.Staging"><span class="server-url-backdrop">https://api-staging.shocklink.net/</span> (Staging)</MudSelectItem>
@if (_customServerUri != null)
{
<MudSelectItem T="BackendServer" Value="BackendServer.Custom"><span class="server-url-backdrop">@_customServerUri</span> (Custom)</MudSelectItem>
}
</MudSelect>

<br/>
<MudButton OnClick="() => _useCustomServerDialog = true" Color="Color.Primary">Use custom server</MudButton>
}
else
{
<MudTextField @bind-Value="_server" Error="@ValidateCustomServerBool()" Label="Custom Server" Variant="Variant.Outlined"></MudTextField>
<br/>

<MudButton OnClick="() => _useCustomServerDialog = false" Variant="Variant.Filled" Color="Color.Primary">Back</MudButton>
<MudButton OnClick="SaveCustomServer" Variant="Variant.Filled" Color="Color.Primary" Disabled="@ValidateCustomServerBool()">Save</MudButton>
}
</MudCollapse>

</MudPaper>

<style>
.server-url-backdrop {
background-color: rgba(66, 66, 66, 1);
border-radius: 5px;
}
</style>

@code {
private bool _useCustomServerDialog = false;

private bool _advancedSettingsExpanded = false;

private string? _server = null;

[Parameter] public Func<Task> ProceedAuthenticated { get; set; }

Check warning on line 67 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ProceedAuthenticated' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 67 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ProceedAuthenticated' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public async Task Login()
{
await ConfigManager.SaveAsync();
ApiClient.SetupApiClient();
await ProceedAuthenticated();
}

private bool ValidateCustomServerBool() => !ValídateCustomServer().IsT0;

private OneOf.OneOf<Success<Uri>, StringIsNull, UriIsNotValid> ValídateCustomServer()
{
if (string.IsNullOrEmpty(_server)) return new StringIsNull();
if (Uri.TryCreate(_server, UriKind.Absolute, out var uri))
{
if (uri.Scheme != "http" && uri.Scheme != "https") return new UriIsNotValid();
return new Success<Uri>(uri);
}

return new UriIsNotValid();
}

private void SaveCustomServer()
{
var validation = ValídateCustomServer();
if (validation.IsT0)
{
_customServerUri = validation.AsT0.Value;
Server = BackendServer.Custom;
_useCustomServerDialog = false;
}
}


private void OnAdvancedSettingsClick()
{
_advancedSettingsExpanded = !_advancedSettingsExpanded;
}

private enum BackendServer
{
Production,
Staging,
Custom
}

private Uri? _customServerUri = null;

private BackendServer Server
{
get => ConfigManager.Config.OpenShock.Backend.ToString() switch
{
ProductionServerString => BackendServer.Production,
StagingServerString => BackendServer.Staging,
_ => BackendServer.Custom
};
set => ConfigManager.Config.OpenShock.Backend = value switch

Check warning on line 124 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(OpenShock.ShockOsc.Ui.Pages.Authentication.LoginPart.BackendServer)3' is not covered.

Check warning on line 124 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 124 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(OpenShock.ShockOsc.Ui.Pages.Authentication.LoginPart.BackendServer)3' is not covered.

Check warning on line 124 in ShockOsc/Ui/Pages/Authentication/LoginPart.razor

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
{
BackendServer.Production => _productionServer,
BackendServer.Staging => _stagingServer,
BackendServer.Custom => _customServerUri,
};
}

private struct WrongSchema;

private struct StringIsNull;

private struct UriIsNotValid;

private const string ProductionServerString = "https://api.shocklink.net/";
private const string StagingServerString = "https://staging-api.shocklink.net/";

private static Uri _productionServer = new(ProductionServerString);
private static Uri _stagingServer = new(StagingServerString);

protected override void OnInitialized()
{
if (Server == BackendServer.Custom) _customServerUri = ConfigManager.Config.OpenShock.Backend;
}

}
4 changes: 2 additions & 2 deletions ShockOsc/Ui/Utils/ThemeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public static class ThemeDefinition
{
Palette = new PaletteDark
{
Primary = "#8f38fd",
PrimaryDarken = "#722cca",
Primary = "#e14a6d",
PrimaryDarken = "#b31e40",
Secondary = MudBlazor.Colors.Green.Accent4,
AppbarBackground = MudBlazor.Colors.Red.Default,
Background = "#2f2f2f",
Expand Down

0 comments on commit 8ac0577

Please sign in to comment.