Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
derdilla committed Jul 18, 2023
1 parent f7cd0a6 commit 583eb2d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 70 deletions.
6 changes: 3 additions & 3 deletions lib/components/display_interval_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ class IntervalPicker extends StatelessWidget {
),
Expanded(
flex: 40,
child: DropdownButton<int>(
child: DropdownButton<TimeStep>(
value: settings.graphStepSize,
isExpanded: true,
onChanged: (int? value) {
onChanged: (TimeStep? value) {
if (value != null) {
settings.changeStepSize(value);
}
},
items: TimeStep.options.map<DropdownMenuItem<int>>((v) {
items: TimeStep.options.map<DropdownMenuItem<TimeStep>>((v) {
return DropdownMenuItem(value: v, child: Text(TimeStep.getName(v, context)));
}).toList(),
),
Expand Down
1 change: 0 additions & 1 deletion lib/model/blood_pressure_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
38 changes: 14 additions & 24 deletions lib/model/ram_only_implementations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -65,9 +65,7 @@ class RamSettings extends ChangeNotifier implements Settings {
List<String> _exportAddableItems = ['isoUTCTime'];
bool _exportCsvHeadline = true;
bool _exportCustomEntries = false;
DateTimeRange _exportDataRange = DateTimeRange(start: DateTime.fromMillisecondsSinceEpoch(0), end: DateTime.fromMillisecondsSinceEpoch(0));
List<String> _exportItems = ['timestampUnixMs', 'systolic', 'diastolic', 'pulse', 'notes'];
bool _exportLimitDataRange = false;
MimeType _exportMimeType = MimeType.csv;
String _defaultExportDir = '';
bool _exportAfterEveryEntry = false;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<String> get exportItems => _exportItems;

Expand All @@ -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;

Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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);
Expand Down
90 changes: 64 additions & 26 deletions lib/model/settings_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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 '-';
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/screens/statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 2 additions & 15 deletions test/model/settings_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
}

0 comments on commit 583eb2d

Please sign in to comment.