[CHANGE] Reworked client creation

This commit is contained in:
max
2025-09-08 21:28:33 +02:00
parent b2c9fc2c52
commit 680b6d2cc9
19 changed files with 563 additions and 1347 deletions

View File

@@ -1,4 +1,5 @@
using System.Net;
using Manager.App.Models.Library;
using Manager.YouTube;
using Microsoft.AspNetCore.Components;
using MudBlazor;
@@ -9,27 +10,79 @@ namespace Manager.App.Components.Dialogs
{
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
[Parameter] public string DefaultUserAgent { get; set; } = "";
public YouTubeClient Client { get; set; } = new();
private bool IsAnonymous { get; set; }
private ClientPrep? PreparingClient { get; set; }
private CookieCollection ImportCookies { get; set; } = [];
private bool _isLoading;
private AccountImportSteps _steps = AccountImportSteps.Authenticate;
private bool _cookieTextValid;
private bool _cookieImportTextValid;
private string _cookieText = "";
private string _cookieDomain = ".youtube.com";
protected override void OnInitialized()
{
Client.UserAgent = DefaultUserAgent;
base.OnInitialized();
}
private bool CanSave()
{
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
{
return true;
}
return false;
}
private bool CanContinue()
{
switch (_steps)
{
case AccountImportSteps.Authenticate:
if (IsAnonymous || ImportCookies.Count != 0)
{
return true;
}
break;
case AccountImportSteps.Validate:
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
{
return true;
}
break;
}
return false;
}
private async Task OnNextStep()
{
switch (_steps)
{
case AccountImportSteps.Authenticate:
if (CanContinue())
{
_steps = AccountImportSteps.Validate;
await BuildClient();
await InvokeAsync(StateHasChanged);
return;
}
SnackbarService.Add("Cannot continue!", Severity.Warning);
break;
case AccountImportSteps.Validate:
if (CanSave())
{
MudDialog?.Close(DialogResult.Ok(PreparingClient));
await InvokeAsync(StateHasChanged);
return;
}
SnackbarService.Add("Cannot save!", Severity.Warning);
break;
}
await InvokeAsync(StateHasChanged);
}
private void ParseCookies()
{
try
{
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
Client.CookieContainer.Add(cookies);
ImportCookies.Clear();
ImportCookies.Add(ParseCookieHeader(_cookieText, _cookieDomain));
_cookieText = string.Empty;
}
catch (Exception e)
@@ -39,6 +92,16 @@ namespace Manager.App.Components.Dialogs
StateHasChanged();
}
private void ClearPreparedClient()
{
PreparingClient?.YouTubeClient?.Dispose();
PreparingClient = null;
IsAnonymous = false;
ImportCookies.Clear();
_steps = AccountImportSteps.Authenticate;
StateHasChanged();
}
private static string? ValidateCookieText(string text)
{
if (string.IsNullOrWhiteSpace(text))
@@ -82,46 +145,41 @@ namespace Manager.App.Components.Dialogs
return collection;
}
private bool CanValidate()
{
if (string.IsNullOrWhiteSpace(Client.UserAgent) || Client.CookieContainer.Count <= 0)
{
return false;
}
return true;
}
private bool CanSave()
{
if (Client.External.State == null)
{
return false;
}
if (string.IsNullOrWhiteSpace(Client.Id))
{
return false;
}
return Client.SapisidCookie != null && Client.External.State.LoggedIn;
}
private async Task ValidateAccount()
private async Task BuildClient()
{
_isLoading = true;
var result = await Client.BuildClientAsync();
if (!result.IsSuccess)
PreparingClient = new ClientPrep();
if (IsAnonymous)
{
SnackbarService.Add(result.Error?.Description ?? "Error validating account.", Severity.Error);
ImportCookies.Clear();
}
var clientResult = await YouTubeClient.CreateAsync(ImportCookies, DefaultUserAgent);
if (clientResult.IsSuccess)
{
PreparingClient.YouTubeClient = clientResult.Value;
}
if (PreparingClient.YouTubeClient == null)
{
SnackbarService.Add("Failed to get client!", Severity.Error);
_isLoading = false;
return;
}
var accountResult = await PreparingClient.YouTubeClient.GetChannelByIdAsync(PreparingClient.YouTubeClient.Id);
if (accountResult.IsSuccess)
{
PreparingClient.Channel = accountResult.Value;
}
_isLoading = false;
}
private void OnSave()
{
MudDialog?.Close(DialogResult.Ok(Client));
await InvokeAsync(StateHasChanged);
}
}
public enum AccountImportSteps
{
Authenticate,
Validate
}
}