From b3f22b2c74947beb5e554d123e8ae9d25c052300 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sun, 26 Feb 2023 16:17:57 +0100 Subject: [PATCH] complete mood form provider setup, use mocked database --- lib/globals.dart | 7 +++ lib/main.dart | 4 +- lib/mock/db_mock.dart | 48 +++++++++++++------ lib/pages/main_page.dart | 2 +- lib/pages/scanner_page.dart | 5 +- lib/providers/input_provider.dart | 27 +++++++++++ lib/widgets/mood_form.dart | 58 ++++++++--------------- lib/widgets/popup_for_start_and_stop.dart | 2 +- lib/widgets/sleep_form.dart | 5 +- lib/widgets/text_formfield.dart | 9 ++-- 10 files changed, 102 insertions(+), 65 deletions(-) create mode 100644 lib/globals.dart diff --git a/lib/globals.dart b/lib/globals.dart new file mode 100644 index 0000000..58229d9 --- /dev/null +++ b/lib/globals.dart @@ -0,0 +1,7 @@ +library app.globals; + +import 'package:smoke_cess_app/mock/db_mock.dart'; +import 'package:smoke_cess_app/services/database_service.dart'; + +DatabaseService databaseService = DatabaseMock(); +// DatabaseService databaseService = DatabaseService.instance; diff --git a/lib/main.dart b/lib/main.dart index 2fe1f09..2c2245d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,14 +1,14 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/pages/main_page.dart'; -import 'package:smoke_cess_app/services/database_service.dart'; import 'package:smoke_cess_app/services/notification_service.dart'; import 'package:timezone/data/latest.dart' as tz; +import 'globals.dart' as globals; void main() { // to ensure all the widgets are initialized. WidgetsFlutterBinding.ensureInitialized(); //init database - DatabaseService.instance; + globals.databaseService; tz.initializeTimeZones(); NotificationService().initNotification(); runApp(const MyApp()); diff --git a/lib/mock/db_mock.dart b/lib/mock/db_mock.dart index 1a92cf9..a06f003 100644 --- a/lib/mock/db_mock.dart +++ b/lib/mock/db_mock.dart @@ -1,23 +1,43 @@ import 'package:smoke_cess_app/interface/db_record.dart'; import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/models/sleep.dart'; +import 'package:smoke_cess_app/services/database_service.dart'; +import 'package:sqflite_common/sqlite_api.dart'; -class DatabaseMock { - final List _moodRecords = []; - final List _sleepRecords = []; +class DatabaseMock implements DatabaseService { + static final DatabaseMock _databaseMock = DatabaseMock._internal(); + factory DatabaseMock() { + return _databaseMock; + } + DatabaseMock._internal(); + + final List _moodRecords = []; + final List _sleepRecords = []; final List _workoutRecords = []; - void saveRecord(DatabaseRecord record) { - if (record is Mood) { - _moodRecords.add(record); - } else if (record is Sleep) { - _sleepRecords.add(record); - } else { - _workoutRecords.add(record); - } + @override + Future addMood(Mood mood) { + _moodRecords.add(mood); + return Future.value(1); } - List getMoodRecords() => _moodRecords; - List getSleepRecords() => _sleepRecords; - List getWorkoutRecords() => _workoutRecords; + @override + Future addSleep(Sleep sleep) { + _sleepRecords.add(sleep); + return Future.value(1); + } + + @override + // TODO: implement database + Future get database => DatabaseService.instance.database; + + @override + Future> getMoodRecords() { + return Future.value(_moodRecords); + } + + @override + Future> getSleepRecords() { + return Future.value(_sleepRecords); + } } diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 2af4f13..0daf3a6 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 = 4; + int _selectedIndex = 0; int? _gruppe; final List _titles = [ diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index 7bded71..7159694 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -9,6 +9,7 @@ import 'package:smoke_cess_app/services/notification_service.dart'; import '../models/sleep.dart'; import '../widgets/missing_config_popup.dart'; +import '../globals.dart' as globals; class ScannerPage extends StatefulWidget { const ScannerPage({super.key}); @@ -80,9 +81,9 @@ class ScannerPageState extends State { style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), onPressed: () async { - List moods = await DatabaseService.instance.getMoodRecords(); + List moods = await globals.databaseService.getMoodRecords(); List sleeps = - await DatabaseService.instance.getSleepRecords(); + await globals.databaseService.getSleepRecords(); for (Mood mood in moods) { print(mood.toCSV()); } diff --git a/lib/providers/input_provider.dart b/lib/providers/input_provider.dart index 95bd423..c0a7601 100644 --- a/lib/providers/input_provider.dart +++ b/lib/providers/input_provider.dart @@ -1,12 +1,39 @@ import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/models/mood.dart'; +import '../globals.dart' as globals; class InputProvider extends ChangeNotifier { double _sliderValue = 50; + final TextEditingController _textController = TextEditingController(text: ''); + final Map _times = {}; double get sliderValue => _sliderValue; + TextEditingController get textController => _textController; set sliderValue(double newValue) { _sliderValue = newValue; notifyListeners(); } + + TimeOfDay? getTimeEntry(String key) { + return _times[key]; + } + + void setTime(String key, TimeOfDay time) { + _times[key] = time; + } + + void _resetFields() { + _sliderValue = 50; + _textController.text = ''; + notifyListeners(); + } + + void saveMood() { + Mood mood = + Mood(_sliderValue.toInt(), _textController.text, DateTime.now()); + globals.databaseService.addMood(mood); + _resetFields(); + } } diff --git a/lib/widgets/mood_form.dart b/lib/widgets/mood_form.dart index f52db8c..9ca1f30 100644 --- a/lib/widgets/mood_form.dart +++ b/lib/widgets/mood_form.dart @@ -9,51 +9,33 @@ import 'package:smoke_cess_app/widgets/text_formfield.dart'; import '../providers/input_provider.dart'; import 'elevated_card.dart'; -class MoodForm extends StatefulWidget { +class MoodForm extends StatelessWidget { const MoodForm({super.key}); - @override - State createState() => _MoodFormState(); -} - -class _MoodFormState extends State { - final GlobalKey _moodFormKey = GlobalKey(); - MySlider slider = MySlider(); - String _textInput = ""; - void submitForm(BuildContext context) { var inputModel = context.watch(); - print(inputModel.sliderValue); - if (_moodFormKey.currentState!.validate()) { - _moodFormKey.currentState?.save(); //call every onSave Method - Mood mood = - Mood(inputModel.sliderValue.toInt(), _textInput, DateTime.now()); - DatabaseService.instance.addMood(mood); - _moodFormKey.currentState?.reset(); - } } - void onFormFieldSave(String? newValue) => _textInput = newValue!; - @override Widget build(BuildContext context) { - return Form( - key: _moodFormKey, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - ElevatedCard( - title: 'Stimmungsbewertung', - child: slider, - ), - const SizedBox(height: 16), - ElevatedCard( - title: 'Beschreibe deine Stimmung', - child: - MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave), - ), - SubmitFormButton(() => submitForm(context)) - ], - )); + var inputModel = context.watch(); + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + ElevatedCard( + title: 'Stimmungsbewertung', + child: MySlider(), + ), + const SizedBox(height: 16), + const ElevatedCard( + title: 'Beschreibe deine Stimmung', + child: MyTextFormField('Beschreibe deine Stimmung'), + ), + ElevatedButton( + onPressed: () => inputModel.saveMood(), + child: const Text('Speichern'), + ) + ], + ); } } diff --git a/lib/widgets/popup_for_start_and_stop.dart b/lib/widgets/popup_for_start_and_stop.dart index b8ed90a..5221541 100644 --- a/lib/widgets/popup_for_start_and_stop.dart +++ b/lib/widgets/popup_for_start_and_stop.dart @@ -34,7 +34,7 @@ class TimerStartStopPopupState extends State { child: MySlider(), ), const SizedBox(height: 16), - MyTextFormField('Beschreibe deinen Motivation', onFormFieldSave), + MyTextFormField('Beschreibe deinen Motivation'), SubmitFormButton(() => submitForm(context)), ], ), diff --git a/lib/widgets/sleep_form.dart b/lib/widgets/sleep_form.dart index d9955bc..7929367 100644 --- a/lib/widgets/sleep_form.dart +++ b/lib/widgets/sleep_form.dart @@ -71,10 +71,9 @@ class _SleepFormState extends State { child: slider, ), const SizedBox(height: 16), - ElevatedCard( + const ElevatedCard( title: 'Schlafbeschreibung', - child: MyTextFormField( - 'Beschreibe deinen Schlaf', onFormFieldSave), + child: MyTextFormField('Beschreibe deinen Schlaf'), ), const SizedBox( height: 80, diff --git a/lib/widgets/text_formfield.dart b/lib/widgets/text_formfield.dart index 102a350..1e6e25c 100644 --- a/lib/widgets/text_formfield.dart +++ b/lib/widgets/text_formfield.dart @@ -1,18 +1,19 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; class MyTextFormField extends StatelessWidget { final String _description; - final Function(String?) onSaveAction; const MyTextFormField( - this._description, - this.onSaveAction, { + this._description, { super.key, }); @override Widget build(BuildContext context) { + var inputProvider = context.watch(); return TextFormField( - onSaved: onSaveAction, + controller: inputProvider.textController, decoration: InputDecoration(hintText: _description), validator: (String? value) => value == null || value.isEmpty ? 'Text eingeben' : null,