Refactoring of Actions

- renaming existing Actions to "Navigation" and adding simple Actions for custom coding
- fitting namespaces to purpose
- cleanup of older test code
This commit is contained in:
Florian Zevedei
2024-01-24 22:05:34 +01:00
parent de93598e6f
commit 3c93daed35
7 changed files with 555 additions and 405 deletions
@@ -1,160 +1,37 @@
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager.Actions
{
public class EndsWithAction : IExternalAction
{
public Type FormType { get; }
public string SearchForString { get; set; }
public string Value { get; set; }
public Action<FormBase, string> SetProperty { get; set; }
public Action<FormBase, String> SetProperty { get; set; }
Func<String, UpdateResult, MessageResult, Task> Action;
String? _lastValue { get; set; }
public EndsWithAction(Type formType, string value, Action<FormBase, string> setProperty)
public EndsWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
{
FormType = formType;
Value = value;
SetProperty = setProperty;
SearchForString = searchFor;
Action = action;
}
public bool DoesFit(string raw_action)
{
if (!raw_action.EndsWith(Value))
return false;
_lastValue = raw_action;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (_lastValue != null)
{
SetProperty(new_form, _lastValue);
}
await session.ActiveForm.NavigateTo(new_form);
}
}
public class EndsWithAction<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public EndsWithAction(string value, Action<TForm, String> setProperty)
{
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
if (!raw_action.EndsWith(Value))
return false;
_lastValue = raw_action;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
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 session.ActiveForm.NavigateTo(new_form);
}
public bool DoesFit(string raw_data) => raw_data.EndsWith(SearchForString);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(raw_data, ur, mr);
}
public static class EndsWithAction_Extensions
{
public static void AddEndsWithAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, String>> propertySelector)
where TForm : FormBase
public static void AddEndsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
{
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 EndsWithAction<TForm>(method, setter));
manager.Add(new EndsWithAction(value, action));
}
public static void AddEndsWithAction<TForm>(this ExternalActionManager manager, string value, Action<TForm, String> setProperty)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new EndsWithAction<TForm>(value, setProperty));
}
public static void AddEndsWithAction(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 EndsWithAction(formType, value, setter));
}
public static void AddEndsWithAction(this ExternalActionManager manager, Type formType, string value, Action<FormBase, String> setProperty)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new EndsWithAction(formType, value, setProperty));
}
}
}
@@ -1,35 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager.Actions
{
public class GuidAction : IExternalAction
{
public Type FormType { get; }
public String Method { get; set; }
public Action<FormBase, Guid> SetProperty { get; set; }
public string Method { get; set; }
Guid? _lastValue { get; set; }
public GuidAction(Type formType, string method, Action<FormBase, Guid> setProperty)
Func<Guid, UpdateResult, MessageResult, Task> Action;
public GuidAction(string method, Func<Guid, UpdateResult, MessageResult, Task> action)
{
FormType = formType;
Method = method;
SetProperty = setProperty;
Action = action;
}
public bool DoesFit(string raw_action)
public bool DoesFit(string raw_data)
{
var cd = CallbackData.Deserialize(raw_action);
var cd = CallbackData.Deserialize(raw_data);
if (cd == null)
return false;
@@ -46,43 +36,30 @@ namespace DemoBot.ActionManager.Actions
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
await mr.ConfirmAction();
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr);
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
if (_lastValue != null)
SetProperty(new_form, _lastValue.Value);
await session.ActiveForm.NavigateTo(new_form);
}
public static CallbackData GetCallback(String method, Guid guid)
{
return new CallbackData(method, guid.ToString());
}
}
public class GuidAction<TForm> : IExternalAction
where TForm : FormBase
{
public String Method { get; set; }
public Action<TForm, Guid> SetProperty { get; set; }
public string Method { get; set; }
Guid? _lastValue { get; set; }
public GuidAction(string method, Action<TForm, Guid> setProperty)
Func<Guid, UpdateResult, MessageResult, Task> Action;
public GuidAction(string method, Func<Guid, UpdateResult, MessageResult, Task> action)
{
Method = method;
SetProperty = setProperty;
Action = action;
}
public bool DoesFit(string raw_action)
public bool DoesFit(string raw_data)
{
var cd = CallbackData.Deserialize(raw_action);
var cd = CallbackData.Deserialize(raw_data);
if (cd == null)
return false;
@@ -99,83 +76,19 @@ namespace DemoBot.ActionManager.Actions
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
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 session.ActiveForm.NavigateTo(new_form);
}
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr);
public static CallbackData GetCallback(String method, Guid guid)
{
return new CallbackData(method, guid.ToString());
}
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
}
public static class GuidAction_Extensions
{
public static void AddGuidAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, Guid>> propertySelector)
where TForm : FormBase
public static void AddGuidAction(this ExternalActionManager manager, string method, Func<Guid, UpdateResult, MessageResult, Task> action)
{
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
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new GuidAction<TForm>(method, action));
}
public static void AddGuidAction(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, Guid>> 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, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new GuidAction(formType, value, setter));
}
public static void AddGuidAction(this ExternalActionManager manager, Type formType, string method, Action<FormBase, Guid> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new GuidAction(formType, method, action));
manager.Add(new GuidAction(method, action));
}
}
}
@@ -1,160 +1,37 @@
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager.Actions
{
public class StartWithAction : IExternalAction
{
public Type FormType { get; }
public string SearchForString { get; set; }
public string Value { get; set; }
public Action<FormBase, string> SetProperty { get; set; }
public Action<FormBase, String> SetProperty { get; set; }
Func<String, UpdateResult, MessageResult, Task> Action;
String? _lastValue { get; set; }
public StartWithAction(Type formType, string value, Action<FormBase, string> setProperty)
public StartWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
{
FormType = formType;
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
if (!raw_action.StartsWith(Value))
return false;
_lastValue = raw_action;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (_lastValue != null)
{
SetProperty(new_form, _lastValue);
}
await session.ActiveForm.NavigateTo(new_form);
}
}
public class StartWithAction<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public StartWithAction(string value, Action<TForm, String> setProperty)
{
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_action)
{
if (!raw_action.StartsWith(Value))
return false;
_lastValue = raw_action;
return true;
}
public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session)
{
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 session.ActiveForm.NavigateTo(new_form);
SearchForString = searchFor;
Action = action;
}
public bool DoesFit(string raw_data) => raw_data.StartsWith(SearchForString);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(raw_data, ur, mr);
}
public static class StartWithAction_Extensions
{
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, String>> propertySelector)
where TForm : FormBase
public static void AddStartsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
{
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));
manager.Add(new StartWithAction(value, action));
}
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string value, Action<TForm, String> setProperty)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StartWithAction<TForm>(value, setProperty));
}
public static void AddStartsWithAction(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 StartWithAction(formType, value, setter));
}
public static void AddStartsWithAction(this ExternalActionManager manager, Type formType, string value, Action<FormBase, String> setProperty)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StartWithAction(formType, value, setProperty));
}
}
}
@@ -0,0 +1,135 @@
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace DemoBot.ActionManager.Navigation
{
public class EndsWithNavigation : IExternalAction
{
public Type FormType { get; }
public string Value { get; set; }
public Action<FormBase, string> SetProperty { get; set; }
public EndsWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
{
FormType = formType;
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (raw_data != null)
{
SetProperty(new_form, raw_data);
}
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
public class EndsWithNavigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, string> SetProperty { get; set; }
public EndsWithNavigation(string value, Action<TForm, string> setProperty)
{
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var type = typeof(TForm);
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
if (raw_data != null)
{
SetProperty(new_form, raw_data);
}
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
public static class EndsWithNavigation_Extensions
{
public static void AddEndsWithNavigation<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 EndsWithNavigation<TForm>(method, setter));
}
public static void AddEndsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> setProperty)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new EndsWithNavigation<TForm>(value, setProperty));
}
public static void AddEndsWithNavigation(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 EndsWithNavigation(formType, value, setter));
}
public static void AddEndsWithNavigation(this ExternalActionManager manager, Type formType, string value, Action<FormBase, string> setProperty)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new EndsWithNavigation(formType, value, setProperty));
}
}
}
@@ -0,0 +1,168 @@
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace DemoBot.ActionManager.Navigation
{
public class GuidNavigation : IExternalAction
{
public Type FormType { get; }
public string Method { get; set; }
public Action<FormBase, Guid> SetProperty { get; set; }
Guid? _lastValue { get; set; }
public GuidNavigation(Type formType, string method, Action<FormBase, Guid> 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;
Guid g;
if (Guid.TryParse(cd.Value, out g))
_lastValue = g;
return true;
}
public async Task DoAction(String data, 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, Guid guid) => new CallbackData(method, guid.ToString());
}
public class GuidNavigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Method { get; set; }
public Action<TForm, Guid> SetProperty { get; set; }
Guid? _lastValue { get; set; }
public GuidNavigation(string method, Action<TForm, Guid> 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;
Guid g;
if (Guid.TryParse(cd.Value, out g))
_lastValue = g;
return true;
}
public async Task DoAction(String raw_data, 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);
}
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
}
public static class GuidNavigation_Extensions
{
public static void AddGuidNavigation<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 GuidNavigation<TForm>(method, setter));
}
public static void AddGuidNavigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, Guid> 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 GuidNavigation<TForm>(method, action));
}
public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, Guid>> 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, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new GuidNavigation(formType, value, setter));
}
public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, Guid> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new GuidNavigation(formType, method, action));
}
}
}
@@ -0,0 +1,132 @@
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace DemoBot.ActionManager.Navigation
{
public class StartWithNavigation : IExternalAction
{
public Type FormType { get; }
public string Value { get; set; }
public Action<FormBase, string> SetProperty { get; set; }
public StartWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
{
FormType = formType;
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
if (raw_data != null)
{
SetProperty(new_form, raw_data);
}
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
public class StartWithNavigation<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, string> SetProperty { get; set; }
public StartWithNavigation(string value, Action<TForm, string> setProperty)
{
Value = value;
SetProperty = setProperty;
}
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr)
{
await mr.ConfirmAction();
var type = typeof(TForm);
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
if (raw_data != null)
{
SetProperty(new_form, raw_data);
}
await ur.Device.ActiveForm.NavigateTo(new_form);
}
}
public static class StartWithNavigation_Extensions
{
public static void AddStartsWithNavigation<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 StartWithNavigation<TForm>(method, setter));
}
public static void AddStartsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> setProperty)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StartWithNavigation<TForm>(value, setProperty));
}
public static void AddStartsWithNavigation(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 StartWithNavigation(formType, value, setter));
}
public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Action<FormBase, string> setProperty)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}
manager.Add(new StartWithNavigation(formType, value, setProperty));
}
}
}