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:
FlorianDahn 2021-02-28 15:34:04 +01:00
parent 91048b4f7c
commit 15a8b8897f
5 changed files with 92 additions and 58 deletions

View File

@ -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;
}
}

View File

@ -67,6 +67,10 @@ namespace TelegramBotBase.Base
}
/// <summary>
/// Will be called on a cleanup.
/// </summary>
/// <returns></returns>
public virtual async Task Cleanup()
{

View File

@ -167,67 +167,68 @@ 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)
return;
if (result.MessageText == PreviousPageLabel)
{
if (this.CurrentPageIndex > 0)
this.CurrentPageIndex--;
this.Updated();
}
else if (result.MessageText == NextPageLabel)
{
if (this.CurrentPageIndex < this.PageCount - 1)
this.CurrentPageIndex++;
this.Updated();
}
else if (this.EnableSearch)
{
if (result.MessageText.StartsWith("🔍"))
{
//Sent note about searching
if (this.SearchQuery == null)
{
await this.Device.Send(this.SearchLabel);
}
this.SearchQuery = null;
this.Updated();
return;
}
this.SearchQuery = result.MessageText;
if (this.SearchQuery != null && this.SearchQuery != "")
{
this.CurrentPageIndex = 0;
this.Updated();
}
}
await OnButtonClicked(new ButtonClickedEventArgs(button, index));
//Remove button click message
if (this.DeletePreviousMessage)
await Device.DeleteMessage(result.MessageId);
result.Handled = true;
return;
}
await OnButtonClicked(new ButtonClickedEventArgs(button));
if (result.MessageText == PreviousPageLabel)
{
if (this.CurrentPageIndex > 0)
this.CurrentPageIndex--;
this.Updated();
}
else if (result.MessageText == NextPageLabel)
{
if (this.CurrentPageIndex < this.PageCount - 1)
this.CurrentPageIndex++;
this.Updated();
}
else if (this.EnableSearch)
{
if (result.MessageText.StartsWith("🔍"))
{
//Sent note about searching
if (this.SearchQuery == null)
{
await this.Device.Send(this.SearchLabel);
}
this.SearchQuery = null;
this.Updated();
return;
}
this.SearchQuery = result.MessageText;
if (this.SearchQuery != null && this.SearchQuery != "")
{
this.CurrentPageIndex = 0;
this.Updated();
}
}
//Remove button click message
if (this.DeletePreviousMessage)
await Device.DeleteMessage(result.MessageId);
result.Handled = true;
}
@ -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;

View File

@ -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;

View File

@ -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>