From d5adeff27ade214ac7e33e476985a9332e080597 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Thu, 11 May 2023 09:52:42 +0200 Subject: [PATCH] start #31 --- MCTG/ConsoleApp/Menu/IMenu.cs | 22 ++++ MCTG/ConsoleApp/Menu/Menu.cs | 79 ++++++++++++ MCTG/ConsoleApp/Menu/SearcherRecipe.cs | 119 ++---------------- MCTG/ConsoleApp/Menu/Selector.cs | 32 +++++ MCTG/ConsoleApp/MenuManager.cs | 64 ++++++++++ MCTG/ConsoleApp/Program.cs | 2 +- .../Manager.cs => Managers/DataManager.cs} | 6 +- 7 files changed, 212 insertions(+), 112 deletions(-) create mode 100644 MCTG/ConsoleApp/Menu/IMenu.cs create mode 100644 MCTG/ConsoleApp/Menu/Menu.cs create mode 100644 MCTG/ConsoleApp/Menu/Selector.cs create mode 100644 MCTG/ConsoleApp/MenuManager.cs rename MCTG/Model/{Manager/Manager.cs => Managers/DataManager.cs} (89%) diff --git a/MCTG/ConsoleApp/Menu/IMenu.cs b/MCTG/ConsoleApp/Menu/IMenu.cs new file mode 100644 index 0000000..4f1aab2 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/IMenu.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal interface IMenu + { + bool WriteMode { get; set; } + StringBuilder InputStr { get; set; } + + void Display(); + void Update(); + void SelectNext(); + void SelectPrevious(); + void EnableWriteMode(); + void DisableWriteMode(); + void Return(); + } +} diff --git a/MCTG/ConsoleApp/Menu/Menu.cs b/MCTG/ConsoleApp/Menu/Menu.cs new file mode 100644 index 0000000..c7ede43 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/Menu.cs @@ -0,0 +1,79 @@ +using Model; +using System; +using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal abstract class Menu : IMenu + { + protected StringBuilder _screenDisplay; + protected readonly List> _selectList; + + public string Title { get; private set; } + public StringBuilder InputStr { get; set; } + public int CurrentLine { get; private set; } + public T CurrentSelected { get; private set; } + public bool WriteMode { get; set; } + + public Menu(string title, params Selector[] selections ) + { + if (selections == null || selections.Length == 0) + throw new ArgumentException("Error: a menu must contain at least 1 selection"); + + Title = title; + CurrentLine = 0; + CurrentSelected = selections[0].Item; + WriteMode = false; + _selectList = new List>(selections); + _screenDisplay = new StringBuilder(); + InputStr = new StringBuilder(); + } + + public virtual void Display() + { + Console.WriteLine(_screenDisplay); + } + + public virtual void Update() + { + _screenDisplay.AppendLine($"[ {Title} ]"); + _screenDisplay.AppendLine("-------------------------------------------\n"); + + foreach (Selector selector in _selectList) + { + if (selector.Equals(CurrentSelected)) + _screenDisplay.Append($"> "); + else + _screenDisplay.Append($" "); + + _screenDisplay.AppendLine($"{selector.Line}"); + } + } + + public void SelectNext() + { + CurrentSelected = _selectList[++CurrentLine].Item; + } + + public void SelectPrevious() + { + CurrentSelected = _selectList[--CurrentLine].Item + } + + public void EnableWriteMode() + { + WriteMode = true; + } + + public void DisableWriteMode() + { + WriteMode = false; + } + + public abstract void Return(); + } +} diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index 4e17512..a213e7d 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -12,114 +12,17 @@ namespace ConsoleApp.Menu /// /// An utility to find a recipe. /// - public class SearcherRecipe : SelectMenu - { - #region Attribute - private RecipeCollection _recipeOnSearch; - #endregion - - #region Properties - /// - /// A collection of recipe where the title contain the search string - /// - public RecipeCollection SearchResult { get; private set; } - - /// - /// The search string - /// - public string ResearchStr { get; set; } - #endregion - - /// - /// Constructor of the SearcherRecipe utility. - /// - /// The collection of recipe where to search - public SearcherRecipe(RecipeCollection recipeOnSearch) - { - _recipeOnSearch = recipeOnSearch; - SearchResult = _recipeOnSearch; - ResearchStr = ""; - } - - #region Methodes - /// - /// Launch a search by name request in the collection of Recipe with with a string. - /// - /// The string for search - /// True if the result of the search gave at least 1 element. False otherwise. - public bool ComputeSearch(string researchStr = "") - { - ResearchStr = researchStr; - SearchResult = _recipeOnSearch.ResearchByName(ResearchStr.ToLower()); - _maxLines = SearchResult.Count - 1; - - return SearchResult.Count > 0; - } - - public override void UpdateDisplay() - { - StringBuilder sb = new StringBuilder(); - sb.AppendLine("---------------------------------------------------------"); - sb.AppendLine($" Research: {ResearchStr}"); - sb.AppendLine("---------------------------------------------------------"); - for (int i = 0; i < SearchResult.Count; i++) - { - if (i == CurrentLine) - { - CurrentSelected = SearchResult[i]; - sb.Append($">"); - } - sb.AppendLine($" [ {SearchResult[i].Id} ]:\t{SearchResult[i].Title} "); - } - Console.Clear(); - Console.WriteLine(sb); - } - - /// - /// Launch and pilot the search menu. - /// - /// The collection of recipe where to search - /// The recipe selected - public static Recipe? ResearchOn(RecipeCollection recipeOnSearch) - { - SearcherRecipe sr = new SearcherRecipe(recipeOnSearch); - StringBuilder sb = new StringBuilder(); - sr.ComputeSearch(); - sr.UpdateDisplay(); - - ConsoleKeyInfo cki; - - do - { - cki = Console.ReadKey(true); - - switch (cki.Key) - { - case ConsoleKey.UpArrow: - sr.SelectPrevioustLine(); - break; - case ConsoleKey.DownArrow: - sr.SelectNextLine(); - break; - case ConsoleKey.Backspace: - if (sb.Length > 0) - { - sb.Remove(sb.Length - 1, 1); - sr.ComputeSearch(sb.ToString()); - } - break; - default: - sb.Append(cki.KeyChar); - sr.ComputeSearch(sb.ToString()); - break; - } - - sr.UpdateDisplay(); - - } while (cki.Key != ConsoleKey.Enter); - - return sr.CurrentSelected; - } + internal class SearcherRecipe : Menu + { + public SearcherRecipe(string title, params Selector[] selections) + : base(title, selections) + { } + + #region Methodes + public override void Return() + { + Console.WriteLine(CurrentSelected); + } #endregion } } diff --git a/MCTG/ConsoleApp/Menu/Selector.cs b/MCTG/ConsoleApp/Menu/Selector.cs new file mode 100644 index 0000000..9a398f4 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/Selector.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class Selector + { + private string _line = ""; + + public string Line { + get => _line; + private set + { + if (string.IsNullOrEmpty(value)) + _line = "no data"; + else + _line = value; + } + } + public T Item { get; private set; } + + public Selector(T item, string line = "") + { + Line = line; + Item = item; + } + } +} diff --git a/MCTG/ConsoleApp/MenuManager.cs b/MCTG/ConsoleApp/MenuManager.cs new file mode 100644 index 0000000..4bdfa76 --- /dev/null +++ b/MCTG/ConsoleApp/MenuManager.cs @@ -0,0 +1,64 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp +{ + internal class MenuManager + { + public DataManager DataManager { get; private set; } + public Stack MenuCallStack { get; set; } + + public MenuManager(DataManager dataManager, Menu.IMenu firstMenu) + { + DataManager = dataManager; + + MenuCallStack = new Stack(); + MenuCallStack.Append(firstMenu); + } + + public void Loop() + { + ConsoleKeyInfo cki; + Menu.IMenu menuOnHead; + do + { + menuOnHead = MenuCallStack.Peek(); + menuOnHead.Update(); + menuOnHead.Display(); + + cki = Console.ReadKey(true); + switch (cki.Key) + { + case ConsoleKey.DownArrow: + menuOnHead.SelectNext(); + break; + case ConsoleKey.UpArrow: + menuOnHead.SelectPrevious(); + break; + case ConsoleKey.Enter: + menuOnHead.Return(); + MenuCallStack.Pop(); + break; + case ConsoleKey.R: + if (menuOnHead.WriteMode) + menuOnHead.DisableWriteMode(); + else + menuOnHead.EnableWriteMode(); + break; + default: + if (menuOnHead.WriteMode) + { + menuOnHead.InputStr.Append(cki.KeyChar); + } + break; + } + + } while (MenuCallStack.Count > 0); + + } + } +} diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index ac9a1e9..ebb65b1 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -20,7 +20,7 @@ RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals(" if (allRecipe == null) throw new ArgumentException("Load AllRecipe in stub: can't find 'All'."); -Manager manager = new Manager(allRecipe); +DataManager manager = new DataManager(allRecipe); Recipe? ret = SearcherRecipe.ResearchOn(allRecipe); Console.WriteLine(ret); diff --git a/MCTG/Model/Manager/Manager.cs b/MCTG/Model/Managers/DataManager.cs similarity index 89% rename from MCTG/Model/Manager/Manager.cs rename to MCTG/Model/Managers/DataManager.cs index 5c4a6a7..02768d1 100644 --- a/MCTG/Model/Manager/Manager.cs +++ b/MCTG/Model/Managers/DataManager.cs @@ -9,7 +9,7 @@ namespace Model /// /// Manager of the model. Here is stoked all the recipes, the users, etc... /// - public class Manager + public class DataManager { /// /// A collection of all the recipe loaded in the app. @@ -19,7 +19,7 @@ namespace Model /// /// The constructor of the manager. /// - public Manager() + public DataManager() { AllRecipes = new RecipeCollection(description: "All Recipes"); } @@ -28,7 +28,7 @@ namespace Model /// The constructor of the manager. /// /// A list of loaded recipes - public Manager(RecipeCollection allRecipes) + public DataManager(RecipeCollection allRecipes) { AllRecipes = new RecipeCollection( description: "All Recipes",