mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-12 23:01:52 +01:00
132 lines
3.9 KiB
C#
132 lines
3.9 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using Borepin.Base;
|
|
using Borepin.Model;
|
|
using Borepin.Service.BFFH;
|
|
using Borepin.Service.BFFH.Exceptions;
|
|
using FabAccessAPI.Exceptions;
|
|
using Prism.Commands;
|
|
using Prism.Navigation;
|
|
using Prism.Services;
|
|
|
|
namespace Borepin.PageModel.AddServerProcess
|
|
{
|
|
public class AuthPlainPageModel : PageModelBase
|
|
{
|
|
#region Private Fields
|
|
private readonly IBFFHService _BFFHService;
|
|
private readonly IPageDialogService _PageDialogService;
|
|
|
|
private Connection _Connection;
|
|
#endregion
|
|
|
|
#region Constructors
|
|
public AuthPlainPageModel(INavigationService navigationService, IBFFHService bffhService, IPageDialogService pageDialogService) : base(navigationService)
|
|
{
|
|
_BFFHService = bffhService;
|
|
_PageDialogService = pageDialogService;
|
|
|
|
AuthenticateCommand = new DelegateCommand(async () => await AuthenticateCommandExecute().ConfigureAwait(false));
|
|
}
|
|
#endregion
|
|
|
|
#region LoadData
|
|
public override Task LoadData()
|
|
{
|
|
Username = _Connection.Username;
|
|
|
|
IsBusy = false;
|
|
return Task.CompletedTask;
|
|
}
|
|
#endregion
|
|
|
|
#region Fields
|
|
private string _Username;
|
|
public string Username
|
|
{
|
|
get => _Username;
|
|
set => SetProperty(ref _Username, value);
|
|
}
|
|
|
|
private string _Password;
|
|
public string Password
|
|
{
|
|
get => _Password;
|
|
set => SetProperty(ref _Password, value);
|
|
}
|
|
#endregion
|
|
|
|
#region Commands
|
|
private ICommand _AuthenticateCommand;
|
|
public ICommand AuthenticateCommand
|
|
{
|
|
get => _AuthenticateCommand;
|
|
set => SetProperty(ref _AuthenticateCommand, value);
|
|
}
|
|
public async Task AuthenticateCommandExecute()
|
|
{
|
|
IsBusy = true;
|
|
|
|
_Connection.Username = Username;
|
|
|
|
if (_BFFHService.IsConnected)
|
|
{
|
|
await _BFFHService.Disconnect().ConfigureAwait(true);
|
|
}
|
|
|
|
try
|
|
{
|
|
await _BFFHService.Connect(_Connection, Password).ConfigureAwait(true);
|
|
}
|
|
catch (ConnectingFailedException)
|
|
{
|
|
await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false);
|
|
|
|
IsBusy = false;
|
|
return;
|
|
}
|
|
catch (AuthenticationFailedException)
|
|
{
|
|
await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false);
|
|
|
|
IsBusy = false;
|
|
return;
|
|
}
|
|
catch(Exception)
|
|
{
|
|
await _PageDialogService.DisplayAlertAsync("Connection failed", "Unexpected Error.", "Ok").ConfigureAwait(false);
|
|
|
|
IsBusy = false;
|
|
return;
|
|
}
|
|
|
|
await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage").ConfigureAwait(false);
|
|
}
|
|
#endregion
|
|
|
|
#region INavigationAware
|
|
public override void OnNavigatedTo(INavigationParameters parameters)
|
|
{
|
|
if (parameters.ContainsKey("instance") && parameters["instance"] is Connection)
|
|
{
|
|
_Connection = parameters["instance"] as Connection;
|
|
}
|
|
else
|
|
{
|
|
_Connection = new Connection();
|
|
}
|
|
|
|
LoadData();
|
|
}
|
|
|
|
public override void OnNavigatedFrom(INavigationParameters parameters)
|
|
{
|
|
if(parameters.GetNavigationMode() == NavigationMode.Back)
|
|
{
|
|
parameters.Add("instance", _Connection);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |