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

Day of week group #148

Merged
merged 3 commits into from
Jan 28, 2020
Merged
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
1 change: 1 addition & 0 deletions device_calendar/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Updated property summaries for issues [121](https://github.com/builttoroam/flutter_plugins/issues/121) and [122](https://github.com/builttoroam/flutter_plugins/issues/122)
* Updated example documentation for issue [119](https://github.com/builttoroam/flutter_plugins/issues/119)
* Read-only calendars cannot be edited or deleted for the example app
* Added `DayOfWeekGroup` enum and an extension `getDays` to get corresponding dates of the enum values (**NOTE**: `DayOfWeekGroup.Custom.getDays` will return an empty list)

## 3.0.0 21st January 2020

Expand Down
22 changes: 22 additions & 0 deletions device_calendar/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ You'll be able to get a reference of how the APIs are used.

For a full API reference, the documentation can be found at [pub.dev](https://pub.dev/documentation/device_calendar/latest/device_calendar/device_calendar-library.html).

## DayOfWeekGroup Enum

`DayOfWeekGroup` enum allows to explicitly choose and return a list of `DayOfWeek` enum values by using an extension `getDays`:

* `DayOfWeekGroup.Weekday.getDays` will return:

```dart
[DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday];
```

* `DayOfWeekGroup.Weekend.getDays` will return:

```dart
[DayOfWeek.Saturday, DayOfWeek.Sunday];
```

* `DayOfWeekGroup.Alldays.getDays` will return:

```dart
[DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday];
```

## Attendee Examples

Examples below present on how to initialise an `Attendee` model in Dart:
Expand Down
98 changes: 81 additions & 17 deletions device_calendar/example/lib/presentation/pages/calendar_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'event_attendee.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:collection/collection.dart';

import '../date_time_picker.dart';
import 'event_reminders.dart';
Expand Down Expand Up @@ -36,6 +37,7 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
TimeOfDay _endTime;

bool _autovalidate = false;
DayOfWeekGroup _dayOfWeekGroup = DayOfWeekGroup.None;

bool _isRecurringEvent = false;
bool _isByDayOfMonth = false;
Expand Down Expand Up @@ -104,6 +106,10 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
_weekOfMonth = _event.recurrenceRule.weekOfMonth ?? WeekNumber.First;
_selectedDayOfWeek = _daysOfWeek.isNotEmpty ? _daysOfWeek.first : DayOfWeek.Monday;
_dayOfMonth = _event.recurrenceRule.dayOfMonth ?? 1;

if (_daysOfWeek.isNotEmpty) {
_updateDaysOfWeekGroup();
}
}
}

Expand Down Expand Up @@ -389,23 +395,38 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
),
if (_recurrenceFrequency == RecurrenceFrequency.Weekly) ... [
Column(
children: [
...DayOfWeek.values.map((day) {
return CheckboxListTile(
title: Text(day.enumToString),
value: _daysOfWeek?.any((dow) => dow == day) ?? false,
onChanged: (selected) {
setState(() {
if (selected) {
_daysOfWeek.add(day);
} else {
_daysOfWeek.remove(day);
}
});
},
);
},
),
children: [
...DayOfWeek.values.map((day) {
return CheckboxListTile(
title: Text(day.enumToString),
value: _daysOfWeek?.any((dow) => dow == day) ?? false,
onChanged: (selected) {
setState(() {
if (selected) {
_daysOfWeek.add(day);
} else {
_daysOfWeek.remove(day);
}
_updateDaysOfWeekGroup(selectedDay: day);
});
},
);
}),
Divider(color: Colors.black),
...DayOfWeekGroup.values.map((group) {
return RadioListTile(
title: Text(group.enumToString),
value: group,
groupValue: _dayOfWeekGroup,
onChanged: (selected) {
setState(() {
_dayOfWeekGroup = selected;
_updateDaysOfWeek();
});
},
controlAffinity: ListTileControlAffinity.trailing
);
}),
],
)
],
Expand Down Expand Up @@ -695,6 +716,49 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
}
}

void _updateDaysOfWeek() {
var days = _dayOfWeekGroup.getDays;

switch (_dayOfWeekGroup) {
case DayOfWeekGroup.Weekday:
case DayOfWeekGroup.Weekend:
case DayOfWeekGroup.Alldays:
_daysOfWeek.clear();
_daysOfWeek.addAll(days.where((a) => _daysOfWeek.every((b) => a != b)));
break;
case DayOfWeekGroup.None:
_daysOfWeek.clear();
break;
default: // DayOfWeekGroup.Custom, it shouldn't be changed
break;
}
}

void _updateDaysOfWeekGroup({DayOfWeek selectedDay}) {
var deepEquality = const DeepCollectionEquality.unordered().equals;

// If _daysOfWeek contains nothing
if (_daysOfWeek.isEmpty && _dayOfWeekGroup != DayOfWeekGroup.None) {
_dayOfWeekGroup = DayOfWeekGroup.None;
}
// If _daysOfWeek contains Monday to Friday
else if (deepEquality(_daysOfWeek, DayOfWeekGroup.Weekday.getDays) && _dayOfWeekGroup != DayOfWeekGroup.Weekday) {
_dayOfWeekGroup = DayOfWeekGroup.Weekday;
}
// If _daysOfWeek contains Saturday and Sunday
else if (deepEquality(_daysOfWeek, DayOfWeekGroup.Weekend.getDays) && _dayOfWeekGroup != DayOfWeekGroup.Weekend) {
_dayOfWeekGroup = DayOfWeekGroup.Weekend;
}
// If _daysOfWeek contains all days
else if (deepEquality(_daysOfWeek, DayOfWeekGroup.Alldays.getDays) && _dayOfWeekGroup != DayOfWeekGroup.Alldays) {
_dayOfWeekGroup = DayOfWeekGroup.Alldays;
}
// Otherwise, set as Custom
else {
_dayOfWeekGroup = DayOfWeekGroup.Custom;
}
}

String _validateTotalOccurrences(String value) {
if (value.isNotEmpty && int.tryParse(value) == null) {
return 'Total occurrences needs to be a valid number';
Expand Down
30 changes: 30 additions & 0 deletions device_calendar/lib/src/common/calendar_enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ enum DayOfWeek {
Sunday
}

enum DayOfWeekGroup {
None,
Weekday,
Weekend,
Alldays,
Custom
}

enum MonthOfYear {
January,
Feburary,
Expand Down Expand Up @@ -62,6 +70,28 @@ extension DayOfWeekExtension on DayOfWeek {
String get enumToString => _enumToString(this);
}

extension DaysOfWeekGroupExtension on DayOfWeekGroup {
static List<DayOfWeek> _getDays(DayOfWeekGroup val) {
switch (val) {
case DayOfWeekGroup.Weekday:
return [DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday];
case DayOfWeekGroup.Weekend:
return [DayOfWeek.Saturday, DayOfWeek.Sunday];
case DayOfWeekGroup.Alldays:
return [DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday];
default:
return [];
}
}

String _enumToString(DayOfWeekGroup enumValue) {
return enumValue.toString().split('.').last;
}

List<DayOfWeek> get getDays => _getDays(this);
String get enumToString => _enumToString(this);
}

extension MonthOfYearExtension on MonthOfYear {
static int _value(MonthOfYear val) {
switch (val) {
Expand Down
1 change: 1 addition & 0 deletions device_calendar/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.1.8
collection: any

dev_dependencies:
flutter_test:
Expand Down