DefaultStateForm
- adding DefaultStateForm to redirect Sessions after reloading Sessions to a different Form instead of "restart" the session (i.e. on an in Bot wizard you just want to let the user continue on the main menu) - adding to SaveSessions to save the "main menu" instead of skipping the ignored form
This commit is contained in:
parent
11eeb0dcab
commit
2fc16b18da
@ -3,11 +3,14 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
namespace TelegramBotBase.Interfaces
|
namespace TelegramBotBase.Interfaces
|
||||||
{
|
{
|
||||||
public interface IStateMachine
|
public interface IStateMachine
|
||||||
{
|
{
|
||||||
|
Type DefaultStateForm { get; }
|
||||||
|
|
||||||
void SaveFormStates(SaveStatesEventArgs e);
|
void SaveFormStates(SaveStatesEventArgs e);
|
||||||
|
|
||||||
StateContainer LoadFormStates();
|
StateContainer LoadFormStates();
|
||||||
|
|||||||
@ -88,7 +88,7 @@ namespace TelegramBotBase
|
|||||||
if (d != null)
|
if (d != null)
|
||||||
{
|
{
|
||||||
this.SessionList.Remove(deviceId);
|
this.SessionList.Remove(deviceId);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +222,14 @@ namespace TelegramBotBase
|
|||||||
//Skip classes where IgnoreState attribute is existing
|
//Skip classes where IgnoreState attribute is existing
|
||||||
if (form.GetType().GetCustomAttributes(typeof(IgnoreState), true).Length != 0)
|
if (form.GetType().GetCustomAttributes(typeof(IgnoreState), true).Length != 0)
|
||||||
{
|
{
|
||||||
continue;
|
if (statemachine.DefaultStateForm == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Replace form by default State one.
|
||||||
|
se.FormUri = statemachine.DefaultStateForm.FullName;
|
||||||
|
se.QualifiedName = statemachine.DefaultStateForm.AssemblyQualifiedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Is Subclass of IStateForm
|
//Is Subclass of IStateForm
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using System.Runtime.Serialization.Formatters;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.States
|
namespace TelegramBotBase.States
|
||||||
@ -18,13 +19,28 @@ namespace TelegramBotBase.States
|
|||||||
|
|
||||||
public bool Overwrite { get; set; }
|
public bool Overwrite { get; set; }
|
||||||
|
|
||||||
public JSONStateMachine(String file, bool overwrite = true)
|
public Type DefaultStateForm { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Will initialize the state machine.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file">Path of the file and name where to save the session details.</param>
|
||||||
|
/// <param name="defaultStateForm">Type of Form which will be saved instead of Form which has <seealso cref="Attributes.IgnoreState"/> attribute declared. Needs to be subclass of <seealso cref="Form.FormBase"/>.</param>
|
||||||
|
/// <param name="overwrite">Declares of the file could be overwritten.</param>
|
||||||
|
public JSONStateMachine(String file, Type defaultStateForm = null, bool overwrite = true)
|
||||||
{
|
{
|
||||||
if (file is null)
|
if (file is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(file));
|
throw new ArgumentNullException(nameof(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.DefaultStateForm = defaultStateForm ?? typeof(FormBase);
|
||||||
|
|
||||||
|
if (!this.DefaultStateForm.IsSubclassOf(typeof(FormBase)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("DefaultStateForm is not a subclass of FormBase");
|
||||||
|
}
|
||||||
|
|
||||||
this.FilePath = file;
|
this.FilePath = file;
|
||||||
this.Overwrite = overwrite;
|
this.Overwrite = overwrite;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using System.Runtime.Serialization.Formatters;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.States
|
namespace TelegramBotBase.States
|
||||||
@ -18,13 +19,28 @@ namespace TelegramBotBase.States
|
|||||||
|
|
||||||
public bool Overwrite { get; set; }
|
public bool Overwrite { get; set; }
|
||||||
|
|
||||||
public SimpleJSONStateMachine(String file, bool overwrite = true)
|
public Type DefaultStateForm { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Will initialize the state machine.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file">Path of the file and name where to save the session details.</param>
|
||||||
|
/// <param name="defaultStateForm">Type of Form which will be saved instead of Form which has <seealso cref="Attributes.IgnoreState"/> attribute declared. Needs to be subclass of <seealso cref="Form.FormBase"/>.</param>
|
||||||
|
/// <param name="overwrite">Declares of the file could be overwritten.</param>
|
||||||
|
public SimpleJSONStateMachine(String file, Type defaultStateForm = null, bool overwrite = true)
|
||||||
{
|
{
|
||||||
if (file is null)
|
if (file is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(file));
|
throw new ArgumentNullException(nameof(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.DefaultStateForm = defaultStateForm ?? typeof(FormBase);
|
||||||
|
|
||||||
|
if (!this.DefaultStateForm.IsSubclassOf(typeof(FormBase)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("DefaultStateForm is not a subclass of FormBase");
|
||||||
|
}
|
||||||
|
|
||||||
this.FilePath = file;
|
this.FilePath = file;
|
||||||
this.Overwrite = overwrite;
|
this.Overwrite = overwrite;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using System.Xml;
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.States
|
namespace TelegramBotBase.States
|
||||||
@ -17,13 +18,28 @@ namespace TelegramBotBase.States
|
|||||||
|
|
||||||
public bool Overwrite { get; set; }
|
public bool Overwrite { get; set; }
|
||||||
|
|
||||||
public XMLStateMachine(String file, bool overwrite = true)
|
public Type DefaultStateForm { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Will initialize the state machine.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file">Path of the file and name where to save the session details.</param>
|
||||||
|
/// <param name="defaultStateForm">Type of Form which will be saved instead of Form which has <seealso cref="Attributes.IgnoreState"/> attribute declared. Needs to be subclass of <seealso cref="Form.FormBase"/>.</param>
|
||||||
|
/// <param name="overwrite">Declares of the file could be overwritten.</param>
|
||||||
|
public XMLStateMachine(String file, Type defaultStateForm = null, bool overwrite = true)
|
||||||
{
|
{
|
||||||
if (file is null)
|
if (file is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(file));
|
throw new ArgumentNullException(nameof(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.DefaultStateForm = defaultStateForm ?? typeof(FormBase);
|
||||||
|
|
||||||
|
if (!this.DefaultStateForm.IsSubclassOf(typeof(FormBase)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("DefaultStateForm is not a subclass of FormBase");
|
||||||
|
}
|
||||||
|
|
||||||
this.FilePath = file;
|
this.FilePath = file;
|
||||||
this.Overwrite = overwrite;
|
this.Overwrite = overwrite;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user