Skip to content

Commit

Permalink
✨ Quick fixes before PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRBaraiya committed Nov 24, 2023
1 parent 79a30dc commit 1e413a5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
5 changes: 5 additions & 0 deletions example/lib/widgets/add_event_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
_endDate = DateTime.now().withoutTime;
_startTime = null;
_endTime = null;
_color = Colors.blue;

if (mounted) {
setState(() {});
}
}

void _displayColorPicker() {
Expand Down
2 changes: 0 additions & 2 deletions example/lib/widgets/date_time_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class DateTimeSelectorFormField extends StatefulWidget {
}

class _DateTimeSelectorFormFieldState extends State<DateTimeSelectorFormField> {
bool _isSelectorOpen = false;

late var _minimumDate = CalendarConstants.minDate.withoutTime;

late var _textEditingController =
Expand Down
46 changes: 37 additions & 9 deletions lib/src/calendar_event_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import 'package:flutter/material.dart';

import '../calendar_view.dart';
import 'extensions.dart';

/// Stores all the events on [date]
@immutable

/// {@macro calendar_event_data_doc}
class CalendarEventData<T extends Object?> {
/// Specifies date on which all these events are.
final DateTime date;
Expand Down Expand Up @@ -43,13 +45,10 @@ class CalendarEventData<T extends Object?> {
/// Define style of description.
final TextStyle? descriptionStyle;

/// Stores all the events on [date].
///
/// If [startTime] and [endTime] both are 0 or either of them is null, then
/// event will be considered a full day event.
///
const CalendarEventData({
/// {@macro calendar_event_data_doc}
CalendarEventData({
required this.title,
required DateTime date,
this.description,
this.event,
this.color = Colors.blue,
Expand All @@ -58,8 +57,8 @@ class CalendarEventData<T extends Object?> {
this.titleStyle,
this.descriptionStyle,
DateTime? endDate,
required this.date,
}) : _endDate = endDate;
}) : _endDate = endDate?.withoutTime,
date = date.withoutTime;

DateTime get endDate => _endDate ?? date;

Expand All @@ -75,6 +74,13 @@ class CalendarEventData<T extends Object?> {
(startTime!.isDayStart && endTime!.isDayStart));
}

bool occursOnDate(DateTime currentDate) {
return currentDate == date ||
currentDate == endDate ||
(currentDate.isBefore(endDate.withoutTime) &&
currentDate.isAfter(date.withoutTime));
}

Map<String, dynamic> toJson() => {
"date": date,
"startTime": startTime,
Expand Down Expand Up @@ -139,3 +145,25 @@ class CalendarEventData<T extends Object?> {
@override
int get hashCode => super.hashCode;
}

/// {@template calendar_event_data_doc}
/// Stores all the events on [date].
///
/// If [startTime] and [endTime] both are 0 or either of them is null, then
/// event will be considered a full day event.
///
/// - [date] and [endDate] are used to define dates only. So, If you
/// are providing any time information with these two arguments,
/// it will be ignored.
///
/// - [startTime] and [endTime] are used to define the time span of the event.
/// So, If you are providing any day information (year, month, day), it will
/// be ignored. It will also, consider only hour and minutes as time. So,
/// seconds, milliseconds and microseconds will be ignored as well.
///
/// - [startTime] and [endTime] can no span more then one day. For example,
/// If start time is 11th Nov 11:30 PM and end time is 12th Nov 1:30 AM, it
/// will not be considered as valid time. Events with invalid time will throw
/// [AssertionError] in debug mode and will be ignored in release mode
/// in [DayView] and [WeekView].
/// {@endtemplate}
1 change: 1 addition & 0 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {

@override
Widget build(BuildContext context) {
// TODO: Use SizedBox If possible.
return Container(
height: height,
width: width,
Expand Down
5 changes: 4 additions & 1 deletion lib/src/day_view/_internal_day_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
date: date,
onTileTap: onTileTap,
eventArranger: eventArranger,
events: controller.getEventsOnDay(date),
events: controller.getEventsOnDay(
date,
includeFullDayEvents: false,
),
heightPerMinute: heightPerMinute,
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollNotifier,
Expand Down
30 changes: 16 additions & 14 deletions lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ class EventController<T extends Object?> extends ChangeNotifier {
//ignore: deprecated_member_use_from_same_package
if (_eventFilter != null) return _eventFilter!.call(date, this.events);

return _calendarData.getEventsOnDay(date,
includeFullDays: includeFullDayEvents);
return _calendarData.getEventsOnDay(date.withoutTime,
includeFullDayEvents: includeFullDayEvents);
}

/// Returns full day events on given day.
List<CalendarEventData<T>> getFullDayEvent(DateTime date) {
return _calendarData.getFullDayEvent(date);
return _calendarData.getFullDayEvent(date.withoutTime);
}

/// Updates the [eventFilter].
Expand Down Expand Up @@ -186,13 +186,18 @@ class CalendarData<T> {
UnmodifiableListView<CalendarEventData<T>> get rangingEventList =>
UnmodifiableListView(_rangingEventList);

/// Stores all full day events(24hr event)
/// Stores all full day events(24hr event).
///
/// This includes all full day events that are recurring day events as well.
///
///
final _fullDayEventList = <CalendarEventData<T>>[];
UnmodifiableListView<CalendarEventData<T>> get fullDayEventList =>
UnmodifiableListView(_fullDayEventList);

//#region Data Manipulation Methods
void addFullDayEvent(CalendarEventData<T> event) {
// TODO: add separate logic for adding full day event and ranging event.
_fullDayEventList.addEventInSortedManner(event);
_eventList.add(event);
}
Expand All @@ -203,7 +208,7 @@ class CalendarData<T> {
}

void addSingleDayEvent(CalendarEventData<T> event) {
final date = event.date.withoutTime;
final date = event.date;

if (_singleDayEvents[date] == null) {
_singleDayEvents.addAll({
Expand Down Expand Up @@ -245,7 +250,7 @@ class CalendarData<T> {
}

void removeSingleDayEvent(CalendarEventData<T> event) {
if (_singleDayEvents[event.date.withoutTime]?.remove(event) ?? false) {
if (_singleDayEvents[event.date]?.remove(event) ?? false) {
_eventList.remove(event);
}
}
Expand Down Expand Up @@ -286,23 +291,20 @@ class CalendarData<T> {

//#region Data Fetch Methods
List<CalendarEventData<T>> getEventsOnDay(DateTime date,
{bool includeFullDays = true}) {
{bool includeFullDayEvents = true}) {
final events = <CalendarEventData<T>>[];

if (_singleDayEvents[date] != null) {
events.addAll(_singleDayEvents[date]!);
}

for (final rangingEvent in _rangingEventList) {
if (date == rangingEvent.date ||
date == rangingEvent.endDate ||
(date.isBefore(rangingEvent.endDate) &&
date.isAfter(rangingEvent.date))) {
if (rangingEvent.occursOnDate(date)) {
events.add(rangingEvent);
}
}

if (includeFullDays) {
if (includeFullDayEvents) {
events.addAll(getFullDayEvent(date));
}

Expand All @@ -312,9 +314,9 @@ class CalendarData<T> {
/// Returns full day events on given day.
List<CalendarEventData<T>> getFullDayEvent(DateTime date) {
final events = <CalendarEventData<T>>[];

for (final event in fullDayEventList) {
if (date.difference(event.date).inDays >= 0 &&
event.endDate.difference(date).inDays > 0) {
if (event.occursOnDate(date)) {
events.add(event);
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
eventArranger: eventArranger,
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollConfiguration,
events: controller
.getEventsOnDay(filteredDates[index]),
events: controller.getEventsOnDay(
filteredDates[index],
includeFullDayEvents: false,
),
heightPerMinute: heightPerMinute,
),
],
Expand Down

0 comments on commit 1e413a5

Please sign in to comment.