diff --git a/docs/pages/docs/calendar.mdx b/docs/pages/docs/calendar.mdx index f9aed8e21..293e1b808 100644 --- a/docs/pages/docs/calendar.mdx +++ b/docs/pages/docs/calendar.mdx @@ -33,7 +33,7 @@ to customize the date selection behavior. FCalendar( controller: FCalendarValueController( initialSelection: DateTime.utc(2024, 9, 13), - canSelect: (date) => true, + selectable: (date) => allowedDates.contains(date), ), start: DateTime.utc(2024), end: DateTime.utc(2030), @@ -91,7 +91,7 @@ FCalendar( FCalendar( controller: FCalendarMultiValueController( initialSelections: {DateTime.utc(2024, 7, 17), DateTime.utc(2024, 7, 20)}, - canSelect: (date) => !{DateTime.utc(2024, 7, 18), DateTime.utc(2024, 7, 19)}.contains(date), + selectable: (date) => !{DateTime.utc(2024, 7, 18), DateTime.utc(2024, 7, 19)}.contains(date), ), start: DateTime.utc(2000), today: DateTime.utc(2024, 7, 15), diff --git a/forui/CHANGELOG.md b/forui/CHANGELOG.md index dc63ee797..dcb51346f 100644 --- a/forui/CHANGELOG.md +++ b/forui/CHANGELOG.md @@ -9,6 +9,7 @@ * Change number of years displayed per page in `FCalendar` from 12 to 15. * **Breaking:** Move `FCalendar.enabled` to `FCalendarController.canSelect(...)`. +* **Breaking:** Rename `FCalendarController.contains(...)` to `FCalendarController.selected(...)`. * **Breaking:** Rename `FCalendarController.onPress(...)` to `FCalendarController.select(...)`. * **Breaking:** Rename `FCalendarEntryStyle.focusedBackgroundColor` to `FCalendarEntryStyle.hoveredBackgroundColor`. diff --git a/forui/lib/src/widgets/calendar/calendar.dart b/forui/lib/src/widgets/calendar/calendar.dart index 3dcaafa27..094a3d2ae 100644 --- a/forui/lib/src/widgets/calendar/calendar.dart +++ b/forui/lib/src/widgets/calendar/calendar.dart @@ -112,8 +112,8 @@ class FCalendar extends StatelessWidget { end: end.toLocalDate(), today: today.toLocalDate(), initial: _month.value, - selectable: (date) => controller.canSelect(date.toNative()), - selected: (date) => controller.contains(date.toNative()), + selectable: (date) => controller.selectable(date.toNative()), + selected: (date) => controller.selected(date.toNative()), onMonthChange: (date) { _month.value = date; onMonthChange?.call(date.toNative()); diff --git a/forui/lib/src/widgets/calendar/calendar_controller.dart b/forui/lib/src/widgets/calendar/calendar_controller.dart index 3f8b52bf1..c5d522b45 100644 --- a/forui/lib/src/widgets/calendar/calendar_controller.dart +++ b/forui/lib/src/widgets/calendar/calendar_controller.dart @@ -23,12 +23,12 @@ abstract class FCalendarController extends ValueNotifier { /// ## Note /// It is unsafe for this function to have side effects since it may be called more than once for a single date. As it /// is called frequently, it should not be computationally expensive. - bool canSelect(DateTime date); + bool selectable(DateTime date); /// Returns true if the given [date] is selected. /// /// [date] should always in UTC timezone and truncated to the nearest day. - bool contains(DateTime date); + bool selected(DateTime date); /// Selects the given [date]. /// @@ -40,26 +40,26 @@ abstract class FCalendarController extends ValueNotifier { /// /// The [DateTime]s are always in UTC timezone and truncated to the nearest date. class FCalendarValueController extends FCalendarController { - final Predicate _canSelect; + final Predicate _selectable; /// Creates a [FCalendarValueController] with the given initially selected date. /// - /// [canSelect] will always return true if not given. + /// [selectable] will always return true if not given. /// /// ## Contract /// Throws [AssertionError] if [initialSelection] is not in UTC timezone. FCalendarValueController({ DateTime? initialSelection, - Predicate? canSelect, + Predicate? selectable, }) : assert(initialSelection?.isUtc ?? true, 'value must be in UTC timezone'), - _canSelect = canSelect ?? _true, + _selectable = selectable ?? _true, super(initialSelection); @override - bool canSelect(DateTime date) => _canSelect(date); + bool selectable(DateTime date) => _selectable(date); @override - bool contains(DateTime date) => value?.toLocalDate() == date.toLocalDate(); + bool selected(DateTime date) => value?.toLocalDate() == date.toLocalDate(); @override void select(DateTime date) => value = value?.toLocalDate() == date.toLocalDate() ? null : date; @@ -70,7 +70,7 @@ class FCalendarValueController extends FCalendarController { /// /// The [DateTime]s are always in UTC timezone and truncated to the nearest day. class FCalendarMultiValueController extends FCalendarController> { - final Predicate _canSelect; + final Predicate _selectable; /// Creates a [FCalendarMultiValueController] with the given initial [value]. /// @@ -80,14 +80,14 @@ class FCalendarMultiValueController extends FCalendarController> { Set initialSelections = const {}, Predicate? canSelect, }) : assert(initialSelections.every((d) => d.isUtc), 'dates must be in UTC timezone'), - _canSelect = canSelect ?? _true, + _selectable = canSelect ?? _true, super(initialSelections); @override - bool canSelect(DateTime date) => _canSelect(date); + bool selectable(DateTime date) => _selectable(date); @override - bool contains(DateTime date) => value.contains(date); + bool selected(DateTime date) => value.contains(date); @override void select(DateTime date) { @@ -101,7 +101,7 @@ class FCalendarMultiValueController extends FCalendarController> { /// Both the start and end dates of the range is inclusive. The selected dates are always in UTC timezone and truncated /// to the nearest day. Unselectable dates within the selected range are selected regardless. class FCalendarRangeController extends FCalendarController<(DateTime, DateTime)?> { - final Predicate _canSelect; + final Predicate _selectable; /// Creates a [FCalendarRangeController] with the given initial [value]. /// @@ -122,14 +122,14 @@ class FCalendarRangeController extends FCalendarController<(DateTime, DateTime)? initialSelection.$1.isAtSameMomentAs(initialSelection.$2)), 'end date must be greater than or equal to start date', ), - _canSelect = canSelect ?? _true, + _selectable = canSelect ?? _true, super(initialSelection); @override - bool canSelect(DateTime date) => _canSelect(date); + bool selectable(DateTime date) => _selectable(date); @override - bool contains(DateTime date) { + bool selected(DateTime date) { if (value case (final first, final last)) { final current = date.toLocalDate(); return first.toLocalDate() <= current && current <= last.toLocalDate(); diff --git a/forui/lib/src/widgets/calendar/day/day_picker.dart b/forui/lib/src/widgets/calendar/day/day_picker.dart index 613ebafc4..3e8dee733 100644 --- a/forui/lib/src/widgets/calendar/day/day_picker.dart +++ b/forui/lib/src/widgets/calendar/day/day_picker.dart @@ -19,7 +19,7 @@ class DayPicker extends StatefulWidget { final LocalDate month; final LocalDate today; final LocalDate? focused; - final Predicate canSelect; + final Predicate selectable; final Predicate selected; final ValueChanged onPress; final ValueChanged onLongPress; @@ -29,7 +29,7 @@ class DayPicker extends StatefulWidget { required this.month, required this.today, required this.focused, - required this.canSelect, + required this.selectable, required this.selected, required this.onPress, required this.onLongPress, @@ -47,7 +47,7 @@ class DayPicker extends StatefulWidget { ..add(DiagnosticsProperty('month', month)) ..add(DiagnosticsProperty('today', today)) ..add(DiagnosticsProperty('focused', focused)) - ..add(DiagnosticsProperty('selectable', canSelect)) + ..add(DiagnosticsProperty('selectable', selectable)) ..add(DiagnosticsProperty('selected', selected)) ..add(DiagnosticsProperty('onPress', onPress)) ..add(DiagnosticsProperty('onLongPress', onLongPress)); @@ -112,7 +112,7 @@ class _DayPickerState extends State { focusNode: focusNode, current: date.month == widget.month.month, today: date == widget.today, - canSelect: widget.canSelect, + selectable: widget.selectable, selected: widget.selected, onPress: widget.onPress, onLongPress: widget.onLongPress, diff --git a/forui/lib/src/widgets/calendar/day/paged_day_picker.dart b/forui/lib/src/widgets/calendar/day/paged_day_picker.dart index 1851b6e1b..7cf86c8e9 100644 --- a/forui/lib/src/widgets/calendar/day/paged_day_picker.dart +++ b/forui/lib/src/widgets/calendar/day/paged_day_picker.dart @@ -51,7 +51,7 @@ class _PagedDayPickerState extends PagedPickerState { month: widget.start.truncate(to: DateUnit.months).plus(months: page), today: widget.today, focused: focusedDate, - canSelect: widget.selectable, + selectable: widget.selectable, selected: widget.selected, onPress: (date) { setState(() => focusedDate = date); diff --git a/forui/lib/src/widgets/calendar/shared/entry.dart b/forui/lib/src/widgets/calendar/shared/entry.dart index 3d21ef775..69535ddda 100644 --- a/forui/lib/src/widgets/calendar/shared/entry.dart +++ b/forui/lib/src/widgets/calendar/shared/entry.dart @@ -21,17 +21,17 @@ abstract class Entry extends StatelessWidget { required FocusNode focusNode, required bool current, required bool today, - required Predicate canSelect, + required Predicate selectable, required Predicate selected, required ValueChanged onPress, required ValueChanged onLongPress, }) { - final selectable = canSelect(date); - final select = selected(date); + final canSelect = selectable(date); + final isSelected = selected(date); - final styles = selectable ? style.selectableStyles : style.unselectableStyles; + final styles = canSelect ? style.selectableStyles : style.unselectableStyles; final dayStyle = current ? styles.current : styles.enclosing; - final entryStyle = select ? dayStyle.selectedStyle : dayStyle.unselectedStyle; + final entryStyle = isSelected ? dayStyle.selectedStyle : dayStyle.unselectedStyle; Widget builder(BuildContext context, FTappableState state, Widget? child) => _Content( style: entryStyle, @@ -44,12 +44,12 @@ abstract class Entry extends StatelessWidget { current: today, ); - if (select) { + if (canSelect) { return _SelectableEntry( focusNode: focusNode, date: date, semanticLabel: '${_yMMMMd.format(date.toNative())}${today ? ', Today' : ''}', - selected: select, + selected: isSelected, onPress: onPress, onLongPress: onLongPress, style: entryStyle, diff --git a/forui/test/src/widgets/calendar/calendar_controller_test.dart b/forui/test/src/widgets/calendar/calendar_controller_test.dart index 1eba6f407..2dd125823 100644 --- a/forui/test/src/widgets/calendar/calendar_controller_test.dart +++ b/forui/test/src/widgets/calendar/calendar_controller_test.dart @@ -15,7 +15,7 @@ void main() { ]) { test('contains(...) contains date', () { final controller = FCalendarValueController(initialSelection: DateTime.utc(2024, 5, 4)); - expect(controller.contains(date), expected); + expect(controller.selected(date), expected); }); } @@ -39,7 +39,7 @@ void main() { ]) { test('contains(...)', () { final controller = FCalendarMultiValueController(initialSelections: {DateTime.utc(2024)}); - expect(controller.contains(date), expected); + expect(controller.selected(date), expected); }); } @@ -71,9 +71,10 @@ void main() { ((DateTime.utc(2024), DateTime.utc(2025)), DateTime.utc(2026), false), (null, DateTime.utc(2023), false), ]) { - test('contains(...)', () { + test('selected(...)', () { + test('selected(...)', () { final controller = FCalendarRangeController(initialSelection: initial); - expect(controller.contains(date), expected); + expect(controller.selected(date), expected); }); }