From a053c813bcf9eee369baefb9bff49e462eea3929 Mon Sep 17 00:00:00 2001 From: Arun Muralidharan Date: Tue, 22 Jun 2021 20:19:16 +0200 Subject: [PATCH] Start Time Validator Implemented start time validator to ensure start time is not ahead of end time in gym mode. --- lib/helpers/misc.dart | 17 +++++++++++++++++ lib/l10n/app_en.arb | 1 + lib/widgets/workouts/gym_mode.dart | 11 ++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/helpers/misc.dart b/lib/helpers/misc.dart index bbee7333..70147e0a 100644 --- a/lib/helpers/misc.dart +++ b/lib/helpers/misc.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'package:flutter/material.dart'; import 'package:wger/helpers/consts.dart'; import 'package:wger/models/workouts/repetition_unit.dart'; import 'package:wger/models/workouts/weight_unit.dart'; @@ -69,3 +70,19 @@ List daysInRange(DateTime first, DateTime last) { (index) => DateTime.utc(first.year, first.month, first.day + index), ); } + +extension TimeOfDayExtension on TimeOfDay { + bool isAfter(TimeOfDay other) { + if(toMinutes() > other.toMinutes()) return true; + else return false; + } + + bool isBefore(TimeOfDay other) { + if(toMinutes() < other.toMinutes()) return true; + else return false; + } + + int toMinutes() { + return ((hour*60) + minute); + } +} \ No newline at end of file diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index c37c279f..191af707 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -274,6 +274,7 @@ "@timeEnd": { "description": "The end time of a workout" }, + "timeStartAhead": "Start time cannot be ahead of end time", "ingredient": "Ingredient", "@ingredient": {}, "energy": "Energy", diff --git a/lib/widgets/workouts/gym_mode.dart b/lib/widgets/workouts/gym_mode.dart index 27c8e84b..6999bce7 100644 --- a/lib/widgets/workouts/gym_mode.dart +++ b/lib/widgets/workouts/gym_mode.dart @@ -27,6 +27,7 @@ import 'package:wger/helpers/consts.dart'; import 'package:wger/helpers/gym_mode.dart'; import 'package:wger/helpers/json.dart'; import 'package:wger/helpers/ui.dart'; +import 'package:wger/helpers/misc.dart'; import 'package:wger/models/exercises/exercise.dart'; import 'package:wger/models/http_exception.dart'; import 'package:wger/models/workouts/day.dart'; @@ -823,6 +824,14 @@ class _SessionPageState extends State { onSaved: (newValue) { _session.timeStart = stringToTime(newValue); }, + validator: (_) { + TimeOfDay startTime = stringToTime(timeStartController.text); + TimeOfDay endTime = stringToTime(timeEndController.text); + if(startTime.isAfter(endTime)) { + return AppLocalizations.of(context).timeStartAhead; + } + return null; + } ), ), SizedBox(width: 10), @@ -842,7 +851,7 @@ class _SessionPageState extends State { initialTime: TimeOfDay.now(), ); - timeStartController.text = timeToString(pickedTime)!; + timeEndController.text = timeToString(pickedTime)!; }, onSaved: (newValue) { _session.timeEnd = stringToTime(newValue);