98 lines
3.1 KiB
C#
98 lines
3.1 KiB
C#
namespace BotPages.Core
|
|
{
|
|
/// <summary>
|
|
/// Билдер для удобного создания <see cref="PageResult"/>.
|
|
/// Мутабельный, но итоговый объект иммутабелен.
|
|
/// </summary>
|
|
public sealed class PageResultBuilder
|
|
{
|
|
private PageNavigate? _navigateTo;
|
|
private PageMessage? _message;
|
|
private List<FileDescriptor>? _files;
|
|
private List<PageAction>? _actions;
|
|
|
|
/// <summary>
|
|
/// Устанавливает текст сообщения.
|
|
/// </summary>
|
|
public PageResultBuilder WithText(string text, MessageFormat format)
|
|
=> WithText(new PageMessage()
|
|
{
|
|
Text = text,
|
|
Format = format,
|
|
});
|
|
|
|
/// <summary>
|
|
/// Устанавливает текст сообщения.
|
|
/// </summary>
|
|
public PageResultBuilder WithText(string text)
|
|
=> WithText(new PageMessage()
|
|
{
|
|
Text = text,
|
|
Format = MessageFormat.Plain,
|
|
});
|
|
|
|
/// <summary>
|
|
/// Устанавливает текст сообщения.
|
|
/// </summary>
|
|
public PageResultBuilder WithText(PageMessage message)
|
|
{
|
|
_message = message;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавляет клавиатуру (набор кнопок).
|
|
/// </summary>
|
|
public PageResultBuilder WithKeyboard(IEnumerable<PageAction> actions)
|
|
{
|
|
_actions = actions?.ToList();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавляет файлы.
|
|
/// </summary>
|
|
public PageResultBuilder WithFiles(IEnumerable<FileDescriptor> files)
|
|
{
|
|
_files = files?.ToList();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Устанавливает навигацию на другую страницу.
|
|
/// </summary>
|
|
public PageResultBuilder WithNavigate(string pageId, object? args = null, bool replace = true)
|
|
=> WithNavigate(new PageNavigate()
|
|
{
|
|
PageId = pageId,
|
|
Args = args,
|
|
Replace = replace,
|
|
});
|
|
|
|
/// <summary>
|
|
/// Устанавливает навигацию на другую страницу.
|
|
/// </summary>
|
|
public PageResultBuilder WithNavigate(PageNavigate navigate)
|
|
{
|
|
_navigateTo = navigate;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Собирает итоговый иммутабельный <see cref="PageResult"/>.
|
|
/// </summary>
|
|
public PageResult Build() => new PageResult
|
|
{
|
|
Message = _message,
|
|
Actions = _actions,
|
|
Files = _files,
|
|
NavigateTo = _navigateTo,
|
|
};
|
|
|
|
/// <summary>
|
|
/// Создаёт новый пустой билдер.
|
|
/// </summary>
|
|
public static PageResultBuilder Empty() => new PageResultBuilder();
|
|
}
|
|
|
|
} |