Updates and improvements
- refactoring of ButtonGrid control for more readability - refactoring of TaggedButtonGrid control for more readability - adding Index parameter to ButtenClickedEventArgs - adding FindRowByButton method to ButtonForm to get the row index -
This commit is contained in:
parent
91048b4f7c
commit
15a8b8897f
@ -14,6 +14,8 @@ namespace TelegramBotBase.Args
|
||||
{
|
||||
public ButtonBase Button { get; set; }
|
||||
|
||||
public int Index { get; set; }
|
||||
|
||||
|
||||
public ButtonClickedEventArgs()
|
||||
{
|
||||
@ -23,6 +25,13 @@ namespace TelegramBotBase.Args
|
||||
public ButtonClickedEventArgs(ButtonBase button)
|
||||
{
|
||||
this.Button = button;
|
||||
this.Index = -1;
|
||||
}
|
||||
|
||||
public ButtonClickedEventArgs(ButtonBase button, int Index)
|
||||
{
|
||||
this.Button = button;
|
||||
this.Index = Index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -67,6 +67,10 @@ namespace TelegramBotBase.Base
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will be called on a cleanup.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual async Task Cleanup()
|
||||
{
|
||||
|
||||
|
||||
@ -167,14 +167,27 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
if (result.MessageText == null)
|
||||
return;
|
||||
|
||||
var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
?? ButtonsForm.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||
|
||||
if (button == null)
|
||||
var index = ButtonsForm.FindRowByButton(button);
|
||||
|
||||
if (button != null)
|
||||
{
|
||||
if (result.MessageText == null)
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeletePreviousMessage)
|
||||
await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (result.MessageText == PreviousPageLabel)
|
||||
{
|
||||
@ -217,18 +230,6 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeletePreviousMessage)
|
||||
await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
}
|
||||
|
||||
public async override Task Action(MessageResult result, string value = null)
|
||||
@ -250,9 +251,11 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
?? ButtonsForm.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||
|
||||
var index = ButtonsForm.FindRowByButton(button);
|
||||
|
||||
if (button != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
|
||||
@ -178,12 +178,18 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
if (result.MessageText == null)
|
||||
return;
|
||||
|
||||
var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
?? ButtonsForm.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||
|
||||
if (result.MessageText == null)
|
||||
return;
|
||||
var index = HeadLayoutButtonRow?.IndexOf(button)
|
||||
?? SubHeadLayoutButtonRow?.IndexOf(button)
|
||||
?? ButtonsForm.ToList().IndexOf(button);
|
||||
|
||||
|
||||
|
||||
switch (this.SelectedViewIndex)
|
||||
{
|
||||
@ -191,7 +197,7 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
|
||||
if (button != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeletePreviousMessage)
|
||||
@ -304,6 +310,10 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
|
||||
public async override Task Action(MessageResult result, string value = null)
|
||||
{
|
||||
//Find clicked button depending on Text or Value (depending on markup type)
|
||||
if (this.KeyboardType != eKeyboardType.InlineKeyBoard)
|
||||
return;
|
||||
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
@ -312,18 +322,17 @@ namespace TelegramBotBase.Controls.Hybrid
|
||||
|
||||
await result.ConfirmAction(this.ConfirmationText ?? "");
|
||||
|
||||
//Find clicked button depending on Text or Value (depending on markup type)
|
||||
if (this.KeyboardType != eKeyboardType.InlineKeyBoard)
|
||||
return;
|
||||
|
||||
|
||||
var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
?? ButtonsForm.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||
|
||||
var index = HeadLayoutButtonRow?.IndexOf(button)
|
||||
?? SubHeadLayoutButtonRow?.IndexOf(button)
|
||||
?? ButtonsForm.ToList().IndexOf(button);
|
||||
|
||||
if (button != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
|
||||
@ -151,6 +151,15 @@ namespace TelegramBotBase.Form
|
||||
return ikb;
|
||||
}
|
||||
|
||||
public int FindRowByButton(ButtonBase button)
|
||||
{
|
||||
var row = this.Buttons.FirstOrDefault(a => a.Count(b => b == button) > 0);
|
||||
if (row == null)
|
||||
return -1;
|
||||
|
||||
return this.Buttons.IndexOf(row);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the first Button with the given value.
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user