diff --git a/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs b/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs new file mode 100644 index 0000000..2e8e471 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal abstract partial class Entry + { + public class EntryStep + { + private Type _entryType; + + public string Description { get; private set; } + public string? Input { get; private set; } + + internal EntryStep(string description, Type type) + { + Description = description; + Input = null; + _entryType = type; + } + + public object GetEntry() + { + Input = Console.ReadLine(); + + if (Input is null) + throw new ArgumentException("Error: input is null."); + + try + { + if (_entryType == typeof(string)) + return Input; + if (_entryType == typeof(int)) + return Int32.Parse(Input); + if (_entryType == typeof(DateTime)) + return DateTime.Parse(Input); + } + catch (FormatException fe) + { + Console.Error.WriteLine(fe); + } + + throw new NotImplementedException("Error: parse of this type is not implemented."); + } + } + } +} diff --git a/MCTG/ConsoleApp/Menu/Entry.cs b/MCTG/ConsoleApp/Menu/Entry.cs new file mode 100644 index 0000000..21f323e --- /dev/null +++ b/MCTG/ConsoleApp/Menu/Entry.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal abstract partial class Entry : Menu + { + private List> _allSelectors; + + protected Entry(string title) : base(title) + { + _allSelectors = _selectList; + } + + protected override List> SearchInSelection() + { + return _allSelectors; + } + + public override IMenu? Return() + { + if (CurrentSelected == null) + throw new Exception("Error: CurrentSelected is null."); + + + return null; + } + + + } +} diff --git a/MCTG/ConsoleApp/Menu/IMenu.cs b/MCTG/ConsoleApp/Menu/IMenu.cs index 0816923..a9e15fa 100644 --- a/MCTG/ConsoleApp/Menu/IMenu.cs +++ b/MCTG/ConsoleApp/Menu/IMenu.cs @@ -19,6 +19,7 @@ namespace ConsoleApp.Menu void EnableWriteMode(); void DisableWriteMode(); void ToggleWriteMode(); + void WriteMenuMode(ConsoleKeyInfo cki); IMenu? Return(); } } diff --git a/MCTG/ConsoleApp/Menu/Menu.cs b/MCTG/ConsoleApp/Menu/Menu.cs index ae0deba..1f38516 100644 --- a/MCTG/ConsoleApp/Menu/Menu.cs +++ b/MCTG/ConsoleApp/Menu/Menu.cs @@ -13,6 +13,8 @@ namespace ConsoleApp.Menu where T : notnull { protected StringBuilder _screenDisplay; + + protected List> _allSelectors = new List>(); protected List> _selectList = new List>(); public string Title { get; private set; } @@ -53,13 +55,48 @@ namespace ConsoleApp.Menu return; } - _selectList = selections.ToList(); - CurrentSelected = _selectList[0].Item; + _allSelectors = selections.ToList(); + _selectList = _allSelectors; + CurrentSelected = _allSelectors[0].Item; } public abstract IMenu? Return(); - protected abstract List> SearchInSelection(); + protected virtual List> SearchInSelection() + { + if (_allSelectors is null) + throw new Exception("Error: _allSelector is null."); + + return _allSelectors.FindAll(x => + x.Line.ToLower().Contains(InputStr.ToString().ToLower())); + } + + public void WriteMenuMode(ConsoleKeyInfo cki) + { + if (!WriteMode && cki.Key == ConsoleKey.R) + { + EnableWriteMode(); + return; + } + + if (WriteMode) + { + if (cki.Key == ConsoleKey.Escape) + { + DisableWriteMode(); + InputStr.Clear(); + return; + } + + if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0) + { + InputStr.Remove(InputStr.Length - 1, 1); + return; + } + + InputStr.Append(cki.KeyChar); + } + } public void Update() { diff --git a/MCTG/ConsoleApp/Menu/PlainText.cs b/MCTG/ConsoleApp/Menu/PlainText.cs index fa67426..3495ac7 100644 --- a/MCTG/ConsoleApp/Menu/PlainText.cs +++ b/MCTG/ConsoleApp/Menu/PlainText.cs @@ -29,5 +29,6 @@ namespace ConsoleApp.Menu public void SelectPrevious() { return; } public void ToggleWriteMode() { return; } public void Update() { return; } + public void WriteMenuMode(ConsoleKeyInfo cki) { return; } } } diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index c517763..68abaef 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -19,21 +19,18 @@ namespace ConsoleApp.Menu public SearcherRecipe(RecipeCollection recipeCollection) : base("Search recipe") { _recipeCollectionOnSearch = recipeCollection; - _selectList = SearchInSelection(); + _allSelectors = ConvertRecipeCollectionInSelectors(); + _selectList = _allSelectors; } #region Methodes - protected override List> SearchInSelection() + private List> ConvertRecipeCollectionInSelectors() { - RecipeCollection newRCSearchView = - _recipeCollectionOnSearch.ResearchByName(InputStr.ToString()); - List> newSelectors = new List>(); - foreach (Recipe recipe in newRCSearchView) + foreach (Recipe recipe in _recipeCollectionOnSearch) { newSelectors.Add(new Selector(recipe, $"[{recipe.Id}]: {recipe.Title}")); } - return newSelectors; } diff --git a/MCTG/ConsoleApp/MenuManager.cs b/MCTG/ConsoleApp/MenuManager.cs index 7eef410..3131d5c 100644 --- a/MCTG/ConsoleApp/MenuManager.cs +++ b/MCTG/ConsoleApp/MenuManager.cs @@ -20,33 +20,6 @@ namespace ConsoleApp MenuCallStack.Push(firstMenu); } - private void WriteMenuMode(Menu.IMenu menu, ConsoleKeyInfo cki) - { - if (!menu.WriteMode && cki.Key == ConsoleKey.R) - { - menu.EnableWriteMode(); - return; - } - - if (menu.WriteMode) - { - if (cki.Key == ConsoleKey.Escape) - { - menu.DisableWriteMode(); - menu.InputStr.Clear(); - return; - } - - if (cki.Key == ConsoleKey.Backspace && menu.InputStr.Length > 0) - { - menu.InputStr.Remove(menu.InputStr.Length - 1, 1); - return; - } - - menu.InputStr.Append(cki.KeyChar); - } - } - public void Loop() { ConsoleKeyInfo cki; @@ -75,7 +48,7 @@ namespace ConsoleApp MenuCallStack.Pop(); break; default: - WriteMenuMode(menuOnHead, cki); + menuOnHead.WriteMenuMode(cki); break; } } while (MenuCallStack.Count > 0); diff --git a/MCTG/Model/Recipes/RecipeCollection.cs b/MCTG/Model/Recipes/RecipeCollection.cs index a532edb..a1d8393 100644 --- a/MCTG/Model/Recipes/RecipeCollection.cs +++ b/MCTG/Model/Recipes/RecipeCollection.cs @@ -89,7 +89,7 @@ namespace Model public override int GetHashCode() { - return this.GetHashCode(); + return Description.GetHashCode(); } public override string ToString()