75 lines
1.7 KiB
C#
75 lines
1.7 KiB
C#
using System;
|
|
using System.Reactive;
|
|
using System.Threading.Tasks;
|
|
using AutoCat.Utils;
|
|
using AutoCatCore.Services.Api;
|
|
using AutoCatCore.Services.Storage;
|
|
using ReactiveUI;
|
|
|
|
namespace AutoCat.ViewModels;
|
|
|
|
public class AuthWindowViewModel: ViewModelBase
|
|
{
|
|
#region Dependencies
|
|
|
|
private readonly IApiService _apiService;
|
|
private readonly IStorageService _storageService;
|
|
|
|
#endregion
|
|
|
|
#region Fields
|
|
|
|
private string _email = "";
|
|
private string _password = "";
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public string Email
|
|
{
|
|
get => _email;
|
|
set => this.RaiseAndSetIfChanged(ref _email, value);
|
|
}
|
|
|
|
public string Password
|
|
{
|
|
get => _password;
|
|
set => this.RaiseAndSetIfChanged(ref _password, value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Commands
|
|
|
|
public ReactiveCommand<Unit, Unit> LoginCommand { get; }
|
|
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
public event EventHandler? ReplaceWithMainWindowEvent;
|
|
|
|
#endregion
|
|
|
|
public AuthWindowViewModel(IApiService apiService, IStorageService storageService)
|
|
{
|
|
_apiService = apiService;
|
|
_storageService = storageService;
|
|
|
|
var loginEnabled = this.WhenAnyValue(
|
|
vm => vm.Email,
|
|
vm => vm.Password,
|
|
(login, password) => login.Length > 3 && password.Length > 3);
|
|
|
|
LoginCommand = ReactiveCommand.CreateFromTask(Login, loginEnabled);
|
|
LoginCommand.ThrownExceptions.Subscribe(Alerts.ShowError);
|
|
}
|
|
|
|
private async Task Login()
|
|
{
|
|
var user = await _apiService.Login(Email, Password);
|
|
await _storageService.SetUser(user);
|
|
ReplaceWithMainWindowEvent?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
} |