getDayPartOfTime method

DayParts? getDayPartOfTime({
  1. required DateTime time,
})

Implementation

DayParts? getDayPartOfTime({required DateTime time}) {
  final List<DayPartInfo?> listDayPartInfo = [];

  /// prepare list od day parts
  for (final element in listDayParts) {
    /// normal day part ex: from 4 to 10
    if (element.end > element.start) {
      listDayPartInfo.add(element);
    } else {
      /// composite day part [two day] ex: from 10 to 4
      /// split time to tow parts [from start to 24 , from 0 to end]
      listDayPartInfo.add(
        DayPartInfo(
          dayPart: element.dayPart,
          start: element.start,
          end: 24,
        ),
      );
      listDayPartInfo.add(
        DayPartInfo(
          dayPart: element.dayPart,
          start: 0,
          end: element.end,
        ),
      );
    }
  }

  final DayPartInfo? dayPartInfo = listDayPartInfo.firstWhere(
    (element) => element!.start <= time.hour && element.end > time.hour,
    orElse: () => null,
  );

  return dayPartInfo?.dayPart;
}