From abbbc4a8c49b712a5155455895dd8afaaf4716de Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sat, 25 Feb 2023 18:54:23 +0100 Subject: [PATCH] changed models, added function from db to model --- lib/interface/db_record.dart | 2 +- lib/models/hiit_workout.dart | 13 ++++++------- lib/models/mood.dart | 10 ++++++++-- lib/models/sleep.dart | 24 ++++++++++++++++++------ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/interface/db_record.dart b/lib/interface/db_record.dart index df9c329..ed213ec 100644 --- a/lib/interface/db_record.dart +++ b/lib/interface/db_record.dart @@ -1,5 +1,5 @@ abstract class DatabaseRecord { String toCSV(); Map toMap(); - DatabaseRecord.fromMap(Map map); + DatabaseRecord.fromDatabase(Map map); } diff --git a/lib/models/hiit_workout.dart b/lib/models/hiit_workout.dart index 48e599c..c4ec0da 100644 --- a/lib/models/hiit_workout.dart +++ b/lib/models/hiit_workout.dart @@ -9,13 +9,12 @@ class HIITWorkout implements DatabaseRecord { HIITWorkout(this._workoutDuration, this._commentBefore, this._commentAfter, this._workoutDate); - /* @override - HIITWorkout.fromMap(Map map) { - _workoutDuration = map['_workoutDuration']; - _commentBefore = map['_commentBefore']; - _commentAfter = map['_commentAfter']; - _workoutDate = map['_workoutDate']; - } */ + //TODO Felder anpassen + @override + factory HIITWorkout.fromMap(Map map) { + return HIITWorkout(map['_workoutDuration'], map['_commentBefore'], + map['_commentAfter'], map['_workoutDate']); + } @override String toCSV() => diff --git a/lib/models/mood.dart b/lib/models/mood.dart index 5d71e5b..562b2b4 100644 --- a/lib/models/mood.dart +++ b/lib/models/mood.dart @@ -1,15 +1,21 @@ import 'package:smoke_cess_app/interface/db_record.dart'; class Mood implements DatabaseRecord { - final double _moodValue; + final int _moodValue; final String _comment; final DateTime _date; Mood(this._moodValue, this._comment, this._date); + @override + factory Mood.fromDatabase(Map map) { + DateTime date = DateTime.parse(map['date']); + return Mood(map['value'], map['comment'], date); + } + @override String toCSV() { - return "${_date.toIso8601String()}, ${_moodValue.round().toString()}, $_comment"; + return "${_date.toIso8601String()}, $_moodValue, $_comment"; } @override diff --git a/lib/models/sleep.dart b/lib/models/sleep.dart index 05459fd..d5335a8 100644 --- a/lib/models/sleep.dart +++ b/lib/models/sleep.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/interface/db_record.dart'; class Sleep implements DatabaseRecord { - final double _sleepQualityValue; + final int _sleepQualityValue; final String _comment; final DateTime _date; final TimeOfDay _sleepedAt; @@ -11,19 +11,31 @@ class Sleep implements DatabaseRecord { Sleep(this._sleepQualityValue, this._comment, this._date, this._sleepedAt, this._wokeUpAt); + @override + factory Sleep.fromDatabase(Map map) { + DateTime date = DateTime.parse(map['date']); + TimeOfDay sleepedAt = + TimeOfDay(hour: map['sleepedAtHour'], minute: map['sleepedAtMinute']); + TimeOfDay wokeUpAt = + TimeOfDay(hour: map['wokeUpAtHour'], minute: map['wokeUpAtMinute']); + return Sleep(map['value'], map['comment'], date, sleepedAt, wokeUpAt); + } + @override String toCSV() { - return "${_date.toIso8601String()}, ${_sleepQualityValue.round().toString()}, $_sleepedAt, $_wokeUpAt, $_comment"; + return "${_date.toIso8601String()}, $_sleepQualityValue, ${_sleepedAt.hour}:${_sleepedAt.minute}, ${_wokeUpAt.hour}:${_wokeUpAt.minute}, $_comment"; } @override Map toMap() { return { - 'sleepQualityValue': _sleepQualityValue, + 'value': _sleepQualityValue, 'comment': _comment, - 'date': _date, - 'sleepedAt': _sleepedAt, - 'wokeUpAt': _wokeUpAt, + 'date': _date.toIso8601String(), + 'sleepedAtHour': _sleepedAt.hour, + 'sleepedAtMinute': _sleepedAt.minute, + 'wokeUpAtHour': _wokeUpAt.hour, + 'wokeUpAtMinute': _wokeUpAt.minute, }; } }