diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Actions/StringAction.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Actions/StringAction.cs index 54f46ba..1833996 100644 --- a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Actions/StringAction.cs +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Actions/StringAction.cs @@ -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); } diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/GuidNavigation.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/GuidNavigation.cs index 0d361a3..1a58097 100644 --- a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/GuidNavigation.cs +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/GuidNavigation.cs @@ -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()); } } diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.Extensions.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.Extensions.cs new file mode 100644 index 0000000..250330d --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.Extensions.cs @@ -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(this ExternalActionManager manager, string method, Expression> 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>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new Int32Navigation(method, setter)); + } + + public static void AddInt32Navigation(this ExternalActionManager manager, string method, Action 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(method, action)); + } + + public static void AddInt32Navigation(this ExternalActionManager manager, Type formType, string value, Expression> 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>(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 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)); + } + } +} diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.cs new file mode 100644 index 0000000..b647190 --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int32Navigation.cs @@ -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 SetProperty { get; set; } + + int? _lastValue { get; set; } + + public Int32Navigation(Type formType, string method, Action 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 : IExternalAction + where TForm : FormBase + { + public string Method { get; set; } + + public Action SetProperty { get; set; } + + int? _lastValue { get; set; } + + public Int32Navigation(string method, Action 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); + } + } +} diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.Extensions.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.Extensions.cs new file mode 100644 index 0000000..be32477 --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.Extensions.cs @@ -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(this ExternalActionManager manager, string method, Expression> 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>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new Int64Navigation(method, setter)); + } + + public static void AddInt64Navigation(this ExternalActionManager manager, string method, Action 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(method, action)); + } + + public static void AddInt64Navigation(this ExternalActionManager manager, Type formType, string value, Expression> 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>(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 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)); + } + } +} diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.cs new file mode 100644 index 0000000..d3e0eab --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/Int64Navigation.cs @@ -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 SetProperty { get; set; } + + long? _lastValue { get; set; } + + public Int64Navigation(Type formType, string method, Action 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 : IExternalAction + where TForm : FormBase + { + public string Method { get; set; } + + public Action SetProperty { get; set; } + + long? _lastValue { get; set; } + + public Int64Navigation(string method, Action 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); + } + } +} diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.Extensions.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.Extensions.cs new file mode 100644 index 0000000..4853757 --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.Extensions.cs @@ -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(this ExternalActionManager manager, string method, Expression> 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>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new StringNavigation(method, setter)); + } + + public static void AddStringNavigation(this ExternalActionManager manager, string method, Action 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(method, action)); + } + + public static void AddStringNavigation(this ExternalActionManager manager, Type formType, string value, Expression> 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>(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 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)); + } + } +} diff --git a/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.cs b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.cs new file mode 100644 index 0000000..0c58557 --- /dev/null +++ b/Experiments/ExternalActionManager/TelegramBotBase.Experiments/ActionManager/Navigation/StringNavigation.cs @@ -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 SetProperty { get; set; } + + String? _lastValue { get; set; } + + public StringNavigation(Type formType, string method, Action 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 : IExternalAction + where TForm : FormBase + { + public string Method { get; set; } + + public Action SetProperty { get; set; } + + String? _lastValue { get; set; } + + public StringNavigation(string method, Action 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); + } + } +}