Commit d111d8e

derdilla <derdilla06@gmail.com>
2023-05-29 08:47:51
change position in time of default interval
1 parent 03ce2a5
lib/components/display_interval_picker.dart
@@ -0,0 +1,68 @@
+import 'package:blood_pressure_app/model/settings_store.dart';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+
+class IntervalPicker extends StatelessWidget{
+  const IntervalPicker({super.key});
+
+  @override
+  Widget build(BuildContext context) {
+    return Consumer<Settings>(
+        builder: (context, settings, child) {
+          return Row(
+              children: [
+                Expanded(
+                  flex: 30,
+                  child: MaterialButton(
+                    onPressed: () {
+                      settings.moveDisplayDataByStep(-1);
+                    },
+                    child: const Icon(
+                      Icons.chevron_left,
+                      size: 48,
+                    ),
+                  ),
+                ),
+
+                Expanded(
+                  flex: 40,
+                  child: DropdownButton<int>(
+                    value: settings.graphStepSize,
+                    isExpanded: true,
+                    onChanged: (int? value) {
+                      if (value != null) {
+                        settings.changeStepSize(value);
+                      }
+                    },
+                    items: TimeStep.options.map<DropdownMenuItem<int>>((v) {
+                      return DropdownMenuItem(
+                          value: v,
+                          child: Text(
+                              TimeStep.getName(v)
+                          )
+                      );
+                    }).toList(),
+                  ),
+                ),
+
+
+                Expanded(
+                  flex: 30,
+                  child: MaterialButton(
+                    onPressed: () {
+                      settings.moveDisplayDataByStep(1);
+                    },
+                    child: const Icon(
+                      Icons.chevron_right,
+                      size: 48,
+                    ),
+                  ),
+                ),
+              ]
+          );
+        }
+    );
+  }
+
+}
\ No newline at end of file
lib/components/measurement_graph.dart
@@ -1,5 +1,6 @@
 import 'dart:collection';
 
+import 'package:blood_pressure_app/components/display_interval_picker.dart';
 import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:flutter/material.dart';
 import 'package:fl_chart/fl_chart.dart';
@@ -166,33 +167,6 @@ class MeasurementGraph extends StatelessWidget {
   final double height;
   const MeasurementGraph({super.key, this.height = 290});
 
-  void moveGraphWithStep(int directionalStep, Settings settings) {
-    final oldStart = settings.displayDataStart;
-    final oldEnd = settings.displayDataEnd;
-    switch (settings.graphStepSize) {
-      case TimeStep.day:
-        settings.displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep);
-        settings.displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep);
-        break;
-      case TimeStep.week:
-        settings.displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 7);
-        settings.displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 7);
-        break;
-      case TimeStep.month:
-        settings.displayDataStart = oldStart.copyWith(month: oldStart.month + directionalStep);
-        settings.displayDataEnd = oldEnd.copyWith(month: oldEnd.month + directionalStep);
-        break;
-      case TimeStep.year:
-        settings.displayDataStart = oldStart.copyWith(year: oldStart.year + directionalStep);
-        settings.displayDataEnd = oldEnd.copyWith(year: oldEnd.year + directionalStep);
-        break;
-      case TimeStep.lifetime:
-        settings.displayDataStart = DateTime.fromMillisecondsSinceEpoch(0);
-        settings.displayDataEnd = oldStart;
-        break;
-    }
-  }
-
   @override
   Widget build(BuildContext context) {
     return SizedBox(
@@ -217,85 +191,8 @@ class MeasurementGraph extends StatelessWidget {
                 }
             ),
             const SizedBox(height: 2,),
-            Consumer<Settings>(
-                builder: (context, settings, child) {
-                  return Row(
-                    children: [
-                      Expanded(
-                        flex: 30,
-                        child: MaterialButton(
-                          onPressed: () {
-                            moveGraphWithStep(-1, settings);
-                          },
-                          child: const Icon(
-                            Icons.chevron_left,
-                            size: 48,
-                          ),
-                        ),
-                      ),
-
-                      Expanded(
-                        flex: 40,
-                          child: DropdownButton<int>(
-                            value: settings.graphStepSize,
-                            isExpanded: true,
-                            onChanged: (int? value) {
-                              if (value != null) {
-                                settings.graphStepSize = value;
-                                final now = DateTime.now();
-                                switch (settings.graphStepSize) {
-                                  case TimeStep.day:
-                                    settings.displayDataStart = DateTime(now.year, now.month, now.day);
-                                    settings.displayDataEnd = settings.displayDataStart.copyWith(day: now.day + 1);
-                                    break;
-                                  case TimeStep.week:
-                                    settings.displayDataStart = DateTime(now.year, now.month, now.day - (now.weekday - 1)); // monday
-                                    settings.displayDataEnd = settings.displayDataStart.copyWith(day: settings.displayDataStart.day + DateTime.sunday); // end of sunday
-                                    break;
-                                  case TimeStep.month:
-                                    settings.displayDataStart = DateTime(now.year, now.month);
-                                    settings.displayDataEnd = settings.displayDataStart.copyWith(month: now.month + 1);
-                                    break;
-                                  case TimeStep.year:
-                                    settings.displayDataStart = DateTime(now.year);
-                                    settings.displayDataEnd = settings.displayDataStart.copyWith(year: now.year + 1);
-                                    break;
-                                  case TimeStep.lifetime:
-                                    settings.displayDataStart = DateTime.fromMillisecondsSinceEpoch(0);
-                                    settings.displayDataEnd = now;
-                                    break;
-                                }
-                              }
-                            },
-                            items: TimeStep.options.map<DropdownMenuItem<int>>((v) {
-                              return DropdownMenuItem(
-                                  value: v,
-                                  child: Text(
-                                      TimeStep.getName(v)
-                                  )
-                              );
-                            }).toList(),
-                          ),
-                      ),
-
-
-                      Expanded(
-                        flex: 30,
-                        child: MaterialButton(
-                          onPressed: () {
-                            moveGraphWithStep(1, settings);
-                          },
-                          child: const Icon(
-                            Icons.chevron_right,
-                            size: 48,
-                          ),
-                        ),
-                      ),
-                    ]
-                  );
-                }
-            ),
-                      ],
+            const IntervalPicker()
+          ],
         ),
       ),
     );
lib/model/settings_store.dart
@@ -42,8 +42,71 @@ class Settings extends ChangeNotifier {
     notifyListeners();
   }
 
+  void changeStepSize(int value) {
+    graphStepSize = value;
+    final newInterval = getMostRecentDisplayIntervall();
+    displayDataStart = newInterval[0];
+    displayDataEnd = newInterval[1];
+  }
+
+  void moveDisplayDataByStep(int directionalStep) {
+    final oldStart = displayDataStart;
+    final oldEnd =displayDataEnd;
+    switch (graphStepSize) {
+      case TimeStep.day:
+        displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep);
+        displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep);
+        break;
+      case TimeStep.week:
+        displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 7);
+        displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 7);
+        break;
+      case TimeStep.month:
+        displayDataStart = oldStart.copyWith(month: oldStart.month + directionalStep);
+        displayDataEnd = oldEnd.copyWith(month: oldEnd.month + directionalStep);
+        break;
+      case TimeStep.year:
+        displayDataStart = oldStart.copyWith(year: oldStart.year + directionalStep);
+        displayDataEnd = oldEnd.copyWith(year: oldEnd.year + directionalStep);
+        break;
+      case TimeStep.lifetime:
+        displayDataStart = DateTime.fromMillisecondsSinceEpoch(0);
+        displayDataEnd = oldStart;
+        break;
+    }
+  }
+
+  List<DateTime> getMostRecentDisplayIntervall() {
+    final now = DateTime.now();
+    switch (graphStepSize) {
+      case TimeStep.day:
+        final start = DateTime(now.year, now.month, now.day);
+        return [start, start.copyWith(day: now.day + 1)];
+      case TimeStep.week:
+        final start = DateTime(now.year, now.month, now.day - (now.weekday - 1)); // monday
+        return [start, start.copyWith(day: start.day + DateTime.sunday)]; // end of sunday
+      case TimeStep.month:
+        final start = DateTime(now.year, now.month);
+        return [start,  start.copyWith(month: now.month + 1)];
+      case TimeStep.year:
+        final start = DateTime(now.year);
+        return [start,  start.copyWith(year: now.year + 1)];
+      case TimeStep.lifetime:
+        final start = DateTime.fromMillisecondsSinceEpoch(0);
+        return [start,  now];
+      default:
+        assert(false);
+        final start = DateTime.fromMillisecondsSinceEpoch(0);
+        return [start,  now];
+    }
+  }
+
   DateTime get displayDataStart {
-    return DateTime.fromMillisecondsSinceEpoch(_prefs.getInt('graphStart') ?? -1);
+    var setting = _prefs.getInt('graphStart');
+    if (setting != null) {
+      return DateTime.fromMillisecondsSinceEpoch(setting);
+    }
+    return getMostRecentDisplayIntervall()[0];
   }
   set displayDataStart(DateTime newGraphStart) {
     _prefs.setInt('graphStart', newGraphStart.millisecondsSinceEpoch);
@@ -51,7 +114,11 @@ class Settings extends ChangeNotifier {
   }
 
   DateTime get displayDataEnd {
-    return DateTime.fromMillisecondsSinceEpoch(_prefs.getInt('graphEnd') ?? -1);
+    var setting = _prefs.getInt('graphEnd');
+    if (setting != null) {
+      return DateTime.fromMillisecondsSinceEpoch(setting);
+    }
+    return getMostRecentDisplayIntervall()[1];
   }
   set displayDataEnd(DateTime newGraphEnd) {
     _prefs.setInt('graphEnd', newGraphEnd.millisecondsSinceEpoch);