diff --git a/lib/models/relapse.dart b/lib/models/relapse.dart new file mode 100644 index 0000000..78eeb12 --- /dev/null +++ b/lib/models/relapse.dart @@ -0,0 +1,27 @@ +import 'package:smoke_cess_app/interface/db_record.dart'; + +class Relapse implements DatabaseRecord { + final String _comment; + final DateTime _date; + + Relapse(this._comment, this._date); + + @override + factory Relapse.fromDatabase(Map map) { + DateTime date = DateTime.parse(map['date']); + return Relapse(map['comment'], date); + } + + @override + String toCSV() { + return "${_date.toIso8601String()}, {_wokeUpAt.minute}, $_comment"; + } + + @override + Map toMap() { + return { + 'comment': _comment, + 'date': _date.toIso8601String(), + }; + } +} diff --git a/lib/services/database_service.dart b/lib/services/database_service.dart index 8dc8788..030a5af 100644 --- a/lib/services/database_service.dart +++ b/lib/services/database_service.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:path/path.dart'; import 'package:smoke_cess_app/models/mood.dart'; +import 'package:smoke_cess_app/models/relapse.dart'; import 'package:sqflite/sqflite.dart'; // ignore: depend_on_referenced_packages import 'package:path_provider/path_provider.dart'; @@ -53,6 +54,15 @@ class DatabaseService { return sleepList; } + Future> getRelapseRecords() async { + Database db = await instance.database; + var relapseRecords = await db.query('relapse'); + List relapseList = relapseRecords.isNotEmpty + ? relapseRecords.map((e) => Relapse.fromDatabase(e)).toList() + : []; + return relapseList; + } + Future addMood(Mood mood) async { Database db = await instance.database; return await db.insert('mood', mood.toMap()); @@ -62,6 +72,11 @@ class DatabaseService { Database db = await instance.database; return await db.insert('sleep', sleep.toMap()); } + + Future addRelapse(Relapse relapse) async { + Database db = await instance.database; + return await db.insert('relapse', relapse.toMap()); + } } String _createMoodTable = '''