Adding new simple label control and example
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Localizations;
|
||||
|
||||
namespace TelegramBotBase.Controls.Inline;
|
||||
|
||||
[DebuggerDisplay("{Text}")]
|
||||
public class LabelControl : ControlBase
|
||||
{
|
||||
private bool _renderNecessary = true;
|
||||
|
||||
private string _text = string.Empty;
|
||||
|
||||
public String Text
|
||||
{
|
||||
get
|
||||
{
|
||||
return _text;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_text == value)
|
||||
return;
|
||||
|
||||
|
||||
_text = value;
|
||||
_renderNecessary = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private ParseMode _parseMode = ParseMode.Markdown;
|
||||
|
||||
public ParseMode ParseMode
|
||||
{
|
||||
get
|
||||
{
|
||||
return _parseMode;
|
||||
}
|
||||
set
|
||||
{
|
||||
_parseMode = value;
|
||||
_renderNecessary = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public LabelControl()
|
||||
{
|
||||
}
|
||||
|
||||
public LabelControl(string text)
|
||||
{
|
||||
_text = text;
|
||||
}
|
||||
|
||||
public LabelControl(string text, ParseMode parseMode)
|
||||
{
|
||||
_text = text;
|
||||
_parseMode = parseMode;
|
||||
}
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
|
||||
|
||||
public override async Task Render(MessageResult result)
|
||||
{
|
||||
if (!_renderNecessary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Message m;
|
||||
|
||||
//Update ?
|
||||
if (MessageId != null)
|
||||
{
|
||||
m = await Device.Raw(a => a.EditMessageTextAsync(Device.DeviceId, MessageId.Value, Text, _parseMode));
|
||||
_renderNecessary = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//New Message
|
||||
m = await Device.Raw(a => a.SendTextMessageAsync(Device.DeviceId, Text, disableNotification: true, parseMode: _parseMode));
|
||||
if (m != null)
|
||||
{
|
||||
MessageId = m.MessageId;
|
||||
}
|
||||
|
||||
_renderNecessary = false;
|
||||
}
|
||||
|
||||
public override async Task Cleanup()
|
||||
{
|
||||
if (this.MessageId == null)
|
||||
return;
|
||||
|
||||
|
||||
await Device.DeleteMessage(MessageId.Value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user