Skip to content

Commit 32cf04e

Browse files
authored
Merge pull request #27 from GGrouppFoundation/feature/allow-date-suggestion-rows
Improve flow date flow step with suggestions rows
2 parents c6522a8 + d772596 commit 32cf04e

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

src/Application/Application.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<InvariantGlobalization>false</InvariantGlobalization>
99
<RootNamespace>GGroupp.Internal.Timesheet</RootNamespace>
1010
<AssemblyName>GGroupp.Internal.Timesheet.Bot.Application</AssemblyName>
11-
<Version>1.5.2</Version>
11+
<Version>1.5.3</Version>
1212
</PropertyGroup>
1313

1414
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Application' " />

src/DateTimesheet.Get/Step.GetDate/DateGetFlowStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static class DateGetFlowStep
88
internal static ChatFlow<DateTimesheetFlowState> GetDate(
99
this ChatFlow<DateTimesheetFlowState> chatFlow)
1010
=>
11-
chatFlow.AwaitTimesheetDate("Дата", WithDate);
11+
chatFlow.AwaitTimesheetDate("Дата", 3, WithDate);
1212

1313
private static DateTimesheetFlowState WithDate(DateTimesheetFlowState state, DateOnly date)
1414
=>

src/Timesheet.Create/Step.GetDate/DateGetFlowStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal static class DateGetFlowStep
77
{
88
internal static ChatFlow<TimesheetCreateFlowState> GetDate(this ChatFlow<TimesheetCreateFlowState> chatFlow)
99
=>
10-
chatFlow.AwaitTimesheetDate("Дата списания", WithDate);
10+
chatFlow.AwaitTimesheetDate("Дата списания", 2, WithDate);
1111

1212
private static TimesheetCreateFlowState WithDate(TimesheetCreateFlowState state, DateOnly date)
1313
=>
Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54
using GGroupp.Infra.Bot.Builder;
65
using Microsoft.Bot.Builder;
76

87
namespace GGroupp.Internal.Timesheet;
98

9+
using IDateSuggestionsRow = IReadOnlyCollection<KeyValuePair<string, DateOnly>>;
10+
1011
public static class TimesheetDateGetFlowStep
1112
{
12-
private const int DaySuggestionsCount = 7;
13+
private const int DaysInRow = 3;
14+
15+
private const string DatePlaceholder = "дд.мм.гг";
1316

1417
public static ChatFlow<TFlowState> AwaitTimesheetDate<TFlowState>(
15-
this ChatFlow<TFlowState> chatFlow, string propertyDisplayName, Func<TFlowState, DateOnly, TFlowState> mapFlowState)
18+
this ChatFlow<TFlowState> chatFlow, string propertyDisplayName, short days, Func<TFlowState, DateOnly, TFlowState> mapFlowState)
1619
{
1720
_ = chatFlow ?? throw new ArgumentNullException(nameof(chatFlow));
1821
_ = mapFlowState ?? throw new ArgumentNullException(nameof(mapFlowState));
1922

2023
return chatFlow.AwaitDate(InnerCreateOptions, GetResultMessage, mapFlowState);
2124

25+
DateStepOption InnerCreateOptions(IChatFlowContext<TFlowState> context)
26+
=>
27+
CreateOptions(context, days);
28+
2229
string GetResultMessage(IChatFlowContext<TFlowState> context, DateOnly date)
2330
=>
2431
propertyDisplayName + ": " + context.EncodeTextWithStyle(date.ToStringRussianCulture(), BotTextStyle.Bold);
2532
}
2633

27-
private static DateStepOption InnerCreateOptions<TFlowState>(IChatFlowContext<TFlowState> context)
34+
private static DateStepOption CreateOptions<TFlowState>(IChatFlowContext<TFlowState> context, short days)
2835
=>
2936
new(
3037
text: GetDateText(context),
3138
confirmButtonText: "Выбрать",
3239
invalidDateText: "Не удалось распознать дату",
3340
DateOnly.FromDateTime(DateTime.Now),
34-
placeholder: "дд.мм.гг",
35-
suggestions: context.CreateSuggestions(DaySuggestionsCount));
41+
placeholder: DatePlaceholder,
42+
suggestions: context.CreateSuggestions(days));
3643

3744
private static string GetDateText(ITurnContext context)
3845
{
@@ -41,33 +48,37 @@ private static string GetDateText(ITurnContext context)
4148
return "Выберите дату списания";
4249
}
4350

44-
var textBuilder = new StringBuilder("Введите дату списания в формате дд.мм.гг");
45-
4651
if (context.IsTelegramChannel())
4752
{
48-
var lastDay = DateOnly.FromDateTime(DateTime.Now);
49-
var firstDay = lastDay.AddDays(1 - DaySuggestionsCount);
50-
51-
textBuilder.Append(' ').Append($"или выберите день недели с {ToString(firstDay)} по {ToString(lastDay)}");
53+
return $"Выберите или введите дату списания в формате {DatePlaceholder}";
5254
}
5355

54-
return textBuilder.ToString();
55-
56-
static string ToString(DateOnly date) => date.ToStringRussianCulture("dd.MM");
56+
return $"Введите дату списания в формате {DatePlaceholder}";
5757
}
5858

59-
private static IReadOnlyCollection<KeyValuePair<string, DateOnly>> CreateSuggestions(this ITurnContext context, int count)
59+
private static IReadOnlyCollection<IDateSuggestionsRow> CreateSuggestions(this ITurnContext context, short rows)
6060
{
6161
if (context.IsNotTelegramChannel())
6262
{
63-
return Array.Empty<KeyValuePair<string, DateOnly>>();
63+
return Array.Empty<IDateSuggestionsRow>();
6464
}
6565

66-
var now = DateOnly.FromDateTime(DateTime.Now);
67-
return Enumerable.Range(1 - count, count).Select(now.AddDays).Select(CreateSuggestion).ToArray();
66+
var today = DateOnly.FromDateTime(DateTime.Now);
67+
var days = DaysInRow * rows;
68+
69+
return Enumerable.Range(1 - days, days).GroupBy(GetRowNumber).Select(CreateRow).ToArray();
70+
71+
int GetRowNumber(int index)
72+
=>
73+
(index + days - 1) / DaysInRow;
74+
75+
IDateSuggestionsRow CreateRow<TCollection>(TCollection days)
76+
where TCollection : IEnumerable<int>
77+
=>
78+
days.Select(today.AddDays).Select(CreateSuggestion).ToArray();
6879

69-
static KeyValuePair<string, DateOnly> CreateSuggestion(DateOnly date)
80+
KeyValuePair<string, DateOnly> CreateSuggestion(DateOnly date)
7081
=>
71-
new(date.ToStringRussianCulture("ddd"), date);
82+
date == today ? new("Сегодня", date) : new(date.ToStringRussianCulture("dd.MM ddd"), date);
7283
}
7384
}

src/Utility.Date.Get/Utility.Date.Get.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Date" Version="2.4.1" />
14+
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Date" Version="2.5.0" />
1515
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)