diff --git a/Borepin/Borepin/App.xaml.cs b/Borepin/Borepin/App.xaml.cs index cd90a6e..58055cc 100644 --- a/Borepin/Borepin/App.xaml.cs +++ b/Borepin/Borepin/App.xaml.cs @@ -1,14 +1,11 @@ -using System; -using Prism.Ioc; +using Prism.Ioc; using Borepin.PageModel; using Borepin.Page; using Xamarin.Forms; -using Borepin.Service; -using Borepin.Service.Instances; -using Borepin.View; -using Borepin.ViewModel; using Borepin.Dialog; using Borepin.DialogModel; +using Borepin.Service.Connections; +using Borepin.Service.BFFH; namespace Borepin { @@ -23,9 +20,7 @@ namespace Borepin { InitializeComponent(); - //var result = await NavigationService.NavigateAsync("/NavigationPage/HostSelectPage"); var result = await NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage"); - //var result = await NavigationService.NavigateAsync("/ListPage"); if (!result.Success) { @@ -35,6 +30,7 @@ namespace Borepin protected override void RegisterTypes(IContainerRegistry containerRegistry) { + // Register Navigation containerRegistry.RegisterForNavigation<NavigationPage>(); containerRegistry.RegisterForNavigation<MainPage, MainPagePageModel>(); containerRegistry.RegisterForNavigation<MachinesPage, MachinesPageModel>(); @@ -44,16 +40,17 @@ namespace Borepin containerRegistry.RegisterForNavigation<HostSelectPage, HostSelectPageModel>(); containerRegistry.RegisterForNavigation<LoginChoosePage, LoginChoosePageModel>(); containerRegistry.RegisterForNavigation<ServerListPage, ServerListPageModel>(); - + containerRegistry.RegisterForNavigation<ServerPage, ServerPageModel>(); containerRegistry.RegisterForNavigation<ListPage, ListPageModel>(); + // Register Dialog containerRegistry.RegisterDialog<ConfirmDialog, ConfirmDialogModel>(); - - - InstanceService bffhInstanceService = new InstanceService(); - containerRegistry.RegisterInstance<IInstanceService>(bffhInstanceService); - containerRegistry.RegisterInstance<BFFHService>(new BFFHService(bffhInstanceService)); + // Register Service + IConnectionService connectionService = new ConnectionService(); + containerRegistry.RegisterInstance<IConnectionService>(connectionService); + containerRegistry.RegisterInstance<IBFFHService>(new BFFHService(connectionService)); + containerRegistry.RegisterInstance<IConnectionService>(new ConnectionService()); } } } diff --git a/Borepin/Borepin/Borepin.csproj b/Borepin/Borepin/Borepin.csproj index ad86a1c..96bf76b 100644 --- a/Borepin/Borepin/Borepin.csproj +++ b/Borepin/Borepin/Borepin.csproj @@ -76,6 +76,9 @@ <EmbeddedResource Update="Page\ServerListPage.xaml"> <Generator>MSBuild:UpdateDesignTimeXaml</Generator> </EmbeddedResource> + <EmbeddedResource Update="Page\ServerPage.xaml"> + <Generator>MSBuild:UpdateDesignTimeXaml</Generator> + </EmbeddedResource> <EmbeddedResource Update="Page\SettingsPage.xaml"> <Generator>MSBuild:UpdateDesignTimeXaml</Generator> </EmbeddedResource> diff --git a/Borepin/Borepin/Model/Connection.cs b/Borepin/Borepin/Model/Connection.cs new file mode 100644 index 0000000..91050a4 --- /dev/null +++ b/Borepin/Borepin/Model/Connection.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; + +namespace Borepin.Model +{ + public enum ConnectionTyp + { + SINGLE, + FEDERATED + } + + public enum AuthenticationTyp + { + PLAIN + } + + public class Connection + { + public Uri Address { get; set; } + + public DateTime LastTime { get; set; } + + public string Username { get; set; } + + public ConnectionTyp ConnectionTyp { get; set; } + + public AuthenticationTyp AuthenticationTyp { get; set; } + + public override bool Equals(object obj) + { + return obj is Connection connection && + EqualityComparer<Uri>.Default.Equals(Address, connection.Address) && + Username == connection.Username && + ConnectionTyp == connection.ConnectionTyp && + AuthenticationTyp == connection.AuthenticationTyp; + } + + public override int GetHashCode() + { + int hashCode = -904541792; + hashCode = hashCode * -1521134295 + EqualityComparer<Uri>.Default.GetHashCode(Address); + hashCode = hashCode * -1521134295 + LastTime.GetHashCode(); + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Username); + hashCode = hashCode * -1521134295 + ConnectionTyp.GetHashCode(); + hashCode = hashCode * -1521134295 + AuthenticationTyp.GetHashCode(); + return hashCode; + } + } +} diff --git a/Borepin/Borepin/Page/ServerListPage.xaml b/Borepin/Borepin/Page/ServerListPage.xaml index 2ed3335..4816e3e 100644 --- a/Borepin/Borepin/Page/ServerListPage.xaml +++ b/Borepin/Borepin/Page/ServerListPage.xaml @@ -18,7 +18,6 @@ </ListView.ItemTemplate> </ListView> <Button Text="Connect to new Server" Command="{Binding AddInstancesCommand}"/> - <Button Text="Delete All Servers" Command="{Binding DeleteAllInstancesCommand}"/> </StackLayout> </ContentPage.Content> </ContentPage> \ No newline at end of file diff --git a/Borepin/Borepin/Page/ServerPage.xaml b/Borepin/Borepin/Page/ServerPage.xaml new file mode 100644 index 0000000..789840d --- /dev/null +++ b/Borepin/Borepin/Page/ServerPage.xaml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8" ?> +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" + x:Class="Borepin.Page.ServerPage"> + <ContentPage.Content> + <StackLayout> + <Label Text="Server Page" + VerticalOptions="CenterAndExpand" + HorizontalOptions="CenterAndExpand" /> + </StackLayout> + </ContentPage.Content> +</ContentPage> \ No newline at end of file diff --git a/Borepin/Borepin/Page/ServerPage.xaml.cs b/Borepin/Borepin/Page/ServerPage.xaml.cs new file mode 100644 index 0000000..49f6ee5 --- /dev/null +++ b/Borepin/Borepin/Page/ServerPage.xaml.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms; +using Xamarin.Forms.Xaml; + +namespace Borepin.Page +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + public partial class ServerPage : ContentPage + { + public ServerPage() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/Borepin/Borepin/PageModel/AddServerDialog/HostSelectPageModel.cs b/Borepin/Borepin/PageModel/AddServerDialog/HostSelectPageModel.cs index 6bf5f23..9e39e9a 100644 --- a/Borepin/Borepin/PageModel/AddServerDialog/HostSelectPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerDialog/HostSelectPageModel.cs @@ -1,5 +1,6 @@ -using Borepin.Service; -using Borepin.Service.Instances; +using Borepin.Model; +using Borepin.Service.BFFH; +using Borepin.Service.Connections; using Prism.Mvvm; using Prism.Navigation; using System; @@ -13,24 +14,24 @@ namespace Borepin.PageModel public class HostSelectPageModel : BindableBase { private INavigationService _NavigationService; - private IInstanceService _BFFHInstanceService; - private BFFHService _BFFHService; + private IBFFHService _BFFHService; + private IConnectionService _ConnectionService; - public HostSelectPageModel(INavigationService navigationService, BFFHService bffhService, IInstanceService bffhInstanceService) + public HostSelectPageModel(INavigationService navigationService, IBFFHService bffhService, IConnectionService connectionService) { _NavigationService = navigationService; - _BFFHInstanceService = bffhInstanceService; _BFFHService = bffhService; + _ConnectionService = connectionService; UseHostCommand = new Command(UseHostCommandExecuted); DetectHostCommand = new Command(DetectHostCommandExecuted); - LoadData(); + Task.Run(LoadData); } - private async Task LoadData() + private void LoadData() { - + } private string _Host; @@ -62,7 +63,12 @@ namespace Borepin.PageModel builder.Port = 59661; } - _BFFHService.Connect(builder.Uri); + Connection connection = new Connection() + { + Address = builder.Uri + }; + + await _BFFHService.Connect(connection); INavigationResult result = await _NavigationService.NavigateAsync("LoginChoosePage"); if (!result.Success) diff --git a/Borepin/Borepin/PageModel/AddServerDialog/LoginPasswordPageModel.cs b/Borepin/Borepin/PageModel/AddServerDialog/LoginPasswordPageModel.cs index 91379de..658abda 100644 --- a/Borepin/Borepin/PageModel/AddServerDialog/LoginPasswordPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerDialog/LoginPasswordPageModel.cs @@ -1,7 +1,8 @@ -using Borepin.Service; +using Borepin.Model; +using Borepin.Service.BFFH; +using Borepin.Service.Connections; using Prism.Mvvm; using Prism.Navigation; -using System; using System.Threading.Tasks; using System.Windows.Input; using Xamarin.Forms; @@ -11,23 +12,25 @@ namespace Borepin.PageModel public class LoginPasswordPageModel : BindableBase { private INavigationService _NavigationService; - private BFFHService _BFFHService; + private IBFFHService _BFFHService; + private IConnectionService _ConnectionService; private string _KnownUsername; - public LoginPasswordPageModel(INavigationService navigationService, BFFHService bffhService) + public LoginPasswordPageModel(INavigationService navigationService, IBFFHService bffhService, IConnectionService connectionService) { _NavigationService = navigationService; _BFFHService = bffhService; + _ConnectionService = connectionService; AuthenticateCommand = new Command(AuthenticateCommandExecuted); - LoadData(); + Task.Run(LoadData); } - private async Task LoadData() + private void LoadData() { - _KnownUsername = await _BFFHService.GetUsername(); + _KnownUsername = _BFFHService.ActiveConnection.Username; if (_KnownUsername != null) { @@ -61,11 +64,18 @@ namespace Borepin.PageModel { if(_KnownUsername == Username) { - await _BFFHService.AuthenticateAsync(); + await _BFFHService.Authenticate(); + await _ConnectionService.LogConnect(_BFFHService.ActiveConnection); } else { - await _BFFHService.Authenticate(Username, Password); + Connection connection_update = _BFFHService.ActiveConnection; + + connection_update.Username = Username; + await _BFFHService.Authenticate(connection_update, Password); + + await _ConnectionService.AddConnection(_BFFHService.ActiveConnection); + await _ConnectionService.LogConnect(_BFFHService.ActiveConnection); } var result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachinesPage"); diff --git a/Borepin/Borepin/PageModel/MachinePageModel.cs b/Borepin/Borepin/PageModel/MachinePageModel.cs index 851c015..e23b89e 100644 --- a/Borepin/Borepin/PageModel/MachinePageModel.cs +++ b/Borepin/Borepin/PageModel/MachinePageModel.cs @@ -1,5 +1,5 @@ using Borepin.Model; -using Borepin.Service; +using Borepin.Service.BFFH; using Prism.Mvvm; using Prism.Navigation; using System.Windows.Input; @@ -10,9 +10,9 @@ namespace Borepin.PageModel public class MachinePageModel : BindableBase, INavigationAware { private INavigationService _NavigationService; - private BFFHService _BFFHService; + private IBFFHService _BFFHService; - public MachinePageModel(INavigationService navigationService, BFFHService bffhService) + public MachinePageModel(INavigationService navigationService, IBFFHService bffhService) { _NavigationService = navigationService; _BFFHService = bffhService; diff --git a/Borepin/Borepin/PageModel/MachinesPageModel.cs b/Borepin/Borepin/PageModel/MachinesPageModel.cs index 1be6b2c..e0ef75f 100644 --- a/Borepin/Borepin/PageModel/MachinesPageModel.cs +++ b/Borepin/Borepin/PageModel/MachinesPageModel.cs @@ -26,8 +26,6 @@ namespace Borepin.PageModel private async void GoToMachineCommandExecuted(object obj) { - _NavigationService.GetNavigationUriPath(); - Machine machine = obj as Machine; NavigationParameters navigationParams = new NavigationParameters(); diff --git a/Borepin/Borepin/PageModel/ServerListPageModel.cs b/Borepin/Borepin/PageModel/ServerListPageModel.cs index 6e6491e..15ca675 100644 --- a/Borepin/Borepin/PageModel/ServerListPageModel.cs +++ b/Borepin/Borepin/PageModel/ServerListPageModel.cs @@ -1,5 +1,4 @@ -using Borepin.Service.Instances; -using Borepin.ViewModel; +using Borepin.ViewModel; using Prism.Mvvm; using System.Collections.Generic; using System.Threading.Tasks; @@ -8,33 +7,39 @@ using Borepin.Model; using System.Windows.Input; using Prism.Commands; using Prism.Navigation; -using Prism.Services.Dialogs; +using Borepin.Service.Connections; +using Borepin.Service.BFFH; namespace Borepin.PageModel { - public class ServerListPageModel : BindableBase + public class ServerListPageModel : BindableBase, INavigationAware { private INavigationService _NavigationService; - private IDialogService _DialogService; - private IInstanceService _InstanceService; + private IConnectionService _ConnectionService; + private IBFFHService _BFFHService; - public ServerListPageModel(INavigationService navigationService, IDialogService dialogService, IInstanceService instanceService) + public ServerListPageModel(INavigationService navigationService, IConnectionService connectionService, IBFFHService bffhService) { _NavigationService = navigationService; - _DialogService = dialogService; - _InstanceService = instanceService; + _ConnectionService = connectionService; + _BFFHService = bffhService; - DeleteAllInstancesCommand = new DelegateCommand(DeleteAllInstancesCommandExecuted); AddInstancesCommand = new DelegateCommand(AddInstancesCommandExecuted); + SelectInstanceCommand = new DelegateCommand<object>(SelectInstanceCommandExecuted); - LoadData(); + Task.Run(LoadData); } private async Task<bool> LoadData() { - List<BFFHInstance> list = await _InstanceService.GetBFFHInstances(); + List<Connection> list = await _ConnectionService.GetConnectionList(); ServerListItemViewModel_List = list.Select(x => new ServerListItemViewModel(x)).ToList(); + if(_BFFHService.ActiveConnection != null) + { + ActiveConnection = new ServerListItemViewModel(_BFFHService.ActiveConnection); + } + return await Task.FromResult(true); } @@ -45,30 +50,33 @@ namespace Borepin.PageModel get => _ServerListItemViewModel_List; set => SetProperty(ref _ServerListItemViewModel_List, value); } + + private ServerListItemViewModel _ActiveConnection; + public ServerListItemViewModel ActiveConnection + { + get => _ActiveConnection; + set => SetProperty(ref _ActiveConnection, value); + } #endregion #region Commands - private ICommand _DeleteAllInstancesCommand; - public ICommand DeleteAllInstancesCommand + private ICommand _SelectInstanceCommand; + public ICommand SelectInstanceCommand { - get => _DeleteAllInstancesCommand; - set => SetProperty(ref _DeleteAllInstancesCommand, value); + get => _SelectInstanceCommand; + set => SetProperty(ref _SelectInstanceCommand, value); } - private void DeleteAllInstancesCommandExecuted() + private async void SelectInstanceCommandExecuted(object obj) { - var parameters = new DialogParameters - { - { "title", "Remove all Servers" }, - { "message", "Do you really want to remove all known Servers?" } - }; - _DialogService.ShowDialog("ConfirmDialog", parameters, DeleteAllInstancesConfirm); - } + ServerListItemViewModel viewmodel = obj as ServerListItemViewModel; - private void DeleteAllInstancesConfirm(IDialogResult result) - { - if (result.Parameters.ContainsKey("confirm")) + NavigationParameters navigationParams = new NavigationParameters(); + navigationParams.Add("instance", viewmodel.Instance); + + INavigationResult result = await _NavigationService.NavigateAsync($"ServerPage", navigationParams); + if (!result.Success) { - _InstanceService.RemoveAllBFFHInstances(); + System.Diagnostics.Debugger.Break(); } } @@ -78,7 +86,6 @@ namespace Borepin.PageModel get => _AddInstancesCommand; set => SetProperty(ref _AddInstancesCommand, value); } - private async void AddInstancesCommandExecuted() { INavigationResult result = await _NavigationService.NavigateAsync("HostSelectPage"); @@ -88,5 +95,17 @@ namespace Borepin.PageModel } } #endregion + + #region INavigationAware + public void OnNavigatedFrom(INavigationParameters parameters) + { + + } + + public void OnNavigatedTo(INavigationParameters parameters) + { + Task.Run(LoadData); + } + #endregion } } diff --git a/Borepin/Borepin/PageModel/ServerPageModel.cs b/Borepin/Borepin/PageModel/ServerPageModel.cs new file mode 100644 index 0000000..40ef226 --- /dev/null +++ b/Borepin/Borepin/PageModel/ServerPageModel.cs @@ -0,0 +1,40 @@ +using Borepin.Model; +using Borepin.Service.Connections; +using Prism.Mvvm; +using Prism.Navigation; + +namespace Borepin.PageModel +{ + public class ServerPageModel : BindableBase, INavigationAware + { + private INavigationService _NavigationService; + private IConnectionService _ConnectionService; + + public ServerPageModel(INavigationService navigationService, IConnectionService connectionService) + { + _NavigationService = navigationService; + _ConnectionService = connectionService; + } + + #region Properties + private Connection _Connection_Item; + public Connection Connection_Item + { + get => _Connection_Item; + set => SetProperty(ref _Connection_Item, value); + } + #endregion + + #region INavigationAware + public void OnNavigatedFrom(INavigationParameters parameters) + { + + } + + public void OnNavigatedTo(INavigationParameters parameters) + { + Connection_Item = parameters["instance"] as Connection; + } + #endregion + } +} diff --git a/Borepin/Borepin/Service/BFFH/BFFHService.cs b/Borepin/Borepin/Service/BFFH/BFFHService.cs new file mode 100644 index 0000000..3080960 --- /dev/null +++ b/Borepin/Borepin/Service/BFFH/BFFHService.cs @@ -0,0 +1,80 @@ +using Borepin.Model; +using Borepin.Service.Connections; +using Borepin.Service.Credentials; +using System.Threading.Tasks; +using Capnp.Rpc; +using System.Collections.Generic; + +namespace Borepin.Service.BFFH +{ + public class BFFHService : IBFFHService + { + private IConnectionService _ConnectionService; + private ICredentialService _CredentialService; + + private FabAccessAPI.Connection _Connection; + + public BFFHService(IConnectionService connectionService) + { + _ConnectionService = connectionService; + _CredentialService = new CredentialService(); + } + + public Model.Connection ActiveConnection { get; private set; } + + public bool IsAuthenticated { get; private set; } + + public Task<bool> Connect(Model.Connection connection) + { + if (_Connection != null || ActiveConnection != null) + { + throw new System.Exception("Still connected"); + } + + var rpcClient = new TcpRpcClient(); + + rpcClient.Connect(connection.Address.Host, connection.Address.Port); + + FabAccessAPI.Connection connection_test = new FabAccessAPI.Connection(rpcClient); + + _Connection = connection_test; + ActiveConnection = connection; + + return Task.FromResult(true); + } + + public Task<bool> Disconnect() + { + _Connection.RpcClient?.Dispose(); + _Connection = null; + ActiveConnection = null; + + return Task.FromResult(true); + } + + public bool CanAuthenticate() + { + return ActiveConnection.AuthenticationTyp == AuthenticationTyp.PLAIN && ActiveConnection.Username != string.Empty; + } + + public async Task<bool> Authenticate() + { + string password = await _CredentialService.GetPasswordAsync(ActiveConnection); + + await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", ActiveConnection.Username }, { "Password", password } }); + + return await Task.FromResult(true); + } + + public async Task<bool> Authenticate(Model.Connection connection, string password) + { + await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", ActiveConnection.Username }, { "Password", password } }); + + await _CredentialService.AddCredentialsAsync(connection, password); + + ActiveConnection = connection; + + return await Task.FromResult(true); + } + } +} diff --git a/Borepin/Borepin/Service/BFFH/IBFFHService.cs b/Borepin/Borepin/Service/BFFH/IBFFHService.cs new file mode 100644 index 0000000..f70189a --- /dev/null +++ b/Borepin/Borepin/Service/BFFH/IBFFHService.cs @@ -0,0 +1,49 @@ +using Borepin.Model; +using System.Threading.Tasks; + +namespace Borepin.Service.BFFH +{ + public interface IBFFHService + { + /// <summary> + /// Connection BFFH Service is currently connected + /// </summary> + Connection ActiveConnection { get; } + + /// <summary> + /// Connection is connected and authenticated + /// </summary> + bool IsAuthenticated { get; } + + /// <summary> + /// Can Connection with Credetials get Authenticated + /// </summary> + /// <returns></returns> + bool CanAuthenticate(); + + /// <summary> + /// Connect to BFFH Instance + /// </summary> + /// <param name="connection">Connection with address</param> + Task<bool> Connect(Connection connection); + + /// <summary> + /// Disconnect from BFFH Instance + /// </summary> + Task<bool> Disconnect(); + + + /// <summary> + /// Authenticate with ActiveConnection + /// Load password from SecureStorage + /// </summary> + Task<bool> Authenticate(); + + /// <summary> + /// Authenticate with ActiveConnection and password + /// If authenticate is successfull, save password to SecureStorage + /// </summary> + /// <param name="connection">address + username</param> + Task<bool> Authenticate(Connection connection, string password); + } +} diff --git a/Borepin/Borepin/Service/BFFHService.cs b/Borepin/Borepin/Service/BFFHService.cs deleted file mode 100644 index 89cc2bd..0000000 --- a/Borepin/Borepin/Service/BFFHService.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.Collections.Generic; -using FabAccessAPI; -using Capnp.Rpc; -using Borepin.Service.Credentials; -using Borepin.Model; -using System.Threading.Tasks; -using Borepin.Service.Instances; - -namespace Borepin.Service -{ - public class BFFHService - { - private IInstanceService _BFFHInstanceService; - private CredentialsService _CredentialsService; - - private Connection _Connection; - - public Uri ConnectedHost { get; private set; } - - public BFFHService(IInstanceService bffhInstanceService) - { - _BFFHInstanceService = bffhInstanceService; - _CredentialsService = new CredentialsService(); - } - - public void Connect(Uri host) - { - if(_Connection != null) - { - throw new System.Exception("Still connected"); - } - - var rpcClient = new TcpRpcClient(); - rpcClient.Connect(host.Host, host.Port); - - Connection connection_test = new Connection(rpcClient); - - _BFFHInstanceService.AddBFFHInstance(new BFFHInstance() { Address = host }); - - _Connection = connection_test; - ConnectedHost = host; - } - - public void Connect() - { - if (ConnectedHost == null) - { - throw new System.Exception("No Host selected"); - } - - Connect(ConnectedHost); - } - - public void Disconnect() - { - _Connection.RpcClient?.Dispose(); - _Connection = null; - } - - public async Task<string> GetUsername() - { - return await Task.FromResult((await _CredentialsService.GetCredentialsAsync(ConnectedHost)).Username); - } - - public async Task<bool> AuthenticateAsync() - { - HostCredentials credentials = await _CredentialsService.GetCredentialsAsync(ConnectedHost); - - await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", credentials.Username }, { "Password", credentials.Password } }); - - return await Task.FromResult(true); - } - - public async Task<bool> Authenticate(string username, string password) - { - await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", username }, { "Password", password } }); - - await _CredentialsService.LogCredentialsAsync(new HostCredentials() { Host = ConnectedHost, Username = username, Password = password }); - - return await Task.FromResult(true); - } - } -} diff --git a/Borepin/Borepin/Service/Connections/ConnectionService.cs b/Borepin/Borepin/Service/Connections/ConnectionService.cs new file mode 100644 index 0000000..2ed0acd --- /dev/null +++ b/Borepin/Borepin/Service/Connections/ConnectionService.cs @@ -0,0 +1,68 @@ +using Borepin.Model; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xamarin.Essentials; + +namespace Borepin.Service.Connections +{ + public class ConnectionService : IConnectionService + { + public Task<List<Connection>> GetConnectionList() + { + return Task.FromResult(JsonConvert.DeserializeObject<List<Connection>>(Preferences.Get("connection_list", "[]"))); + } + + public Task<bool> AddConnection(Connection connection) + { + List<Connection> connection_list = JsonConvert.DeserializeObject<List<Connection>>(Preferences.Get("connection_list", "[]")); + + if(connection_list.Contains(connection)) + { + throw new ArgumentException("Connection allready exist"); + } + + connection_list.Add(connection); + + Preferences.Set("connection_list", JsonConvert.SerializeObject(connection_list)); + + return Task.FromResult(true); + } + + public Task<bool> RemoveConnection(Connection connection) + { + List<Connection> connection_list = JsonConvert.DeserializeObject<List<Connection>>(Preferences.Get("connection_list", "[]")); + + if (!connection_list.Contains(connection)) + { + throw new ArgumentException("Connection does not exist"); + } + + connection_list.RemoveAll(x => x.Equals(connection)); + + Preferences.Set("connection_list", JsonConvert.SerializeObject(connection_list)); + + return Task.FromResult(true); + } + + public Task<bool> LogConnect(Connection connection) + { + List<Connection> connection_list = JsonConvert.DeserializeObject<List<Connection>>(Preferences.Get("connection_list", "[]")); + + if (!connection_list.Contains(connection)) + { + throw new ArgumentException("Connection does not exist"); + } + + int index = connection_list.IndexOf(connection); + Connection connection_update = connection_list[index]; + connection_update.LastTime = DateTime.UtcNow; + connection_list[index] = connection_update; + + Preferences.Set("connection_list", JsonConvert.SerializeObject(connection_list)); + + return Task.FromResult(true); + } + } +} diff --git a/Borepin/Borepin/Service/Connections/IConnectionService.cs b/Borepin/Borepin/Service/Connections/IConnectionService.cs new file mode 100644 index 0000000..ddf779b --- /dev/null +++ b/Borepin/Borepin/Service/Connections/IConnectionService.cs @@ -0,0 +1,17 @@ +using Borepin.Model; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Borepin.Service.Connections +{ + public interface IConnectionService + { + Task<List<Connection>> GetConnectionList(); + + Task<bool> AddConnection(Connection connection); + + Task<bool> RemoveConnection(Connection connection); + + Task<bool> LogConnect(Connection connection); + } +} diff --git a/Borepin/Borepin/Service/Credentials/CredentialService.cs b/Borepin/Borepin/Service/Credentials/CredentialService.cs new file mode 100644 index 0000000..ae568d5 --- /dev/null +++ b/Borepin/Borepin/Service/Credentials/CredentialService.cs @@ -0,0 +1,34 @@ +using Borepin.Model; +using System; +using System.Threading.Tasks; +using Xamarin.Essentials; + +namespace Borepin.Service.Credentials +{ + public class CredentialService : ICredentialService + { + public async Task<string> GetPasswordAsync(Connection connection) + { + if (connection.AuthenticationTyp != AuthenticationTyp.PLAIN) + { + throw new ArgumentException("AuthenticationTyp is not PLAIN"); + } + + return await SecureStorage.GetAsync(string.Format("bffh_password_{0}_{1}", connection.Address.ToString(), connection.Username)); + } + + public async Task<bool> AddCredentialsAsync(Connection connection, string password) + { + await SecureStorage.SetAsync(string.Format("bffh_password_{0}_{1}", connection.Address.ToString(), connection.Username), password); + + return await Task.FromResult(true); + } + + public async Task<bool> RemoveCredentialsAsync(Connection connection) + { + SecureStorage.Remove(string.Format("bffh_password_{0}_{1}", connection.Address.ToString(), connection.Username)); + + return await Task.FromResult(true); + } + } +} diff --git a/Borepin/Borepin/Service/Credentials/CredentialsService.cs b/Borepin/Borepin/Service/Credentials/CredentialsService.cs deleted file mode 100644 index 8ce035f..0000000 --- a/Borepin/Borepin/Service/Credentials/CredentialsService.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Borepin.Model; -using System; -using System.Threading.Tasks; -using Xamarin.Essentials; - -namespace Borepin.Service.Credentials -{ - public class CredentialsService : ICredentialService - { - public async Task<HostCredentials> GetCredentialsAsync(Uri host) - { - string username = await SecureStorage.GetAsync(string.Format("bffh_username_{0}", host.ToString())); - string password = await SecureStorage.GetAsync(string.Format("bffh_password_{0}", host.ToString())); - - HostCredentials hc = new HostCredentials() { Host = host, Username = username, Password = password }; - - return await Task.FromResult(hc); - } - - public async Task<bool> LogCredentialsAsync(HostCredentials credentials) - { - await SecureStorage.SetAsync(string.Format("bffh_username_{0}", credentials.Host.ToString()), credentials.Username); - await SecureStorage.SetAsync(string.Format("bffh_password_{0}", credentials.Host.ToString()), credentials.Password); - - return await Task.FromResult(true); - } - } -} diff --git a/Borepin/Borepin/Service/Credentials/ICredentialService.cs b/Borepin/Borepin/Service/Credentials/ICredentialService.cs index b535d73..cc1027c 100644 --- a/Borepin/Borepin/Service/Credentials/ICredentialService.cs +++ b/Borepin/Borepin/Service/Credentials/ICredentialService.cs @@ -1,15 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using Borepin.Model; namespace Borepin.Service.Credentials { public interface ICredentialService { - Task<HostCredentials> GetCredentialsAsync(Uri host); + /// <summary> + /// Get Password for Connection from SecureStorage + /// </summary> + /// <param name="connection">address + username</param> + Task<string> GetPasswordAsync(Connection connection); - Task<bool> LogCredentialsAsync(HostCredentials credentials); + /// <summary> + /// Add Credentials to SecureStorage + /// </summary> + /// <param name="connection">address + username</param> + Task<bool> AddCredentialsAsync(Connection connection, string password); + + /// <summary> + /// Remove Credentials from SecureStorage + /// </summary> + /// <param name="connection"><address + username/param> + Task<bool> RemoveCredentialsAsync(Connection connection); } } diff --git a/Borepin/Borepin/Service/Instances/IInstanceService.cs b/Borepin/Borepin/Service/Instances/IInstanceService.cs deleted file mode 100644 index 7ca5806..0000000 --- a/Borepin/Borepin/Service/Instances/IInstanceService.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Borepin.Model; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace Borepin.Service.Instances -{ - public interface IInstanceService - { - Task<List<BFFHInstance>> GetBFFHInstances(); - - Task<bool> AddBFFHInstance(BFFHInstance instance); - Task<bool> UpdateBFFHInstance(BFFHInstance instance); - - Task<bool> RemoveBFFHInstance(BFFHInstance instance); - - Task<bool> RemoveAllBFFHInstances(); - } -} diff --git a/Borepin/Borepin/Service/Instances/InstanceService.cs b/Borepin/Borepin/Service/Instances/InstanceService.cs deleted file mode 100644 index 6dff4b9..0000000 --- a/Borepin/Borepin/Service/Instances/InstanceService.cs +++ /dev/null @@ -1,98 +0,0 @@ -using Borepin.Model; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Xamarin.Essentials; - -namespace Borepin.Service.Instances -{ - /// <summary> - /// Save connected BFFHInstances in Xamarin.Essentials.Preferences - /// </summary> - public class InstanceService : IInstanceService - { - private List<BFFHInstance> _BFFHInstance_List; - - public Task<List<BFFHInstance>> GetBFFHInstances() - { - List<BFFHInstance> bffhInstances = JsonConvert.DeserializeObject<List<BFFHInstance>>(Preferences.Get("bffhinstances", "[]")); - - _BFFHInstance_List = bffhInstances; - - return Task.FromResult(bffhInstances); - } - - public Task<bool> AddBFFHInstance(BFFHInstance instance) - { - if(_BFFHInstance_List == null) - { - GetBFFHInstances(); - } - - if(_BFFHInstance_List.Find(x => x.Address == instance.Address) != null) - { - return Task.FromResult(true); - } - - _BFFHInstance_List.Add(instance); - - SaveList(); - - return Task.FromResult(true); - } - - public Task<bool> UpdateBFFHInstance(BFFHInstance instance) - { - if (_BFFHInstance_List == null) - { - GetBFFHInstances(); - } - - if (_BFFHInstance_List.Find(x => x.Address == instance.Address) == null) - { - throw new ArgumentException("BFFH Instance does not exist"); - } - - foreach (BFFHInstance bffh in _BFFHInstance_List.Where(x => x.Address == instance.Address)) - { - bffh.Name = instance.Name; - bffh.Description = instance.Description; - } - - SaveList(); - - return Task.FromResult(true); - } - - private Task<bool> SaveList() - { - Preferences.Set("bffhinstances", JsonConvert.SerializeObject(_BFFHInstance_List)); - - return Task.FromResult(true); - } - - public Task<bool> RemoveBFFHInstance(BFFHInstance instance) - { - if (_BFFHInstance_List == null) - { - GetBFFHInstances(); - } - - _BFFHInstance_List.RemoveAll(x => x.Address == instance.Address); - - SaveList(); - - return Task.FromResult(true); - } - - public Task<bool> RemoveAllBFFHInstances() - { - Preferences.Remove("bffhinstances"); - _BFFHInstance_List.Clear(); - - return Task.FromResult(true); - } - } -} diff --git a/Borepin/Borepin/View/ServerListItemView.xaml b/Borepin/Borepin/View/ServerListItemView.xaml index 6aec04a..df05ca2 100644 --- a/Borepin/Borepin/View/ServerListItemView.xaml +++ b/Borepin/Borepin/View/ServerListItemView.xaml @@ -1,10 +1,12 @@ <?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" - x:Class="Borepin.View.ServerListItemView"> + x:Class="Borepin.View.ServerListItemView" + xmlns:pagemodel="clr-namespace:Borepin.PageModel"> <ContentView.Content> - <StackLayout> + <StackLayout Orientation="Horizontal"> <Label Text="{Binding Address}" /> - </StackLayout> + <Button Text="->" Command="{Binding Source={RelativeSource AncestorType={x:Type pagemodel:ServerListPageModel}}, Path=SelectInstanceCommand}" CommandParameter="{Binding .}" /> + </StackLayout> </ContentView.Content> </ContentView> \ No newline at end of file diff --git a/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs b/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs index 7e6b8aa..d9ec40e 100644 --- a/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs +++ b/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs @@ -5,9 +5,17 @@ namespace Borepin.ViewModel { public class ServerListItemViewModel : BindableBase { - public ServerListItemViewModel(BFFHInstance server) + public ServerListItemViewModel(Connection instance) { - Address = server.Address.ToString(); + _Instance = instance; + Address = instance.Address.ToString(); + } + + private Connection _Instance; + public Connection Instance + { + get => _Instance; + set => SetProperty(ref _Instance, value); } private string _Address;