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:
FlorianDahn 2020-05-04 02:12:50 +02:00
parent 11eeb0dcab
commit 2fc16b18da
5 changed files with 63 additions and 5 deletions

View File

@ -3,11 +3,14 @@ using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBase.Interfaces
{
public interface IStateMachine
{
Type DefaultStateForm { get; }
void SaveFormStates(SaveStatesEventArgs e);
StateContainer LoadFormStates();

View File

@ -88,7 +88,7 @@ namespace TelegramBotBase
if (d != null)
{
this.SessionList.Remove(deviceId);
}
}
@ -222,7 +222,14 @@ namespace TelegramBotBase
//Skip classes where IgnoreState attribute is existing
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

View File

@ -5,6 +5,7 @@ using System.Runtime.Serialization.Formatters;
using System.Text;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.States
@ -18,13 +19,28 @@ namespace TelegramBotBase.States
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)
{
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.Overwrite = overwrite;
}

View File

@ -5,6 +5,7 @@ using System.Runtime.Serialization.Formatters;
using System.Text;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.States
@ -18,13 +19,28 @@ namespace TelegramBotBase.States
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)
{
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.Overwrite = overwrite;
}

View File

@ -7,6 +7,7 @@ using System.Xml;
using System.Xml.Serialization;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.States
@ -17,13 +18,28 @@ namespace TelegramBotBase.States
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)
{
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.Overwrite = overwrite;
}