diff --git a/BotPages.Core/Pages/StatefullPage.cs b/BotPages.Core/Pages/StatefullPage.cs index c1e1a1e..b9bdc77 100644 --- a/BotPages.Core/Pages/StatefullPage.cs +++ b/BotPages.Core/Pages/StatefullPage.cs @@ -42,7 +42,17 @@ public abstract class StatefullPage : Page /// protected async Task SaveState(PageContext ctx, CancellationToken ct) { - foreach (var prop in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) + var t = GetType(); + foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) + { + var attr = prop.GetCustomAttribute(); + if (attr is null) continue; + + var value = prop.GetValue(this); + await ctx.StateStorage.SetAsync(ctx.SessionKey, attr.Key, value, ct); + } + + foreach (var prop in t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { var attr = prop.GetCustomAttribute(); if (attr is null) continue; diff --git a/BotPages.Core/Storage/InMemoryStateStorage.cs b/BotPages.Core/Storage/InMemoryStateStorage.cs index 39cf041..dc78040 100644 --- a/BotPages.Core/Storage/InMemoryStateStorage.cs +++ b/BotPages.Core/Storage/InMemoryStateStorage.cs @@ -19,6 +19,10 @@ public sealed class InMemoryStateStorage : IStateStorage /// public Task SetAsync(CompositeSessionKey session, string key, T state, CancellationToken ct) { + if (!_store.ContainsKey(session)) + { + _store[session] = new(); + } _store[session][key] = state!; return Task.CompletedTask; } diff --git a/Demo/Pages/DetailsPage.cs b/Demo/Pages/DetailsPage.cs index 0dc5b82..97d6b78 100644 --- a/Demo/Pages/DetailsPage.cs +++ b/Demo/Pages/DetailsPage.cs @@ -6,7 +6,7 @@ namespace Demo.Pages; /// /// Страница ввода деталей заявки. -/// Страница с параметрами и сохранением состояния. +/// Страница с параметрами и получением состояния. /// public sealed class DetailsPage : StatefullPage { diff --git a/Demo/Pages/FilesPage.cs b/Demo/Pages/FilesPage.cs index 54dc493..392403c 100644 --- a/Demo/Pages/FilesPage.cs +++ b/Demo/Pages/FilesPage.cs @@ -30,7 +30,7 @@ public sealed class FilesPage : SingletonPage await ctx.SetStorageAsync("Request", request, ct); await new MessageBuilder(ctx) - .Text($"Получено файлов: {files.Count}", MessageFormat.Plain) + .Text($"По заявке '{request.Title}' Получено файлов: {files.Count}", MessageFormat.Plain) .Inline("Далее", "next") .SendAsync(ct); }