Adding minimal version of property selector, using reflection
This commit is contained in:
parent
d0a6b74244
commit
161f191c43
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
@ -123,6 +124,23 @@ namespace DemoBot.ActionManager.Actions
|
||||
public static class GuidAction_Extensions
|
||||
{
|
||||
|
||||
public static void AddGuidAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, Guid>> propertySelector)
|
||||
where TForm : FormBase
|
||||
{
|
||||
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||
}
|
||||
|
||||
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||
|
||||
var assign = Expression.Lambda<Action<TForm, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||
|
||||
var setter = assign.Compile(true);
|
||||
|
||||
manager.Add(new GuidAction<TForm>(method, setter));
|
||||
}
|
||||
|
||||
public static void AddGuidAction<TForm>(this ExternalActionManager manager, string method, Action<TForm, Guid> action)
|
||||
where TForm : FormBase
|
||||
{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq.Expressions;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.DependencyInjection;
|
||||
using TelegramBotBase.Form;
|
||||
@ -101,7 +102,22 @@ namespace DemoBot.ActionManager.Actions
|
||||
|
||||
public static class StartWithAction_Extensions
|
||||
{
|
||||
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, String>> propertySelector)
|
||||
where TForm : FormBase
|
||||
{
|
||||
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||
}
|
||||
|
||||
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||
|
||||
var assign = Expression.Lambda<Action<TForm, String>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||
|
||||
var setter = assign.Compile(true);
|
||||
|
||||
manager.Add(new StartWithAction<TForm>(method, setter));
|
||||
}
|
||||
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string value, Action<TForm, String> setProperty)
|
||||
where TForm : FormBase
|
||||
{
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace DemoBot.Forms
|
||||
{
|
||||
public class HiddenLetterForm : AutoCleanForm
|
||||
{
|
||||
|
||||
public Guid letterId { get; set; }
|
||||
|
||||
public HiddenLetterForm()
|
||||
{
|
||||
|
||||
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
if (message.RawData != "start")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await message.ConfirmAction("Lets go");
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var st = new StartForm();
|
||||
|
||||
await NavigateTo(st);
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
|
||||
var bf = new ButtonForm();
|
||||
|
||||
bf.AddButtonRow("Goto Start", "start");
|
||||
|
||||
await Device.Send($"Welcome to Hidden letter form\n\nYour letter Id is: {letterId}", bf);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -27,6 +27,11 @@ namespace DemoBot
|
||||
a.value = b;
|
||||
});
|
||||
|
||||
|
||||
//Minimal version, using reflection right now
|
||||
eam.AddStartsWithAction<HiddenForm>("a_", a => a.value);
|
||||
|
||||
|
||||
eam.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) =>
|
||||
{
|
||||
var hf = a as HiddenForm;
|
||||
@ -36,11 +41,17 @@ namespace DemoBot
|
||||
hf.value = b;
|
||||
});
|
||||
|
||||
|
||||
eam.AddGuidAction<HiddenTicketForm>("tickets", (a, b) =>
|
||||
{
|
||||
a.ticketId = b;
|
||||
});
|
||||
|
||||
|
||||
//Minimal version, using reflection right now
|
||||
eam.AddGuidAction<HiddenLetterForm>("letters", a => a.letterId);
|
||||
|
||||
|
||||
eam.AddGuidAction<HiddenOpenForm>("open", (a, b) =>
|
||||
{
|
||||
a.guid = b;
|
||||
@ -122,14 +133,18 @@ namespace DemoBot
|
||||
|
||||
String max_value2 = "t_".PadRight(32, '5'); //Starts with
|
||||
|
||||
String max_value3 = "a_".PadRight(32, '5'); //Starts with
|
||||
|
||||
Guid test_value = Guid.NewGuid(); //Unhandled caller
|
||||
|
||||
var callback_guid = GuidAction.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm
|
||||
|
||||
var callback_tickets = GuidAction.GetCallback("tickets", Guid.NewGuid()); //HiddenTicketForm
|
||||
|
||||
var callback_letters = GuidAction.GetCallback("letters", Guid.NewGuid()); //HiddenLetterForm
|
||||
|
||||
String message = $"Test notification from 'outside'\n\nTest values are:\n\nTest: {max_value}\nTest2: {max_value2}\nTest (Guid): {test_value.ToString()}\nTest (Callback Guid): {callback_guid.Value}\nTickets (Guid): {callback_tickets.Value}\n";
|
||||
|
||||
String message = $"Test notification from 'outside'\n\nTest values are:\n\nTest: {max_value}\nTest2: {max_value2}\nTest3: {max_value3}\nTest (Guid): {test_value.ToString()}\nTest (Callback Guid): {callback_guid.Value}\nTickets (Guid): {callback_tickets.Value}\nLetters (Guid): {callback_letters.Value}\n";
|
||||
|
||||
|
||||
var tb = new TelegramBotClient(Token);
|
||||
@ -142,12 +157,16 @@ namespace DemoBot
|
||||
|
||||
bf.AddButtonRow("Test2", max_value2);
|
||||
|
||||
bf.AddButtonRow("Test3", max_value3);
|
||||
|
||||
bf.AddButtonRow("Test (Guid)", test_value.ToString());
|
||||
|
||||
bf.AddButtonRow("Test (Callback Gui)", callback_guid);
|
||||
|
||||
bf.AddButtonRow("Tickets", callback_tickets);
|
||||
|
||||
bf.AddButtonRow("Letters", callback_letters);
|
||||
|
||||
bf.AddButtonRow("Close", "close");
|
||||
|
||||
await tb.SendTextMessageAsync(e.DeviceId, message, disableNotification: true, replyMarkup: (InlineKeyboardMarkup)bf);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user