Commit 175c487

derdilla <82763757+derdilla@users.noreply.github.com>
2024-11-04 08:55:26
Fix custom date range ending before midnight (#467)
* test for including end of day in custom time step * include end of day in custom time step * improve test scenario
1 parent 8470492
Changed files (2)
app
app/lib/data_util/interval_picker.dart
@@ -12,10 +12,18 @@ import 'package:week_of_year/date_week_extensions.dart';
 /// in both directions.
 class IntervalPicker extends StatelessWidget {
   /// Create a selector for [IntervalStorage] values.
-  const IntervalPicker({super.key, required this.type});
+  const IntervalPicker({super.key,
+    required this.type,
+    this.customRangePickerCurrentDay,
+  });
 
   /// Which range to display and modify.
   final IntervalStoreManagerLocation type;
+
+  /// The day from which to start the custom date range picker.
+  ///
+  /// Defaults to the current day which is the desired value in all non-test scenarios.
+  final DateTime? customRangePickerCurrentDay;
   
   @override
   Widget build(BuildContext context) => Consumer<IntervalStoreManager>(
@@ -52,11 +60,15 @@ class IntervalPicker extends StatelessWidget {
                       final res = await showDateRangePicker(
                         context: context,
                         firstDate: DateTime.fromMillisecondsSinceEpoch(1),
-                        lastDate: DateTime.now(),
+                        lastDate: customRangePickerCurrentDay ?? DateTime.now(),
+                        currentDate: customRangePickerCurrentDay,
                       );
                       if (res != null) {
                         intervall.changeStepSize(value!);
-                        intervall.currentRange = res.dateRange;
+                        final dateRange = res.dateRange.copyWith(
+                          end: res.end.copyWith(hour: 23, minute: 59, second: 59),
+                        );
+                        intervall.currentRange = dateRange;
                       }
                     } else if (value != null) {
                       intervall.changeStepSize(value);
app/test/data_util/interval_picker_test.dart
@@ -84,4 +84,48 @@ void main() {
     await tester.pumpAndSettle();
     expect(find.text('${year + 1}'), findsOneWidget);
   });
+  testWidgets('selected custom interval gets interpreted correctly', (tester) async {
+    final s = IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage());
+
+    await tester.pumpWidget(materialApp(ChangeNotifierProvider.value(
+      value: s,
+      child: IntervalPicker(
+        type: IntervalStoreManagerLocation.mainPage,
+        customRangePickerCurrentDay: DateTime(2024, 1, 25),
+      ),
+    )));
+    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
+    final materialLocalizations = await DefaultMaterialLocalizations.delegate.load(const Locale('en'));
+
+    await tester.tap(find.byType(DropdownButton<TimeStep>));
+    await tester.pumpAndSettle();
+    
+    await tester.tap(find.text(localizations.custom));
+    await tester.pumpAndSettle(); // opens date interval selection
+
+    await tester.tap(find.text('20').first);
+    await tester.pump();
+    await tester.tap(find.text('25').first);
+    await tester.pump();
+    await tester.tap(find.text(materialLocalizations.saveButtonLabel).first);
+    await tester.pumpAndSettle();
+
+    expect(find.byType(DateRangePickerDialog), findsNothing);
+
+    expect(s.mainPage.stepSize, TimeStep.custom);
+    expect(s.mainPage.currentRange.start.year, 2024);
+    expect(s.mainPage.currentRange.start.month, 1);
+    expect(s.mainPage.currentRange.start.day, 20);
+    expect(s.mainPage.currentRange.end.year, 2024);
+    expect(s.mainPage.currentRange.end.month, 1);
+    expect(s.mainPage.currentRange.end.day, 25);
+
+    expect(s.mainPage.currentRange.end.hour, 23, reason: 'should always be after newer measurements (#466)');
+    expect(s.mainPage.currentRange.end.minute, 59, reason: 'should always be after newer measurements (#466)');
+    expect(s.mainPage.currentRange.end.second, 59, reason: 'should always be after newer measurements (#466)');
+
+    expect(s.mainPage.currentRange.start.hour, 0);
+    expect(s.mainPage.currentRange.start.minute, 0);
+    expect(s.mainPage.currentRange.start.second, 0);
+  });
 }