Skip to content

Commit

Permalink
Fixed PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Jul 26, 2024
1 parent c1d46b1 commit 86846b2
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/pages/docs/calendar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions forui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions forui/lib/src/widgets/calendar/calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
32 changes: 16 additions & 16 deletions forui/lib/src/widgets/calendar/calendar_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ abstract class FCalendarController<T> extends ValueNotifier<T> {
/// ## 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].
///
Expand All @@ -40,26 +40,26 @@ abstract class FCalendarController<T> extends ValueNotifier<T> {
///
/// The [DateTime]s are always in UTC timezone and truncated to the nearest date.
class FCalendarValueController extends FCalendarController<DateTime?> {
final Predicate<DateTime> _canSelect;
final Predicate<DateTime> _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<DateTime>? canSelect,
Predicate<DateTime>? 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;
Expand All @@ -70,7 +70,7 @@ class FCalendarValueController extends FCalendarController<DateTime?> {
///
/// The [DateTime]s are always in UTC timezone and truncated to the nearest day.
class FCalendarMultiValueController extends FCalendarController<Set<DateTime>> {
final Predicate<DateTime> _canSelect;
final Predicate<DateTime> _selectable;

/// Creates a [FCalendarMultiValueController] with the given initial [value].
///
Expand All @@ -80,14 +80,14 @@ class FCalendarMultiValueController extends FCalendarController<Set<DateTime>> {
Set<DateTime> initialSelections = const {},
Predicate<DateTime>? 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) {
Expand All @@ -101,7 +101,7 @@ class FCalendarMultiValueController extends FCalendarController<Set<DateTime>> {
/// 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<DateTime> _canSelect;
final Predicate<DateTime> _selectable;

/// Creates a [FCalendarRangeController] with the given initial [value].
///
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions forui/lib/src/widgets/calendar/day/day_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DayPicker extends StatefulWidget {
final LocalDate month;
final LocalDate today;
final LocalDate? focused;
final Predicate<LocalDate> canSelect;
final Predicate<LocalDate> selectable;
final Predicate<LocalDate> selected;
final ValueChanged<LocalDate> onPress;
final ValueChanged<LocalDate> onLongPress;
Expand All @@ -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,
Expand All @@ -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));
Expand Down Expand Up @@ -112,7 +112,7 @@ class _DayPickerState extends State<DayPicker> {
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,
Expand Down
2 changes: 1 addition & 1 deletion forui/lib/src/widgets/calendar/day/paged_day_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _PagedDayPickerState extends PagedPickerState<PagedDayPicker> {
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);
Expand Down
14 changes: 7 additions & 7 deletions forui/lib/src/widgets/calendar/shared/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ abstract class Entry extends StatelessWidget {
required FocusNode focusNode,
required bool current,
required bool today,
required Predicate<LocalDate> canSelect,
required Predicate<LocalDate> selectable,
required Predicate<LocalDate> selected,
required ValueChanged<LocalDate> onPress,
required ValueChanged<LocalDate> 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,
Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions forui/test/src/widgets/calendar/calendar_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 86846b2

Please sign in to comment.