- Added ToggleButton as an easy switch control (i.e. for settings page)
- added control id, for easier separating of controls in one form - added automatic event selection for specific controls, to not raise an event for other controls who has invoked it - changed Action method - added example
This commit is contained in:
parent
805ffb9ec7
commit
56c7754408
49
TelegramBaseTest/Tests/Controls/ToggleButtons.cs
Normal file
49
TelegramBaseTest/Tests/Controls/ToggleButtons.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Controls;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBaseTest.Tests.Controls
|
||||||
|
{
|
||||||
|
public class ToggleButtons : AutoCleanForm
|
||||||
|
{
|
||||||
|
public ToggleButtons()
|
||||||
|
{
|
||||||
|
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||||
|
|
||||||
|
this.Init += ToggleButtons_Init;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ToggleButtons_Init(object sender, TelegramBotBase.Base.InitEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
var tb = new ToggleButton();
|
||||||
|
tb.Checked = true;
|
||||||
|
tb.Toggled += Tb_Toggled;
|
||||||
|
|
||||||
|
this.AddControl(tb);
|
||||||
|
|
||||||
|
tb = new ToggleButton();
|
||||||
|
tb.Checked = false;
|
||||||
|
tb.Toggled += Tb_Toggled;
|
||||||
|
|
||||||
|
this.AddControl(tb);
|
||||||
|
|
||||||
|
tb = new ToggleButton();
|
||||||
|
tb.Checked = true;
|
||||||
|
tb.Toggled += Tb_Toggled;
|
||||||
|
|
||||||
|
this.AddControl(tb);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Tb_Toggled(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var tb = sender as ToggleButton;
|
||||||
|
Console.WriteLine(tb.ID.ToString() + " was pressed, and toggled to " + (tb.Checked ? "Checked" : "Unchecked"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -113,6 +113,16 @@ namespace TelegramBaseTest.Tests
|
|||||||
|
|
||||||
await this.NavigateTo(tree);
|
await this.NavigateTo(tree);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "togglebuttons":
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var tb = new Controls.ToggleButtons();
|
||||||
|
|
||||||
|
await this.NavigateTo(tb);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +148,8 @@ namespace TelegramBaseTest.Tests
|
|||||||
|
|
||||||
btn.AddButtonRow(new ButtonBase("#10 TreeView", new CallbackData("a", "treeview").Serialize()));
|
btn.AddButtonRow(new ButtonBase("#10 TreeView", new CallbackData("a", "treeview").Serialize()));
|
||||||
|
|
||||||
|
btn.AddButtonRow(new ButtonBase("#11 ToggleButtons", new CallbackData("a", "togglebuttons").Serialize()));
|
||||||
|
|
||||||
await this.Device.Send("Choose your test:", btn);
|
await this.Device.Send("Choose your test:", btn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,18 +15,28 @@ namespace TelegramBotBase.Base
|
|||||||
|
|
||||||
public int ID { get; set; }
|
public int ID { get; set; }
|
||||||
|
|
||||||
|
public String ControlID
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "#c" + this.ID.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines if the control should be rendered and invoked with actions
|
/// Defines if the control should be rendered and invoked with actions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Enabled { get; set; } = true;
|
public bool Enabled { get; set; } = true;
|
||||||
|
|
||||||
public virtual async Task Action(MessageResult result)
|
public virtual async Task Action(MessageResult result, String value = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public virtual async Task Render(MessageResult result)
|
public virtual async Task Render(MessageResult result)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@ -169,6 +169,17 @@ namespace TelegramBotBase.Form
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task ActionControls(MessageResult message)
|
public async Task ActionControls(MessageResult message)
|
||||||
{
|
{
|
||||||
|
//Looking for the control by id, if not listened, raise event for all
|
||||||
|
if (message.RawData.StartsWith("#c"))
|
||||||
|
{
|
||||||
|
var c = this.Controls.FirstOrDefault(a => a.ControlID == message.RawData.Split('_')[0]);
|
||||||
|
if (c != null)
|
||||||
|
{
|
||||||
|
await c.Action(message, message.RawData.Split('_')[1]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var b in this.Controls)
|
foreach (var b in this.Controls)
|
||||||
{
|
{
|
||||||
if (!b.Enabled)
|
if (!b.Enabled)
|
||||||
|
|||||||
@ -49,7 +49,7 @@ namespace TelegramBotBase.Controls
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public override async Task Action(MessageResult result)
|
public override async Task Action(MessageResult result, String value = null)
|
||||||
{
|
{
|
||||||
await result.ConfirmAction();
|
await result.ConfirmAction();
|
||||||
|
|
||||||
|
|||||||
145
TelegramBotBase/Controls/ToggleButton.cs
Normal file
145
TelegramBotBase/Controls/ToggleButton.cs
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Controls
|
||||||
|
{
|
||||||
|
public class ToggleButton : ControlBase
|
||||||
|
{
|
||||||
|
|
||||||
|
public String UncheckedIcon { get; set; } = "⚪";
|
||||||
|
|
||||||
|
public String CheckedIcon { get; set; } = "⚫";
|
||||||
|
|
||||||
|
public String CheckedString { get; set; } = "On";
|
||||||
|
|
||||||
|
public String UncheckedString { get; set; } = "Off";
|
||||||
|
|
||||||
|
public String Title { get; set; } = "Toggle";
|
||||||
|
|
||||||
|
public int? MessageId { get; set; }
|
||||||
|
|
||||||
|
public bool Checked { get; set; }
|
||||||
|
|
||||||
|
private bool RenderNecessary = true;
|
||||||
|
|
||||||
|
private static readonly object __evToggled = new object();
|
||||||
|
|
||||||
|
private readonly EventHandlerList Events = new EventHandlerList();
|
||||||
|
|
||||||
|
|
||||||
|
public ToggleButton()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ToggleButton(String CheckedString, String UncheckedString)
|
||||||
|
{
|
||||||
|
this.CheckedString = CheckedString;
|
||||||
|
this.UncheckedString = UncheckedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public event EventHandler Toggled
|
||||||
|
{
|
||||||
|
add
|
||||||
|
{
|
||||||
|
this.Events.AddHandler(__evToggled, value);
|
||||||
|
}
|
||||||
|
remove
|
||||||
|
{
|
||||||
|
this.Events.RemoveHandler(__evToggled, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnToggled(EventArgs e)
|
||||||
|
{
|
||||||
|
(this.Events[__evToggled] as EventHandler)?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult result, String value = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (result.Handled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await result.ConfirmAction();
|
||||||
|
|
||||||
|
switch (value ?? "unknown")
|
||||||
|
{
|
||||||
|
case "on":
|
||||||
|
|
||||||
|
if (this.Checked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
RenderNecessary = true;
|
||||||
|
|
||||||
|
this.Checked = true;
|
||||||
|
|
||||||
|
OnToggled(new EventArgs());
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "off":
|
||||||
|
|
||||||
|
if (!this.Checked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
RenderNecessary = true;
|
||||||
|
|
||||||
|
this.Checked = false;
|
||||||
|
|
||||||
|
OnToggled(new EventArgs());
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
RenderNecessary = false;
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Handled = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult result)
|
||||||
|
{
|
||||||
|
if (!RenderNecessary)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var bf = new ButtonForm(this);
|
||||||
|
|
||||||
|
ButtonBase bOn = new ButtonBase((this.Checked ? CheckedIcon : UncheckedIcon) + " " + CheckedString, "on");
|
||||||
|
|
||||||
|
ButtonBase bOff = new ButtonBase((!this.Checked ? CheckedIcon : UncheckedIcon) + " " + UncheckedString, "off");
|
||||||
|
|
||||||
|
bf.AddButtonRow(bOn, bOff);
|
||||||
|
|
||||||
|
if (this.MessageId != null)
|
||||||
|
{
|
||||||
|
var m = await this.Device.Edit(this.MessageId.Value, this.Title, bf);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var m = await this.Device.Send(this.Title, bf, disableNotification: true);
|
||||||
|
if (m != null)
|
||||||
|
{
|
||||||
|
this.MessageId = m.MessageId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.RenderNecessary = false;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -29,16 +29,16 @@ namespace TelegramBotBase.Controls
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override async Task Action(MessageResult result)
|
public override async Task Action(MessageResult result, String value = null)
|
||||||
{
|
{
|
||||||
await result.ConfirmAction();
|
await result.ConfirmAction();
|
||||||
|
|
||||||
if (result.Handled)
|
if (result.Handled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var value = result.RawData;
|
var val = result.RawData;
|
||||||
|
|
||||||
switch (value)
|
switch (val)
|
||||||
{
|
{
|
||||||
case "up":
|
case "up":
|
||||||
case "parent":
|
case "parent":
|
||||||
@ -50,7 +50,7 @@ namespace TelegramBotBase.Controls
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
||||||
var n = (this.VisibleNode != null ? this.VisibleNode.FindNodeByValue(value) : this.Nodes.FirstOrDefault(a => a.Value == value));
|
var n = (this.VisibleNode != null ? this.VisibleNode.FindNodeByValue(val) : this.Nodes.FirstOrDefault(a => a.Value == val));
|
||||||
|
|
||||||
if (n != null)
|
if (n != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -31,11 +31,12 @@ namespace TelegramBotBase.Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public InlineKeyboardButton ToInlineButton()
|
public InlineKeyboardButton ToInlineButton(ButtonForm form)
|
||||||
{
|
{
|
||||||
|
String id = (form.DependencyControl != null ? form.DependencyControl.ControlID + "_" : "");
|
||||||
if (this.Url == null)
|
if (this.Url == null)
|
||||||
{
|
{
|
||||||
return InlineKeyboardButton.WithCallbackData(this.Text, this.Value);
|
return InlineKeyboardButton.WithCallbackData(this.Text, id + this.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ikb = new InlineKeyboardButton();
|
var ikb = new InlineKeyboardButton();
|
||||||
|
|||||||
@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types.ReplyMarkups;
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
|
||||||
namespace TelegramBotBase.Form
|
namespace TelegramBotBase.Form
|
||||||
{
|
{
|
||||||
@ -17,12 +18,18 @@ namespace TelegramBotBase.Form
|
|||||||
|
|
||||||
public IReplyMarkup Markup { get; set; }
|
public IReplyMarkup Markup { get; set; }
|
||||||
|
|
||||||
|
public ControlBase DependencyControl { get; set; }
|
||||||
|
|
||||||
public ButtonForm()
|
public ButtonForm()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ButtonForm(ControlBase control)
|
||||||
|
{
|
||||||
|
this.DependencyControl = control;
|
||||||
|
}
|
||||||
|
|
||||||
public void AddButtonRow(IEnumerable<ButtonBase> row)
|
public void AddButtonRow(IEnumerable<ButtonBase> row)
|
||||||
{
|
{
|
||||||
Buttons.Add(row.ToList());
|
Buttons.Add(row.ToList());
|
||||||
@ -70,7 +77,7 @@ namespace TelegramBotBase.Form
|
|||||||
|
|
||||||
public InlineKeyboardButton[][] ToArray()
|
public InlineKeyboardButton[][] ToArray()
|
||||||
{
|
{
|
||||||
var ikb = this.Buttons.Select(a => a.Select(b => b.ToInlineButton()).ToArray()).ToArray();
|
var ikb = this.Buttons.Select(a => a.Select(b => b.ToInlineButton(this)).ToArray()).ToArray();
|
||||||
|
|
||||||
return ikb;
|
return ikb;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user