From 57b37062416febed26d44f0c9fb8175f6ee698fe Mon Sep 17 00:00:00 2001 From: FrigaT Date: Sat, 6 Dec 2025 07:52:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5?= =?UTF-8?q?=20=D1=80=D0=B0=D1=81=D1=88=D0=B8=D1=80=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BotPages.Core/BotPagesApp.cs | 2 +- BotPages.Core/Context/PageContext.cs | 41 ------------------- .../Context/PageContextAdapterExtensions.cs | 31 ++++++++++++++ .../PageContextNavigationExtensions.cs | 38 +++++++++++++++++ .../Context/PageContextStorageExtensions.cs | 26 ++++++++++++ BotPages.Core/Navigation/NavigationService.cs | 2 +- Demo/Pages/SubmitPage.cs | 2 +- Demo/Pages/TitlePage.cs | 2 +- 8 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 BotPages.Core/Context/PageContextAdapterExtensions.cs create mode 100644 BotPages.Core/Context/PageContextNavigationExtensions.cs create mode 100644 BotPages.Core/Context/PageContextStorageExtensions.cs diff --git a/BotPages.Core/BotPagesApp.cs b/BotPages.Core/BotPagesApp.cs index 60b7989..67be146 100644 --- a/BotPages.Core/BotPagesApp.cs +++ b/BotPages.Core/BotPagesApp.cs @@ -201,7 +201,7 @@ public sealed class BotPagesApp if (page is null) { - await ctx.Navigation.GoToHome(ctx, ct); + await ctx.Navigation.GoToHomeAsync(ctx, ct); return; } diff --git a/BotPages.Core/Context/PageContext.cs b/BotPages.Core/Context/PageContext.cs index 2b4b008..fdebe40 100644 --- a/BotPages.Core/Context/PageContext.cs +++ b/BotPages.Core/Context/PageContext.cs @@ -22,47 +22,6 @@ public sealed class PageContext /// Адаптер мессенджера. public required IMessengerAdapter Adapter { get; init; } - //Storage - - /// Получить состояние по ключу. - public Task GetStorageAsync(string key, CancellationToken ct) - => StateStorage.GetAsync(SessionKey, key, ct); - - /// Сохранить состояние по ключу. - public Task SetStorageAsync(string key, T state, CancellationToken ct) - => StateStorage.SetAsync(SessionKey, key, state, ct); - - /// Удалить состояние по ключу. - public Task RemoveStorageAsync(string key, CancellationToken ct) - => StateStorage.RemoveAsync(SessionKey, key, ct); - - /// Удалить все состояния по ключу. - public Task ClearStorageAsync(CancellationToken ct) - => StateStorage.ClearAsync(SessionKey, ct); - - //Adapter - - /// - /// Отправить текстовое сообщение. - /// - public Task SendTextAsync(string text, MessageFormat format = MessageFormat.Plain, - IEnumerable>? inline = null, - IEnumerable>? reply = null, - CancellationToken ct = default) - => Adapter.SendTextAsync(this.Update.Chat.Id, text, format, inline, reply, ct); - - /// - /// Отправить файл. - /// - public Task SendFileAsync(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default) - => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, captionFormat, ct); - - /// - /// Отправить файл. - /// - public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default) - => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, null, ct); - /// /// Получить билдер альбомов. /// diff --git a/BotPages.Core/Context/PageContextAdapterExtensions.cs b/BotPages.Core/Context/PageContextAdapterExtensions.cs new file mode 100644 index 0000000..246f52d --- /dev/null +++ b/BotPages.Core/Context/PageContextAdapterExtensions.cs @@ -0,0 +1,31 @@ +using BotPages.Core.Abstractions; +using BotPages.Core.Messaging; + +namespace BotPages.Core; + +/// +/// Расширения для работы с +/// +public static class PageContextAdapterExtensions +{ + /// + /// Отправить текстовое сообщение. + /// + public static Task SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain, + IEnumerable>? inline = null, + IEnumerable>? reply = null, + CancellationToken ct = default) + => ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, ct); + + /// + /// Отправить файл. + /// + public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default) + => ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, captionFormat, ct); + + /// + /// Отправить файл. + /// + public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, CancellationToken ct = default) + => ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, null, ct); +} \ No newline at end of file diff --git a/BotPages.Core/Context/PageContextNavigationExtensions.cs b/BotPages.Core/Context/PageContextNavigationExtensions.cs new file mode 100644 index 0000000..282f02d --- /dev/null +++ b/BotPages.Core/Context/PageContextNavigationExtensions.cs @@ -0,0 +1,38 @@ +namespace BotPages.Core; + +/// +/// Расширения для работы с +/// +public static class PageContextNavigationExtensions +{ + + /// + /// Перейти по маршруту без аргументов. + /// + public static Task GoToHomeAsync(this PageContext ctx, CancellationToken ct) + => ctx.Navigation.GoToHomeAsync(ctx, ct); + + /// + /// Перейти по маршруту без аргументов. + /// + public static Task GoToAsync(this PageContext ctx, string route, CancellationToken ct) + => ctx.Navigation.GoToAsync(route, ctx, ct); + + /// + /// Перейти по маршруту с аргументами. + /// + public static Task GoToAsync(this PageContext ctx, string route, TArgs args, CancellationToken ct) + => ctx.Navigation.GoToAsync(route, args, ctx, ct); + + /// + /// Перейти на страницу без аргументов. + /// + public static Task GoToAsync(this PageContext ctx, CancellationToken ct) where TPage : Page + => ctx.Navigation.GoToAsync(ctx, ct); + + /// + /// Перейти на страницу с аргументами. + /// + public static Task GoToAsync(this PageContext ctx, TArgs args, CancellationToken ct) where TPage : StatefullPage + => ctx.Navigation.GoToAsync(ctx, args!, ct); +} diff --git a/BotPages.Core/Context/PageContextStorageExtensions.cs b/BotPages.Core/Context/PageContextStorageExtensions.cs new file mode 100644 index 0000000..24a52cc --- /dev/null +++ b/BotPages.Core/Context/PageContextStorageExtensions.cs @@ -0,0 +1,26 @@ +using BotPages.Core.Abstractions; + +namespace BotPages.Core; + +/// +/// Расширения для работы с +/// +public static class PageContextStorageExtensions +{ + + /// Получить состояние по ключу. + public static Task GetStorageAsync(this PageContext ctx, string key, CancellationToken ct) + => ctx.StateStorage.GetAsync(ctx.SessionKey, key, ct); + + /// Сохранить состояние по ключу. + public static Task SetStorageAsync(this PageContext ctx, string key, T state, CancellationToken ct) + => ctx.StateStorage.SetAsync(ctx.SessionKey, key, state, ct); + + /// Удалить состояние по ключу. + public static Task RemoveStorageAsync(this PageContext ctx, string key, CancellationToken ct) + => ctx.StateStorage.RemoveAsync(ctx.SessionKey, key, ct); + + /// Удалить все состояния по ключу. + public static Task ClearStorageAsync(this PageContext ctx, CancellationToken ct) + => ctx.StateStorage.ClearAsync(ctx.SessionKey, ct); +} diff --git a/BotPages.Core/Navigation/NavigationService.cs b/BotPages.Core/Navigation/NavigationService.cs index 5e634a6..ad6a89c 100644 --- a/BotPages.Core/Navigation/NavigationService.cs +++ b/BotPages.Core/Navigation/NavigationService.cs @@ -31,7 +31,7 @@ public sealed class NavigationService /// /// Перейти по маршруту без аргументов. /// - public Task GoToHome(PageContext ctx, CancellationToken ct) + public Task GoToHomeAsync(PageContext ctx, CancellationToken ct) { return NavigateAsync(_defaultPage!, ctx, null, ct); } diff --git a/Demo/Pages/SubmitPage.cs b/Demo/Pages/SubmitPage.cs index d4f820a..830371b 100644 --- a/Demo/Pages/SubmitPage.cs +++ b/Demo/Pages/SubmitPage.cs @@ -27,7 +27,7 @@ public sealed class SubmitPage : SingletonPage } while (i < 100); - await ctx.Navigation.GoToHome(ctx, ct); + await ctx.Navigation.GoToHomeAsync(ctx, ct); } public override Task OnLeave(PageContext ctx, CancellationToken ct) diff --git a/Demo/Pages/TitlePage.cs b/Demo/Pages/TitlePage.cs index a0d7abc..3e843f1 100644 --- a/Demo/Pages/TitlePage.cs +++ b/Demo/Pages/TitlePage.cs @@ -19,7 +19,7 @@ public sealed class TitlePage : SingletonPage { if (text == "Меню") { - return ctx.Navigation.GoToHome(ctx, ct); + return ctx.Navigation.GoToHomeAsync(ctx, ct); } else {