Доработан менеджер состояний.
This commit is contained in:
20
BotPages.Core/Pages/ActionAttribute.cs
Normal file
20
BotPages.Core/Pages/ActionAttribute.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotPages.Core
|
||||
{
|
||||
public class ActionAttribute : Attribute
|
||||
{
|
||||
public ActionAttribute(string label)
|
||||
{
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public string Label { get; }
|
||||
}
|
||||
}
|
||||
48
BotPages.Core/Pages/ActionExtensions.cs
Normal file
48
BotPages.Core/Pages/ActionExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace BotPages.Core
|
||||
{
|
||||
public static class ActionExtensions
|
||||
{
|
||||
private static readonly Dictionary<Type, Dictionary<string, object>> _cache = new();
|
||||
|
||||
public static string GetActionLabel<T>(this T value)
|
||||
where T : Enum
|
||||
{
|
||||
var fieldName = value.ToString();
|
||||
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
|
||||
return field?.GetCustomAttribute<ActionAttribute>()?.Label ?? fieldName;
|
||||
}
|
||||
|
||||
public static T? FromActionLabel<T>(string? value) where T : struct, Enum
|
||||
{
|
||||
if (value == null) return null;
|
||||
|
||||
var type = typeof(T);
|
||||
if (!_cache.TryGetValue(type, out var map))
|
||||
{
|
||||
map = new Dictionary<string, object>();
|
||||
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var fieldValue = field.GetValue(null)!;
|
||||
var fieldName = field.Name;
|
||||
|
||||
var attr = field.GetCustomAttribute<ActionAttribute>();
|
||||
|
||||
if (attr != null)
|
||||
{
|
||||
fieldName = attr.Label;
|
||||
}
|
||||
|
||||
map[fieldName] = fieldValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return map.TryGetValue(value, out var result) ? (T)result : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace BotPages.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Запись навигационного стека: страница и её аргументы.
|
||||
/// </summary>
|
||||
public sealed record NavEntry(string PageId, object? Args = null);
|
||||
}
|
||||
@@ -18,11 +18,23 @@
|
||||
/// <summary>
|
||||
/// Тип кнопки: inline или reply.
|
||||
/// </summary>
|
||||
public ActionPlacement Placement { get; init; } = ActionPlacement.Inline;
|
||||
public ActionPlacement Placement { get; init; } = ActionPlacement.Reply;
|
||||
|
||||
/// <summary>
|
||||
/// Номер ряда для макета (0 — первая строка).
|
||||
/// </summary>
|
||||
public int Row { get; init; } = 0;
|
||||
|
||||
public PageAction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public PageAction(Enum en)
|
||||
{
|
||||
Label = en.GetActionLabel();
|
||||
Value = en.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user