Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fixes issue #382: Added support for 3-Days week view #427

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
// TODO(Shubham): Fix min date not working
minDay: DateTime(2024, 1, 10),
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
Loading