connection 1st ver. + view profile 1st ver. - error on login

pull/48/head
Alexandre AGOSTINHO 2 years ago
parent 04192a2ef2
commit 6116578585

@ -1,25 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp.Menu.Core;
using Model.Managers;
namespace ConsoleApp.Menu
{
internal class ConnectionMenu : Entry
{
public ConnectionMenu()
: base("Connection",
new Entry.EntryStep("Username: ", typeof(string)),
new Entry.EntryStep("Password: ", typeof(string)))
{ }
public override IMenu? Return()
{
string username = _selectList[0].Item.Input;
string password = _selectList[1].Item.Input;
return new PlainText($"User: {username} connected with password: {password}");
}
}
}
namespace ConsoleApp.Menu
{
internal class ConnectionMenu : Entry
{
private MasterManager _masterMgr;
private bool _wrongInput = false;
public ConnectionMenu(MasterManager masterManager)
: base("Connection",
new Entry.EntryStep("Mail: ", typeof(string)),
new Entry.EntryStep("Password: ", typeof(string), true))
{
_masterMgr = masterManager;
}
public override void Display()
{
base.Display();
if (_wrongInput)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wrong input...");
Console.ResetColor();
}
}
public override IMenu? Return()
{
string mail = _selectList[0].Item.Input;
string password = _selectList[1].Item.Input;
if (!_masterMgr.Login(mail, password))
{
_wrongInput = true;
return this;
}
else
return null;
}
}
}

@ -25,6 +25,11 @@ namespace ConsoleApp.Menu.Core
/// Contain the input gave by the menu.
/// </summary>
public string Input { get; internal set; }
/// <summary>
/// Define whether the input need to be hidden. Useful for password.
/// </summary>
public bool Hidden { get; private set; }
#endregion
#region Constructors
@ -33,11 +38,12 @@ namespace ConsoleApp.Menu.Core
/// </summary>
/// <param name="description">The text generally placed before the input in the menu.</param>
/// <param name="type">The type of the returned input.</param>
public EntryStep(string description, Type type)
public EntryStep(string description, Type type, bool hidden = false)
{
Description = description;
Input = "";
_entryType = type;
Hidden = hidden;
}
#endregion

@ -1,120 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp.Menu.Core;
namespace ConsoleApp.Menu.Core
{
/// <summary>
/// Define an Entry menu.
/// <br/>It allows you to navigate through the entries and completes them with a console input.
/// </summary>
internal abstract partial class Entry : Menu<Entry.EntryStep>
{
#region Attributes & Properties
private List<EntryStep> _steps;
#endregion
#region Constructors
/// <summary>
/// Constructor of the entry menu, based on the Menu constructor.
/// </summary>
/// <param name="title">The title of this menu.</param>
/// <param name="entrySteps">All the entries of this menu.</param>
protected Entry(string title, params EntryStep[] entrySteps) : base(title)
{
_steps = entrySteps.ToList();
_allSelectors = ConvertEntryStepsInSelector();
_selectList = _allSelectors;
}
#endregion
#region Methods
private List<Selector<EntryStep>> ConvertEntryStepsInSelector()
{
List<Selector<EntryStep>> newSelectors = new List<Selector<EntryStep>>();
foreach (EntryStep step in _steps)
{
newSelectors.Add(new Selector<EntryStep>(step, step.Description));
}
return newSelectors;
}
#endregion
#region IMenu implementation
public override void WriteMenuMode(ConsoleKeyInfo cki)
{
if (!WriteMode && cki.Key == ConsoleKey.R)
{
EnableWriteMode();
return;
}
if (WriteMode)
{
if (cki.Key == ConsoleKey.Escape)
{
if (CurrentSelected is null)
throw new ArgumentNullException("CurrentSelected");
CurrentSelected.Input = InputStr.ToString();
DisableWriteMode();
InputStr.Clear();
return;
}
if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0)
{
InputStr.Remove(InputStr.Length - 1, 1);
return;
}
InputStr.Append(cki.KeyChar);
}
}
public override void Update()
{
if (_selectList.Count == 0)
{
CurrentSelected = default;
return;
}
CurrentSelected = _selectList[CurrentLine].Item;
}
public override void Display()
{
_screenDisplay.Clear();
Console.Clear();
_screenDisplay.AppendLine($"[ {Title} ]");
_screenDisplay.AppendLine("-------------------------------------------");
for (int i = 0; i < _selectList.Count; i++)
{
if (CurrentLine == i)
_screenDisplay.Append($"> ");
else
_screenDisplay.Append($" ");
_screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}");
if (CurrentLine == i && WriteMode)
_screenDisplay.Append(InputStr);
_screenDisplay.AppendLine();
}
if (_selectList.Count == 0)
_screenDisplay.AppendLine("Empty...");
_screenDisplay.AppendLine(
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode");
Console.WriteLine(_screenDisplay);
}
#endregion
}
}
namespace ConsoleApp.Menu.Core
{
/// <summary>
/// Define an Entry menu.
/// <br/>It allows you to navigate through the entries and completes them with a console input.
/// </summary>
internal abstract partial class Entry : Menu<Entry.EntryStep>
{
#region Attributes & Properties
private List<EntryStep> _steps;
#endregion
#region Constructors
/// <summary>
/// Constructor of the entry menu, based on the Menu constructor.
/// </summary>
/// <param name="title">The title of this menu.</param>
/// <param name="entrySteps">All the entries of this menu.</param>
protected Entry(string title, params EntryStep[] entrySteps)
: base(title)
{
_steps = entrySteps.ToList();
_allSelectors = ConvertEntryStepsInSelector();
_selectList = _allSelectors;
}
#endregion
#region Methods
private List<Selector<EntryStep>> ConvertEntryStepsInSelector()
{
List<Selector<EntryStep>> newSelectors = new List<Selector<EntryStep>>();
foreach (EntryStep step in _steps)
{
newSelectors.Add(new Selector<EntryStep>(step, step.Description));
}
return newSelectors;
}
#endregion
#region IMenu implementation
public override void WriteMenuMode(ConsoleKeyInfo cki)
{
if (!WriteMode && cki.Key == ConsoleKey.R)
{
EnableWriteMode();
return;
}
if (WriteMode)
{
if (cki.Key == ConsoleKey.Escape)
{
if (CurrentSelected is null)
throw new ArgumentNullException("CurrentSelected");
CurrentSelected.Input = InputStr.ToString();
DisableWriteMode();
InputStr.Clear();
return;
}
if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0)
{
InputStr.Remove(InputStr.Length - 1, 1);
return;
}
if (CurrentSelected is not null && CurrentSelected.Hidden)
InputStr.Append('*');
else
InputStr.Append(cki.KeyChar);
}
}
public override void Update()
{
if (_selectList.Count == 0)
{
CurrentSelected = default;
return;
}
CurrentSelected = _selectList[CurrentLine].Item;
}
public override void Display()
{
_screenDisplay.Clear();
Console.Clear();
_screenDisplay.AppendLine($"[ {Title} ]");
_screenDisplay.AppendLine("-------------------------------------------");
for (int i = 0; i < _selectList.Count; i++)
{
if (CurrentLine == i)
_screenDisplay.Append($"> ");
else
_screenDisplay.Append($" ");
_screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}");
if (CurrentLine == i && WriteMode)
_screenDisplay.Append(InputStr);
_screenDisplay.AppendLine();
}
if (_selectList.Count == 0)
_screenDisplay.AppendLine("Empty...");
_screenDisplay.AppendLine(
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode");
Console.WriteLine(_screenDisplay);
}
#endregion
}
}

@ -22,8 +22,8 @@ namespace ConsoleApp.Menu
new SearcherRecipe(masterManager.DataMgr.GetRecipes("search")), "Recipe search"));
_allSelectors.Add(MasterManager.CurrentConnectedUser is null ?
new Selector<IMenu>(new ConnectionMenu(), "Connection")
: new Selector<IMenu>(new PlainText("User profile"), "User profile"));
new Selector<IMenu>(new ConnectionMenu(masterManager), "Connection")
: new Selector<IMenu>(new ProfileMenu(masterManager), "User profile"));
}
protected override List<Selector<IMenu>> SearchInSelection()

@ -0,0 +1,24 @@
using ConsoleApp.Menu.Core;
using Model.Managers;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal class ProfileMenu : Menu<IMenu>
{
public ProfileMenu(MasterManager masterManager)
: base("Profile")
{
_allSelectors.Add(new Selector<IMenu>(
new SearcherRecipe(new RecipeCollection("My recipes",
masterManager.DataMgr.GetRecipes().Where(r => r.Author == MasterManager.CurrentConnectedUser).ToArray())),
"My recipes"));
}
}
}

@ -9,8 +9,8 @@ using Model.Managers;
Console.WriteLine("Hello, World!\n\n");
MasterManager masterMgr = new MasterManager(new Stubs());
//MasterMgr masterMgr = new MasterMgr(new DataContractXML());
//MasterMgr masterMgr = new MasterMgr(new DataContractJSON());
//_masterMgr masterMgr = new _masterMgr(new DataContractXML());
//_masterMgr masterMgr = new _masterMgr(new DataContractJSON());
masterMgr.DataMgr.Serializer = new DataContractXML();
//masterMgr.Serializer = new DataContractJSON();

Loading…
Cancel
Save