Skip to content

Commit 9b3957f

Browse files
committed
Use "clock" for mocking dates and datetimes
This should make the tests a bit more robust, specially in CI since these tended to fail depending on how long and when the tests run. Also, rework a bit the way the start and end dates in sessions were being handled and initialised
1 parent 15805f2 commit 9b3957f

File tree

5 files changed

+193
-183
lines changed

5 files changed

+193
-183
lines changed

lib/helpers/json.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ String dateToUtcIso8601(DateTime dateTime) {
6767
* Needed e.g. when the wger api only sends a time but no date information.
6868
*/
6969
TimeOfDay stringToTime(String? time) {
70-
final String out = time ?? '00:00';
71-
return TimeOfDay.fromDateTime(DateTime.parse('2020-01-01 $out'));
70+
time ??= '00:00';
71+
return TimeOfDay.fromDateTime(DateTime.parse('2020-01-01 $time'));
7272
}
7373

7474
TimeOfDay? stringToTimeNull(String? time) {

lib/providers/gym_state.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:clock/clock.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_riverpod/flutter_riverpod.dart';
34
import 'package:logging/logging.dart';
@@ -26,7 +27,7 @@ class GymState {
2627
DateTime? validUntil,
2728
TimeOfDay? startTime}) {
2829
this.validUntil = validUntil ?? DateTime.now().add(DEFAULT_DURATION);
29-
this.startTime = startTime ?? TimeOfDay.now();
30+
this.startTime = startTime ?? TimeOfDay.fromDateTime(clock.now());
3031
}
3132

3233
GymState copyWith({

lib/widgets/routines/forms/session.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class SessionForm extends StatefulWidget {
4444
dayId: dayId,
4545
impression: DEFAULT_IMPRESSION,
4646
date: clock.now(),
47-
timeEnd: TimeOfDay.now(),
48-
timeStart: TimeOfDay.now(),
47+
timeEnd: TimeOfDay.fromDateTime(clock.now()),
48+
timeStart: null,
4949
);
5050

5151
@override
@@ -68,8 +68,10 @@ class _SessionFormState extends State<SessionForm> {
6868
void initState() {
6969
super.initState();
7070

71-
timeStartController.text = timeToString(widget._session.timeStart) ?? '';
72-
timeEndController.text = timeToString(widget._session.timeEnd) ?? '';
71+
timeStartController.text =
72+
widget._session.timeStart == null ? '' : timeToString(widget._session.timeStart)!;
73+
timeEndController.text =
74+
widget._session.timeEnd == null ? '' : timeToString(widget._session.timeEnd)!;
7375
notesController.text = widget._session.notes;
7476

7577
selectedImpression[widget._session.impression - 1] = true;
@@ -129,6 +131,7 @@ class _SessionFormState extends State<SessionForm> {
129131
},
130132
),
131133
Row(
134+
spacing: 10,
132135
children: [
133136
Flexible(
134137
child: TextFormField(
@@ -163,16 +166,19 @@ class _SessionFormState extends State<SessionForm> {
163166
if (timeStartController.text.isEmpty && timeEndController.text.isEmpty) {
164167
return null;
165168
}
166-
final TimeOfDay startTime = stringToTime(timeStartController.text);
167-
final TimeOfDay endTime = stringToTime(timeEndController.text);
168-
if (startTime.isAfter(endTime)) {
169-
return AppLocalizations.of(context).timeStartAhead;
169+
170+
if (timeStartController.text.isNotEmpty && timeEndController.text.isNotEmpty) {
171+
final TimeOfDay startTime = stringToTime(timeStartController.text);
172+
final TimeOfDay endTime = stringToTime(timeEndController.text);
173+
if (startTime.isAfter(endTime)) {
174+
return AppLocalizations.of(context).timeStartAhead;
175+
}
170176
}
177+
171178
return null;
172179
},
173180
),
174181
),
175-
const SizedBox(width: 10),
176182
Flexible(
177183
child: TextFormField(
178184
key: const ValueKey('time-end'),

lib/widgets/routines/gym_mode/session_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class SessionPage extends StatelessWidget {
4545
routineId: _routine.id!,
4646
impression: DEFAULT_IMPRESSION,
4747
date: clock.now(),
48-
timeEnd: TimeOfDay.now(),
4948
timeStart: start,
49+
timeEnd: TimeOfDay.fromDateTime(clock.now()),
5050
),
5151
);
5252

0 commit comments

Comments
 (0)