Last active
January 4, 2024 12:13
-
-
Save ahmed-alhelali/51f52aa7f6d8c4d8b41cb350d7b93657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:intl/intl.dart'; | |
import 'package:light_app/data/data.dart'; | |
class BranchHelpers { | |
const BranchHelpers._(); | |
//Don't change this list order, dart is uses the same list order, as [monday]..[sunday]. | |
static const weekDaysNames = [ | |
'monday', | |
'tuesday', | |
'wednesday', | |
'thursday', | |
'friday', | |
'saturday', | |
'sunday', | |
]; | |
static DayModel getSpecificDay({required BranchModel selectedBranch, required int weekDayNumber}) { | |
DateTime currentTime = DateTime.now(); | |
String todayName = weekDaysNames[weekDayNumber - 1].toLowerCase(); | |
String yesterdayName = weekDayNumber == 1 ? weekDaysNames[6].toLowerCase() : weekDaysNames[weekDayNumber - 2].toLowerCase(); | |
DayModel today = selectedBranch.days.firstWhere((day) => day.day.toLowerCase() == todayName); | |
DayModel yesterday = selectedBranch.days.firstWhere((day) => day.day.toLowerCase() == yesterdayName); | |
TimeOfDay todayOpeningTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(today.openAt)); | |
TimeOfDay todayClosingTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(today.closeAt)); | |
TimeOfDay yesterdayOpeningTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(yesterday.openAt)); | |
TimeOfDay yesterdayClosingTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(yesterday.closeAt)); | |
DateTime todayCloseAt = todayClosingTime.hour < todayOpeningTime.hour | |
? DateTime(currentTime.year, currentTime.month, currentTime.day, todayClosingTime.hour, todayClosingTime.minute).add(const Duration(days: 1)) | |
: DateTime(currentTime.year, currentTime.month, currentTime.day, todayClosingTime.hour, todayClosingTime.minute); | |
DateTime yesterdayCloseAt = yesterdayClosingTime.hour < const TimeOfDay(hour: 12, minute: 0).hour | |
? DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayClosingTime.hour, yesterdayClosingTime.minute).add(const Duration(days: 1)) | |
: DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayClosingTime.hour, yesterdayClosingTime.minute); | |
bool isOpenToday = currentTime.isAfter(DateTime(currentTime.year, currentTime.month, currentTime.day, todayOpeningTime.hour, todayOpeningTime.minute)) && currentTime.isBefore(todayCloseAt); | |
bool isOpenFromYesterday = currentTime.isBefore(DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayCloseAt.hour, yesterdayOpeningTime.minute)) && currentTime.isBefore(yesterdayCloseAt); | |
if (isOpenToday) { | |
return today; | |
} else { | |
if (isOpenFromYesterday) { | |
return yesterday; | |
} else { | |
return today; | |
} | |
} | |
} | |
//weekDayNumber gonna be int as => final todayWeekDay = DateTime.now().weekday; | |
//selectedBranch gonna be the selectedBranch | |
static bool isOpenBranch({required BranchModel selectedBranch, required int weekDayNumber}) { | |
DateTime currentTime = DateTime.now(); | |
String todayName = weekDaysNames[weekDayNumber - 1].toLowerCase(); | |
String yesterdayName = weekDayNumber == 1 ? weekDaysNames[6].toLowerCase() : weekDaysNames[weekDayNumber - 2].toLowerCase(); | |
DayModel today = selectedBranch.days.firstWhere((day) => day.day.toLowerCase() == todayName); | |
DayModel yesterday = selectedBranch.days.firstWhere((day) => day.day.toLowerCase() == yesterdayName); | |
TimeOfDay todayOpeningTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(today.openAt)); | |
TimeOfDay todayClosingTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(today.closeAt)); | |
TimeOfDay yesterdayOpeningTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(yesterday.openAt)); | |
TimeOfDay yesterdayClosingTime = TimeOfDay.fromDateTime(DateFormat.jm().parse(yesterday.closeAt)); | |
DateTime todayCloseAt = todayClosingTime.hour < todayOpeningTime.hour | |
? DateTime(currentTime.year, currentTime.month, currentTime.day, todayClosingTime.hour, todayClosingTime.minute).add(const Duration(days: 1)) | |
: DateTime(currentTime.year, currentTime.month, currentTime.day, todayClosingTime.hour, todayClosingTime.minute); | |
DateTime yesterdayCloseAt = yesterdayClosingTime.hour < const TimeOfDay(hour: 12, minute: 0).hour | |
? DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayClosingTime.hour, yesterdayClosingTime.minute).add(const Duration(days: 1)) | |
: DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayClosingTime.hour, yesterdayClosingTime.minute); | |
bool isOpenToday = currentTime.isAfter(DateTime(currentTime.year, currentTime.month, currentTime.day, todayOpeningTime.hour, todayOpeningTime.minute)) && currentTime.isBefore(todayCloseAt); | |
bool isOpenFromYesterday = currentTime.isBefore(DateTime(currentTime.year, currentTime.month, currentTime.day, yesterdayCloseAt.hour, yesterdayOpeningTime.minute)) && currentTime.isBefore(yesterdayCloseAt); | |
return isOpenToday || isOpenFromYesterday; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment