diff --git a/TelegramBotBase/Datasources/ButtonFormDataSource.cs b/TelegramBotBase/Datasources/ButtonFormDataSource.cs new file mode 100644 index 0000000..e91668c --- /dev/null +++ b/TelegramBotBase/Datasources/ButtonFormDataSource.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using TelegramBotBase.Controls.Hybrid; +using TelegramBotBase.Form; +using TelegramBotBase.Interfaces; + +namespace TelegramBotBase.DataSources; + +public class ButtonFormDataSource : IDataSource +{ + private ButtonForm _buttonform; + + public ButtonFormDataSource() + { + _buttonform = new ButtonForm(); + } + + public ButtonFormDataSource(ButtonForm bf) + { + _buttonform = bf; + } + + public virtual ButtonForm ButtonForm + { + get => _buttonform; + set => _buttonform = value; + } + + + /// + /// Returns the amount of rows. + /// + public virtual int RowCount => ButtonForm.Rows; + + /// + /// Returns the maximum amount of columns. + /// + public virtual int ColumnCount => ButtonForm.Cols; + + + /// + /// Returns the amount of rows existing. + /// + /// + public virtual int Count => ButtonForm.Count; + + /// + /// Returns the row with the specific index. + /// + /// + /// + public virtual ButtonRow ItemAt(int index) + { + return ButtonForm[index]; + } + + public virtual List ItemRange(int start, int count) + { + return ButtonForm.GetRange(start, count); + } + + public virtual List AllItems() + { + return ButtonForm.ToArray(); + } + + public virtual ButtonForm PickItems(int start, int count, string filter = null) + { + var bf = new ButtonForm(); + ButtonForm dataForm = null; + + if (filter == null) + { + dataForm = ButtonForm.Duplicate(); + } + else + { + dataForm = ButtonForm.FilterDuplicate(filter, true); + } + + for (var i = 0; i < count; i++) + { + var it = start + i; + + if (it > dataForm.Rows - 1) + { + break; + } + + bf.AddButtonRow(dataForm[it]); + } + + return bf; + } + + public virtual ButtonForm PickAllItems(string filter = null) + { + if (filter == null) + { + return ButtonForm.Duplicate(); + } + + + return ButtonForm.FilterDuplicate(filter, true); + } + + public virtual Tuple FindRow(string text, bool useText = true) + { + return ButtonForm.FindRow(text, useText); + } + + /// + /// Returns the maximum items of this data source. + /// + /// + /// + public virtual int CalculateMax(string filter = null) + { + return PickAllItems(filter).Rows; + } + + public virtual ButtonRow Render(object data) + { + return data as ButtonRow; + } + + + public static implicit operator ButtonFormDataSource(ButtonForm bf) + { + return new ButtonFormDataSource(bf); + } + + public static implicit operator ButtonForm(ButtonFormDataSource ds) + { + return ds.ButtonForm; + } +}