Files
VDownload/VDownload.Core/VDownload.Core.ViewModels/Authentication/AuthenticationViewModel.cs

181 lines
6.4 KiB
C#
Raw Normal View History

2024-02-13 02:59:40 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
2024-02-13 02:59:40 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
2024-02-13 02:59:40 +01:00
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using VDownload.Services.Data.Configuration;
using VDownload.Services.UI.Dialogs;
using VDownload.Services.UI.StringResources;
using VDownload.Services.UI.WebView;
2024-02-13 02:59:40 +01:00
using VDownload.Sources.Twitch.Authentication;
namespace VDownload.Core.ViewModels.Authentication
2024-02-13 02:59:40 +01:00
{
public partial class AuthenticationViewModel : ObservableObject
{
#region ENUMS
public enum AuthenticationButton
{
SignIn,
SignOut,
Loading
}
#endregion
#region SERVICES
protected readonly IDialogsService _dialogsService;
protected readonly IWebViewService _webViewService;
protected readonly IConfigurationService _configurationService;
protected readonly IStringResourcesService _stringResourcesService;
protected readonly ITwitchAuthenticationService _twitchAuthenticationService;
2024-02-13 02:59:40 +01:00
#endregion
#region PROPERTIES
[ObservableProperty]
protected AuthenticationButton _twitchButtonState = AuthenticationButton.Loading;
2024-02-13 02:59:40 +01:00
[ObservableProperty]
protected bool _twitchButtonEnable = true;
[ObservableProperty]
protected string _twitchDescription;
2024-02-13 02:59:40 +01:00
#endregion
#region CONSTRUCTORS
public AuthenticationViewModel(IDialogsService dialogsService, IWebViewService webViewService, IConfigurationService configurationService, IStringResourcesService stringResourcesService, ITwitchAuthenticationService twitchAuthenticationService)
2024-02-13 02:59:40 +01:00
{
_dialogsService = dialogsService;
2024-02-13 02:59:40 +01:00
_webViewService = webViewService;
_configurationService = configurationService;
_stringResourcesService = stringResourcesService;
2024-02-13 02:59:40 +01:00
_twitchAuthenticationService = twitchAuthenticationService;
}
#endregion
#region PUBLIC METHODS
[RelayCommand]
public async Task Navigation()
{
List<Task> refreshTasks = new List<Task>
{
TwitchAuthenticationRefresh()
};
await Task.WhenAll(refreshTasks);
}
2024-02-13 02:59:40 +01:00
[RelayCommand]
public async Task TwitchAuthentication()
{
AuthenticationButton state = TwitchButtonState;
TwitchButtonState = AuthenticationButton.Loading;
if (state == AuthenticationButton.SignOut)
{
await _twitchAuthenticationService.DeleteToken();
}
else
{
Sources.Twitch.Configuration.Models.Authentication auth = _configurationService.Twitch.Authentication;
string authUrl = string.Format(auth.Url, auth.ClientId, auth.RedirectUrl, auth.ResponseType, string.Join(' ', auth.Scopes));
string url = await _webViewService.Show(new Uri(authUrl), (url) => url.StartsWith(auth.RedirectUrl), _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationWindowTitle"));
2024-02-13 02:59:40 +01:00
Regex regex = new Regex(auth.RedirectUrlRegex);
Match match = regex.Match(url);
2024-02-13 02:59:40 +01:00
if (match.Success)
{
string token = match.Groups[1].Value;
await _twitchAuthenticationService.SetToken(Encoding.UTF8.GetBytes(token));
}
else
{
string title = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDialogTitle");
string message = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDialogMessage");
await _dialogsService.ShowOk(title, message);
2024-02-13 02:59:40 +01:00
}
}
await TwitchAuthenticationRefresh();
}
#endregion
#region PRIVATE METHODS
private async Task TwitchAuthenticationRefresh()
{
TwitchButtonState = AuthenticationButton.Loading;
TwitchButtonEnable = true;
2024-02-13 02:59:40 +01:00
byte[]? token = await _twitchAuthenticationService.GetToken();
if (token is null)
{
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
TwitchButtonEnable = false;
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionNotAuthenticatedNoInternetConnection");
}
else
{
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionNotAuthenticated");
}
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton.SignIn;
}
else
{
TwitchValidationResult validationResult;
try
{
validationResult = await _twitchAuthenticationService.ValidateToken(token);
}
catch (Exception ex) when (ex is TaskCanceledException || ex is HttpRequestException)
{
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionCannotValidate");
TwitchButtonState = AuthenticationButton.SignIn;
TwitchButtonEnable = false;
return;
}
2024-02-13 02:59:40 +01:00
if (validationResult.Success)
{
TwitchDescription = string.Format(_stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionAuthenticated"), validationResult.TokenData.Login, validationResult.TokenData.ExpirationDate);
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton.SignOut;
}
else
{
await _twitchAuthenticationService.DeleteToken();
TwitchDescription = _stringResourcesService.AuthenticationViewResources.Get("TwitchAuthenticationDescriptionAuthenticationInvalid");
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton.SignIn;
}
}
}
#endregion
}
}