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; } = "";

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"] = "Текст метки по умолчанию";
} }
} }