Skip to content

Commit

Permalink
♻️ Refactor comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Nov 25, 2024
1 parent 7346d4f commit 0df874c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ List<CalendarEventData> _events = [
endDate: _now.subtract(Duration(days: 3)),
frequency: RepeatFrequency.daily,
recurrenceEndOn: RecurrenceEnd.after,
interval: 5,
occurrences: 5,
),
title: 'Physics test prep',
description: 'Prepare for physics test',
Expand All @@ -63,7 +63,7 @@ List<CalendarEventData> _events = [
endDate: _now.add(Duration(days: 5)),
frequency: RepeatFrequency.daily,
recurrenceEndOn: RecurrenceEnd.after,
interval: 5,
occurrences: 5,
),
title: "Wedding anniversary",
description: "Attend uncle's wedding anniversary.",
Expand Down
4 changes: 2 additions & 2 deletions example/lib/widgets/add_event_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
endDate: _recurrenceEndDate,
frequency: _selectedFrequency ?? RepeatFrequency.daily,
weekdays: _selectedIndexes,
interval: int.tryParse(_occurrenceController.text),
occurrences: int.tryParse(_occurrenceController.text),
recurrenceEndOn: _selectedRecurrenceEnd ?? RecurrenceEnd.never,
);

Expand Down Expand Up @@ -623,7 +623,7 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
event.recurrenceSettings?.recurrenceEndOn ?? RecurrenceEnd.never;
_recurrenceEndDate = event.recurrenceSettings?.endDate ?? _startDate;
_occurrenceController.text =
(event.recurrenceSettings?.interval ?? 0).toString();
(event.recurrenceSettings?.occurrences ?? 0).toString();
}

void _resetForm() {
Expand Down
42 changes: 24 additions & 18 deletions lib/src/modals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,26 @@ class LiveTimeIndicatorSettings {
}

/// Set `frequency = RepeatFrequency.daily` to repeat every day
/// after current date & event day.
/// starting from event date (Inclusive).
///
/// Set `frequency = RepeatFrequency.weekly` & provide list of weekdays
/// to repeat on.
///
/// [startDate]: Defines start date of repeating events.
/// [endDate]: Defines end date of repeating events.
/// [interval]: Defines repetition of event after given [interval] in days.
/// [frequency]: Defines repeat daily, weekly, monthly or yearly.
/// [occurrence]: Defines repetition of an event for the given number of
/// occurrences.
///
/// [frequency]: Defines mode of repetition like repeat daily, weekly, monthly
/// or yearly.
///
/// [weekdays]: Contains list of weekdays to repeat starting from 0 index.
/// By default weekday of event is considered if not provided.
/// By default selected weekday is the start date of an event.
class RecurrenceSettings {
RecurrenceSettings({
required this.startDate,
this.endDate,
this.interval,
this.occurrences,
this.frequency = RepeatFrequency.weekly,
this.recurrenceEndOn = RecurrenceEnd.never,
this.excludeDates,
Expand All @@ -115,7 +121,7 @@ class RecurrenceSettings {
RecurrenceSettings.withCalculatedEndDate({
required this.startDate,
required DateTime endDate,
this.interval,
this.occurrences,
this.frequency = RepeatFrequency.weekly,
this.recurrenceEndOn = RecurrenceEnd.never,
this.excludeDates,
Expand All @@ -126,7 +132,7 @@ class RecurrenceSettings {

final DateTime startDate;
late DateTime? endDate;
final int? interval;
final int? occurrences;
final RepeatFrequency frequency;
final RecurrenceEnd recurrenceEndOn;
final List<int> weekdays;
Expand All @@ -143,10 +149,10 @@ class RecurrenceSettings {
/// after 2 occurrences,
/// the end date will be 29-03-25 because February does not have a 29th date.
DateTime get _endDateMonthly {
var occurrences = (interval ?? 1) - 1;
var repetition = (occurrences ?? 1) - 1;
var nextDate = startDate;

while (occurrences > 0) {
while (repetition > 0) {
nextDate = DateTime(
nextDate.year,
nextDate.month + 1,
Expand All @@ -164,7 +170,7 @@ class RecurrenceSettings {
startDate.day,
);
}
occurrences--;
repetition--;
}
return nextDate;
}
Expand Down Expand Up @@ -194,7 +200,7 @@ class RecurrenceSettings {
..sort();

final totalWeekdays = sortedWeekdays.length;
final targetOccurrence = interval ?? 1;
final targetOccurrence = occurrences ?? 1;

// Calculate the number of full weeks and remaining occurrences
final fullWeeks = (targetOccurrence - 1) ~/ totalWeekdays;
Expand Down Expand Up @@ -222,19 +228,19 @@ class RecurrenceSettings {

/// Calculate end date for yearly recurring event
DateTime get _endDateYearly {
var occurrences = (interval ?? 1) - 1;
var repetition = (occurrences ?? 1) - 1;
var nextDate = startDate;

// If the start date is not 29th Feb, we can directly calculate last year.
if (startDate.day != 29 && startDate.month != DateTime.february) {
return DateTime(
nextDate.year + occurrences,
nextDate.year + repetition,
startDate.month,
startDate.day,
);
}
// TODO(Shubham): Optimize for larger recurrences if required
while (occurrences > 0) {
while (repetition > 0) {
final newDate = DateTime(
nextDate.year + 1,
startDate.month,
Expand All @@ -249,7 +255,7 @@ class RecurrenceSettings {
continue;
}
nextDate = newDate;
occurrences--;
repetition--;
}
return nextDate;
}
Expand Down Expand Up @@ -282,7 +288,7 @@ class RecurrenceSettings {

// Finds the end date to repeat and event for the given number of occurrences
DateTime? _handleOccurrence(DateTime endDate) {
final occurrence = interval ?? 1;
final occurrence = occurrences ?? 1;
if (occurrence <= 1) {
return endDate;
}
Expand All @@ -304,7 +310,7 @@ class RecurrenceSettings {
String toString() {
return 'start date: $startDate, '
'end date: $endDate, '
'interval: $interval, '
'interval: $occurrences, '
'frequency: $frequency '
'weekdays: $weekdays'
'recurrence Ends on: $recurrenceEndOn'
Expand All @@ -323,7 +329,7 @@ class RecurrenceSettings {
return RecurrenceSettings(
startDate: startDate ?? this.startDate,
endDate: endDate ?? this.endDate,
interval: interval ?? this.interval,
occurrences: interval ?? this.occurrences,
frequency: frequency ?? this.frequency,
recurrenceEndOn: recurrenceEndOn ?? this.recurrenceEndOn,
weekdays: weekdays ?? this.weekdays,
Expand Down

0 comments on commit 0df874c

Please sign in to comment.