From 9144664c98824924e3d7668557a4455693bcf7f6 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Tue, 16 Aug 2022 15:14:43 +0200 Subject: saving and reading --- lib/shift.dart | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'lib/shift.dart') diff --git a/lib/shift.dart b/lib/shift.dart index 382b39f..143fe58 100644 --- a/lib/shift.dart +++ b/lib/shift.dart @@ -16,10 +16,23 @@ class Shift { Shift({this.end = null, required this.start, required this.type}); + Shift.fromJson(Map json) + : start = DateTime.parse(json['start']), + end = json['end'] == null ? null : DateTime.tryParse(json['end']), + type = ShiftType.values.firstWhere((e) => e.toString() == json['type']), + isActive = json['isActive'] == 'true'; + + Map toJson() { + return { + 'start': start.toIso8601String(), + 'end': end?.toIso8601String(), + 'type': type.toString(), + 'isActive': isActive.toString(), + }; + } + Widget getStatusIcon() { - if (end == null && - start.isBefore(DateTime.now()) && - !DateUtilities.DateUtils.isSameDay(start, DateTime.now())) { + if (shiftIsOpenButBeforeToday()) { return Icon(Icons.pending); } if (getIsActive() && end == null) { @@ -88,6 +101,12 @@ class Shift { return 0.22916666; } + bool shiftIsOpenButBeforeToday() { + return end == null && + start.isBefore(DateTime.now()) && + !DateUtilities.DateUtils.isSameDay(start, DateTime.now()); + } + double getMoneyForActiveSession() { if (getIsActive()) { Duration elapsed = DateTime.now().difference(start); @@ -120,9 +139,7 @@ class Shift { } Color getStatusColor() { - if (end == null && - start.isBefore(DateTime.now()) && - !DateUtilities.DateUtils.isSameDay(start, DateTime.now())) { + if (shiftIsOpenButBeforeToday()) { return Colors.orange; } if (getIsActive() && end == null) { -- cgit v1.2.3-70-g09d2