Initial commit

This commit is contained in:
Vladimir Solodovnikov
2024-10-01 00:55:06 +03:00
commit db1b8f8f74
54 changed files with 820 additions and 0 deletions
@@ -0,0 +1,69 @@
using System.Text;
using CommandHandler.Commands;
namespace CommandHandler.Navigation.States;
internal abstract class NavigationStateBase : INavigationState, INavigationDrawer
{
private INavigationContext? _context;
protected readonly IDictionary<string, Func<ICommand>> Commands = new Dictionary<string, Func<ICommand>>();
protected ICommand ChangeNavigationState(INavigationState newState)
{
if (_context == null)
{
throw new InvalidOperationException();
}
var commands = new List<ICommand>
{
new ChangeNavigationStateCommand(_context, newState)
};
if (newState is INavigationDrawer drawer)
{
commands.Add(new DrawMenuCommand(drawer));
}
return new CombinedCommand([.. commands]);
}
public void SetContext(INavigationContext context)
{
_context = context;
}
public ICommand GetCommand(string message)
{
var parts = message.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length <= 0)
{
return new EmptyCommand();
}
if (Commands.TryGetValue(parts[0], out var commandFactory))
{
return commandFactory();
}
if (this is INavigationDrawer navigationDrawer)
{
return new DrawMenuCommand(navigationDrawer);
}
throw new NotImplementedException();
}
public string DrawNavigationMenu()
{
var builder = new StringBuilder();
foreach (var command in Commands.Keys)
{
builder.AppendLine(command);
}
return builder.ToString();
}
}