Adding Int32, Int64 and StringNavigation

This commit is contained in:
Florian Zevedei 2024-02-02 14:15:26 +01:00
parent 97e04f4162
commit b8fd3badd4
8 changed files with 502 additions and 4 deletions

View File

@ -36,7 +36,7 @@ namespace TelegramBotBase.Experiments.ActionManager.Actions
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue, ur, mr);
public static CallbackData GetCallback(string method, long l) => new CallbackData(method, l.ToString());
public static CallbackData GetCallback(string method, string str) => new CallbackData(method, str);
}

View File

@ -102,8 +102,5 @@ namespace TelegramBotBase.Experiments.ActionManager.Navigation
await ur.Device.ActiveForm.NavigateTo(new_form);
}
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
}
}

View File

@ -0,0 +1,63 @@
using System.Linq.Expressions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public static class Int32Navigation_Extensions
{
public static void AddInt32Navigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, int>> 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, int>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new Int32Navigation<TForm>(method, setter));
}
public static void AddInt32Navigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, int> action)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new Int32Navigation<TForm>(method, action));
}
public static void AddInt32Navigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, int>> propertySelector)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
var newValue = Expression.Parameter(propertySelector.Body.Type);
var assign = Expression.Lambda<Action<FormBase, int>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new Int32Navigation(formType, value, setter));
}
public static void AddInt32Navigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, int> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new Int32Navigation(formType, method, action));
}
}
}

View File

@ -0,0 +1,106 @@
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public class Int32Navigation : IExternalAction
{
public Type FormType { get; }
public string Method { get; set; }
public Action<FormBase, int> SetProperty { get; set; }
int? _lastValue { get; set; }
public Int32Navigation(Type formType, string method, Action<FormBase, int> setProperty)
{
FormType = formType;
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
var cd = CallbackData.Deserialize(raw_action);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
int i;
if (int.TryParse(cd.Value, out i))
_lastValue = i;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (_lastValue != null)
SetProperty(new_form, _lastValue.Value);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
public static CallbackData GetCallback(string method, int i) => new CallbackData(method, i.ToString());
}
public class Int32Navigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Method { get; set; }
public Action<TForm, int> SetProperty { get; set; }
int? _lastValue { get; set; }
public Int32Navigation(string method, Action<TForm, int> setProperty)
{
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data)
{
var cd = CallbackData.Deserialize(raw_data);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
int g;
if (int.TryParse(cd.Value, out g))
_lastValue = g;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var type = typeof(TForm);
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
if (_lastValue != null)
SetProperty(new_form, _lastValue.Value);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
}

View File

@ -0,0 +1,63 @@
using System.Linq.Expressions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public static class Int64Navigation_Extensions
{
public static void AddInt64Navigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, long>> 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, long>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new Int64Navigation<TForm>(method, setter));
}
public static void AddInt64Navigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, long> action)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new Int64Navigation<TForm>(method, action));
}
public static void AddInt64Navigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, long>> propertySelector)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
var newValue = Expression.Parameter(propertySelector.Body.Type);
var assign = Expression.Lambda<Action<FormBase, long>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new Int64Navigation(formType, value, setter));
}
public static void AddInt64Navigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, long> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new Int64Navigation(formType, method, action));
}
}
}

View File

@ -0,0 +1,106 @@
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public class Int64Navigation : IExternalAction
{
public Type FormType { get; }
public string Method { get; set; }
public Action<FormBase, long> SetProperty { get; set; }
long? _lastValue { get; set; }
public Int64Navigation(Type formType, string method, Action<FormBase, long> setProperty)
{
FormType = formType;
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
var cd = CallbackData.Deserialize(raw_action);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
long l;
if (long.TryParse(cd.Value, out l))
_lastValue = l;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (_lastValue != null)
SetProperty(new_form, _lastValue.Value);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
public static CallbackData GetCallback(string method, long l) => new CallbackData(method, l.ToString());
}
public class Int64Navigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Method { get; set; }
public Action<TForm, long> SetProperty { get; set; }
long? _lastValue { get; set; }
public Int64Navigation(string method, Action<TForm, long> setProperty)
{
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data)
{
var cd = CallbackData.Deserialize(raw_data);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
long l;
if (long.TryParse(cd.Value, out l))
_lastValue = l;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var type = typeof(TForm);
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
if (_lastValue != null)
SetProperty(new_form, _lastValue.Value);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
}

View File

@ -0,0 +1,63 @@
using System.Linq.Expressions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public static class StringNavigation_Extensions
{
public static void AddStringNavigation<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 StringNavigation<TForm>(method, setter));
}
public static void AddStringNavigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, String> action)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StringNavigation<TForm>(method, action));
}
public static void AddStringNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, String>> propertySelector)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
var newValue = Expression.Parameter(propertySelector.Body.Type);
var assign = Expression.Lambda<Action<FormBase, String>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new StringNavigation(formType, value, setter));
}
public static void AddStringNavigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, String> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StringNavigation(formType, method, action));
}
}
}

View File

@ -0,0 +1,100 @@
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBase.Experiments.ActionManager.Navigation
{
public class StringNavigation : IExternalAction
{
public Type FormType { get; }
public string Method { get; set; }
public Action<FormBase, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public StringNavigation(Type formType, string method, Action<FormBase, String> setProperty)
{
FormType = formType;
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
var cd = CallbackData.Deserialize(raw_action);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
_lastValue = cd.Value;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (_lastValue != null)
SetProperty(new_form, _lastValue);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
public static CallbackData GetCallback(string method, String str) => new CallbackData(method, str);
}
public class StringNavigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Method { get; set; }
public Action<TForm, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public StringNavigation(string method, Action<TForm, String> setProperty)
{
Method = method;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data)
{
var cd = CallbackData.Deserialize(raw_data);
if (cd == null)
return false;
if (cd.Method != Method)
return false;
_lastValue = cd.Value;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var type = typeof(TForm);
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
if (_lastValue != null)
SetProperty(new_form, _lastValue);
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
}