From 368b1be3c0b23be4f868981c9711a18e046048ca Mon Sep 17 00:00:00 2001 From: Rafeek-Khoudare Date: Mon, 7 Jul 2025 17:07:23 +0300 Subject: [PATCH] fix conditions for start and end time for reservation --- .../manage_bookable_spaces_screen.dart | 18 +++++-- .../screens/setup_bookable_spaces_dialog.dart | 12 +++-- .../widgets/step_two_details_widget.dart | 50 +++++++++++++++---- .../widgets/time_picker_widget.dart | 5 +- lib/utils/string_utils.dart | 19 +++++++ 5 files changed, 86 insertions(+), 18 deletions(-) diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/manage_bookable_spaces_screen.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/manage_bookable_spaces_screen.dart index f3a21be1..dad161f1 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/manage_bookable_spaces_screen.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/manage_bookable_spaces_screen.dart @@ -142,12 +142,22 @@ class ManageBookableSpacesWidget extends StatelessWidget { .toList(), ), )), - DataCell(Padding( + DataCell( + Padding( padding: const EdgeInsetsGeometry.only(left: 10), - child: Text(space.spaceConfig.bookingStartTime))), - DataCell(Padding( + child: Text( + space.spaceConfig.bookingStartTime.format(context), + ), + ), + ), + DataCell( + Padding( padding: const EdgeInsetsGeometry.only(left: 10), - child: Text(space.spaceConfig.bookingEndTime))), + child: Text( + space.spaceConfig.bookingEndTime.format(context), + ), + ), + ), DataCell(Padding( padding: const EdgeInsetsGeometry.only(left: 10), child: Text('${space.spaceConfig.cost} Points'))), diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart index f073b6ba..a4f32f63 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/screens/setup_bookable_spaces_dialog.dart @@ -79,6 +79,7 @@ class SetupBookableSpacesDialog extends StatelessWidget { if (selectedSpaces.isNotEmpty) { context.read().goToNextStep(); } else { + ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Please select at least one space.'), @@ -87,18 +88,23 @@ class SetupBookableSpacesDialog extends StatelessWidget { } } else if (stepsState is StepTwoState) { selectedSpaces.forEach( - (e) => - e.spaceConfig.cost = int.parse(pointsController.text), + (e) => e.spaceConfig.cost = int.parse( + pointsController.text.isEmpty + ? '0' + : pointsController.text), ); if (selectedSpaces.any( (element) => !element.isValid, )) { + ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Please fill the required fields.'), ), ); - } else {} + } else { + print(selectedSpaces.first.spaceUuid); + } } }, onCancelPressed: () => context.pop(), diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/step_two_details_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/step_two_details_widget.dart index 8825fb6a..dc750ebb 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/step_two_details_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/step_two_details_widget.dart @@ -5,6 +5,8 @@ import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/prese import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/search_unbookable_spaces_widget.dart'; import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart'; import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart'; +import 'package:syncrow_web/utils/color_manager.dart'; +import 'package:syncrow_web/utils/string_utils.dart'; class StepTwoDetailsWidget extends StatelessWidget { final TextEditingController pointsController; @@ -29,22 +31,52 @@ class StepTwoDetailsWidget extends StatelessWidget { TitleAndTimePickerWidget( title: 'Booking Start Time', onTimePicked: (timePicked) { + if (timePicked == null) { + return; + } final nonBookableBloc = context.read(); - nonBookableBloc.selectedBookableSpaces.forEach( - (e) => e.spaceConfig.bookingStartTime = - timePicked!.format(context), - ); + if (isEndTimeAfterStartTime( + timePicked, nonBookableBloc.endTime)) { + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + content: + Text("You can't choose start Time Before End time"), + duration: Duration(seconds: 2), + backgroundColor: ColorsManager.red, + )); + throw Exception(); + } else { + nonBookableBloc.selectedBookableSpaces.forEach( + (e) => e.spaceConfig.bookingStartTime = timePicked, + ); + } }, ), - SizedBox(width: 20,), + const SizedBox( + width: 20, + ), TitleAndTimePickerWidget( title: 'Booking End Time', onTimePicked: (timePicked) { + if (timePicked == null) { + return; + } final nonBookableBloc = context.read(); - nonBookableBloc.selectedBookableSpaces.forEach( - (e) => e.spaceConfig.bookingEndTime = - timePicked!.format(context), - ); + if (isEndTimeAfterStartTime( + nonBookableBloc.startTime, timePicked)) { + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + content: + Text("You can't choose End Time After Start time"), + duration: Duration(seconds: 2), + backgroundColor: ColorsManager.red, + )); + throw Exception(); + } else { + nonBookableBloc.selectedBookableSpaces.forEach( + (e) => e.spaceConfig.bookingEndTime = timePicked, + ); + } }, ) ], diff --git a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart index 608fc06a..c02491f3 100644 --- a/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart +++ b/lib/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart @@ -23,7 +23,7 @@ class _TimePickerWidgetState extends State { return InkWell( borderRadius: BorderRadius.circular(10), onTap: () async { - timePicked = await showTimePicker( + final tempTime = await showTimePicker( context: context, initialTime: TimeOfDay.now(), builder: (context, child) { @@ -38,7 +38,8 @@ class _TimePickerWidgetState extends State { ); }, ); - widget.onTimePicked(timePicked); + widget.onTimePicked(tempTime); + timePicked = tempTime; setState(() {}); }, child: Row( diff --git a/lib/utils/string_utils.dart b/lib/utils/string_utils.dart index c8d8e2af..b1424412 100644 --- a/lib/utils/string_utils.dart +++ b/lib/utils/string_utils.dart @@ -1,6 +1,25 @@ +import 'package:flutter/material.dart'; + class StringUtils { static String capitalizeFirstLetter(String text) { if (text.isEmpty) return text; return text[0].toUpperCase() + text.substring(1); } } + +bool isEndTimeAfterStartTime(TimeOfDay start, TimeOfDay end) { + final startMinutes = start.hour * 60 + start.minute; + final endMinutes = end.hour * 60 + end.minute; + + if (endMinutes <= startMinutes) { + return true; + } + + return false; +} + +String formatTimeOfDayTo24HourString(TimeOfDay time) { + final hour = time.hour.toString().padLeft(2, '0'); + final minute = time.minute.toString().padLeft(2, '0'); + return '$hour:$minute'; +}