Skip to content

Commit

Permalink
♻️: Update UI
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Oct 25, 2024
1 parent 6a1e330 commit 43b323a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
55 changes: 40 additions & 15 deletions example/lib/widgets/add_event_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
DateTime? _startTime;
DateTime? _endTime;
RepeatFrequency? _selectedFrequency;
int _selectedIndex = DateTime.now().weekday - 1;
List<bool> _selectedDays = List.filled(7, false);

Color _color = Colors.blue;

Expand All @@ -45,6 +45,7 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
super.initState();

_setDefaults();
_setInitialWeekday();
}

@override
Expand Down Expand Up @@ -298,13 +299,18 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
(int index) {
return ChoiceChip(
label: Text(AppConstants.weekTitles[index]),
selected: _selectedIndex == index,
showCheckmark: false,
selected: _selectedDays[index],
onSelected: (bool selected) {
if (selected) {
_selectedIndex = index;
} else if (_selectedIndex == index) {
_selectedIndex = index;
}
setState(() {
_selectedDays[index] = selected;
if (!_selectedDays.contains(true)) {
_selectedDays[_startDate.weekday - 1] = true;
}
});

debugPrint('weekdays: ${_selectedDays}');
debugPrint('weekdays: ${_selectedIndexes}');
},
shape: CircleBorder(),
);
Expand Down Expand Up @@ -350,19 +356,38 @@ class _AddOrEditEventFormState extends State<AddOrEditEventForm> {
_form.currentState?.save();

final event = CalendarEventData(
date: _startDate,
endTime: _endTime,
startTime: _startTime,
endDate: _endDate,
color: _color,
title: _titleController.text.trim(),
description: _descriptionController.text.trim(),
);
date: _startDate,
endTime: _endTime,
startTime: _startTime,
endDate: _endDate,
color: _color,
title: _titleController.text.trim(),
description: _descriptionController.text.trim(),
recurrenceSettings: RecurrenceSettings(
_startDate,
frequency: _selectedFrequency ?? RepeatFrequency.daily,
weekdays: _selectedIndexes,
));

widget.onEventAdd?.call(event);
_resetForm();
}

List<int> get _selectedIndexes {
List<int> selectedIndexes = [];
for (int i = 0; i < _selectedDays.length; i++) {
if (_selectedDays[i] == true) {
selectedIndexes.add(i);
}
}
return selectedIndexes;
}

void _setInitialWeekday() {
final currentWeekday = DateTime.now().weekday - 1;
_selectedDays[currentWeekday] = true;
}

void _setDefaults() {
if (widget.event == null) return;

Expand Down
3 changes: 2 additions & 1 deletion lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class EventController<T extends Object?> extends ChangeNotifier {
final events = _calendarData.getEventsOnDay(date.withoutTime,
includeFullDayEvents: includeFullDayEvents);
events.addAll(getRepeatedEvents(date));
debugPrint('repeated: ${getRepeatedEvents(date)}');
return events;
}

Expand All @@ -160,7 +161,7 @@ class EventController<T extends Object?> extends ChangeNotifier {
events.add(event);
break;
case RepeatFrequency.weekly:
if (event.recurrenceSettings!.weekdays.contains(date.weekday)) {
if (event.recurrenceSettings!.weekdays.contains(date.weekday - 1)) {
events.add(event);
}
break;
Expand Down
25 changes: 25 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ extension DateTimeExtensions on DateTime {
"This extension is not being used in this package and will be removed "
"in next major release. Please use withoutTime instead.")
DateTime get dateYMD => DateTime(year, month, day);

/// Get weekday from day of date
WeekDays get getWeekDays {
switch (this.weekday) {
case 1:
return WeekDays.monday;
case 2:
return WeekDays.tuesday;
case 3:
return WeekDays.wednesday;
case 4:
return WeekDays.thursday;
case 5:
return WeekDays.friday;
case 6:
return WeekDays.saturday;
case 7:
return WeekDays.sunday;
default:
throw Exception("""
Date $this has unrecognisable weekday expencted [1-7],
but ${this.weekday} was provided.
""");
}
}
}

extension ColorExtension on Color {
Expand Down

0 comments on commit 43b323a

Please sign in to comment.