Retail_manager/RMWPFUserInterface/ViewModels/LoginViewModel.cs
s459315 57d2ab60a7 Created Sales View and Database prodecures for displaying items.
Created and binded a View, created logic behind Cart Product Model
2022-07-23 17:09:13 +02:00

112 lines
2.6 KiB
C#

using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFUserInterface.EventModels;
using RMWPFUserInterface.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFUserInterface.ViewModels
{
public class LoginViewModel : Screen
{
private string _userName;
private string _password;
private IAPIHelper _apiHelper;
private IEventAggregator _events;
public LoginViewModel(IAPIHelper apiHelper, IEventAggregator events)
{
_apiHelper = apiHelper;
_events = events;
}
public string UserName
{
get { return _userName; }
set
{
_userName = value;
NotifyOfPropertyChange(() => UserName);
NotifyOfPropertyChange(() => CanLogIn);
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange(() => Password);
NotifyOfPropertyChange(() => CanLogIn);
}
}
public bool IsErrorVisible
{
get
{
bool output = false;
if (ErrorMessage?.Length > 0)
{
output = true;
}
return output;
}
}
private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set
{
_errorMessage = value;
NotifyOfPropertyChange(() => IsErrorVisible);
NotifyOfPropertyChange(() => ErrorMessage);
}
}
public bool CanLogIn
{
get{
bool output = false;
if (UserName?.Length > 0 && Password?.Length > 0)
{
output = true;
}
return output;
}
}
public async Task LogIn()
{
try
{
ErrorMessage = "";
var result = await _apiHelper.Authenticate(UserName, Password);
// Capture more information
await _apiHelper.GetLogedInUserInfo(result.Access_Token);
await _events.PublishOnUIThreadAsync(new LogOnEvent());
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
}
}
}