From fcbbb28a341f2d188a3414c3cb7e663c850069c5 Mon Sep 17 00:00:00 2001 From: Maxime BATISTA Date: Wed, 10 May 2023 14:07:29 +0200 Subject: [PATCH] add UserNotifier for user notifications with a logger implementation --- App.xaml.cs | 5 +++-- ConnectAppShell.xaml.cs | 4 ++-- ConsoleUserNotifier.cs | 30 +++++++++++++++++++++++++++++ Controllers/ConnectionController.cs | 19 +++++++++++++++--- LocalEndpoint/AccountManager.cs | 3 +++ UserNotifier.cs | 18 +++++++++++++++++ 6 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 ConsoleUserNotifier.cs create mode 100644 UserNotifier.cs diff --git a/App.xaml.cs b/App.xaml.cs index 031cc02..a9fce9e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -7,14 +7,15 @@ using LocalEndpoint; public partial class App : Application, ConnectionObserver { - private IEndpoint endpoint = new LocalEndpoint(); + private IEndpoint Endpoint = new LocalEndpoint(); + private UserNotifier Notifier = new ConsoleUserNotifier(); public App() { InitializeComponent(); - Shell shell = new ConnectAppShell(this, endpoint.AccountManager); + Shell shell = new ConnectAppShell(this, Endpoint.AccountManager, Notifier); shell.GoToAsync("//Splash"); MainPage = shell; } diff --git a/ConnectAppShell.xaml.cs b/ConnectAppShell.xaml.cs index 8cc6953..84655c8 100644 --- a/ConnectAppShell.xaml.cs +++ b/ConnectAppShell.xaml.cs @@ -7,9 +7,9 @@ using ShoopNCook.Pages; public partial class ConnectAppShell : Shell { - public ConnectAppShell(ConnectionObserver observer, IAccountManager accounts) + public ConnectAppShell(ConnectionObserver observer, IAccountManager accounts, UserNotifier notifier) { - ConnectionController controller = new ConnectionController(observer, accounts); + ConnectionController controller = new ConnectionController(observer, accounts, notifier); InitializeComponent(); LoginPage.ContentTemplate = new DataTemplate(() => new LoginPage(controller)); RegisterPage.ContentTemplate = new DataTemplate(() => new RegisterPage(controller)); diff --git a/ConsoleUserNotifier.cs b/ConsoleUserNotifier.cs new file mode 100644 index 0000000..bcde5d5 --- /dev/null +++ b/ConsoleUserNotifier.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShoopNCook +{ + /// + /// A notice reporter implementation that prints in console the applications's user notices. + /// + public class ConsoleUserNotifier : + UserNotifier + { + public void Error(string message) + { + Console.WriteLine(" Error: " + message); + } + + public void Notice(string message) + { + Console.WriteLine(" Notice: " + message); + } + + public void Warn(string message) + { + Console.WriteLine(" Warn: " + message); + } + } +} diff --git a/Controllers/ConnectionController.cs b/Controllers/ConnectionController.cs index 8aa7803..d099efc 100644 --- a/Controllers/ConnectionController.cs +++ b/Controllers/ConnectionController.cs @@ -7,19 +7,32 @@ namespace ShoopNCook.Controllers { private readonly ConnectionObserver observer; private readonly IAccountManager accounts; - public ConnectionController(ConnectionObserver observer, IAccountManager accounts) { + private readonly UserNotifier notifier; + public ConnectionController(ConnectionObserver observer, IAccountManager accounts, UserNotifier notifier) { this.observer = observer; this.accounts = accounts; + this.notifier = notifier; } + public void Login(string email, string password) { - Account acc = accounts.Login(email, password); + Account? acc = accounts.Login(email, password); + if (acc == null) + { + notifier.Error("Email or password invalid."); + return; + } observer.OnAccountConnected(acc); } public void Register(string username, string email, string password) { - Account acc = accounts.Register(username, email, password); + Account? acc = accounts.Register(username, email, password); + if (acc == null) + { + notifier.Error("Invalid credentials."); + return; + } observer.OnAccountConnected(acc); } } diff --git a/LocalEndpoint/AccountManager.cs b/LocalEndpoint/AccountManager.cs index 5f79089..591e0a5 100644 --- a/LocalEndpoint/AccountManager.cs +++ b/LocalEndpoint/AccountManager.cs @@ -26,6 +26,9 @@ namespace LocalEndpoint public Account? Register(string email, string username, string password) { + if (email == null || username == null || password == null) + return null; + userAccount = new Account(new User(DEFAULT_ACCOUNT_IMAGE, username), email); userPassword = password; return userAccount; diff --git a/UserNotifier.cs b/UserNotifier.cs new file mode 100644 index 0000000..bfd4c07 --- /dev/null +++ b/UserNotifier.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShoopNCook +{ + public interface UserNotifier + + { + public void Notice(string message); + + public void Error(string message); + + public void Warn(string message); + } +}