start #31
continuous-integration/drone/push Build is failing Details

pull/38/head
Alexandre AGOSTINHO 2 years ago
parent bc732d0b3b
commit d5adeff27a

@ -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();
}
}

@ -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<T> : IMenu
{
protected StringBuilder _screenDisplay;
protected readonly List<Selector<T>> _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<T>[] 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<Selector<T>>(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<T> 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();
}
}

@ -12,114 +12,17 @@ namespace ConsoleApp.Menu
/// <summary> /// <summary>
/// An utility to find a recipe. /// An utility to find a recipe.
/// </summary> /// </summary>
public class SearcherRecipe : SelectMenu<Recipe> internal class SearcherRecipe : Menu<Recipe>
{ {
#region Attribute public SearcherRecipe(string title, params Selector<Recipe>[] selections)
private RecipeCollection _recipeOnSearch; : base(title, selections)
#endregion { }
#region Properties #region Methodes
/// <summary> public override void Return()
/// A collection of recipe where the title contain the search string {
/// </summary> Console.WriteLine(CurrentSelected);
public RecipeCollection SearchResult { get; private set; } }
/// <summary>
/// The search string
/// </summary>
public string ResearchStr { get; set; }
#endregion
/// <summary>
/// Constructor of the SearcherRecipe utility.
/// </summary>
/// <param name="recipeOnSearch">The collection of recipe where to search</param>
public SearcherRecipe(RecipeCollection recipeOnSearch)
{
_recipeOnSearch = recipeOnSearch;
SearchResult = _recipeOnSearch;
ResearchStr = "";
}
#region Methodes
/// <summary>
/// Launch a search by name request in the collection of Recipe with with a string.
/// </summary>
/// <param name="researchStr">The string for search</param>
/// <returns>True if the result of the search gave at least 1 element. False otherwise.</returns>
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);
}
/// <summary>
/// Launch and pilot the search menu.
/// </summary>
/// <param name="recipeOnSearch">The collection of recipe where to search</param>
/// <returns>The recipe selected</returns>
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;
}
#endregion #endregion
} }
} }

@ -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<T>
{
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;
}
}
}

@ -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<Menu.IMenu> MenuCallStack { get; set; }
public MenuManager(DataManager dataManager, Menu.IMenu firstMenu)
{
DataManager = dataManager;
MenuCallStack = new Stack<Menu.IMenu>();
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);
}
}
}

@ -20,7 +20,7 @@ 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'.");
Manager manager = new Manager(allRecipe); DataManager manager = new DataManager(allRecipe);
Recipe? ret = SearcherRecipe.ResearchOn(allRecipe); Recipe? ret = SearcherRecipe.ResearchOn(allRecipe);
Console.WriteLine(ret); Console.WriteLine(ret);

@ -9,7 +9,7 @@ namespace Model
/// <summary> /// <summary>
/// Manager of the model. Here is stoked all the recipes, the users, etc... /// Manager of the model. Here is stoked all the recipes, the users, etc...
/// </summary> /// </summary>
public class Manager public class DataManager
{ {
/// <summary> /// <summary>
/// A collection of all the recipe loaded in the app. /// A collection of all the recipe loaded in the app.
@ -19,7 +19,7 @@ namespace Model
/// <summary> /// <summary>
/// The constructor of the manager. /// The constructor of the manager.
/// </summary> /// </summary>
public Manager() public DataManager()
{ {
AllRecipes = new RecipeCollection(description: "All Recipes"); AllRecipes = new RecipeCollection(description: "All Recipes");
} }
@ -28,7 +28,7 @@ namespace Model
/// The constructor of the manager. /// The constructor of the manager.
/// </summary> /// </summary>
/// <param name="allRecipes">A list of loaded recipes</param> /// <param name="allRecipes">A list of loaded recipes</param>
public Manager(RecipeCollection allRecipes) public DataManager(RecipeCollection allRecipes)
{ {
AllRecipes = new RecipeCollection( AllRecipes = new RecipeCollection(
description: "All Recipes", description: "All Recipes",
Loading…
Cancel
Save