Commit ad86e02

derdilla <derdilla06@gmail.com>
2023-06-05 16:28:55
make label count adjustable
1 parent 14e8237
lib/components/measurement_graph.dart
@@ -88,19 +88,22 @@ class _LineChartState extends State<_LineChart> {
                                                   // calculate new intervall
                                                   double graphWidth = meta.max - meta.min;
                                                   assert(graphWidth > 0);
-
-                                                  if ((graphWidth - 2) / 5 != _lineChartTitleIntervall) {
+                                                  if (((graphWidth - 2) / settings.graphTitlesCount) !=
+                                                      _lineChartTitleIntervall) {
+                                                    // simple hack needed to change the state during build
+                                                    // https://stackoverflow.com/a/63607696/21489239
                                                     Future.delayed(Duration.zero, () async {
                                                       setState(() {
-                                                        _lineChartTitleIntervall = (graphWidth - 2) / 5;
+                                                        _lineChartTitleIntervall =
+                                                            (graphWidth - 2) / settings.graphTitlesCount;
                                                       });
                                                     });
                                                   }
 
+                                                  // don't show fixed titles, as they are replaced by long dates below
                                                   if (meta.axisPosition <= 1 || pos >= meta.max) {
                                                     return const SizedBox.shrink();
                                                   }
-                                                  print(_lineChartTitleIntervall);
 
                                                   late final DateFormat formatter;
                                                   switch (settings.graphStepSize) {
lib/model/ram_only_implementations.dart
@@ -112,6 +112,7 @@ class RamSettings extends ChangeNotifier implements Settings {
   double _sysWarn = 120;
   bool _useExportCompatability = false;
   bool _validateInputs = true;
+  int _graphTitlesCount = 5;
 
   RamSettings() {
     _accentColor = createMaterialColor(0xFF009688);
@@ -319,6 +320,15 @@ class RamSettings extends ChangeNotifier implements Settings {
     notifyListeners();
   }
 
+  @override
+  int get graphTitlesCount => _graphTitlesCount;
+
+  @override
+  set graphTitlesCount(int value) {
+    _graphTitlesCount = value;
+    notifyListeners();
+  }
+
   @override
   void changeStepSize(int value) {
     graphStepSize = value;
lib/model/settings_store.dart
@@ -234,10 +234,20 @@ class Settings extends ChangeNotifier {
   bool get confirmDeletion {
     return _prefs.getBool('confirmDeletion') ?? true;
   }
+
   set confirmDeletion(bool confirmDeletion) {
     _prefs.setBool('confirmDeletion', confirmDeletion);
     notifyListeners();
   }
+
+  int get graphTitlesCount {
+    return _prefs.getInt('titlesCount') ?? 5;
+  }
+
+  set graphTitlesCount(int newCount) {
+    _prefs.setInt('titlesCount', newCount);
+    notifyListeners();
+  }
 }
 
 class TimeStep {
lib/screens/settings.dart
@@ -97,27 +97,39 @@ class SettingsPage extends StatelessWidget {
                   ),
                   SliderSettingsTile(
                     key: const Key('animationSpeed'),
-                    title: const Text('animation duration'),
-                    leading: const Icon(Icons.speed),
-                    onChanged: (double value) {
-                      settings.animationSpeed = value.toInt();
-                    },
-                    initialValue: settings.animationSpeed.toDouble(),
-                    start: 0,
-                    end: 1000,
-                    stepSize: 50,
-                  ),
-                  ColorSelectionSettingsTile(
-                      key: const Key('accentColor'),
-                      onMainColorChanged: (color) => settings.accentColor = createMaterialColor((color ?? Colors.teal).value),
-                      initialColor: settings.accentColor,
-                      title: const Text('theme color')
-                  ),
-                  ColorSelectionSettingsTile(
-                      key: const Key('sysColor'),
-                      onMainColorChanged: (color) => settings.sysColor = createMaterialColor((color ?? Colors.green).value),
-                      initialColor: settings.sysColor,
-                      title: const Text('systolic color')
+                title: const Text('animation duration'),
+                leading: const Icon(Icons.speed),
+                onChanged: (double value) {
+                  settings.animationSpeed = value.toInt();
+                },
+                initialValue: settings.animationSpeed.toDouble(),
+                start: 0,
+                end: 1000,
+                stepSize: 50,
+              ),
+              SliderSettingsTile(
+                key: const Key('graphTitlesCount'),
+                title: const Text('graph label count'),
+                leading: const Icon(Icons.functions),
+                onChanged: (double value) {
+                  settings.graphTitlesCount = value.toInt();
+                },
+                initialValue: settings.graphTitlesCount.toDouble(),
+                start: 2,
+                end: 10,
+                stepSize: 1,
+              ),
+              ColorSelectionSettingsTile(
+                  key: const Key('accentColor'),
+                  onMainColorChanged: (color) =>
+                      settings.accentColor = createMaterialColor((color ?? Colors.teal).value),
+                  initialColor: settings.accentColor,
+                  title: const Text('theme color')),
+              ColorSelectionSettingsTile(
+                  key: const Key('sysColor'),
+                  onMainColorChanged: (color) => settings.sysColor = createMaterialColor((color ?? Colors.green).value),
+                  initialColor: settings.sysColor,
+                  title: const Text('systolic color')
                   ),
                   ColorSelectionSettingsTile(
                       key: const Key('diaColor'),
test/model/settings_test.dart
@@ -1,10 +1,9 @@
+import 'package:blood_pressure_app/model/ram_only_implementations.dart';
 import 'package:blood_pressure_app/model/settings_store.dart';
 import 'package:flutter_test/flutter_test.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
 
-import 'package:blood_pressure_app/model/ram_only_implementations.dart';
-
 void main() {
   TestWidgetsFlutterBinding.ensureInitialized();
   SharedPreferences.setMockInitialValues({});
@@ -48,6 +47,7 @@ void main() {
       expect(s.graphLineThickness, 3);
       expect(s.animationSpeed, 150);
       expect(s.confirmDeletion, true);
+      expect(s.graphTitlesCount, 5);
 
       s.overrideWarnValues = true;
       expect(s.sysWarn, 120);
@@ -88,6 +88,7 @@ void main() {
       s.graphLineThickness = 5;
       s.animationSpeed = 100;
       s.confirmDeletion = false;
+      s.graphTitlesCount = 7;
 
       expect(s.displayDataStart, DateTime.fromMillisecondsSinceEpoch(10000));
       expect(s.displayDataEnd, DateTime.fromMillisecondsSinceEpoch(200000));
@@ -108,6 +109,7 @@ void main() {
       expect(s.graphLineThickness, 5);
       expect(s.animationSpeed, 100);
       expect(s.confirmDeletion, false);
+      expect(s.graphTitlesCount, 7);
     });
 
     test('setting fields should notify listeners and change values', () async {
@@ -139,8 +141,9 @@ void main() {
       s.graphLineThickness = 5;
       s.animationSpeed = 100;
       s.confirmDeletion = true;
-      
-      expect(i, 21);
+      s.graphTitlesCount = 2;
+
+      expect(i, 22);
     });
   });
 
@@ -173,6 +176,7 @@ void main() {
       expect(s.graphLineThickness, 3);
       expect(s.animationSpeed, 150);
       expect(s.confirmDeletion, true);
+      expect(s.graphTitlesCount, 5);
 
       s.overrideWarnValues = true;
       expect(s.sysWarn, 120);
@@ -213,6 +217,7 @@ void main() {
       s.graphLineThickness = 5;
       s.animationSpeed = 100;
       s.confirmDeletion = false;
+      s.graphTitlesCount = 7;
 
       expect(s.displayDataStart, DateTime.fromMillisecondsSinceEpoch(10000));
       expect(s.displayDataEnd, DateTime.fromMillisecondsSinceEpoch(200000));
@@ -233,6 +238,7 @@ void main() {
       expect(s.graphLineThickness, 5);
       expect(s.animationSpeed, 100);
       expect(s.confirmDeletion, false);
+      expect(s.graphTitlesCount, 7);
     });
 
     test('setting fields should notify listeners and change values', () async {
@@ -264,8 +270,9 @@ void main() {
       s.graphLineThickness = 5;
       s.animationSpeed = 100;
       s.confirmDeletion = true;
+      s.graphTitlesCount = 2;
 
-      expect(i, 21);
+      expect(i, 22);
     });
   });
 }
\ No newline at end of file