Added: ConnectionService and simple ServerPage

This commit is contained in:
TheJoKlLa 2021-01-29 17:57:30 +01:00
parent 658b753673
commit 9a50690667
24 changed files with 501 additions and 307 deletions

View File

@ -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());
}
}
}

View File

@ -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>

View File

@ -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;
}
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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();
}
}
}

View File

@ -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)

View File

@ -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");

View File

@ -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;

View File

@ -26,8 +26,6 @@ namespace Borepin.PageModel
private async void GoToMachineCommandExecuted(object obj)
{
_NavigationService.GetNavigationUriPath();
Machine machine = obj as Machine;
NavigationParameters navigationParams = new NavigationParameters();

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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>

View File

@ -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;