complete mood form provider setup, use mocked database

main
Julian Gegner 2023-02-26 16:17:57 +01:00
parent c45b38a1df
commit b3f22b2c74
10 changed files with 102 additions and 65 deletions

7
lib/globals.dart 100644
View File

@ -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;

View File

@ -1,14 +1,14 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/pages/main_page.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:smoke_cess_app/services/notification_service.dart';
import 'package:timezone/data/latest.dart' as tz; import 'package:timezone/data/latest.dart' as tz;
import 'globals.dart' as globals;
void main() { void main() {
// to ensure all the widgets are initialized. // to ensure all the widgets are initialized.
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
//init database //init database
DatabaseService.instance; globals.databaseService;
tz.initializeTimeZones(); tz.initializeTimeZones();
NotificationService().initNotification(); NotificationService().initNotification();
runApp(const MyApp()); runApp(const MyApp());

View File

@ -1,23 +1,43 @@
import 'package:smoke_cess_app/interface/db_record.dart'; import 'package:smoke_cess_app/interface/db_record.dart';
import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/sleep.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 { class DatabaseMock implements DatabaseService {
final List<DatabaseRecord> _moodRecords = []; static final DatabaseMock _databaseMock = DatabaseMock._internal();
final List<DatabaseRecord> _sleepRecords = []; factory DatabaseMock() {
return _databaseMock;
}
DatabaseMock._internal();
final List<Mood> _moodRecords = [];
final List<Sleep> _sleepRecords = [];
final List<DatabaseRecord> _workoutRecords = []; final List<DatabaseRecord> _workoutRecords = [];
void saveRecord(DatabaseRecord record) { @override
if (record is Mood) { Future<int> addMood(Mood mood) {
_moodRecords.add(record); _moodRecords.add(mood);
} else if (record is Sleep) { return Future.value(1);
_sleepRecords.add(record);
} else {
_workoutRecords.add(record);
}
} }
List<DatabaseRecord> getMoodRecords() => _moodRecords; @override
List<DatabaseRecord> getSleepRecords() => _sleepRecords; Future<int> addSleep(Sleep sleep) {
List<DatabaseRecord> getWorkoutRecords() => _workoutRecords; _sleepRecords.add(sleep);
return Future.value(1);
}
@override
// TODO: implement database
Future<Database> get database => DatabaseService.instance.database;
@override
Future<List<Mood>> getMoodRecords() {
return Future.value(_moodRecords);
}
@override
Future<List<Sleep>> getSleepRecords() {
return Future.value(_sleepRecords);
}
} }

View File

@ -15,7 +15,7 @@ class MyHomePage extends StatefulWidget {
} }
class MyHomePageState extends State<MyHomePage> { class MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 4; int _selectedIndex = 0;
int? _gruppe; int? _gruppe;
final List<String> _titles = [ final List<String> _titles = [

View File

@ -9,6 +9,7 @@ import 'package:smoke_cess_app/services/notification_service.dart';
import '../models/sleep.dart'; import '../models/sleep.dart';
import '../widgets/missing_config_popup.dart'; import '../widgets/missing_config_popup.dart';
import '../globals.dart' as globals;
class ScannerPage extends StatefulWidget { class ScannerPage extends StatefulWidget {
const ScannerPage({super.key}); const ScannerPage({super.key});
@ -80,9 +81,9 @@ class ScannerPageState extends State<ScannerPage> {
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)), textStyle: const TextStyle(fontSize: 20)),
onPressed: () async { onPressed: () async {
List<Mood> moods = await DatabaseService.instance.getMoodRecords(); List<Mood> moods = await globals.databaseService.getMoodRecords();
List<Sleep> sleeps = List<Sleep> sleeps =
await DatabaseService.instance.getSleepRecords(); await globals.databaseService.getSleepRecords();
for (Mood mood in moods) { for (Mood mood in moods) {
print(mood.toCSV()); print(mood.toCSV());
} }

View File

@ -1,12 +1,39 @@
import 'package:flutter/cupertino.dart'; 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 { class InputProvider extends ChangeNotifier {
double _sliderValue = 50; double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: '');
final Map<String, TimeOfDay> _times = {};
double get sliderValue => _sliderValue; double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
set sliderValue(double newValue) { set sliderValue(double newValue) {
_sliderValue = newValue; _sliderValue = newValue;
notifyListeners(); 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();
}
} }

View File

@ -9,51 +9,33 @@ import 'package:smoke_cess_app/widgets/text_formfield.dart';
import '../providers/input_provider.dart'; import '../providers/input_provider.dart';
import 'elevated_card.dart'; import 'elevated_card.dart';
class MoodForm extends StatefulWidget { class MoodForm extends StatelessWidget {
const MoodForm({super.key}); const MoodForm({super.key});
@override
State<MoodForm> createState() => _MoodFormState();
}
class _MoodFormState extends State<MoodForm> {
final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>();
MySlider slider = MySlider();
String _textInput = "";
void submitForm(BuildContext context) { void submitForm(BuildContext context) {
var inputModel = context.watch<InputProvider>(); var inputModel = context.watch<InputProvider>();
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Form( var inputModel = context.watch<InputProvider>();
key: _moodFormKey, return Column(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
ElevatedCard( ElevatedCard(
title: 'Stimmungsbewertung', title: 'Stimmungsbewertung',
child: slider, child: MySlider(),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
ElevatedCard( const ElevatedCard(
title: 'Beschreibe deine Stimmung', title: 'Beschreibe deine Stimmung',
child: child: MyTextFormField('Beschreibe deine Stimmung'),
MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave),
), ),
SubmitFormButton(() => submitForm(context)) ElevatedButton(
onPressed: () => inputModel.saveMood(),
child: const Text('Speichern'),
)
], ],
)); );
} }
} }

View File

@ -34,7 +34,7 @@ class TimerStartStopPopupState extends State<TimerStartStopPopup> {
child: MySlider(), child: MySlider(),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
MyTextFormField('Beschreibe deinen Motivation', onFormFieldSave), MyTextFormField('Beschreibe deinen Motivation'),
SubmitFormButton(() => submitForm(context)), SubmitFormButton(() => submitForm(context)),
], ],
), ),

View File

@ -71,10 +71,9 @@ class _SleepFormState extends State<SleepForm> {
child: slider, child: slider,
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
ElevatedCard( const ElevatedCard(
title: 'Schlafbeschreibung', title: 'Schlafbeschreibung',
child: MyTextFormField( child: MyTextFormField('Beschreibe deinen Schlaf'),
'Beschreibe deinen Schlaf', onFormFieldSave),
), ),
const SizedBox( const SizedBox(
height: 80, height: 80,

View File

@ -1,18 +1,19 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/input_provider.dart';
class MyTextFormField extends StatelessWidget { class MyTextFormField extends StatelessWidget {
final String _description; final String _description;
final Function(String?) onSaveAction;
const MyTextFormField( const MyTextFormField(
this._description, this._description, {
this.onSaveAction, {
super.key, super.key,
}); });
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var inputProvider = context.watch<InputProvider>();
return TextFormField( return TextFormField(
onSaved: onSaveAction, controller: inputProvider.textController,
decoration: InputDecoration(hintText: _description), decoration: InputDecoration(hintText: _description),
validator: (String? value) => validator: (String? value) =>
value == null || value.isEmpty ? 'Text eingeben' : null, value == null || value.isEmpty ? 'Text eingeben' : null,