Modal and Form updates

- adding ReturnFromModal method to FormBase, will get invoked if context leaves a ModalDialog
- ModalDialog contains ParentForm for better navigation
- ModalDialog invokes ReturnFromModal on parentForm after closing
- adding ShowBackButton to PromptDialog for show a back button
This commit is contained in:
FlorianDahn 2020-04-04 19:52:15 +07:00
parent 7a3e4721bd
commit 8b9929198a
3 changed files with 38 additions and 2 deletions

View File

@ -107,6 +107,8 @@ namespace TelegramBotBase.Form
} }
} }
public async Task OnClosed(EventArgs e) public async Task OnClosed(EventArgs e)
{ {
if (this.Events[__evClosed] == null) if (this.Events[__evClosed] == null)
@ -136,6 +138,16 @@ namespace TelegramBotBase.Form
} }
} }
/// <summary>
/// Get invoked when a modal child from has been closed.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task ReturnFromModal(ModalDialog modal)
{
}
/// <summary> /// <summary>
/// Pre to form close, cleanup all controls /// Pre to form close, cleanup all controls
@ -337,6 +349,7 @@ namespace TelegramBotBase.Form
ds.ActiveForm = newForm; ds.ActiveForm = newForm;
newForm.Client = parentForm.Client; newForm.Client = parentForm.Client;
newForm.Device = ds; newForm.Device = ds;
newForm.ParentForm = parentForm;
newForm.Closed += async (s, en) => newForm.Closed += async (s, en) =>
{ {

View File

@ -7,18 +7,22 @@ namespace TelegramBotBase.Form
{ {
public class ModalDialog : FormBase public class ModalDialog : FormBase
{ {
/// <summary>
/// Contains the parent from where the modal dialog has been opened.
/// </summary>
public FormBase ParentForm { get; set; }
/// <summary> /// <summary>
/// This is a modal only function and does everything to close this form. /// This is a modal only function and does everything to close this form.
/// </summary> /// </summary>
public async Task CloseForm() public async Task CloseForm()
{ {
await this.CloseControls(); await this.CloseControls();
await this.OnClosed(new EventArgs()); await this.OnClosed(new EventArgs());
this.ParentForm?.ReturnFromModal(this);
} }
} }
} }

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base; using TelegramBotBase.Base;
namespace TelegramBotBase.Form namespace TelegramBotBase.Form
@ -18,6 +19,9 @@ namespace TelegramBotBase.Form
private static object __evCompleted { get; } = new object(); private static object __evCompleted { get; } = new object();
public bool ShowBackButton { get; set; } = false;
public String BackLabel { get; set; } = "Zurück";
public PromptDialog() public PromptDialog()
{ {
@ -34,6 +38,13 @@ namespace TelegramBotBase.Form
if (message.Handled) if (message.Handled)
return; return;
if (this.ShowBackButton && message.MessageText == BackLabel)
{
await this.CloseForm();
return;
}
if (this.Value == null) if (this.Value == null)
{ {
this.Value = message.MessageText; this.Value = message.MessageText;
@ -47,6 +58,14 @@ namespace TelegramBotBase.Form
if (this.Value == null) if (this.Value == null)
{ {
if (this.ShowBackButton)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase(BackLabel, "back"));
await this.Device.Send(this.Message, (ReplyMarkupBase)bf);
return;
}
await this.Device.Send(this.Message); await this.Device.Send(this.Message);
return; return;
} }