From 583eb2d1798628a1d5543df56419148bc80a85c8 Mon Sep 17 00:00:00 2001 From: derdilla Date: Tue, 18 Jul 2023 14:34:15 +0200 Subject: [PATCH] code cleanup --- lib/components/display_interval_picker.dart | 6 +- lib/model/blood_pressure_analyzer.dart | 1 - lib/model/ram_only_implementations.dart | 38 ++++----- lib/model/settings_store.dart | 90 +++++++++++++++------ lib/screens/statistics.dart | 2 +- test/model/settings_test.dart | 17 +--- 6 files changed, 84 insertions(+), 70 deletions(-) diff --git a/lib/components/display_interval_picker.dart b/lib/components/display_interval_picker.dart index c265d925..fc373666 100644 --- a/lib/components/display_interval_picker.dart +++ b/lib/components/display_interval_picker.dart @@ -59,15 +59,15 @@ class IntervalPicker extends StatelessWidget { ), Expanded( flex: 40, - child: DropdownButton( + child: DropdownButton( value: settings.graphStepSize, isExpanded: true, - onChanged: (int? value) { + onChanged: (TimeStep? value) { if (value != null) { settings.changeStepSize(value); } }, - items: TimeStep.options.map>((v) { + items: TimeStep.options.map>((v) { return DropdownMenuItem(value: v, child: Text(TimeStep.getName(v, context))); }).toList(), ), diff --git a/lib/model/blood_pressure_analyzer.dart b/lib/model/blood_pressure_analyzer.dart index cb226fbb..04e05c91 100644 --- a/lib/model/blood_pressure_analyzer.dart +++ b/lib/model/blood_pressure_analyzer.dart @@ -28,7 +28,6 @@ class BloodPressureAnalyser { int get minSys => _safeResult(() => _nonNullSys.reduce(min), (r) => r.systolic); - //TODO make first and last day nullable DateTime? get firstDay { if (_records.isEmpty) return null; _records.sort((a, b) => a.creationTime.compareTo(b.creationTime)); diff --git a/lib/model/ram_only_implementations.dart b/lib/model/ram_only_implementations.dart index 88dbf77d..e39c7cb0 100644 --- a/lib/model/ram_only_implementations.dart +++ b/lib/model/ram_only_implementations.dart @@ -51,7 +51,7 @@ class RamSettings extends ChangeNotifier implements Settings { DateTime? _displayDataStart; bool _followSystemDarkMode = true; double _graphLineThickness = 3; - int _graphStepSize = TimeStep.day; + TimeStep _graphStepSize = TimeStep.day; double _iconSize = 30; MaterialColor _pulColor = Colors.pink; MaterialColor _sysColor = Colors.pink; @@ -65,9 +65,7 @@ class RamSettings extends ChangeNotifier implements Settings { List _exportAddableItems = ['isoUTCTime']; bool _exportCsvHeadline = true; bool _exportCustomEntries = false; - DateTimeRange _exportDataRange = DateTimeRange(start: DateTime.fromMillisecondsSinceEpoch(0), end: DateTime.fromMillisecondsSinceEpoch(0)); List _exportItems = ['timestampUnixMs', 'systolic', 'diastolic', 'pulse', 'notes']; - bool _exportLimitDataRange = false; MimeType _exportMimeType = MimeType.csv; String _defaultExportDir = ''; bool _exportAfterEveryEntry = false; @@ -206,10 +204,10 @@ class RamSettings extends ChangeNotifier implements Settings { } @override - int get graphStepSize => _graphStepSize; + TimeStep get graphStepSize => _graphStepSize; @override - set graphStepSize(int value) { + set graphStepSize(TimeStep value) { _graphStepSize = value; notifyListeners(); } @@ -320,15 +318,6 @@ class RamSettings extends ChangeNotifier implements Settings { notifyListeners(); } - @override - DateTimeRange get exportDataRange => _exportDataRange; - - @override - set exportDataRange(DateTimeRange value) { - _exportDataRange = value; - notifyListeners(); - } - @override List get exportItems => _exportItems; @@ -338,15 +327,6 @@ class RamSettings extends ChangeNotifier implements Settings { notifyListeners(); } - @override - bool get exportLimitDataRange => _exportLimitDataRange; - - @override - set exportLimitDataRange(bool value) { - _exportLimitDataRange = value; - notifyListeners(); - } - @override MimeType get exportMimeType => _exportMimeType; @@ -384,7 +364,7 @@ class RamSettings extends ChangeNotifier implements Settings { } @override - void changeStepSize(int value) { + void changeStepSize(TimeStep value) { graphStepSize = value; final newInterval = getMostRecentDisplayIntervall(); displayDataStart = newInterval[0]; @@ -401,6 +381,7 @@ class RamSettings extends ChangeNotifier implements Settings { displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep); break; case TimeStep.week: + case TimeStep.last7Days: displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 7); displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 7); break; @@ -416,6 +397,9 @@ class RamSettings extends ChangeNotifier implements Settings { displayDataStart = DateTime.fromMillisecondsSinceEpoch(0); displayDataEnd = DateTime.now(); break; + case TimeStep.last30Days: + displayDataStart = oldStart.copyWith(day: oldStart.day + directionalStep * 30); + displayDataEnd = oldEnd.copyWith(day: oldEnd.day + directionalStep * 30); } } @@ -438,6 +422,12 @@ class RamSettings extends ChangeNotifier implements Settings { case TimeStep.lifetime: final start = DateTime.fromMillisecondsSinceEpoch(0); return [start, now]; + case TimeStep.last7Days: + final start = now.copyWith(day: now.day-7); + return [start, now]; + case TimeStep.last30Days: + final start = now.copyWith(day: now.day-30); + return [start, now]; default: assert(false); final start = DateTime.fromMillisecondsSinceEpoch(0); diff --git a/lib/model/settings_store.dart b/lib/model/settings_store.dart index 16f9892d..06f6f158 100644 --- a/lib/model/settings_store.dart +++ b/lib/model/settings_store.dart @@ -49,16 +49,58 @@ class Settings extends ChangeNotifier { return; } - int get graphStepSize { - return _prefs.getInt('graphStepSize') ?? TimeStep.day; + TimeStep get graphStepSize { + int stepInt = _prefs.getInt('graphStepSize') ?? 0; + switch (stepInt) { + case 0: + return TimeStep.day; + case 1: + return TimeStep.month; + case 2: + return TimeStep.year; + case 3: + return TimeStep.lifetime; + case 4: + return TimeStep.week; + case 5: + return TimeStep.last7Days; + case 6: + return TimeStep.last30Days; + } + assert(false); + return TimeStep.day; } - set graphStepSize(int newStepSize) { - _prefs.setInt('graphStepSize', newStepSize); + 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); + } notifyListeners(); } - void changeStepSize(int value) { + void changeStepSize(TimeStep value) { graphStepSize = value; final newInterval = getMostRecentDisplayIntervall(); displayDataStart = newInterval[0]; @@ -434,38 +476,34 @@ class Settings extends ChangeNotifier { } } -class TimeStep { // TODO: replace with enum - static const options = [0, 4, 1, 2, 3, 5, 6]; - - static const day = 0; - static const month = 1; - static const year = 2; - static const lifetime = 3; - static const week = 4; - static const last7Days = 5; - static const last30Days = 6; +enum TimeStep { + day, + month, + year, + lifetime, + week, + last7Days, + last30Days; - TimeStep._create(); + static const options = [TimeStep.day, TimeStep.week, TimeStep.month, TimeStep.year, TimeStep.lifetime, TimeStep.last7Days, TimeStep.last30Days]; - static String getName(int opt, BuildContext context) { + static String getName(TimeStep opt, BuildContext context) { switch (opt) { - case day: + case TimeStep.day: return AppLocalizations.of(context)!.day; - case month: + case TimeStep.month: return AppLocalizations.of(context)!.month; - case year: + case TimeStep.year: return AppLocalizations.of(context)!.year; - case lifetime: + case TimeStep.lifetime: return AppLocalizations.of(context)!.lifetime; - case week: + case TimeStep.week: return AppLocalizations.of(context)!.week; - case last7Days: + case TimeStep.last7Days: return AppLocalizations.of(context)!.last7Days; - case last30Days: + case TimeStep.last30Days: return AppLocalizations.of(context)!.last30Days; } - assert(false); - return '-'; } } diff --git a/lib/screens/statistics.dart b/lib/screens/statistics.dart index 10331f00..b7d2c03d 100644 --- a/lib/screens/statistics.dart +++ b/lib/screens/statistics.dart @@ -170,8 +170,8 @@ class Statistic extends StatelessWidget { @override Widget build(BuildContext context) { + const double top = 20; double sides = 20; - double top = 20; double padding = 20; if (smallEdges) { sides = 0; diff --git a/test/model/settings_test.dart b/test/model/settings_test.dart index 0948ab74..f6da8457 100644 --- a/test/model/settings_test.dart +++ b/test/model/settings_test.dart @@ -1,7 +1,6 @@ import 'package:blood_pressure_app/model/ram_only_implementations.dart'; import 'package:blood_pressure_app/model/settings_store.dart'; import 'package:file_saver/file_saver.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; @@ -44,8 +43,6 @@ void main() { expect(s.exportItems, ['timestampUnixMs', 'systolic', 'diastolic', 'pulse', 'notes']); expect(s.exportAddableItems, ['isoUTCTime']); expect(s.exportCsvHeadline, true); - expect(s.exportDataRange.start.millisecondsSinceEpoch, 0); - expect(s.exportLimitDataRange, false); expect(s.exportMimeType, MimeType.csv); expect(s.defaultExportDir.isEmpty, true); expect(s.exportAfterEveryEntry, false); @@ -90,7 +87,6 @@ void main() { s.exportAddableItems = ['timestampUnixMs']; s.exportItems = ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']; s.exportCsvHeadline = false; - s.exportLimitDataRange = true; s.exportMimeType = MimeType.pdf; s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; @@ -118,7 +114,6 @@ void main() { expect(s.exportItems, ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']); expect(s.exportAddableItems, ['timestampUnixMs']); expect(s.exportCsvHeadline, false); - expect(s.exportLimitDataRange, true); expect(s.exportMimeType, MimeType.pdf); expect(s.defaultExportDir, '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'); expect(s.exportAfterEveryEntry, true); @@ -157,14 +152,12 @@ void main() { s.exportAddableItems = ['timestampUnixMs']; s.exportItems = ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']; s.exportCsvHeadline = false; - s.exportDataRange = DateTimeRange(start: DateTime.fromMillisecondsSinceEpoch(20), end: DateTime.now()); - s.exportLimitDataRange = true; s.exportMimeType = MimeType.pdf; s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; - expect(i, 30); + expect(i, 28); }); }); @@ -202,8 +195,6 @@ void main() { expect(s.exportItems, ['timestampUnixMs', 'systolic', 'diastolic', 'pulse', 'notes']); expect(s.exportAddableItems, ['isoUTCTime']); expect(s.exportCsvHeadline, true); - expect(s.exportDataRange.start.millisecondsSinceEpoch, 0); - expect(s.exportLimitDataRange, false); expect(s.exportMimeType, MimeType.csv); expect(s.defaultExportDir.isEmpty, true); expect(s.exportAfterEveryEntry, false); @@ -248,7 +239,6 @@ void main() { s.exportAddableItems = ['timestampUnixMs']; s.exportItems = ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']; s.exportCsvHeadline = false; - s.exportLimitDataRange = true; s.exportMimeType = MimeType.pdf; s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; @@ -276,7 +266,6 @@ void main() { expect(s.exportItems, ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']); expect(s.exportAddableItems, ['timestampUnixMs']); expect(s.exportCsvHeadline, false); - expect(s.exportLimitDataRange, true); expect(s.exportMimeType, MimeType.pdf); expect(s.defaultExportDir, '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'); expect(s.exportAfterEveryEntry, true); @@ -316,14 +305,12 @@ void main() { s.exportAddableItems = ['timestampUnixMs']; s.exportItems = ['systolic', 'diastolic', 'pulse', 'notes', 'isoUTCTime']; s.exportCsvHeadline = false; - s.exportDataRange = DateTimeRange(start: DateTime.fromMillisecondsSinceEpoch(20), end: DateTime.now()); - s.exportLimitDataRange = true; s.exportMimeType = MimeType.pdf; s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; - expect(i, 30); + expect(i, 28); }); }); }