diff --git a/lib/models/sleep.dart b/lib/models/sleep.dart index d5335a8..3474b03 100644 --- a/lib/models/sleep.dart +++ b/lib/models/sleep.dart @@ -5,25 +5,25 @@ class Sleep implements DatabaseRecord { final int _sleepQualityValue; final String _comment; final DateTime _date; - final TimeOfDay _sleepedAt; + final TimeOfDay _sleptAt; final TimeOfDay _wokeUpAt; - Sleep(this._sleepQualityValue, this._comment, this._date, this._sleepedAt, + Sleep(this._sleepQualityValue, this._comment, this._date, this._sleptAt, this._wokeUpAt); @override factory Sleep.fromDatabase(Map map) { DateTime date = DateTime.parse(map['date']); - TimeOfDay sleepedAt = - TimeOfDay(hour: map['sleepedAtHour'], minute: map['sleepedAtMinute']); + TimeOfDay sleptAt = + TimeOfDay(hour: map['sleptAtHour'], minute: map['sleptAtMinute']); TimeOfDay wokeUpAt = TimeOfDay(hour: map['wokeUpAtHour'], minute: map['wokeUpAtMinute']); - return Sleep(map['value'], map['comment'], date, sleepedAt, wokeUpAt); + return Sleep(map['value'], map['comment'], date, sleptAt, wokeUpAt); } @override String toCSV() { - return "${_date.toIso8601String()}, $_sleepQualityValue, ${_sleepedAt.hour}:${_sleepedAt.minute}, ${_wokeUpAt.hour}:${_wokeUpAt.minute}, $_comment"; + return "${_date.toIso8601String()}, $_sleepQualityValue, ${_sleptAt.hour}:${_sleptAt.minute}, ${_wokeUpAt.hour}:${_wokeUpAt.minute}, $_comment"; } @override @@ -32,8 +32,8 @@ class Sleep implements DatabaseRecord { 'value': _sleepQualityValue, 'comment': _comment, 'date': _date.toIso8601String(), - 'sleepedAtHour': _sleepedAt.hour, - 'sleepedAtMinute': _sleepedAt.minute, + 'sleptAtHour': _sleptAt.hour, + 'sleptAtMinute': _sleptAt.minute, 'wokeUpAtHour': _wokeUpAt.hour, 'wokeUpAtMinute': _wokeUpAt.minute, }; diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 0daf3a6..2af4f13 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -15,7 +15,7 @@ class MyHomePage extends StatefulWidget { } class MyHomePageState extends State { - int _selectedIndex = 0; + int _selectedIndex = 4; int? _gruppe; final List _titles = [ diff --git a/lib/providers/input_provider.dart b/lib/providers/input_provider.dart index c0a7601..881ce2c 100644 --- a/lib/providers/input_provider.dart +++ b/lib/providers/input_provider.dart @@ -1,12 +1,15 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/models/mood.dart'; +import 'package:smoke_cess_app/models/sleep.dart'; import '../globals.dart' as globals; class InputProvider extends ChangeNotifier { double _sliderValue = 50; final TextEditingController _textController = TextEditingController(text: ''); - final Map _times = {}; + final Map _times = { + 'wokeUpAt': const TimeOfDay(hour: 8, minute: 0), + 'sleptAt': const TimeOfDay(hour: 22, minute: 0), + }; double get sliderValue => _sliderValue; TextEditingController get textController => _textController; @@ -16,17 +19,20 @@ class InputProvider extends ChangeNotifier { notifyListeners(); } - TimeOfDay? getTimeEntry(String key) { - return _times[key]; + TimeOfDay getTimeEntry(String key) { + return _times[key] ?? const TimeOfDay(hour: 12, minute: 0); } void setTime(String key, TimeOfDay time) { _times[key] = time; + notifyListeners(); } void _resetFields() { _sliderValue = 50; _textController.text = ''; + setTime('wokeUpAt', const TimeOfDay(hour: 8, minute: 0)); + setTime('sleptAt', const TimeOfDay(hour: 22, minute: 0)); notifyListeners(); } @@ -36,4 +42,11 @@ class InputProvider extends ChangeNotifier { globals.databaseService.addMood(mood); _resetFields(); } + + void saveSleep(String wokeUpKey, String sleptKey) { + Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text, + DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey)); + globals.databaseService.addSleep(sleep); + _resetFields(); + } } diff --git a/lib/services/database_service.dart b/lib/services/database_service.dart index 26973f4..8dc8788 100644 --- a/lib/services/database_service.dart +++ b/lib/services/database_service.dart @@ -79,8 +79,8 @@ String _createSleepTable = ''' value INTEGER, date TEXT, comment TEXT, - sleepedAtHour INTEGER, - sleepedAtMinute INTEGER, + sleptAtHour INTEGER, + sleptAtMinute INTEGER, wokeUpAtHour INTEGER, wokeUpAtMinute INTEGER ) diff --git a/lib/services/query_service.dart b/lib/services/query_service.dart deleted file mode 100644 index fc4f5a3..0000000 --- a/lib/services/query_service.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:smoke_cess_app/interface/db_record.dart'; -import 'package:smoke_cess_app/mock/db_mock.dart'; - -class QueryService { - final DatabaseMock _database; - - QueryService(this._database); - - void addRecord(DatabaseRecord record) => _database.saveRecord(record); - List getWorkoutRecords() => _database.getWorkoutRecords(); - List getMoodRecords() => _database.getMoodRecords(); - List getSleepRecords() => _database.getSleepRecords(); - - String recordsToCSV(List records) { - String csv = ""; - for (DatabaseRecord record in records) { - csv += '${record.toCSV()}\n'; - } - return csv; - } -} diff --git a/lib/widgets/sleep_form.dart b/lib/widgets/sleep_form.dart index 7929367..98d35db 100644 --- a/lib/widgets/sleep_form.dart +++ b/lib/widgets/sleep_form.dart @@ -1,97 +1,51 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:smoke_cess_app/models/sleep.dart'; -import 'package:smoke_cess_app/services/database_service.dart'; import 'package:smoke_cess_app/widgets/elevated_card.dart'; import 'package:smoke_cess_app/widgets/slider.dart'; -import 'package:smoke_cess_app/widgets/submit_form_button.dart'; import 'package:smoke_cess_app/widgets/text_formfield.dart'; import 'package:smoke_cess_app/widgets/timepicker.dart'; import '../providers/input_provider.dart'; -class SleepForm extends StatefulWidget { +class SleepForm extends StatelessWidget { const SleepForm({Key? key}) : super(key: key); - @override - State createState() => _SleepFormState(); -} - -class _SleepFormState extends State { - final GlobalKey _sleepFormKey = GlobalKey(); - MySlider slider = MySlider(); - String _textInput = ""; - TimePicker sleepTimePicker = TimePicker( - const TimeOfDay(hour: 22, minute: 00), - ); - TimePicker wakeUpTimePicker = TimePicker( - const TimeOfDay(hour: 8, minute: 00), - ); - - void submitForm(BuildContext context) { - var inputModel = context.watch(); - if (_sleepFormKey.currentState!.validate()) { - _sleepFormKey.currentState?.save(); //call every onSave Method - Sleep sleep = Sleep( - inputModel.sliderValue.toInt(), - _textInput, - DateTime.now(), - sleepTimePicker.getCurrentTime, - wakeUpTimePicker.getCurrentTime); - DatabaseService.instance.addSleep(sleep); - _sleepFormKey.currentState?.reset(); - } - } - - void onFormFieldSave(String? newValue) => _textInput = newValue!; - @override Widget build(BuildContext context) { - return Form( - key: _sleepFormKey, - child: Stack( - children: [ - SingleChildScrollView( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - ElevatedCard( - title: 'Einschlafzeit', - child: sleepTimePicker, - ), - const SizedBox(height: 16), - ElevatedCard( - title: 'Aufwachzeit', - child: wakeUpTimePicker, - ), - const SizedBox(height: 16), - ElevatedCard( - title: 'Schlafbewertung', - child: slider, - ), - const SizedBox(height: 16), - const ElevatedCard( - title: 'Schlafbeschreibung', - child: MyTextFormField('Beschreibe deinen Schlaf'), - ), - const SizedBox( - height: 80, - ), - ], - ), - ), - Positioned( - bottom: 0, - right: 0, - child: SizedBox( - width: 140, - height: 80, - child: SubmitFormButton(() => submitForm(context)), - ), - ), - ], - ), + InputProvider inputModel = context.watch(); + String wokeUpKey = 'wokeUpAt'; + String sleptKey = 'sleptAt'; + + return Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + ElevatedCard( + title: 'Einschlafzeit', + child: TimePicker(sleptKey), + ), + const SizedBox(height: 16), + ElevatedCard( + title: 'Aufwachzeit', + child: TimePicker(wokeUpKey), + ), + const SizedBox(height: 16), + const ElevatedCard( + title: 'Schlafbewertung', + child: MySlider(), + ), + const SizedBox(height: 16), + const ElevatedCard( + title: 'Schlafbeschreibung', + child: MyTextFormField('Beschreibe deinen Schlaf'), + ), + const SizedBox( + height: 80, + ), + ElevatedButton( + onPressed: () => inputModel.saveSleep(wokeUpKey, sleptKey), + child: const Text('Speichern')) + ], ); } } diff --git a/lib/widgets/slider.dart b/lib/widgets/slider.dart index bf7c5b3..fee5177 100644 --- a/lib/widgets/slider.dart +++ b/lib/widgets/slider.dart @@ -3,11 +3,11 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/providers/input_provider.dart'; class MySlider extends StatelessWidget { - MySlider({Key? key}) : super(key: key); + const MySlider({super.key}); @override Widget build(BuildContext context) { - var inputModel = context.watch(); + InputProvider inputModel = context.watch(); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, diff --git a/lib/widgets/timepicker.dart b/lib/widgets/timepicker.dart index 5e97af5..19fd528 100644 --- a/lib/widgets/timepicker.dart +++ b/lib/widgets/timepicker.dart @@ -1,25 +1,20 @@ import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; +import 'package:provider/provider.dart'; -class TimePicker extends StatefulWidget { - TimeOfDay _initialTime; - TimePicker(this._initialTime, {Key? key}); +class TimePicker extends StatelessWidget { + final String keyMap; - TimeOfDay get getCurrentTime => _initialTime; - - @override - State createState() => TimePickerState(); -} - - -class TimePickerState extends State { - TimePickerState(); + const TimePicker(this.keyMap, {super.key}); @override Widget build(BuildContext context) { + InputProvider inputModel = context.watch(); + return Center( child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [ Text( - '${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}', + '${inputModel.getTimeEntry(keyMap).hour.toString().padLeft(2, '0')}:${inputModel.getTimeEntry(keyMap).minute.toString().padLeft(2, '0')}', style: const TextStyle(fontSize: 22), ), const SizedBox(width: 16), @@ -28,7 +23,7 @@ class TimePickerState extends State { //TODO auslagern TimeOfDay? newTime = await showTimePicker( context: context, - initialTime: widget._initialTime, + initialTime: inputModel.getTimeEntry(keyMap), builder: (context, child) { return MediaQuery( data: MediaQuery.of(context) @@ -38,9 +33,7 @@ class TimePickerState extends State { }, ); if (newTime == null) return; - setState(() { - widget._initialTime = newTime; - }); + inputModel.setTime(keyMap, newTime); }, child: const Text('Zeit einstellen')) ]),