From c7c985be00712edfb4ff64c93a84d701888ad73d Mon Sep 17 00:00:00 2001 From: Bart Ribbers Date: Fri, 19 Jul 2024 10:00:38 +0200 Subject: [PATCH] feat: refactor getStartTimeForDayOfWeek and EndTime to use polymorphism to simplify the code --- .../lib/src/models/templates.dart | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/flutter_availability_data_interface/lib/src/models/templates.dart b/packages/flutter_availability_data_interface/lib/src/models/templates.dart index 55749ad..b112020 100644 --- a/packages/flutter_availability_data_interface/lib/src/models/templates.dart +++ b/packages/flutter_availability_data_interface/lib/src/models/templates.dart @@ -97,22 +97,12 @@ class AvailabilityTemplateModel { ); /// Get the start time for the specified day in the week for this template - DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) { - if (templateType == AvailabilityTemplateType.week) { - return (templateData as WeekTemplateData).data[weekDay]?.startTime; - } - - return (templateData as DayTemplateData).startTime; - } + DateTime? getStartTimeForDayOfWeek(WeekDay weekDay) => + templateData.getStartTimeForDayOfWeek(weekDay); /// Get the end time for the specified day in the week for this template - DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) { - if (templateType == AvailabilityTemplateType.week) { - return (templateData as WeekTemplateData).data[weekDay]?.endTime; - } - - return (templateData as DayTemplateData).endTime; - } + DateTime? getEndTimeForDayOfWeek(WeekDay weekDay) => + templateData.getEndTimeForDayOfWeek(weekDay); } /// Used as the key for defining week-based templates @@ -171,6 +161,12 @@ abstract interface class TemplateData { /// Serialize the template to representational data Map 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 @@ -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 @@ -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 _getDatesBetween(DateTime startDate, DateTime endDate) {