Skip to content

Commit

Permalink
:feat: Fixes issue #382: 🚧 Added support for 3-Days week view
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Jan 8, 2025
1 parent 469cc29 commit 083e19b
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 53 deletions.
4 changes: 4 additions & 0 deletions example/lib/widgets/week_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class WeekViewWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return WeekView(
minDay: DateTime(2024),
maxDay: DateTime(2026),
key: state,
width: width,
showWeekends: true,
showLiveTimeLineInAllDays: true,
showThreeDaysView: true,
// startDay: WeekDays.tuesday,
eventArranger: SideEventArranger(maxWidth: 30),
timeLineWidth: 65,
scrollPhysics: const BouncingScrollPhysics(),
Expand Down
84 changes: 58 additions & 26 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,26 @@ extension DateTimeExtensions on DateTime {
.abs();

/// Gets difference of weeks between [date] and calling object.
int getWeekDifference(DateTime date, {WeekDays start = WeekDays.monday}) =>
(firstDayOfWeek(start: start)
.difference(date.firstDayOfWeek(start: start))
.inDays
.abs() /
7)
.ceil();
int getWeekDifference(
DateTime date, {
WeekDays start = WeekDays.monday,
bool isThreeDaysView = false,
}) {
final thisFirstDay = firstDayOfWeek(
start: start,
isThreeDaysView: isThreeDaysView,
);
final otherFirstDay = date.firstDayOfWeek(
start: start,
isThreeDaysView: isThreeDaysView,
);

final daysDifference = thisFirstDay.difference(otherFirstDay).inDays.abs();

// Calculate the difference in weeks or 3-day blocks
final divisor = isThreeDaysView ? 3 : 7;
return (daysDifference / divisor).ceil();
}

/// Returns The List of date of Current Week, all of the dates will be without
/// time.
Expand All @@ -55,6 +68,7 @@ extension DateTimeExtensions on DateTime {
List<DateTime> datesOfWeek({
WeekDays start = WeekDays.monday,
bool showWeekEnds = true,
bool showThreeDays = false,
}) {
// Here %7 ensure that we do not subtract >6 and <0 days.
// Initial formula is,
Expand All @@ -63,31 +77,49 @@ extension DateTimeExtensions on DateTime {
// But in WeekDays enum index ranges from 0 to 6 so we are
// adding 1 in index. So, new formula with WeekDays is,
// difference = (weekdays - (start.index + 1))%7
//
final startDay =
DateTime(year, month, day - (weekday - start.index - 1) % 7);

// Generate weekdays with weekends or without weekends
final days = List.generate(
7,
(index) => DateTime(startDay.year, startDay.month, startDay.day + index),
)
.where(
(date) =>
showWeekEnds ||
(date.weekday != DateTime.saturday &&
date.weekday != DateTime.sunday),
)
.toList();
return days;

final days = showThreeDays ? day : day - (weekday - start.index - 1) % 7;
final startDay = DateTime(year, month, days);
// TODO(Shubham): Update for hide/show weekends
return List.generate(
showThreeDays ? 3 : 7,
(index) => startDay.add(Duration(days: index)),
);
}

/// Returns the first date of week containing the current date
DateTime firstDayOfWeek({WeekDays start = WeekDays.monday}) =>
DateTime(year, month, day - ((weekday - start.index - 1) % 7));
DateTime firstDayOfWeek({
WeekDays start = WeekDays.monday,
bool isThreeDaysView = false,
}) =>
DateTime(
year,
month,
day - ((weekday - start.index - 1) % (isThreeDaysView ? 3 : 7)),
);

/// Returns the last date of week containing the current date
DateTime lastDayOfWeek({WeekDays start = WeekDays.monday}) =>
DateTime(year, month, day + (6 - (weekday - start.index - 1) % 7));
DateTime lastDayOfWeek({
WeekDays start = WeekDays.monday,
bool isThreeDaysView = false,
}) {
if (isThreeDaysView) {
// Calculate the current weekday offset (based on the start day)
final offset = (weekday - start.index) % 7;

// For a 3-day view, calculate the remaining days to the end of the 3-day block
final daysToAdd =
(3 - offset % 3) % 3; // Ensures wrap-around within 3-day blocks
final lastDay = DateTime(year, month, day + daysToAdd);
debugPrint('Last Day for 3-Day View: $lastDay');
debugPrint('Last Day: ${lastDay}');
return lastDay;
} else {
return DateTime(year, month, day + (6 - (weekday - start.index - 1) % 7));
}
}

/// Returns list of all dates of [month].
/// All the dates are week based that means it will return array of size 42
Expand Down
5 changes: 5 additions & 0 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
/// Flag to display quarter hours
final bool showQuarterHours;

/// Enable this flag to show 3-days view default is false.
/// i.e 7 days view
final bool showThreeDaysView;

/// Emulate vertical line offset from hour line starts.
final double emulateVerticalOffsetBy;

Expand Down Expand Up @@ -205,6 +209,7 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
required this.showWeekDayAtBottom,
required this.showHalfHours,
required this.showQuarterHours,
required this.showThreeDaysView,
required this.emulateVerticalOffsetBy,
required this.onTileDoubleTap,
required this.endHour,
Expand Down
Loading

0 comments on commit 083e19b

Please sign in to comment.