From 8d02b54b05ab829d9e959d9b44fc4760a807d412 Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Sat, 8 Oct 2022 20:37:21 +0300 Subject: [PATCH] fix: remove obsolete properties & fix NRE --- TelegramBotBase.Test/Program.cs | 8 +++----- .../Tests/Controls/ButtonGridForm.cs | 4 ++-- .../Tests/Controls/ButtonGridPagingForm.cs | 4 ++-- .../Tests/Controls/MultiViewForm.cs | 7 ++++--- TelegramBotBase/Base/MessageResult.cs | 9 ++++++++- TelegramBotBase/Base/ResultBase.cs | 4 ++-- TelegramBotBase/Controls/Hybrid/ButtonGrid.cs | 11 +---------- .../Controls/Hybrid/CheckedButtonList.cs | 17 +++++------------ .../Controls/Hybrid/TaggedButtonGrid.cs | 9 +-------- 9 files changed, 28 insertions(+), 45 deletions(-) diff --git a/TelegramBotBase.Test/Program.cs b/TelegramBotBase.Test/Program.cs index 044dba2..6809cb7 100644 --- a/TelegramBotBase.Test/Program.cs +++ b/TelegramBotBase.Test/Program.cs @@ -11,7 +11,7 @@ namespace TelegramBotBaseTest; internal class Program { - private static void Main(string[] args) + private static async Task Main(string[] args) { var apiKey = ""; @@ -48,16 +48,14 @@ internal class Program Console.WriteLine(en.DeviceId + " " + en.Message.MessageText + " " + (en.Message.RawData ?? "")); }; - bb.Start(); + await bb.Start(); Console.WriteLine("Telegram Bot started..."); - Console.WriteLine("Press q to quit application."); - Console.ReadLine(); - bb.Stop(); + await bb.Stop(); } private static async Task Bb_BotCommand(object sender, BotCommandEventArgs en) diff --git a/TelegramBotBase.Test/Tests/Controls/ButtonGridForm.cs b/TelegramBotBase.Test/Tests/Controls/ButtonGridForm.cs index 0669d2e..e0492ec 100644 --- a/TelegramBotBase.Test/Tests/Controls/ButtonGridForm.cs +++ b/TelegramBotBase.Test/Tests/Controls/ButtonGridForm.cs @@ -32,7 +32,7 @@ public class ButtonGridForm : AutoCleanForm bf.AddButtonRow(new ButtonBase("Button3", "b3"), new ButtonBase("Button4", "b4")); - _mButtons.ButtonsForm = bf; + _mButtons.DataSource.ButtonForm = bf; _mButtons.ButtonClicked += Bg_ButtonClicked; @@ -66,4 +66,4 @@ public class ButtonGridForm : AutoCleanForm await Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}"); } } -} \ No newline at end of file +} diff --git a/TelegramBotBase.Test/Tests/Controls/ButtonGridPagingForm.cs b/TelegramBotBase.Test/Tests/Controls/ButtonGridPagingForm.cs index 47d6205..3ce477a 100644 --- a/TelegramBotBase.Test/Tests/Controls/ButtonGridPagingForm.cs +++ b/TelegramBotBase.Test/Tests/Controls/ButtonGridPagingForm.cs @@ -38,7 +38,7 @@ public class ButtonGridPagingForm : AutoCleanForm bf.AddButtonRow(new ButtonBase(c.EnglishName, c.EnglishName)); } - _mButtons.ButtonsForm = bf; + _mButtons.DataSource.ButtonForm = bf; _mButtons.ButtonClicked += Bg_ButtonClicked; @@ -63,4 +63,4 @@ public class ButtonGridPagingForm : AutoCleanForm await Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}"); } } -} \ No newline at end of file +} diff --git a/TelegramBotBase.Test/Tests/Controls/MultiViewForm.cs b/TelegramBotBase.Test/Tests/Controls/MultiViewForm.cs index 0511cb6..7e4bba2 100644 --- a/TelegramBotBase.Test/Tests/Controls/MultiViewForm.cs +++ b/TelegramBotBase.Test/Tests/Controls/MultiViewForm.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using TelegramBotBase.Args; using TelegramBotBase.Controls.Hybrid; +using TelegramBotBase.DataSources; using TelegramBotBase.Enums; using TelegramBotBase.Form; using TelegramBotBaseTest.Tests.Controls.Subclass; @@ -26,9 +27,9 @@ public class MultiViewForm : AutoCleanForm _bg = new ButtonGrid { - ButtonsForm = new ButtonForm() + DataSource = new ButtonFormDataSource() }; - _bg.ButtonsForm.AddButtonRow("Back", "$back$"); + _bg.DataSource.ButtonForm.AddButtonRow("Back", "$back$"); _bg.ButtonClicked += Bg_ButtonClicked; _bg.KeyboardType = EKeyboardType.ReplyKeyboard; AddControl(_bg); @@ -47,4 +48,4 @@ public class MultiViewForm : AutoCleanForm break; } } -} \ No newline at end of file +} diff --git a/TelegramBotBase/Base/MessageResult.cs b/TelegramBotBase/Base/MessageResult.cs index 531edf4..c6579ef 100644 --- a/TelegramBotBase/Base/MessageResult.cs +++ b/TelegramBotBase/Base/MessageResult.cs @@ -33,7 +33,7 @@ public class MessageResult : ResultBase /// /// The message id /// - public new int MessageId => + public override int MessageId => UpdateData?.Message?.MessageId ?? Message?.MessageId ?? UpdateData?.CallbackQuery?.Message?.MessageId @@ -45,6 +45,13 @@ public class MessageResult : ResultBase public MessageType MessageType => Message?.Type ?? MessageType.Unknown; + public override Message Message => + UpdateData?.Message + ?? UpdateData?.EditedMessage + ?? UpdateData?.ChannelPost + ?? UpdateData?.EditedChannelPost + ?? UpdateData?.CallbackQuery?.Message; + /// /// Is this an action ? (i.e. button click) /// diff --git a/TelegramBotBase/Base/ResultBase.cs b/TelegramBotBase/Base/ResultBase.cs index 2845237..69a4f74 100644 --- a/TelegramBotBase/Base/ResultBase.cs +++ b/TelegramBotBase/Base/ResultBase.cs @@ -12,7 +12,7 @@ public class ResultBase : EventArgs public virtual long DeviceId { get; set; } - public int MessageId => Message.MessageId; + public virtual int MessageId => Message.MessageId; public virtual Message Message { get; set; } @@ -42,4 +42,4 @@ public class ResultBase : EventArgs { } } -} \ No newline at end of file +} diff --git a/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs b/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs index e2871d9..ca0a895 100644 --- a/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs +++ b/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs @@ -55,15 +55,6 @@ public class ButtonGrid : ControlBase public string ConfirmationText { get; set; } = ""; - /// - /// - [Obsolete("This property is obsolete. Please use the DataSource property instead.")] - public ButtonForm ButtonsForm - { - get => DataSource.ButtonForm; - set => DataSource = new ButtonFormDataSource(value); - } - /// /// Data source of the items. /// @@ -746,4 +737,4 @@ public class ButtonGrid : ControlBase break; } } -} \ No newline at end of file +} diff --git a/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs b/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs index 6e49ebe..ed44fc0 100644 --- a/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs +++ b/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs @@ -55,13 +55,6 @@ public class CheckedButtonList : ControlBase public string ConfirmationText { get; set; } = ""; - [Obsolete("This property is obsolete. Please use the DataSource property instead.")] - public ButtonForm ButtonsForm - { - get => DataSource.ButtonForm; - set => DataSource = new ButtonFormDataSource(value); - } - /// /// Data source of the items. /// @@ -277,7 +270,7 @@ public class CheckedButtonList : ControlBase foreach (var c in CheckedRows) { - lst.Add(ButtonsForm[c][0]); + lst.Add(DataSource.ButtonForm[c][0]); } return lst; @@ -418,7 +411,7 @@ public class CheckedButtonList : ControlBase Updated(); - await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, false)); + await OnCheckedChanged(new CheckedChangedEventArgs(DataSource.ButtonForm[index], index, false)); } else if (result.MessageText.EndsWith(UncheckedIconLabel)) { @@ -435,7 +428,7 @@ public class CheckedButtonList : ControlBase Updated(); - await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, true)); + await OnCheckedChanged(new CheckedChangedEventArgs(DataSource.ButtonForm[index], index, true)); } //else if (this.EnableSearch) //{ @@ -589,7 +582,7 @@ public class CheckedButtonList : ControlBase Updated(); - await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, true)); + await OnCheckedChanged(new CheckedChangedEventArgs(DataSource.ButtonForm[index], index, true)); } break; @@ -604,7 +597,7 @@ public class CheckedButtonList : ControlBase Updated(); - await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, false)); + await OnCheckedChanged(new CheckedChangedEventArgs(DataSource.ButtonForm[index], index, false)); } break; diff --git a/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs b/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs index b3ce36c..c12aced 100644 --- a/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs +++ b/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs @@ -63,13 +63,6 @@ public class TaggedButtonGrid : MultiView public string ConfirmationText { get; set; } - [Obsolete("This property is obsolete. Please use the DataSource property instead.")] - public ButtonForm ButtonsForm - { - get => DataSource.ButtonForm; - set => DataSource = new ButtonFormDataSource(value); - } - /// /// Data source of the items. /// @@ -1017,4 +1010,4 @@ public class TaggedButtonGrid : MultiView } #endregion -} \ No newline at end of file +}