-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathencoder.dart
More file actions
573 lines (506 loc) · 16.7 KB
/
Copy pathencoder.dart
File metadata and controls
573 lines (506 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import 'dart:convert';
import 'package:collection/collection.dart';
import 'package:intl/intl.dart';
import 'package:meta/meta.dart';
import '../../by_week_day_entry.dart';
import '../../frequency.dart';
import '../../recurrence_rule.dart';
import '../../utils.dart';
import 'l10n/l10n.dart';
@immutable
class RecurrenceRuleToTextEncoder extends Converter<RecurrenceRule, String> {
const RecurrenceRuleToTextEncoder(this.l10n, {this.untilDateFormat});
final RruleL10n l10n;
final DateFormat? untilDateFormat;
@override
String convert(RecurrenceRule input) {
input = _normalize(input);
final frequencyIntervalString =
l10n.frequencyInterval(input.frequency, input.actualInterval);
final output = StringBuffer(frequencyIntervalString);
if (input.frequency > Frequency.daily) {
assert(
!input.hasBySetPositions &&
!input.hasBySeconds &&
!input.hasByMinutes &&
!input.hasByHours &&
!input.hasByWeekDays &&
!input.hasByMonthDays &&
!input.hasByYearDays &&
!input.hasByWeeks &&
!input.hasByMonths,
'Frequencies > daily with any `by`-parts are not supported yet in '
'toText().',
);
} else {
if (input.frequency == Frequency.daily) {
_convertDaily(input, output);
} else if (input.frequency == Frequency.weekly) {
_convertWeekly(input, output);
} else if (input.frequency == Frequency.monthly) {
_convertMonthly(input, output);
} else if (input.frequency == Frequency.yearly) {
_convertYearly(input, output);
} else {
throw UnsupportedError('Unsupported frequency: ${input.frequency}');
}
}
if (input.until != null) {
output.write(l10n.until(input.until!, input.frequency, dateFormat: untilDateFormat));
} else if (input.count != null) {
output.write(l10n.count(input.count!));
}
return output.toString();
}
RecurrenceRule _normalize(RecurrenceRule input) {
// Incomplete!
input = input.copyWith(clearInterval: input.interval == 1);
if (input.frequency == Frequency.monthly) {
final byEveryWeekDay = [
for (final weekDay in DateTime.monday.rangeTo(DateTime.sunday))
ByWeekDayEntry(weekDay),
];
if (!input.hasBySeconds &&
!input.hasByMinutes &&
!input.hasByHours &&
const DeepCollectionEquality.unordered()
.equals(input.byWeekDays, byEveryWeekDay) &&
!input.hasByMonthDays &&
!input.hasByYearDays &&
!input.hasByWeeks &&
!input.hasByMonths &&
input.hasBySetPositions) {
input = input.copyWith(
byWeekDays: [],
byMonthDays: input.bySetPositions,
bySetPositions: [],
);
}
}
return input;
}
void _convertDaily(RecurrenceRule input, StringBuffer output) {
assert(input.byWeekDays.noneHasOccurrence);
output
// [in January – March, August & September]
..add(_formatByMonths(input))
// [on the 1st & 2nd-to-last instance]
..add(_formatBySetPositions(input))
// [byWeekDays]:
// [on (Monday, Wednesday – Friday & Sunday | weekdays [& Sunday])]
// [byMonthDays]:
// [on the 1st and last day of the month]
// byWeekDays, byMonthDays:
// on (Monday, Wednesday – Friday & Sunday | weekdays [& Sunday])
// that are also the 1st & 3rd-to-last – last day of the month
..add(
_formatByWeekDays(
input,
variant: input.hasBySetPositions
? InOnVariant.instanceOf
: InOnVariant.simple,
),
)
..add(
_formatByMonthDays(
input,
variant: input.hasByWeekDays
? InOnVariant.also
: input.hasBySetPositions
? InOnVariant.instanceOf
: InOnVariant.simple,
combination: input.hasByWeekDays
? ListCombination.disjunctive
: ListCombination.conjunctiveShort,
),
);
}
void _convertWeekly(RecurrenceRule input, StringBuffer output) {
assert(input.byWeekDays.noneHasOccurrence);
output
// [in January – March, August & September]
..add(_formatByMonths(input))
// [on the 1st & 2nd-to-last instance]
..add(_formatBySetPositions(input))
// [byWeekDays]:
// [on (Monday, Wednesday – Friday & Sunday | a weekday [& Sunday])]
..add(
_formatByWeekDays(
input,
variant: input.hasBySetPositions
? InOnVariant.instanceOf
: InOnVariant.simple,
),
);
}
void _convertMonthly(RecurrenceRule input, StringBuffer output) {
output
// [in January – March, August & September]
..add(_formatByMonths(input))
// [on the 1st & 2nd-to-last instance]
..add(_formatBySetPositions(input))
// [byWeekDays]:
// [on (Monday, Wednesday – Friday & Sunday | weekdays [& Sunday])]
// [byMonthDays]:
// [on the 1st and last day of the month]
// byWeekDays, byMonthDays:
// on (Monday, Wednesday – Friday & Sunday | weekdays [& Sunday])
// that are also the 1st or 3rd-to-last – last day of the month
..add(
_formatByWeekDays(
input,
frequency: DaysOfWeekFrequency.monthly,
indicateFrequency: false,
variant: input.hasBySetPositions
? InOnVariant.instanceOf
: InOnVariant.simple,
),
)
..add(
_formatByMonthDays(
input,
daysOfVariant: input.byWeekDays.anyHasOccurrence
? DaysOfVariant.dayAndFrequency
: input.byMonthDays.any((d) => d < 0)
? DaysOfVariant.day
: DaysOfVariant.simple,
variant: input.hasByWeekDays
? InOnVariant.also
: input.hasBySetPositions
? InOnVariant.instanceOf
: InOnVariant.simple,
combination: input.hasByWeekDays
? ListCombination.disjunctive
: ListCombination.conjunctiveShort,
),
);
}
void _convertYearly(RecurrenceRule input, StringBuffer output) {
output.add(_formatBySetPositions(input));
// Order of remaining by-attributes:
// byWeekDays, byMonthDays, byYearDays, byWeeks, byMonths
final firstVariant =
input.hasBySetPositions ? InOnVariant.instanceOf : InOnVariant.simple;
final startWithByWeekDays = input.hasByWeekDays;
if (startWithByWeekDays) {
final frequency = input.hasByYearDays || input.hasByMonthDays
? DaysOfWeekFrequency.yearly
: input.hasByMonths
? DaysOfWeekFrequency.monthly
: DaysOfWeekFrequency.yearly;
output.add(
_formatByWeekDays(
input,
frequency: frequency,
variant: firstVariant,
),
);
}
final startWithByMonthDays = input.hasByMonthDays && !startWithByWeekDays;
if (startWithByMonthDays) {
output.add(_formatByMonthDays(input, variant: firstVariant));
}
final startWithByYearDays =
input.hasByYearDays && !startWithByWeekDays && !startWithByMonthDays;
if (startWithByYearDays) {
output.add(_formatByYearDays(input, variant: firstVariant));
}
final startWithByWeeks = input.hasByWeeks &&
!startWithByWeekDays &&
!startWithByMonthDays &&
!startWithByYearDays;
if (startWithByWeeks) {
output.add(_formatByWeeks(input, variant: firstVariant));
}
final startWithByMonths = input.hasByMonths &&
!startWithByWeekDays &&
!startWithByMonthDays &&
!startWithByYearDays &&
!startWithByWeeks;
if (startWithByMonths) {
output.add(_formatByMonths(input, variant: firstVariant));
}
final daysOnlyByWeek =
input.hasByWeekDays && !input.hasByMonthDays && !input.hasByYearDays;
final daysOnlyByMonth =
!input.hasByWeekDays && input.hasByMonthDays && !input.hasByYearDays;
final appendByWeeksDirectly = daysOnlyByWeek && input.hasByWeeks;
final appendByMonthsDirectly = (daysOnlyByWeek || daysOnlyByMonth) &&
!input.hasByWeeks &&
input.hasByMonths;
if (appendByWeeksDirectly) {
output.add(_formatByWeeks(input));
}
if (appendByMonthsDirectly) {
assert(!appendByWeeksDirectly);
output.add(_formatByMonths(input));
}
final limits = [
if (!startWithByMonthDays)
_formatByMonthDays(
input,
variant: InOnVariant.also,
combination: ListCombination.disjunctive,
),
if (!startWithByYearDays)
_formatByYearDays(
input,
variant: InOnVariant.also,
combination: ListCombination.disjunctive,
),
if (!startWithByWeeks && !appendByWeeksDirectly)
_formatByWeeks(
input,
variant: InOnVariant.also,
combination: ListCombination.disjunctive,
),
if (!startWithByMonths && !appendByMonthsDirectly)
_formatByMonths(
input,
variant: InOnVariant.also,
combination: ListCombination.disjunctive,
),
].nonNulls.toList();
if (limits.isNotEmpty) {
output.add(l10n.list(limits, ListCombination.conjunctiveLong));
}
}
String? _formatBySetPositions(RecurrenceRule input) {
if (input.bySetPositions.isEmpty) return null;
return l10n.onInstances(input.bySetPositions.formattedForUser(l10n));
}
String? _formatByMonths(
RecurrenceRule input, {
InOnVariant variant = InOnVariant.simple,
ListCombination combination = ListCombination.conjunctiveShort,
}) {
if (input.byMonths.isEmpty) return null;
return l10n.inMonths(
input.byMonths.formattedForUser(
l10n,
map: l10n.month,
combination: combination,
),
variant: variant,
);
}
String? _formatByWeeks(
RecurrenceRule input, {
InOnVariant variant = InOnVariant.simple,
ListCombination combination = ListCombination.conjunctiveShort,
}) {
if (input.byWeeks.isEmpty) return null;
return l10n.inWeeks(
input.byWeeks.formattedForUser(l10n, combination: combination),
variant: variant,
);
}
String? _formatByYearDays(
RecurrenceRule input, {
InOnVariant variant = InOnVariant.simple,
ListCombination combination = ListCombination.conjunctiveShort,
}) {
if (input.byYearDays.isEmpty) return null;
return l10n.onDaysOfYear(
input.byYearDays.formattedForUser(l10n, combination: combination),
variant: variant,
);
}
String? _formatByMonthDays(
RecurrenceRule input, {
DaysOfVariant daysOfVariant = DaysOfVariant.dayAndFrequency,
InOnVariant variant = InOnVariant.simple,
ListCombination combination = ListCombination.conjunctiveShort,
}) {
if (input.byMonthDays.isEmpty) return null;
return l10n.onDaysOfMonth(
input.byMonthDays.formattedForUser(l10n, combination: combination),
daysOfVariant: daysOfVariant,
variant: variant,
);
}
String? _formatByWeekDays(
RecurrenceRule input, {
DaysOfWeekFrequency? frequency,
bool? indicateFrequency,
InOnVariant variant = InOnVariant.simple,
}) {
if (input.byWeekDays.isEmpty) return null;
var addEveryPrefix = frequency != null;
if (frequency == DaysOfWeekFrequency.yearly &&
input.byWeekDays.noneHasOccurrence &&
!input.hasByMonthDays &&
!input.hasByYearDays &&
input.byWeeks.length == 1 &&
!input.hasByMonths) {
addEveryPrefix = false;
}
return l10n.onDaysOfWeek(
input.byWeekDays.formattedForUser(
l10n,
addEveryPrefix: addEveryPrefix,
weekStart: input.actualWeekStart,
),
indicateFrequency: indicateFrequency ?? input.byWeekDays.anyHasOccurrence,
frequency: frequency,
variant: variant,
);
}
}
extension on StringBuffer {
void add(Object? obj) {
if (obj == null) return;
write(' ');
write(obj);
}
}
typedef _ItemToString<T> = String Function(T item);
extension<T> on Iterable<T> {
/// Creates a list with all items sorted by their key like
/// `0, 1, 2, 3, …, -3, -2, -1`.
List<T> sortedForUserGeneral({required int Function(T item) key}) {
final nonNegative = where((e) => key(e) >= 0).sortedBy<num>(key);
final negative = where((e) => key(e) < 0).sortedBy<num>(key);
return nonNegative + negative;
}
}
extension on Iterable<int> {
String formattedForUser(
RruleL10n l10n, {
_ItemToString<int>? map,
ListCombination combination = ListCombination.conjunctiveShort,
}) {
assert(isNotEmpty);
final raw = sortedForUser();
final mapped = <String>[];
for (var i = 0; i < raw.length; i++) {
final startIndex = i;
var current = raw[startIndex];
while (raw.length > i + 1 && raw[i + 1] == current + 1) {
i++;
current = raw[i];
}
mapped._addIndividualOrCombined(
l10n,
raw,
startIndex,
i,
map ?? l10n.ordinal,
);
}
return l10n.list(mapped, combination);
}
List<int> sortedForUser() => sortedForUserGeneral(key: (e) => e);
}
extension on Iterable<ByWeekDayEntry> {
String occurrenceFreeFormattedForUser(
RruleL10n l10n, {
required bool addEveryPrefix,
required int weekStart,
}) {
assert(noneHasOccurrence);
assert(isNotEmpty);
assert(weekStart.isValidRruleDayOfWeek);
// With [addEveryPrefix]:
// every Monday
// weekdays & every Sunday
// weekdays, every Saturday & Sunday
final raw = map((e) => e.day)
.sortedBy<num>((it) => (it - DateTime.monday) % DateTime.daysPerWeek);
final mapped = <String>[];
late final containsAllWeekdays = l10n.weekdays.every(raw.contains);
if (l10n.weekdaysString != null && containsAllWeekdays) {
mapped.add(l10n.weekdaysString!);
raw.removeWhere((d) => l10n.weekdays.contains(d));
}
var addedEveryPrefix = false;
for (var i = 0; i < raw.length; i++) {
final startIndex = i;
final startValue = raw[startIndex];
var current = startValue;
while (raw.length > i + 1 &&
raw[i + 1] == (current + 1) % DateTime.daysPerWeek) {
i++;
current = raw[i];
}
mapped._addIndividualOrCombined<int>(
l10n,
raw,
startIndex,
i,
(day) {
var string = l10n.dayOfWeek(day);
if (addEveryPrefix && !addedEveryPrefix && day == startValue) {
string = '${l10n.everyXDaysOfWeekPrefix}$string';
addedEveryPrefix = true;
}
return string;
},
);
}
return l10n.list(mapped, ListCombination.conjunctiveShort);
}
String formattedForUser(
RruleL10n l10n, {
required bool addEveryPrefix,
required int weekStart,
}) {
assert(isNotEmpty);
assert(weekStart.isValidRruleDayOfWeek);
final grouped = groupBy<ByWeekDayEntry, int?>(this, (e) => e.occurrence)
.entries
.sortedForUserGeneral(key: (it) => it.value.first.occurrence ?? 0);
if (anyHasOccurrence && map((it) => it.day).toSet().length == 1) {
// Simplify this special case:
// All entries contain the same day of the week.
return l10n.nthDaysOfWeek(
grouped.map((it) => it.key!),
l10n.dayOfWeek(first.day),
);
}
final strings = grouped.map((entry) {
final hasOccurrence = entry.key != null;
final daysOfWeek = entry.value
.map((it) => ByWeekDayEntry(it.day))
.occurrenceFreeFormattedForUser(
l10n,
addEveryPrefix: addEveryPrefix && !hasOccurrence,
weekStart: weekStart,
);
return hasOccurrence
? l10n.nthDaysOfWeek(hasOccurrence ? [entry.key!] : [], daysOfWeek)
: daysOfWeek;
}).toList();
// If no inner (short) conjunction is used, we can simply use the short
// variant instead of the long one.
final atMostOneWeekDayPerOccurrence = every((entry) {
return where((e) => e.occurrence == entry.occurrence).length == 1;
});
return l10n.list(
strings,
atMostOneWeekDayPerOccurrence
? ListCombination.conjunctiveShort
: ListCombination.conjunctiveLong,
);
}
}
extension on List<String> {
void _addIndividualOrCombined<T>(
RruleL10n l10n,
List<T> source,
int startIndex,
int endIndex,
_ItemToString<T> map,
) {
assert(startIndex <= endIndex);
switch (endIndex - startIndex) {
case 0:
add(map(source[startIndex]));
case 1:
add(map(source[startIndex]));
add(map(source[endIndex]));
default:
add(l10n.range(map(source[startIndex]), map(source[endIndex])));
}
}
}