Commit 73ecc41

derdilla <derdilla06@gmail.com>
2023-07-20 15:42:21
add "Custom" display intervall
1 parent dd91390
lib/components/display_interval_picker.dart
@@ -31,12 +31,11 @@ class IntervalPicker extends StatelessWidget {
           break;
         case TimeStep.last7Days:
         case TimeStep.last30Days:
-          final f = DateFormat.MMMd(AppLocalizations.of(context)!.localeName);
+        case TimeStep.custom:
+          final f = DateFormat.yMMMd(AppLocalizations.of(context)!.localeName);
           intervallDisplay = Text('${f.format(settings.displayDataStart)} - ${f.format(settings.displayDataEnd)}');
           break;
-        default:
-          assert(false);
-          intervallDisplay =  const Text('-');
+
       }
       return Column(
         children: [
@@ -46,7 +45,7 @@ class IntervalPicker extends StatelessWidget {
           ),
           Row(children: [
             Expanded(
-              flex: 30,
+              flex: 3,
               child: MaterialButton(
                 onPressed: () {
                   settings.moveDisplayDataByStep(-1);
@@ -58,13 +57,23 @@ class IntervalPicker extends StatelessWidget {
               ),
             ),
             Expanded(
-              flex: 40,
+              flex: 4,
               child: DropdownButton<TimeStep>(
                 value: settings.graphStepSize,
                 isExpanded: true,
-                onChanged: (TimeStep? value) {
+                onChanged: (TimeStep? value) async {
                   if (value != null) {
-                    settings.changeStepSize(value);
+                    if (value == TimeStep.custom) {
+                      settings.graphStepSize = value;
+                      final res = await showDateRangePicker(
+                          context: context,
+                          firstDate: DateTime.fromMillisecondsSinceEpoch(0),
+                          lastDate: DateTime.now());
+                      settings.displayDataStart = res?.start ?? DateTime.fromMillisecondsSinceEpoch(-1);
+                      settings.displayDataEnd = res?.end ?? DateTime.fromMillisecondsSinceEpoch(-1);
+                    } else {
+                      settings.changeStepSize(value);
+                    }
                   }
                 },
                 items: TimeStep.options.map<DropdownMenuItem<TimeStep>>((v) {
@@ -73,7 +82,7 @@ class IntervalPicker extends StatelessWidget {
               ),
             ),
             Expanded(
-              flex: 30,
+              flex: 3,
               child: MaterialButton(
                 onPressed: () {
                   settings.moveDisplayDataByStep(1);
lib/components/measurement_graph.dart
@@ -119,6 +119,7 @@ class _LineChartState extends State<_LineChart> {
                                         formatter = DateFormat('yyyy');
                                         break;
                                       case TimeStep.last30Days:
+                                      case TimeStep.custom:
                                         formatter = DateFormat.MMMd();
                                     }
                                     return Text(formatter
lib/l10n/app_en.arb
@@ -338,5 +338,6 @@
   "allowMissingValues": "Allow missing values",
   "@allowMissingValues": {},
   "errTimeAfterNow": "The selected time of day is after this moment. We have automatically reset it to the current time. You can disable this validation in the settings!",
-  "language": "Language"
+  "language": "Language",
+  "custom": "Custom"
 }
lib/model/ram_only_implementations.dart
@@ -92,7 +92,11 @@ class RamSettings extends ChangeNotifier implements Settings {
 
   @override
   DateTime get displayDataStart {
-    return _displayDataStart ?? getMostRecentDisplayIntervall()[0];
+    final s = _displayDataStart ?? getMostRecentDisplayIntervall()[0];
+    if(s.millisecondsSinceEpoch < 0) {
+      changeStepSize(TimeStep.last7Days);
+    }
+    return s;
   }
 
   @override
@@ -103,7 +107,11 @@ class RamSettings extends ChangeNotifier implements Settings {
 
   @override
   DateTime get displayDataEnd {
-    return _displayDataEnd ?? getMostRecentDisplayIntervall()[1];
+    final s = _displayDataEnd ?? getMostRecentDisplayIntervall()[1];
+    if(s.millisecondsSinceEpoch < 0) {
+      changeStepSize(TimeStep.last7Days);
+    }
+    return s;
   }
 
   @override
@@ -381,6 +389,7 @@ class RamSettings extends ChangeNotifier implements Settings {
     displayDataEnd = newInterval[1];
   }
 
+  // directional step either 1 or -1
   @override
   void moveDisplayDataByStep(int directionalStep) {
     final oldStart = displayDataStart;
@@ -410,6 +419,12 @@ class RamSettings extends ChangeNotifier implements Settings {
       case TimeStep.last30Days:
         displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 30);
         displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 30);
+        break;
+      case TimeStep.custom:
+        final step = oldStart.difference(oldEnd) * directionalStep;
+        displayDataStart = oldStart.add(step);
+        displayDataEnd = oldEnd.add(step);
+        break;
     }
   }
 
@@ -438,10 +453,8 @@ class RamSettings extends ChangeNotifier implements Settings {
       case TimeStep.last30Days:
         final start = now.copyWith(day: now.day-30);
         return [start, now];
-      default:
-        assert(false);
-        final start = DateTime.fromMillisecondsSinceEpoch(0);
-        return [start, now];
+      case TimeStep.custom:
+        return [DateTime.fromMillisecondsSinceEpoch(-1), DateTime.fromMillisecondsSinceEpoch(-1)]; // TODO
     }
   }
 }
lib/model/settings_store.dart
@@ -66,37 +66,36 @@ class Settings extends ChangeNotifier {
         return TimeStep.last7Days;
       case 6:
         return TimeStep.last30Days;
+      case 7:
+        return TimeStep.custom;
     }
     assert(false);
     return TimeStep.day;
   }
 
   set graphStepSize(TimeStep newStepSize) {
-    switch (newStepSize) {
-      case TimeStep.day:
-        _prefs.setInt('graphStepSize', 0);
-        break;
-      case TimeStep.month:
-        _prefs.setInt('graphStepSize', 1);
-        break;
-      case TimeStep.year:
-        _prefs.setInt('graphStepSize', 2);
-        break;
-      case TimeStep.lifetime:
-        _prefs.setInt('graphStepSize', 3);
-        break;
-      case TimeStep.week:
-        _prefs.setInt('graphStepSize', 4);
-        break;
-      case TimeStep.last7Days:
-        _prefs.setInt('graphStepSize', 5);
-        break;
-      case TimeStep.last30Days:
-        _prefs.setInt('graphStepSize', 6);
-        break;
-      default:
-        assert(false);
-    }
+    _prefs.setInt('graphStepSize', ((){
+      switch (newStepSize) {
+        case TimeStep.day:
+          return 0;
+        case TimeStep.month:
+          return 1;
+        case TimeStep.year:
+          return 2;
+        case TimeStep.lifetime:
+          return 3;
+        case TimeStep.week:
+          return 4;
+        case TimeStep.last7Days:
+          return 5;
+        case TimeStep.last30Days:
+          return 6;
+        case TimeStep.custom:
+          return 7;
+      }
+    })());
+
+
     notifyListeners();
   }
 
@@ -107,6 +106,7 @@ class Settings extends ChangeNotifier {
     displayDataEnd = newInterval[1];
   }
 
+  // directional step either 1 or -1
   void moveDisplayDataByStep(int directionalStep) {
     final oldStart = displayDataStart;
     final oldEnd = displayDataEnd;
@@ -135,6 +135,12 @@ class Settings extends ChangeNotifier {
       case TimeStep.last30Days:
         displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 30);
         displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 30);
+        break;
+      case TimeStep.custom:
+        final step = oldStart.difference(oldEnd) * directionalStep;
+        displayDataStart = oldStart.add(step);
+        displayDataEnd = oldEnd.add(step);
+        break;
     }
   }
 
@@ -162,15 +168,17 @@ class Settings extends ChangeNotifier {
       case TimeStep.last30Days:
         final start = now.copyWith(day: now.day-30);
         return [start, now];
-      default:
-        assert(false);
-        final start = DateTime.fromMillisecondsSinceEpoch(0);
-        return [start, now];
+      case TimeStep.custom:
+        return [DateTime.fromMillisecondsSinceEpoch(-1), DateTime.fromMillisecondsSinceEpoch(-1)]; // TODO
     }
   }
 
   DateTime get displayDataStart {
-    return _displayDataStart ?? getMostRecentDisplayIntervall()[0];
+    final s = _displayDataStart ?? getMostRecentDisplayIntervall()[0];
+    if(s.millisecondsSinceEpoch < 0) {
+      changeStepSize(TimeStep.last7Days);
+    }
+    return s;
   }
 
   set displayDataStart(DateTime newGraphStart) {
@@ -179,7 +187,11 @@ class Settings extends ChangeNotifier {
   }
 
   DateTime get displayDataEnd {
-    return _displayDataEnd ?? getMostRecentDisplayIntervall()[1];
+    final s = _displayDataEnd ?? getMostRecentDisplayIntervall()[1];
+    if(s.millisecondsSinceEpoch < 0) {
+      changeStepSize(TimeStep.last7Days);
+    }
+    return s;
   }
 
   set displayDataEnd(DateTime newGraphEnd) {
@@ -494,9 +506,10 @@ enum TimeStep {
   lifetime,
   week,
   last7Days,
-  last30Days;
+  last30Days,
+  custom;
 
-  static const options = [TimeStep.day, TimeStep.week, TimeStep.month, TimeStep.year, TimeStep.lifetime, TimeStep.last7Days, TimeStep.last30Days];
+  static const options = [TimeStep.day, TimeStep.week, TimeStep.month, TimeStep.year, TimeStep.lifetime, TimeStep.last7Days, TimeStep.last30Days, TimeStep.custom];
 
   static String getName(TimeStep opt, BuildContext context) {
     switch (opt) {
@@ -514,6 +527,8 @@ enum TimeStep {
         return AppLocalizations.of(context)!.last7Days;
       case TimeStep.last30Days:
         return AppLocalizations.of(context)!.last30Days;
+      case TimeStep.custom:
+        return AppLocalizations.of(context)!.custom;
     }
   }
 }