diff --git a/MCTG/ConsoleApp/Menu/ConnectionMenu.cs b/MCTG/ConsoleApp/Menu/ConnectionMenu.cs index ef410d2..30759be 100644 --- a/MCTG/ConsoleApp/Menu/ConnectionMenu.cs +++ b/MCTG/ConsoleApp/Menu/ConnectionMenu.cs @@ -1,25 +1,50 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using ConsoleApp.Menu.Core; +using Model.Managers; -namespace ConsoleApp.Menu -{ - internal class ConnectionMenu : Entry - { - public ConnectionMenu() - : base("Connection", - new Entry.EntryStep("Username: ", typeof(string)), - new Entry.EntryStep("Password: ", typeof(string))) - { } - - public override IMenu? Return() - { - string username = _selectList[0].Item.Input; - string password = _selectList[1].Item.Input; - return new PlainText($"User: {username} connected with password: {password}"); - } - } -} +namespace ConsoleApp.Menu +{ + internal class ConnectionMenu : Entry + { + private MasterManager _masterMgr; + private bool _wrongInput = false; + + public ConnectionMenu(MasterManager masterManager) + : base("Connection", + new Entry.EntryStep("Mail: ", typeof(string)), + new Entry.EntryStep("Password: ", typeof(string), true)) + { + _masterMgr = masterManager; + } + + public override void Display() + { + base.Display(); + + if (_wrongInput) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Wrong input..."); + Console.ResetColor(); + } + } + + public override IMenu? Return() + { + string mail = _selectList[0].Item.Input; + string password = _selectList[1].Item.Input; + + if (!_masterMgr.Login(mail, password)) + { + _wrongInput = true; + return this; + } + else + return null; + } + } +} diff --git a/MCTG/ConsoleApp/Menu/Core/Entry.EntryStep.cs b/MCTG/ConsoleApp/Menu/Core/Entry.EntryStep.cs index 4193cb3..c4a57fb 100644 --- a/MCTG/ConsoleApp/Menu/Core/Entry.EntryStep.cs +++ b/MCTG/ConsoleApp/Menu/Core/Entry.EntryStep.cs @@ -25,6 +25,11 @@ namespace ConsoleApp.Menu.Core /// Contain the input gave by the menu. /// public string Input { get; internal set; } + + /// + /// Define whether the input need to be hidden. Useful for password. + /// + public bool Hidden { get; private set; } #endregion #region Constructors @@ -33,11 +38,12 @@ namespace ConsoleApp.Menu.Core /// /// The text generally placed before the input in the menu. /// The type of the returned input. - public EntryStep(string description, Type type) + public EntryStep(string description, Type type, bool hidden = false) { Description = description; Input = ""; _entryType = type; + Hidden = hidden; } #endregion diff --git a/MCTG/ConsoleApp/Menu/Core/Entry.cs b/MCTG/ConsoleApp/Menu/Core/Entry.cs index c1702d3..b2f816a 100644 --- a/MCTG/ConsoleApp/Menu/Core/Entry.cs +++ b/MCTG/ConsoleApp/Menu/Core/Entry.cs @@ -1,120 +1,124 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using ConsoleApp.Menu.Core; -namespace ConsoleApp.Menu.Core -{ - /// - /// Define an Entry menu. - ///
It allows you to navigate through the entries and completes them with a console input. - ///
- internal abstract partial class Entry : Menu - { - #region Attributes & Properties - private List _steps; - #endregion - - #region Constructors - /// - /// Constructor of the entry menu, based on the Menu constructor. - /// - /// The title of this menu. - /// All the entries of this menu. - protected Entry(string title, params EntryStep[] entrySteps) : base(title) - { - _steps = entrySteps.ToList(); - _allSelectors = ConvertEntryStepsInSelector(); - _selectList = _allSelectors; - } - #endregion - - #region Methods - private List> ConvertEntryStepsInSelector() - { - List> newSelectors = new List>(); - foreach (EntryStep step in _steps) - { - newSelectors.Add(new Selector(step, step.Description)); - } - return newSelectors; - } - #endregion - - #region IMenu implementation - public override void WriteMenuMode(ConsoleKeyInfo cki) - { - if (!WriteMode && cki.Key == ConsoleKey.R) - { - EnableWriteMode(); - return; - } - - if (WriteMode) - { - if (cki.Key == ConsoleKey.Escape) - { - if (CurrentSelected is null) - throw new ArgumentNullException("CurrentSelected"); - - CurrentSelected.Input = InputStr.ToString(); - DisableWriteMode(); - InputStr.Clear(); - return; - } - - if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0) - { - InputStr.Remove(InputStr.Length - 1, 1); - return; - } - - InputStr.Append(cki.KeyChar); - } - } - - public override void Update() - { - if (_selectList.Count == 0) - { - CurrentSelected = default; - return; - } - CurrentSelected = _selectList[CurrentLine].Item; - } - - public override void Display() - { - _screenDisplay.Clear(); - Console.Clear(); - - _screenDisplay.AppendLine($"[ {Title} ]"); - _screenDisplay.AppendLine("-------------------------------------------"); - - for (int i = 0; i < _selectList.Count; i++) - { - if (CurrentLine == i) - _screenDisplay.Append($"> "); - else - _screenDisplay.Append($" "); - - _screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}"); - - if (CurrentLine == i && WriteMode) - _screenDisplay.Append(InputStr); - - _screenDisplay.AppendLine(); - } - - if (_selectList.Count == 0) - _screenDisplay.AppendLine("Empty..."); - - _screenDisplay.AppendLine( - "\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode"); - Console.WriteLine(_screenDisplay); - } - #endregion - } -} +namespace ConsoleApp.Menu.Core +{ + /// + /// Define an Entry menu. + ///
It allows you to navigate through the entries and completes them with a console input. + ///
+ internal abstract partial class Entry : Menu + { + #region Attributes & Properties + private List _steps; + #endregion + + #region Constructors + /// + /// Constructor of the entry menu, based on the Menu constructor. + /// + /// The title of this menu. + /// All the entries of this menu. + protected Entry(string title, params EntryStep[] entrySteps) + : base(title) + { + _steps = entrySteps.ToList(); + _allSelectors = ConvertEntryStepsInSelector(); + _selectList = _allSelectors; + } + #endregion + + #region Methods + private List> ConvertEntryStepsInSelector() + { + List> newSelectors = new List>(); + foreach (EntryStep step in _steps) + { + newSelectors.Add(new Selector(step, step.Description)); + } + return newSelectors; + } + #endregion + + #region IMenu implementation + public override void WriteMenuMode(ConsoleKeyInfo cki) + { + if (!WriteMode && cki.Key == ConsoleKey.R) + { + EnableWriteMode(); + return; + } + + if (WriteMode) + { + if (cki.Key == ConsoleKey.Escape) + { + if (CurrentSelected is null) + throw new ArgumentNullException("CurrentSelected"); + + CurrentSelected.Input = InputStr.ToString(); + DisableWriteMode(); + InputStr.Clear(); + return; + } + + if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0) + { + InputStr.Remove(InputStr.Length - 1, 1); + return; + } + + if (CurrentSelected is not null && CurrentSelected.Hidden) + InputStr.Append('*'); + else + InputStr.Append(cki.KeyChar); + } + } + + public override void Update() + { + if (_selectList.Count == 0) + { + CurrentSelected = default; + return; + } + CurrentSelected = _selectList[CurrentLine].Item; + } + + public override void Display() + { + _screenDisplay.Clear(); + Console.Clear(); + + _screenDisplay.AppendLine($"[ {Title} ]"); + _screenDisplay.AppendLine("-------------------------------------------"); + + for (int i = 0; i < _selectList.Count; i++) + { + if (CurrentLine == i) + _screenDisplay.Append($"> "); + else + _screenDisplay.Append($" "); + + _screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}"); + + if (CurrentLine == i && WriteMode) + _screenDisplay.Append(InputStr); + + _screenDisplay.AppendLine(); + } + + if (_selectList.Count == 0) + _screenDisplay.AppendLine("Empty..."); + + _screenDisplay.AppendLine( + "\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode"); + Console.WriteLine(_screenDisplay); + } + #endregion + } +} diff --git a/MCTG/ConsoleApp/Menu/MainMenu.cs b/MCTG/ConsoleApp/Menu/MainMenu.cs index bdd75db..1800670 100644 --- a/MCTG/ConsoleApp/Menu/MainMenu.cs +++ b/MCTG/ConsoleApp/Menu/MainMenu.cs @@ -22,8 +22,8 @@ namespace ConsoleApp.Menu new SearcherRecipe(masterManager.DataMgr.GetRecipes("search")), "Recipe search")); _allSelectors.Add(MasterManager.CurrentConnectedUser is null ? - new Selector(new ConnectionMenu(), "Connection") - : new Selector(new PlainText("User profile"), "User profile")); + new Selector(new ConnectionMenu(masterManager), "Connection") + : new Selector(new ProfileMenu(masterManager), "User profile")); } protected override List> SearchInSelection() diff --git a/MCTG/ConsoleApp/Menu/ProfileMenu.cs b/MCTG/ConsoleApp/Menu/ProfileMenu.cs new file mode 100644 index 0000000..5e0d8fa --- /dev/null +++ b/MCTG/ConsoleApp/Menu/ProfileMenu.cs @@ -0,0 +1,24 @@ +using ConsoleApp.Menu.Core; +using Model.Managers; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class ProfileMenu : Menu + { + public ProfileMenu(MasterManager masterManager) + : base("Profile") + { + _allSelectors.Add(new Selector( + new SearcherRecipe(new RecipeCollection("My recipes", + masterManager.DataMgr.GetRecipes().Where(r => r.Author == MasterManager.CurrentConnectedUser).ToArray())), + "My recipes")); + + } + } +} diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index 1a4e3b9..c7937ca 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -9,8 +9,8 @@ using Model.Managers; Console.WriteLine("Hello, World!\n\n"); MasterManager masterMgr = new MasterManager(new Stubs()); -//MasterMgr masterMgr = new MasterMgr(new DataContractXML()); -//MasterMgr masterMgr = new MasterMgr(new DataContractJSON()); +//_masterMgr masterMgr = new _masterMgr(new DataContractXML()); +//_masterMgr masterMgr = new _masterMgr(new DataContractJSON()); masterMgr.DataMgr.Serializer = new DataContractXML(); //masterMgr.Serializer = new DataContractJSON();