select recipe on screen work

pull/38/head
Alexandre AGOSTINHO 2 years ago
parent d76ddfe7e5
commit 2df795a347

@ -12,15 +12,26 @@ namespace ConsoleApp.Menu
where T : notnull where T : notnull
{ {
protected StringBuilder _screenDisplay; protected StringBuilder _screenDisplay;
protected readonly List<Selector<T>>? _selectList; protected List<Selector<T>> _selectList = new List<Selector<T>>();
public string Title { get; private set; } public string Title { get; private set; }
public StringBuilder InputStr { get; set; } public StringBuilder InputStr { get; set; }
public int CurrentLine { get; private set; }
public T? CurrentSelected { get; private set; } private int _currentLine;
public int CurrentLine {
get => _currentLine;
private set
{
_currentLine = value;
if (_currentLine < 0) _currentLine = 0;
if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count-1;
}
}
public T? CurrentSelected { get; protected set; }
public bool WriteMode { get; set; } public bool WriteMode { get; set; }
private Menu(string title) protected Menu(string title)
{ {
Title = title; Title = title;
CurrentLine = 0; CurrentLine = 0;
@ -29,7 +40,7 @@ namespace ConsoleApp.Menu
InputStr = new StringBuilder(); InputStr = new StringBuilder();
} }
public Menu(string title, params Selector<T>[] selections ) : this(title) protected Menu(string title, params Selector<T>[] selections ) : this(title)
{ {
if (selections == null || selections.Length == 0) if (selections == null || selections.Length == 0)
throw new ArgumentException("Error: a menu must contain at least 1 selection"); throw new ArgumentException("Error: a menu must contain at least 1 selection");
@ -40,6 +51,7 @@ namespace ConsoleApp.Menu
public virtual void Display() public virtual void Display()
{ {
Console.Clear();
Console.WriteLine(_screenDisplay); Console.WriteLine(_screenDisplay);
} }
@ -48,14 +60,14 @@ namespace ConsoleApp.Menu
_screenDisplay.AppendLine($"[ {Title} ]"); _screenDisplay.AppendLine($"[ {Title} ]");
_screenDisplay.AppendLine("-------------------------------------------\n"); _screenDisplay.AppendLine("-------------------------------------------\n");
foreach (Selector<T> selector in _selectList) for (int i = 0; i < _selectList.Count; i++)
{ {
if (selector.Equals(CurrentSelected)) if (CurrentLine == i)
_screenDisplay.Append($"> "); _screenDisplay.Append($"> ");
else else
_screenDisplay.Append($" "); _screenDisplay.Append($" ");
_screenDisplay.AppendLine($"{selector.Line}"); _screenDisplay.AppendLine($"{_selectList[i].Line}");
} }
} }

@ -14,19 +14,13 @@ namespace ConsoleApp.Menu
/// </summary> /// </summary>
internal class SearcherRecipe : Menu<Recipe> internal class SearcherRecipe : Menu<Recipe>
{ {
public SearcherRecipe() : base("Search recipe") public SearcherRecipe(RecipeCollection recipeCollection) : base("Search recipe")
{ {
}
public static SearcherRecipe SearchInRecipeCollection(string title, RecipeCollection recipeCollection)
{
Dictionary<Recipe, string> recipesSelectors = new Dictionary<Recipe, string>();
foreach (Recipe recipe in recipeCollection) foreach (Recipe recipe in recipeCollection)
{ {
recipesSelectors.Add(recipe, recipe.Title); _selectList.Add(new Selector<Recipe>(recipe, $"[{recipe.Id}]: {recipe.Title}"));
} }
return new SearcherRecipe(title, CreateSelection(recipesSelectors)); CurrentSelected = _selectList[0].Item;
} }
#region Methodes #region Methodes

@ -17,7 +17,7 @@ namespace ConsoleApp
DataManager = dataManager; DataManager = dataManager;
MenuCallStack = new Stack<Menu.IMenu>(); MenuCallStack = new Stack<Menu.IMenu>();
MenuCallStack.Append(firstMenu); MenuCallStack.Push(firstMenu);
} }
public void Loop() public void Loop()

@ -20,9 +20,10 @@ RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("
if (allRecipe == null) if (allRecipe == null)
throw new ArgumentException("Load AllRecipe in stub: can't find 'All'."); throw new ArgumentException("Load AllRecipe in stub: can't find 'All'.");
DataManager manager = new DataManager(allRecipe); DataManager dataMgr = new DataManager(allRecipe);
MenuManager menuManager = new MenuManager(manager, ConsoleApp.Menu.SearcherRecipe.SearchInRecipeCollection("search recipe", manager.AllRecipes)); MenuManager menuMgr = new MenuManager(dataMgr, new SearcherRecipe(dataMgr.AllRecipes));
menuMgr.Loop();
// press any key to quit // press any key to quit
Console.ReadKey(); Console.ReadKey();

@ -77,7 +77,7 @@ namespace Model
{ {
if (other == null) return false; if (other == null) return false;
if (other == this) return true; if (other == this) return true;
return _description.Equals(other.Description) && this.Equals(other.this); return this.Description.Equals(other.Description);
} }
public override bool Equals(object? obj) public override bool Equals(object? obj)

Loading…
Cancel
Save