feat: add button for clearing selected availabilities on availability overview

This commit is contained in:
Freek van de Ven 2024-07-10 16:59:02 +02:00 committed by Bart Ribbers
parent e3474e779b
commit 5bfac2f4e3
3 changed files with 66 additions and 15 deletions

View file

@ -18,6 +18,9 @@ class AvailabilityTranslations {
required this.availabilityWithoutTemplateLabel, required this.availabilityWithoutTemplateLabel,
required this.overviewScreenTitle, required this.overviewScreenTitle,
required this.createTemplateButton, required this.createTemplateButton,
required this.clearAvailabilityButton,
required this.clearAvailabilityConfirmTitle,
required this.clearAvailabilityConfirmDescription,
required this.unavailableForDay, required this.unavailableForDay,
required this.unavailableForMultipleDays, required this.unavailableForMultipleDays,
required this.availabilityAddTemplateTitle, required this.availabilityAddTemplateTitle,
@ -63,6 +66,10 @@ class AvailabilityTranslations {
this.templateSelectionLabel = "Selected day(s)", this.templateSelectionLabel = "Selected day(s)",
this.availabilityWithoutTemplateLabel = "Availabilty without template", this.availabilityWithoutTemplateLabel = "Availabilty without template",
this.createTemplateButton = "Create a new template", this.createTemplateButton = "Create a new template",
this.clearAvailabilityButton = "Not available on these days",
this.clearAvailabilityConfirmTitle = "Are you sure you want to clear?",
this.clearAvailabilityConfirmDescription =
"This will remove all availabilities for the selected days",
this.unavailableForDay = "I am not available this day", this.unavailableForDay = "I am not available this day",
this.unavailableForMultipleDays = "I am not available these days", this.unavailableForMultipleDays = "I am not available these days",
this.availabilityAddTemplateTitle = "Add template to availability", this.availabilityAddTemplateTitle = "Add template to availability",
@ -123,6 +130,16 @@ class AvailabilityTranslations {
/// The label on the button to go to the template screen /// The label on the button to go to the template screen
final String createTemplateButton; final String createTemplateButton;
/// The text shown on the button to clear the selected range on the
/// overview page
final String clearAvailabilityButton;
/// The title for the confirmation dialog when clearing the availability
final String clearAvailabilityConfirmTitle;
/// The description for the confirmation dialog when clearing the availability
final String clearAvailabilityConfirmDescription;
/// The text shown to clear the availability for a day /// The text shown to clear the availability for a day
final String unavailableForDay; final String unavailableForDay;

View file

@ -51,6 +51,20 @@ class _AvailabilityOverviewState extends State<AvailabilityOverview> {
var availabilitySnapshot = useStream(availabilityStream); var availabilitySnapshot = useStream(availabilityStream);
var selectedAvailabilities = _selectedRange != null
? availabilitySnapshot.data
?.where(
(a) =>
!a.availabilityModel.startDate
.isBefore(_selectedRange!.start) &&
!a.availabilityModel.endDate.isAfter(_selectedRange!.end),
)
.toList() ??
<AvailabilityWithTemplate>[]
: <AvailabilityWithTemplate>[];
var availabilitiesAreSelected = selectedAvailabilities.isNotEmpty;
var title = Center( var title = Center(
child: Text( child: Text(
translations.overviewScreenTitle, translations.overviewScreenTitle,
@ -82,26 +96,36 @@ class _AvailabilityOverviewState extends State<AvailabilityOverview> {
var onButtonPress = _selectedRange == null var onButtonPress = _selectedRange == null
? null ? null
: () { : () {
var availabilitesWithinSelectedRange = availabilitySnapshot.data
?.where(
(a) =>
a.availabilityModel.startDate
.isAfter(_selectedRange!.start) &&
a.availabilityModel.endDate
.isBefore(_selectedRange!.end),
)
.toList() ??
<AvailabilityWithTemplate>[];
widget.onEditDateRange( widget.onEditDateRange(
_selectedRange!, _selectedRange!,
availabilitesWithinSelectedRange, selectedAvailabilities,
); );
setState(() { setState(() {
_selectedRange = null; _selectedRange = null;
}); });
}; };
Future<void> onClearButtonClicked() async {
var confirmed = await options.confirmationDialogBuilder(
context,
title: translations.clearAvailabilityConfirmTitle,
description: translations.clearAvailabilityConfirmDescription,
);
if (confirmed ?? false) {
await service
.clearAvailabilities(selectedAvailabilities.getAvailabilities());
setState(() {
_selectedRange = null;
});
}
}
var clearSelectedButton = options.textButtonBuilder(
context,
onClearButtonClicked,
Text(translations.clearAvailabilityButton),
);
var startEditButton = options.primaryButtonBuilder( var startEditButton = options.primaryButtonBuilder(
context, context,
onButtonPress, onButtonPress,
@ -135,7 +159,15 @@ class _AvailabilityOverviewState extends State<AvailabilityOverview> {
), ),
child: Align( child: Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
child: startEditButton, child: Column(
children: [
startEditButton,
if (availabilitiesAreSelected) ...[
const SizedBox(height: 8),
clearSelectedButton,
],
],
),
), ),
), ),
), ),

View file

@ -105,9 +105,11 @@ class _AvailabilityDayTemplateEditState
var timeSection = TemplateTimeSelection( var timeSection = TemplateTimeSelection(
key: ValueKey(_template.templateData), key: ValueKey(_template.templateData),
startTime: TimeOfDay.fromDateTime( startTime: TimeOfDay.fromDateTime(
(_template.templateData as DayTemplateData).startTime,), (_template.templateData as DayTemplateData).startTime,
),
endTime: TimeOfDay.fromDateTime( endTime: TimeOfDay.fromDateTime(
(_template.templateData as DayTemplateData).endTime,), (_template.templateData as DayTemplateData).endTime,
),
onStartChanged: (start) { onStartChanged: (start) {
var startTime = (_template.templateData as DayTemplateData).startTime; var startTime = (_template.templateData as DayTemplateData).startTime;
var updatedStartTime = DateTime( var updatedStartTime = DateTime(