diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index c200562..5094043 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -1,16 +1,14 @@ -// See https://aka.ms/new-console-template for more information - -using ConsoleApp; +using ConsoleApp; +using Model; using ConsoleApp.Menu; using DataPersistence; -using Model; using System.Linq; -using System.Text; - -Console.WriteLine("Hello, World!\n\n"); - - -// TESTS: +using System.Text; + + + +Console.WriteLine("Hello, World!\n\n"); + DataManager dataMgr = new DataManager(new Stubs()); //DataManager dataMgr = new DataManager(new DataContractXML()); //DataManager dataMgr = new DataManager(new DataContractJSON()); @@ -39,10 +37,10 @@ User user = dataMgr.GetUsers().Last(); //rc[1].AddReview(new Review(user, 2, "Mais celle-ci oui !")); dataMgr.Save(); - + MenuManager menuMgr = new MenuManager(dataMgr); - + menuMgr.Loop(); -Console.WriteLine(passwordManager.VerifyPassword(user.Password, "pamigos")); Console.ReadKey(); + diff --git a/MCTG/DataPersistence/Stubs.cs b/MCTG/DataPersistence/Stubs.cs index 47dc9bb..da7378a 100644 --- a/MCTG/DataPersistence/Stubs.cs +++ b/MCTG/DataPersistence/Stubs.cs @@ -14,8 +14,6 @@ namespace DataPersistence { public Dictionary> Load() { - PasswordManager passwordManager = new PasswordManager(); - Dictionary> data = new Dictionary> { { @@ -93,45 +91,46 @@ namespace DataPersistence }) }) #endregion - }, + }, { - #region Data: User - nameof(User), + #region Data: User + nameof(User), new List(new[] { new User( name: "Admin", surname: "Admin", mail: "admin@mctg.fr", - password: passwordManager.HashPassword("admin")), + password: "admin"), new User( name: "Pedros", surname: "Amigos", mail: "pedrosamigos@hotmail.com", - password: passwordManager.HashPassword("pamigos")) + password: "pamigos") + }) #endregion - } - }; - - return data; - } - - #region Not supported methods - public void Save(Dictionary> elements) - { - throw new NotSupportedException(); - } - - public void Export(T obj, string pathToExport) where T : class - { - throw new NotSupportedException(); - } - - public KeyValuePair Import(string pathToImport) where T : class - { - throw new NotSupportedException(); - } - #endregion - } -} + } + }; + + return data; + } + + #region Not supported methods + public void Save(Dictionary> elements) + { + throw new NotSupportedException(); + } + + public void Export(T obj, string pathToExport) where T : class + { + throw new NotSupportedException(); + } + + public KeyValuePair Import(string pathToImport) where T : class + { + throw new NotSupportedException(); + } + #endregion + } +} diff --git a/MCTG/Model/Recipes/Review.cs b/MCTG/Model/Recipes/Review.cs index b98d498..d560d23 100644 --- a/MCTG/Model/Recipes/Review.cs +++ b/MCTG/Model/Recipes/Review.cs @@ -66,8 +66,7 @@ namespace Model } public Review(int stars, string content) - : this(new User("admin", "admin", "admin@mctg.fr", new PasswordManager().HashPassword("admin")), - null, stars, content) + : this(new User(), null, stars, content) { } diff --git a/MCTG/Model/User/User.cs b/MCTG/Model/User/User.cs index 22ed1ba..27243f9 100644 --- a/MCTG/Model/User/User.cs +++ b/MCTG/Model/User/User.cs @@ -1,3 +1,4 @@ + using System; using System.Collections.Generic; using System.Collections; @@ -7,6 +8,7 @@ using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using System.Text; using System.Runtime.Serialization; +using System.ComponentModel; namespace Model { @@ -15,7 +17,7 @@ namespace Model /// This user can login with an Id and a password /// [DataContract(Name = "user")] - public class User : IEquatable + public class User : IEquatable , INotifyPropertyChanged { #region Private Attributes @@ -25,6 +27,8 @@ namespace Model [DataMember] private string picture = ""; [DataMember] private int password ; [DataMember] private List priorities; + + public event PropertyChangedEventHandler? PropertyChanged; #endregion #region Properties @@ -36,13 +40,11 @@ namespace Model public string Name { get { return name; } - private set + set { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException("Impossible d'avoir un champ Nom vide!"); - } + name = value; + OnPropertyChanged(); } } @@ -53,13 +55,11 @@ namespace Model public string Surname { get { return surname; } - private set + set { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException("Impossible d'avoir un champ Prénom vide!"); - } + surname = value; + OnPropertyChanged(); } } @@ -129,6 +129,21 @@ namespace Model throw new NotImplementedException(); } + + protected void OnPropertyChanged ([CallerMemberName] string? propertyName = null) + { + if (PropertyChanged != null) + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public override string ToString() + { + return $"{Name} {Surname}"; + } + + public IPasswordManager psswMgr { get; private set; } + + #endregion #region Constructors @@ -136,15 +151,18 @@ namespace Model /// /// Construtors of user. /// - /// The _name of the user - /// The surname of the user - /// The user needs an email to login. - public User(string name, string surname, string mail, int password) + /// The name of the user + /// The surname of the user + /// The user needs an email to login. + /// The password of the new user. + /// The password manager to manage the user password. + public User(string name, string surname, string mail, string password, IPasswordManager passwordManager ) { Name = name; Surname = surname; Mail = mail; - Password = password; + psswMgr = passwordManager; + Password = psswMgr.HashPassword(password); priorities = new List { Priority.Gourmet, Priority.Economic, @@ -155,8 +173,40 @@ namespace Model } - + /// + /// + /// + public User(string name, string surname, string mail, string password) + : this(name, surname,mail, password, new PasswordManager()) + { + + } + + /// + /// + /// + public User() + : this("John", "Doe", "truc@gmail.com", "mdp") + { + + } + + /// + /// + /// + public User (User user) + { + Name = user.Name; + Surname = user.Surname; + Mail = user.Mail; + psswMgr = user.psswMgr; + Password = user.Password; + priorities = user.Priorities; + ProfilPict = user.ProfilPict; + } + #endregion } } + diff --git a/MCTG/Tests/Model_UnitTests/test_unit_user.cs b/MCTG/Tests/Model_UnitTests/test_unit_user.cs index 5df6579..02cbcdb 100644 --- a/MCTG/Tests/Model_UnitTests/test_unit_user.cs +++ b/MCTG/Tests/Model_UnitTests/test_unit_user.cs @@ -13,7 +13,7 @@ namespace Model_UnitTests public void TestConstructUser() { PasswordManager passwordManager = new PasswordManager(); - User user = new User("Bob", "Dylan", "bd@gmail.com", passwordManager.HashPassword("bobby")); + User user = new User("Bob", "Dylan", "bd@gmail.com", "bobby"); Assert.Equal("Bob", user.Name); Assert.Equal("Dylan", user.Surname); Assert.Equal("bd@gmail.com", user.Mail); diff --git a/MCTG/Views/App.xaml.cs b/MCTG/Views/App.xaml.cs index 304aed5..be9eef9 100644 --- a/MCTG/Views/App.xaml.cs +++ b/MCTG/Views/App.xaml.cs @@ -1,38 +1,44 @@ -#if WINDOWS -using Microsoft.UI; -using Microsoft.UI.Windowing; -using Windows.Graphics; -#endif - -namespace Views -{ - public partial class App : Application - { - const int WindowWidth = 1200; - const int WindowHeight = 800; - - public App() - { - InitializeComponent(); - - Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) => - { -#if WINDOWS - var mauiWindow = handler.VirtualView; - var nativeWindow = handler.PlatformView; - nativeWindow.Activate(); - IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow); - WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle); - AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId); - appWindow.Resize(new SizeInt32(WindowWidth, WindowHeight)); -#endif - }); - - - /* - Comment(ctrl-k + ctrl-c)/Uncomment(ctrl-k + ctrl-u) to change page - */ - UserAppTheme = AppTheme.Light; - MainPage = new MyProfil(); - //MainPage = new MyPosts(); - } - } -} +#if WINDOWS +using Microsoft.UI; +using Microsoft.UI.Windowing; +using Windows.Graphics; +#endif + +using DataPersistence; +using Model; + +namespace Views +{ + public partial class App : Application + { + public DataManager DataMgr { get; private set; } = new DataManager(new Stubs()); + public User user { get; set; } + const int WindowWidth = 1200; + const int WindowHeight = 800; + + public App() + { + user = DataMgr.Data[nameof(User)].Cast().Last(); + InitializeComponent(); + + Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) => + { +#if WINDOWS + var mauiWindow = handler.VirtualView; + var nativeWindow = handler.PlatformView; + nativeWindow.Activate(); + IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow); + WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle); + AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId); + appWindow.Resize(new SizeInt32(WindowWidth, WindowHeight)); +#endif + }); + + + /* - Comment(ctrl-k + ctrl-c)/Uncomment(ctrl-k + ctrl-u) to change page - */ + UserAppTheme = AppTheme.Light; + MainPage = new AddRecipe(); + //MainPage = new MyPosts(); + } + } +} diff --git a/MCTG/Views/ContainerFlyout.xaml b/MCTG/Views/ContainerFlyout.xaml index 77a742e..07193e3 100644 --- a/MCTG/Views/ContainerFlyout.xaml +++ b/MCTG/Views/ContainerFlyout.xaml @@ -28,11 +28,19 @@ Style="{StaticResource button2}" IsVisible="{Binding IsNotConnected, Source={x:Reference fl}}" IsEnabled="{Binding IsNotConnected, Source={x:Reference fl}}"/> -