Initial commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using CommandHandler.Commands;
|
||||
|
||||
namespace CommandHandler.Navigation.States;
|
||||
|
||||
internal class NavigationMainState : NavigationStateBase, INavigationDrawer
|
||||
{
|
||||
public NavigationMainState()
|
||||
{
|
||||
Commands.Add("/some", () => ChangeNavigationState(new NavigationSomeState()));
|
||||
Commands.Add("/exit", () => new ExitCommand());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace CommandHandler.Navigation.States;
|
||||
|
||||
internal class NavigationSomeState : NavigationStateBase, INavigationDrawer
|
||||
{
|
||||
public NavigationSomeState()
|
||||
{
|
||||
Commands.Add("/back", () => ChangeNavigationState(new NavigationMainState()));
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user