1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
- using System . Text ;
5
4
using GGroupp . Infra . Bot . Builder ;
6
5
using Microsoft . Bot . Builder ;
7
6
8
7
namespace GGroupp . Internal . Timesheet ;
9
8
9
+ using IDateSuggestionsRow = IReadOnlyCollection < KeyValuePair < string , DateOnly > > ;
10
+
10
11
public static class TimesheetDateGetFlowStep
11
12
{
12
- private const int DaySuggestionsCount = 7 ;
13
+ private const int DaysInRow = 3 ;
14
+
15
+ private const string DatePlaceholder = "дд.мм.гг" ;
13
16
14
17
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 )
16
19
{
17
20
_ = chatFlow ?? throw new ArgumentNullException ( nameof ( chatFlow ) ) ;
18
21
_ = mapFlowState ?? throw new ArgumentNullException ( nameof ( mapFlowState ) ) ;
19
22
20
23
return chatFlow . AwaitDate ( InnerCreateOptions , GetResultMessage , mapFlowState ) ;
21
24
25
+ DateStepOption InnerCreateOptions ( IChatFlowContext < TFlowState > context )
26
+ =>
27
+ CreateOptions ( context , days ) ;
28
+
22
29
string GetResultMessage ( IChatFlowContext < TFlowState > context , DateOnly date )
23
30
=>
24
31
propertyDisplayName + ": " + context . EncodeTextWithStyle ( date . ToStringRussianCulture ( ) , BotTextStyle . Bold ) ;
25
32
}
26
33
27
- private static DateStepOption InnerCreateOptions < TFlowState > ( IChatFlowContext < TFlowState > context )
34
+ private static DateStepOption CreateOptions < TFlowState > ( IChatFlowContext < TFlowState > context , short days )
28
35
=>
29
36
new (
30
37
text : GetDateText ( context ) ,
31
38
confirmButtonText : "Выбрать" ,
32
39
invalidDateText : "Не удалось распознать дату" ,
33
40
DateOnly . FromDateTime ( DateTime . Now ) ,
34
- placeholder : "дд.мм.гг" ,
35
- suggestions : context . CreateSuggestions ( DaySuggestionsCount ) ) ;
41
+ placeholder : DatePlaceholder ,
42
+ suggestions : context . CreateSuggestions ( days ) ) ;
36
43
37
44
private static string GetDateText ( ITurnContext context )
38
45
{
@@ -41,33 +48,37 @@ private static string GetDateText(ITurnContext context)
41
48
return "Выберите дату списания" ;
42
49
}
43
50
44
- var textBuilder = new StringBuilder ( "Введите дату списания в формате дд.мм.гг" ) ;
45
-
46
51
if ( context . IsTelegramChannel ( ) )
47
52
{
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 } ";
52
54
}
53
55
54
- return textBuilder . ToString ( ) ;
55
-
56
- static string ToString ( DateOnly date ) => date . ToStringRussianCulture ( "dd.MM" ) ;
56
+ return $ "Введите дату списания в формате { DatePlaceholder } ";
57
57
}
58
58
59
- private static IReadOnlyCollection < KeyValuePair < string , DateOnly > > CreateSuggestions ( this ITurnContext context , int count )
59
+ private static IReadOnlyCollection < IDateSuggestionsRow > CreateSuggestions ( this ITurnContext context , short rows )
60
60
{
61
61
if ( context . IsNotTelegramChannel ( ) )
62
62
{
63
- return Array . Empty < KeyValuePair < string , DateOnly > > ( ) ;
63
+ return Array . Empty < IDateSuggestionsRow > ( ) ;
64
64
}
65
65
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 ( ) ;
68
79
69
- static KeyValuePair < string , DateOnly > CreateSuggestion ( DateOnly date )
80
+ KeyValuePair < string , DateOnly > CreateSuggestion ( DateOnly date )
70
81
=>
71
- new ( date . ToStringRussianCulture ( "ddd" ) , date ) ;
82
+ date == today ? new ( "Сегодня" , date ) : new ( date . ToStringRussianCulture ( "dd.MM ddd" ) , date ) ;
72
83
}
73
84
}
0 commit comments