- add: Auth service

This commit is contained in:
2024-12-22 21:38:19 +03:00
parent 4d64c995f1
commit fd347a4fc8
30 changed files with 894 additions and 211 deletions

View File

@@ -1,13 +1,117 @@
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views.Pages;
namespace Nebula.Launcher.ViewModels;
[ViewRegister(typeof(AccountInfoView))]
public class AccountInfoViewModel : ViewModelBase
public partial class AccountInfoViewModel : ViewModelBase
{
public AccountInfoViewModel(IServiceProvider serviceProvider) : base(serviceProvider)
private readonly AuthService _authService;
public ObservableCollection<AuthLoginPasswordModel> Accounts { get; } = new();
[ObservableProperty]
private string _currentLogin = String.Empty;
[ObservableProperty]
private string _currentPassword = String.Empty;
[ObservableProperty]
private string _currentAuthServer = String.Empty;
[ObservableProperty] private bool _pageEnabled = true;
public AuthLoginPassword CurrentALP => new AuthLoginPassword(CurrentLogin, CurrentPassword, CurrentAuthServer);
//Design think
public AccountInfoViewModel()
{
AddAccount(new AuthLoginPassword("Binka","12341",""));
}
//Real think
public AccountInfoViewModel(IServiceProvider serviceProvider, AuthService authService) : base(serviceProvider)
{
_authService = authService;
}
public void AuthByALP(AuthLoginPassword authLoginPassword)
{
CurrentLogin = authLoginPassword.Login;
CurrentPassword = authLoginPassword.Password;
CurrentAuthServer = authLoginPassword.AuthServer;
DoAuth();
}
public async void DoAuth()
{
PageEnabled = false;
if(await _authService.Auth(CurrentALP))
Console.WriteLine("Hello, " + _authService.SelectedAuth!.Username);
else
Console.WriteLine("Shit!");
PageEnabled = true;
}
private void AddAccount(AuthLoginPassword authLoginPassword)
{
var onDelete = new DelegateCommand<AuthLoginPasswordModel>(a => Accounts.Remove(a));
var onSelect = new DelegateCommand<AuthLoginPasswordModel>(AuthByALP);
var alpm = new AuthLoginPasswordModel(
authLoginPassword.Login,
authLoginPassword.Password,
authLoginPassword.AuthServer,
onSelect,
onDelete);
onDelete.TRef.Value = alpm;
onSelect.TRef.Value = alpm;
Accounts.Add(alpm);
}
[RelayCommand]
public void OnSaveProfile()
{
AddAccount(CurrentALP);
}
}
internal class Ref<T>
{
public T Value = default!;
}
internal class DelegateCommand<T> : ICommand
{
private readonly Action<T> _func;
public readonly Ref<T> TRef = new();
public DelegateCommand(Action<T> func)
{
_func = func;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
{
_func(TRef.Value);
}
public event EventHandler? CanExecuteChanged;
}
public record AuthLoginPasswordModel(string Login, string Password, string AuthServer, ICommand OnSelect = default!, ICommand OnDelete = default!)
: AuthLoginPassword(Login, Password, AuthServer);

View File

@@ -11,18 +11,22 @@ namespace Nebula.Launcher.ViewModels;
[ViewRegister(typeof(ServerListView))]
public partial class ServerListViewModel : ViewModelBase
{
public ObservableCollection<ServerInfo> ServerInfos { get; }
public ObservableCollection<ServerHubInfo> ServerInfos { get; }
[ObservableProperty]
private ServerInfo? _selectedListItem;
private ServerHubInfo? _selectedListItem;
//Design think
public ServerListViewModel()
{
ServerInfos = new ObservableCollection<ServerInfo>();
ServerInfos = new ObservableCollection<ServerHubInfo>();
ServerInfos.Add(new ServerHubInfo("ss14://localhost",new ServerStatus("","TestCraft", [], "super", 12,55,1,false,DateTime.Now, 20),[]));
}
//real think
public ServerListViewModel(IServiceProvider serviceProvider, HubService hubService) : base(serviceProvider)
{
ServerInfos = new ObservableCollection<ServerInfo>();
ServerInfos = new ObservableCollection<ServerHubInfo>();
hubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
}