fix: handle nullable duration for breaks and correctly update breaks

This commit is contained in:
Freek van de Ven 2024-07-18 16:37:38 +02:00 committed by FlutterJoey
parent 5b98ae487b
commit 020d7a71f8
2 changed files with 16 additions and 4 deletions

View file

@ -33,6 +33,14 @@ class BreakViewModel {
/// [endTime] /// [endTime]
final Duration? duration; final Duration? duration;
/// Get the duration in minutes
/// If the duration is null, return the difference between the start and end
/// time in minutes
int get durationInMinutes =>
duration?.inMinutes ??
((endTime!.hour * 60 + endTime!.minute) -
(startTime!.hour * 60 + startTime!.minute));
/// Returns true if the break is valid /// Returns true if the break is valid
/// The start is before the end and the duration is equal or lower than the /// The start is before the end and the duration is equal or lower than the
/// difference between the start and end /// difference between the start and end

View file

@ -56,8 +56,9 @@ class PauseSelection extends StatelessWidget {
Future<void> onEditBreak(BreakViewModel availabilityBreak) async { Future<void> onEditBreak(BreakViewModel availabilityBreak) async {
var updatedBreak = await openBreakDialog(availabilityBreak); var updatedBreak = await openBreakDialog(availabilityBreak);
if (updatedBreak == null) return; if (updatedBreak == null) return;
// remove the old break and add the updated one
var updatedBreaks = [...breaks, updatedBreak]; var updatedBreaks = [...breaks, updatedBreak];
updatedBreaks.remove(availabilityBreak);
onBreaksChanged(updatedBreaks); onBreaksChanged(updatedBreaks);
} }
@ -145,6 +146,8 @@ class BreakDisplay extends StatelessWidget {
breakModel.endTime!, breakModel.endTime!,
); );
var breakDuration = breakModel.durationInMinutes;
return InkWell( return InkWell(
onTap: onClick, onTap: onClick,
child: Container( child: Container(
@ -157,7 +160,7 @@ class BreakDisplay extends StatelessWidget {
child: Row( child: Row(
children: [ children: [
Text( Text(
"${breakModel.duration!.inMinutes} " "$breakDuration "
"${translations.timeMinutes} | " "${translations.timeMinutes} | "
"$starTime - " "$starTime - "
"$endTime", "$endTime",
@ -267,9 +270,10 @@ class _AvailabilityBreakSelectionDialogState
? () { ? () {
if (_breakViewModel.isValid) { if (_breakViewModel.isValid) {
Navigator.of(context).pop(_breakViewModel); Navigator.of(context).pop(_breakViewModel);
} else {
debugPrint("Break is not valid");
// TODO(freek): show error message
} }
debugPrint("Break is not valid");
// TODO(freek): show error message
} }
: null; : null;