using Borepin.Base; using Borepin.Model; using Borepin.Service.BFFH; using Borepin.Service.Connections; using Borepin.Service.Credentials; using Prism.Commands; using Prism.Navigation; using Prism.Services; using Prism.Services.Dialogs; using System.Threading.Tasks; using System.Windows.Input; namespace Borepin.PageModel { public class ServerPageModel : PageModelBase { #region Private Properties private readonly IDialogService _DialogService; private readonly IConnectionService _ConnectionService; private readonly IBFFHService _BFFHService; private readonly ICredentialService _CredentialService; private readonly IPageDialogService _PageDialogService; #endregion #region Constructors public ServerPageModel(INavigationService navigationService, IDialogService dialogService, IConnectionService connectionService, IBFFHService bffhService, ICredentialService credentialService, IPageDialogService pageDialogService) : base(navigationService) { _DialogService = dialogService; _ConnectionService = connectionService; _BFFHService = bffhService; _CredentialService = credentialService; _PageDialogService = pageDialogService; ConnectCommand = new DelegateCommand(async () => await ConnectCommandExecuted()); DeleteCommand = new DelegateCommand(DeleteCommandExecuted); } #endregion #region Data public override Task LoadData() { throw new System.NotImplementedException(); } #endregion #region Properties private Connection _Connection_Item; public Connection Connection_Item { get => _Connection_Item; set => SetProperty(ref _Connection_Item, value); } private bool _IsConnected; public bool IsConnected { get => _IsConnected; set => SetProperty(ref _IsConnected, value); } #endregion #region Commands private ICommand _ConnectCommand; public ICommand ConnectCommand { get => _ConnectCommand; set => SetProperty(ref _ConnectCommand, value); } private async Task ConnectCommandExecuted() { IsBusy = true; if(IsConnected) { await _BFFHService.Disconnect(); IsConnected = false; } else { try { await _BFFHService.Connect(Connection_Item); } catch(Capnp.Rpc.RpcException exception) when (exception.Message == "TcpRpcClient is unable to connect") { await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok"); IsBusy = false; return; } if(!await _BFFHService.Authenticate()) { await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok"); IsBusy = false; return; } IsConnected = true; var result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage"); if (!result.Success) { System.Diagnostics.Debugger.Break(); } } IsBusy = false; } private ICommand _DeleteCommand; public ICommand DeleteCommand { get => _DeleteCommand; set => SetProperty(ref _DeleteCommand, value); } private void DeleteCommandExecuted() { DialogParameters parameters = new DialogParameters { { "title", "Delete Server" }, { "message", "Do you really want to delete this Server?" }, { "instance", Connection_Item } }; _DialogService.ShowDialog("ConfirmDialog", parameters, DeleteCommandExecuted_Dialog); } private async void DeleteCommandExecuted_Dialog(IDialogResult result) { if(result.Parameters.GetValue("result") == "confirm") { Connection connection = (Connection)result.Parameters.GetValue("instance"); if(_BFFHService.ActiveConnection != null && connection == _BFFHService.ActiveConnection) { await _BFFHService.Disconnect(); } await _ConnectionService.RemoveConnection(connection); await _CredentialService.RemoveCredentialsAsync(connection); await _NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage"); } else { INavigationParameters parameters = new NavigationParameters() { { "instance" , result.Parameters.GetValue("instance")} }; OnNavigatedTo(parameters); } } #endregion #region INavigationAware public override void OnNavigatedFrom(INavigationParameters parameters) { } public override void OnNavigatedTo(INavigationParameters parameters) { Connection_Item = parameters["instance"] as Connection; if (_BFFHService.ActiveConnection != null && Connection_Item != null) { IsConnected = Connection_Item.Equals(_BFFHService.ActiveConnection); } else { IsConnected = false; } IsBusy = false; } #endregion } }