Skip to content

Commit

Permalink
fix: 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 Dec 10, 2024
1 parent f5f6777 commit 988f3d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
6 changes: 5 additions & 1 deletion example/lib/widgets/week_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class WeekViewWidget extends StatelessWidget {
key: state,
width: width,
showLiveTimeLineInAllDays: true,
eventArranger: SideEventArranger(maxWidth: 30),
showThreeDayView: true,
// TODO(Shubham): May required additional asserts
// If weekdays provided, shown/hidden weekends
eventArranger: SideEventArranger(),
timeLineWidth: 65,
// weekDays: [WeekDays.monday, WeekDays.tuesday, WeekDays.wednesday],
scrollPhysics: const BouncingScrollPhysics(),
liveTimeIndicatorSettings: LiveTimeIndicatorSettings(
color: Colors.redAccent,
Expand Down
1 change: 1 addition & 0 deletions lib/src/components/headers/week_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class WeekPageHeader extends CalendarPageHeader {
headerStyle: headerStyle,
);

// TODO(Shubham): Update this for 3-days view
static String _weekStringBuilder(DateTime date, {DateTime? secondaryDate}) =>
"${date.day} / ${date.month} / ${date.year} to "
"${secondaryDate != null ? "${secondaryDate.day} / "
Expand Down
23 changes: 10 additions & 13 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ extension DateTimeExtensions on DateTime {
/// will return dates
/// [6,7,8,9,10,11,12]
/// Where on 6th there will be monday and on 12th there will be Sunday
List<DateTime> datesOfWeek({WeekDays start = WeekDays.monday}) {
List<DateTime> datesOfWeek({
bool showThreeDays = false,
WeekDays start = WeekDays.monday,
}) {
// Here %7 ensure that we do not subtract >6 and <0 days.
// Initial formula is,
// difference = (weekday - startInt)%7
Expand All @@ -61,18 +64,12 @@ extension DateTimeExtensions on DateTime {
// 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);

return [
startDay,
DateTime(startDay.year, startDay.month, startDay.day + 1),
DateTime(startDay.year, startDay.month, startDay.day + 2),
DateTime(startDay.year, startDay.month, startDay.day + 3),
DateTime(startDay.year, startDay.month, startDay.day + 4),
DateTime(startDay.year, startDay.month, startDay.day + 5),
DateTime(startDay.year, startDay.month, startDay.day + 6),
];
final days = showThreeDays ? day : day - (weekday - start.index - 1) % 7;
final startDay = DateTime(year, month, days);

return List.generate(showThreeDays ? 3 : 7, (index) {
return startDay.add(Duration(days: index));
});
}

/// Returns the first date of week containing the current date
Expand Down
23 changes: 17 additions & 6 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,8 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
/// Flag to display quarter hours
final bool showQuarterHours;

final bool showThreeDaysView;

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

Expand Down Expand Up @@ -205,6 +207,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 Expand Up @@ -505,15 +508,23 @@ class _InternalWeekViewPageState<T extends Object?>

List<DateTime> _filteredDate() {
final output = <DateTime>[];

final weekDays = widget.weekDays.toList();

for (final date in widget.dates) {
if (weekDays.any((weekDay) => weekDay.index + 1 == date.weekday)) {
output.add(date);
final totalWeekDays = widget.showThreeDaysView ? 3 : 7;

if (widget.showThreeDaysView) {
final startDate = widget.dates.first;
int i = 0; // Add 2 days to show 3 days
while (i < 3) {
output.add(startDate.add(Duration(days: i)));
i++;
}
} else {
for (final date in widget.dates.take(totalWeekDays)) {
if (weekDays.any((weekDay) => weekDay.index + 1 == date.weekday)) {
output.add(date);
}
}
}

return output;
}
}
27 changes: 18 additions & 9 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class WeekView<T extends Object?> extends StatefulWidget {
/// saturday and sunday, only monday and tuesday will be visible in week view.
final bool showWeekends;

final bool showThreeDayView;

/// Defines which days should be displayed in one week.
///
/// By default all the days will be visible.
Expand Down Expand Up @@ -287,6 +289,7 @@ class WeekView<T extends Object?> extends StatefulWidget {
this.onDateTap,
this.weekDays = WeekDays.values,
this.showWeekends = true,
this.showThreeDayView = false,
this.startDay = WeekDays.monday,
this.minuteSlotSize = MinuteSlotSize.minutes60,
this.weekDetectorBuilder,
Expand Down Expand Up @@ -514,9 +517,10 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
physics: widget.pageViewPhysics,
onPageChanged: _onPageChange,
itemBuilder: (_, index) {
final dates = DateTime(_minDate.year, _minDate.month,
_minDate.day + (index * DateTime.daysPerWeek))
.datesOfWeek(start: widget.startDay);
final dates = _currentStartDate.datesOfWeek(
start: widget.startDay,
showThreeDays: widget.showThreeDayView,
);

return ValueListenableBuilder(
valueListenable: _scrollConfiguration,
Expand Down Expand Up @@ -565,6 +569,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
startHour: _startHour,
showHalfHours: widget.showHalfHours,
showQuarterHours: widget.showQuarterHours,
showThreeDaysView: widget.showThreeDayView,
emulateVerticalOffsetBy:
widget.emulateVerticalOffsetBy,
showWeekDayAtBottom: widget.showWeekDayAtBottom,
Expand Down Expand Up @@ -623,7 +628,8 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
"Make sure you are providing weekdays in initialization of "
"WeekView. or showWeekends is true if you are providing only "
"saturday or sunday in weekDays.");
_totalDaysInWeek = _weekDays.length;
// TODO(Shubham): Update weekdays to 3
_totalDaysInWeek = widget.showThreeDayView ? 3 : _weekDays.length;
}

void _updateViewDimensions() {
Expand All @@ -648,6 +654,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
assert(_hourIndicatorSettings.height < _hourHeight,
"hourIndicator height must be less than minuteHeight * 60");

// TODO(Shubham): Update total days in week
_weekTitleWidth =
(_width - _timeLineWidth - _hourIndicatorSettings.offset) /
_totalDaysInWeek;
Expand Down Expand Up @@ -716,9 +723,8 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
} else if (_currentWeek.isAfter(_maxDate)) {
_currentWeek = _maxDate;
}

_currentStartDate = _currentWeek.firstDayOfWeek(start: widget.startDay);
_currentEndDate = _currentWeek.lastDayOfWeek(start: widget.startDay);
_currentEndDate = _currentWeek.add(Duration(days: 2));
_currentIndex =
_minDate.getWeekDifference(_currentEndDate, start: widget.startDay);
}
Expand All @@ -728,7 +734,6 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
_minDate = (widget.minDay ?? CalendarConstants.epochDate)
.firstDayOfWeek(start: widget.startDay)
.withoutTime;

_maxDate = (widget.maxDay ?? CalendarConstants.maxDate)
.lastDayOfWeek(start: widget.startDay)
.withoutTime;
Expand Down Expand Up @@ -781,7 +786,11 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
}

/// Default builder for week number.
// TODO(Shubham): Update for 3 day view
Widget _defaultWeekNumberBuilder(DateTime date) {
if (widget.showThreeDayView) {
return SizedBox.shrink();
}
final daysToAdd = DateTime.thursday - date.weekday;
final thursday = daysToAdd > 0
? date.add(Duration(days: daysToAdd))
Expand Down Expand Up @@ -886,9 +895,9 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
_currentStartDate = DateTime(
_currentStartDate.year,
_currentStartDate.month,
_currentStartDate.day + (index - _currentIndex) * 7,
_currentStartDate.day + (index - _currentIndex) * 3,
);
_currentEndDate = _currentStartDate.add(Duration(days: 6));
_currentEndDate = _currentStartDate.add(Duration(days: 2));
_currentIndex = index;
});
}
Expand Down

0 comments on commit 988f3d7

Please sign in to comment.