@@ -36,13 +36,26 @@ extension DateTimeExtensions on DateTime {
3636 .abs ();
3737
3838 /// Gets difference of weeks between [date] and calling object.
39- int getWeekDifference (DateTime date, {WeekDays start = WeekDays .monday}) =>
40- (firstDayOfWeek (start: start)
41- .difference (date.firstDayOfWeek (start: start))
42- .inDays
43- .abs () /
44- 7 )
45- .ceil ();
39+ int getWeekDifference (
40+ DateTime date, {
41+ WeekDays start = WeekDays .monday,
42+ bool isThreeDaysView = false ,
43+ }) {
44+ final thisFirstDay = firstDayOfWeek (
45+ start: start,
46+ isThreeDaysView: isThreeDaysView,
47+ );
48+ final otherFirstDay = date.firstDayOfWeek (
49+ start: start,
50+ isThreeDaysView: isThreeDaysView,
51+ );
52+
53+ final daysDifference = thisFirstDay.difference (otherFirstDay).inDays.abs ();
54+
55+ // Calculate the difference in weeks or 3-day blocks
56+ final divisor = isThreeDaysView ? 3 : 7 ;
57+ return (daysDifference / divisor).ceil ();
58+ }
4659
4760 /// Returns The List of date of Current Week, all of the dates will be without
4861 /// time.
@@ -55,6 +68,7 @@ extension DateTimeExtensions on DateTime {
5568 List <DateTime > datesOfWeek ({
5669 WeekDays start = WeekDays .monday,
5770 bool showWeekEnds = true ,
71+ bool showThreeDays = false ,
5872 }) {
5973 // Here %7 ensure that we do not subtract >6 and <0 days.
6074 // Initial formula is,
@@ -63,31 +77,49 @@ extension DateTimeExtensions on DateTime {
6377 // But in WeekDays enum index ranges from 0 to 6 so we are
6478 // adding 1 in index. So, new formula with WeekDays is,
6579 // difference = (weekdays - (start.index + 1))%7
66- //
67- final startDay =
68- DateTime (year, month, day - (weekday - start.index - 1 ) % 7 );
80+
6981 // Generate weekdays with weekends or without weekends
70- final days = List .generate (
71- 7 ,
72- (index) => DateTime (startDay.year, startDay.month, startDay.day + index),
73- )
74- .where (
75- (date) =>
76- showWeekEnds ||
77- (date.weekday != DateTime .saturday &&
78- date.weekday != DateTime .sunday),
79- )
80- .toList ();
81- return days;
82+
83+ final days = showThreeDays ? day : day - (weekday - start.index - 1 ) % 7 ;
84+ final startDay = DateTime (year, month, days);
85+ // TODO(Shubham): Update for hide/show weekends
86+ return List .generate (
87+ showThreeDays ? 3 : 7 ,
88+ (index) => startDay.add (Duration (days: index)),
89+ );
8290 }
8391
8492 /// Returns the first date of week containing the current date
85- DateTime firstDayOfWeek ({WeekDays start = WeekDays .monday}) =>
86- DateTime (year, month, day - ((weekday - start.index - 1 ) % 7 ));
93+ DateTime firstDayOfWeek ({
94+ WeekDays start = WeekDays .monday,
95+ bool isThreeDaysView = false ,
96+ }) =>
97+ DateTime (
98+ year,
99+ month,
100+ day - ((weekday - start.index - 1 ) % (isThreeDaysView ? 3 : 7 )),
101+ );
87102
88103 /// Returns the last date of week containing the current date
89- DateTime lastDayOfWeek ({WeekDays start = WeekDays .monday}) =>
90- DateTime (year, month, day + (6 - (weekday - start.index - 1 ) % 7 ));
104+ DateTime lastDayOfWeek ({
105+ WeekDays start = WeekDays .monday,
106+ bool isThreeDaysView = false ,
107+ }) {
108+ if (isThreeDaysView) {
109+ // Calculate the current weekday offset (based on the start day)
110+ final offset = (weekday - start.index) % 7 ;
111+
112+ // For a 3-day view, calculate the remaining days to the end of the 3-day block
113+ final daysToAdd =
114+ (3 - offset % 3 ) % 3 ; // Ensures wrap-around within 3-day blocks
115+ final lastDay = DateTime (year, month, day + daysToAdd);
116+ debugPrint ('Last Day for 3-Day View: $lastDay ' );
117+ debugPrint ('Last Day: ${lastDay }' );
118+ return lastDay;
119+ } else {
120+ return DateTime (year, month, day + (6 - (weekday - start.index - 1 ) % 7 ));
121+ }
122+ }
91123
92124 /// Returns list of all dates of [month] .
93125 /// All the dates are week based that means it will return array of size 42
0 commit comments