feat: refactor getStartTimeForDayOfWeek and EndTime to use polymorphism to simplify the code

This commit is contained in:
Bart Ribbers 2024-07-19 10:00:38 +02:00 committed by Freek van de Ven
parent cdaca15901
commit c7c985be00

View file

@ -97,22 +97,12 @@ class AvailabilityTemplateModel {
); );
/// Get the start time for the specified day in the week for this template /// Get the start time for the specified day in the week for this template
DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) { DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) =>
if (templateType == AvailabilityTemplateType.week) { templateData.getStartTimeForDayOfWeek(weekDay);
return (templateData as WeekTemplateData).data[weekDay]?.startTime;
}
return (templateData as DayTemplateData).startTime;
}
/// Get the end time for the specified day in the week for this template /// Get the end time for the specified day in the week for this template
DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) { DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) =>
if (templateType == AvailabilityTemplateType.week) { templateData.getEndTimeForDayOfWeek(weekDay);
return (templateData as WeekTemplateData).data[weekDay]?.endTime;
}
return (templateData as DayTemplateData).endTime;
}
} }
/// Used as the key for defining week-based templates /// Used as the key for defining week-based templates
@ -171,6 +161,12 @@ abstract interface class TemplateData {
/// Serialize the template to representational data /// Serialize the template to representational data
Map<String, dynamic> toMap(); Map<String, dynamic> toMap();
/// Get the start time for the specified day in the week for this template
DateTime? getStartTimeForDayOfWeek(WeekDay weekDay);
/// Get the end time for the specified day in the week for this template
DateTime? getEndTimeForDayOfWeek(WeekDay weekDay);
} }
/// A week based template data structure /// A week based template data structure
@ -256,6 +252,15 @@ class WeekTemplateData implements TemplateData {
), ),
]; ];
} }
/// Get the start time for the specified day in the week for this template
@override
DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) =>
_data[weekDay]?.startTime;
/// Get the end time for the specified day in the week for this template
@override
DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) => _data[weekDay]?.endTime;
} }
/// A day based template data structure /// A day based template data structure
@ -357,6 +362,14 @@ class DayTemplateData implements TemplateData {
], ],
], ],
}; };
/// Get the start time for the specified day in the week for this template
@override
DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) => startTime;
/// Get the end time for the specified day in the week for this template
@override
DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) => endTime;
} }
List<DateTime> _getDatesBetween(DateTime startDate, DateTime endDate) { List<DateTime> _getDatesBetween(DateTime startDate, DateTime endDate) {