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
{
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 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; }
private Menu(string title)
protected Menu(string title)
{
Title = title;
CurrentLine = 0;
@ -29,7 +40,7 @@ namespace ConsoleApp.Menu
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)
throw new ArgumentException("Error: a menu must contain at least 1 selection");
@ -40,6 +51,7 @@ namespace ConsoleApp.Menu
public virtual void Display()
{
Console.Clear();
Console.WriteLine(_screenDisplay);
}
@ -48,14 +60,14 @@ namespace ConsoleApp.Menu
_screenDisplay.AppendLine($"[ {Title} ]");
_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($"> ");
else
_screenDisplay.Append($" ");
_screenDisplay.AppendLine($"{selector.Line}");
_screenDisplay.AppendLine($"{_selectList[i].Line}");
}
}

@ -1,32 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace ConsoleApp.Menu
{
/// <summary>
/// An utility to find a recipe.
/// </summary>
internal class SearcherRecipe : Menu<Recipe>
{
public SearcherRecipe() : base("Search recipe")
{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
public static SearcherRecipe SearchInRecipeCollection(string title, RecipeCollection recipeCollection)
using Model;
namespace ConsoleApp.Menu
{
/// <summary>
/// An utility to find a recipe.
/// </summary>
internal class SearcherRecipe : Menu<Recipe>
{
public SearcherRecipe(RecipeCollection recipeCollection) : base("Search recipe")
{
Dictionary<Recipe, string> recipesSelectors = new Dictionary<Recipe, string>();
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
@ -34,6 +28,6 @@ namespace ConsoleApp.Menu
{
Console.WriteLine(CurrentSelected);
}
#endregion
}
}
#endregion
}
}

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

@ -20,9 +20,10 @@ RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("
if (allRecipe == null)
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));
// press any key to quit
Console.ReadKey();
MenuManager menuMgr = new MenuManager(dataMgr, new SearcherRecipe(dataMgr.AllRecipes));
menuMgr.Loop();
// press any key to quit
Console.ReadKey();

@ -77,7 +77,7 @@ namespace Model
{
if (other == null) return false;
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)

Loading…
Cancel
Save