Adding null/empty checks to some controls to prevent invalid behaviour

This commit is contained in:
Florian Zevedei 2024-06-09 14:19:04 +02:00
parent 834038ff44
commit 6adcc52ea2
10 changed files with 130 additions and 22 deletions

View File

@ -51,7 +51,24 @@ public class ButtonGrid : ControlBase
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } = ""; public string ConfirmationText { get; set; } = "";
@ -340,14 +357,14 @@ public class ButtonGrid : ControlBase
} }
//var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText) //var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText)
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText); // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText);
// bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText) // bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText)
//var index = bf.FindRowByButton(button); //var index = bf.FindRowByButton(button);
check: check:
//Remove button click message //Remove button click message
if (DeleteReplyMessage) if (DeleteReplyMessage)
@ -449,15 +466,15 @@ public class ButtonGrid : ControlBase
} }
//var bf = DataSource.ButtonForm; //var bf = DataSource.ButtonForm;
//var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) //var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
// ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData); // ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData);
//var index = bf.FindRowByButton(button); //var index = bf.FindRowByButton(button);
check: check:
if (match != null) if (match != null)
{ {
await result.ConfirmAction(ConfirmationText ?? ""); await result.ConfirmAction(ConfirmationText ?? "");
@ -506,13 +523,13 @@ public class ButtonGrid : ControlBase
if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !EnablePaging) if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !EnablePaging)
{ {
throw new MaximumRowsReachedException throw new MaximumRowsReachedException
{ Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows }; { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows };
} }
if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols) if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols)
{ {
throw new MaximumColsException throw new MaximumColsException
{ Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols }; { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols };
} }
break; break;
@ -522,13 +539,13 @@ public class ButtonGrid : ControlBase
if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !EnablePaging) if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !EnablePaging)
{ {
throw new MaximumRowsReachedException throw new MaximumRowsReachedException
{ Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows }; { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows };
} }
if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols) if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols)
{ {
throw new MaximumColsException throw new MaximumColsException
{ Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols }; { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols };
} }
break; break;
@ -693,7 +710,7 @@ public class ButtonGrid : ControlBase
Updated(); Updated();
} }
else else
//Remove event handler //Remove event handler
{ {
Device.MessageDeleted -= Device_MessageDeleted; Device.MessageDeleted -= Device_MessageDeleted;
} }

View File

@ -13,6 +13,7 @@ using TelegramBotBase.Enums;
using TelegramBotBase.Exceptions; using TelegramBotBase.Exceptions;
using TelegramBotBase.Form; using TelegramBotBase.Form;
using TelegramBotBase.Localizations; using TelegramBotBase.Localizations;
using static System.Net.Mime.MediaTypeNames;
using static TelegramBotBase.Base.Async; using static TelegramBotBase.Base.Async;
namespace TelegramBotBase.Controls.Hybrid; namespace TelegramBotBase.Controls.Hybrid;
@ -51,7 +52,24 @@ public class CheckedButtonList : ControlBase
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } = ""; public string ConfirmationText { get; set; } = "";

View File

@ -67,7 +67,24 @@ public class TaggedButtonGrid : MultiView
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } public string ConfirmationText { get; set; }

View File

@ -16,7 +16,7 @@ public class Label : ControlBase
{ {
private bool _renderNecessary = true; private bool _renderNecessary = true;
private string _text = string.Empty; private string _text = Default.Language["Label_Text"];
public String Text public String Text
{ {
@ -29,6 +29,10 @@ public class Label : ControlBase
if (_text == value) if (_text == value)
return; return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Text)}", $"{nameof(Text)} property must have been a value unequal to null/empty");
}
_text = value; _text = value;
_renderNecessary = true; _renderNecessary = true;
@ -36,6 +40,8 @@ public class Label : ControlBase
} }
} }
private ParseMode _parseMode = ParseMode.Markdown; private ParseMode _parseMode = ParseMode.Markdown;
public ParseMode ParseMode public ParseMode ParseMode

View File

@ -32,10 +32,32 @@ public class MultiToggleButton : ControlBase
/// </summary> /// </summary>
public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"]; public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"];
private string _title = Default.Language["MultiToggleButton_Title"];
/// <summary> /// <summary>
/// This holds the title of the control. /// This holds the title of the control.
/// </summary> /// </summary>
public string Title { get; set; } = Default.Language["MultiToggleButton_Title"]; public String Title
{
get
{
return _title;
}
set
{
if (_title == value)
return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} must have been a value unequal to null/empty");
}
_title = value;
_renderNecessary = true;
}
}
public int? MessageId { get; set; } public int? MessageId { get; set; }

View File

@ -36,7 +36,31 @@ public class ToggleButton : ControlBase
public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"]; public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"];
public string Title { get; set; } = Default.Language["ToggleButton_Title"]; private string _title = Default.Language["ToggleButton_Title"];
public String Title
{
get
{
return _title;
}
set
{
if (_title == value)
return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
_title = value;
_renderNecessary = true;
}
}
public int? MessageId { get; set; } public int? MessageId { get; set; }

View File

@ -34,5 +34,6 @@ public sealed class English : Localization
Values["ToggleButton_Changed"] = "Setting changed"; Values["ToggleButton_Changed"] = "Setting changed";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Default label text";
} }
} }

View File

@ -34,5 +34,6 @@ public sealed class German : Localization
Values["ToggleButton_Changed"] = "Einstellung geändert"; Values["ToggleButton_Changed"] = "Einstellung geändert";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Standard Label Text";
} }
} }

View File

@ -34,6 +34,7 @@ public sealed class Persian : Localization
Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد"; Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "متن برچسب پیش‌فرض";
} }
} }

View File

@ -34,5 +34,6 @@ public sealed class Russian : Localization
Values["ToggleButton_Changed"] = "Настройки изменены"; Values["ToggleButton_Changed"] = "Настройки изменены";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Текст метки по умолчанию";
} }
} }