83 lines
1.9 KiB
C#
83 lines
1.9 KiB
C#
using System;
|
|
using System.Reactive;
|
|
using System.Threading.Tasks;
|
|
using AutoCatCore.Services.Api;
|
|
using AutoCatCore.Services.Storage;
|
|
using ReactiveUI;
|
|
using MsBox.Avalonia;
|
|
using MsBox.Avalonia.Dto;
|
|
using MsBox.Avalonia.Enums;
|
|
|
|
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
|
|
|
|
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(ShowError);
|
|
}
|
|
|
|
private async Task Login()
|
|
{
|
|
var user = await _apiService.Login(Email, Password);
|
|
//await _storageService.SetUser(user);
|
|
Close();
|
|
}
|
|
|
|
private async void ShowError(Exception error)
|
|
{
|
|
var msgBox = MessageBoxManager.GetMessageBoxStandard(new MessageBoxStandardParams
|
|
{
|
|
ButtonDefinitions = ButtonEnum.Ok,
|
|
ContentTitle = "Error",
|
|
ContentMessage = error.Message
|
|
});
|
|
|
|
await msgBox.ShowAsync();
|
|
}
|
|
} |