refactor timepicker and moodform

main
Julian Gegner 2023-02-26 17:07:38 +01:00
parent b3f22b2c74
commit 646a97926d
8 changed files with 75 additions and 136 deletions

View File

@ -5,25 +5,25 @@ class Sleep implements DatabaseRecord {
final int _sleepQualityValue; final int _sleepQualityValue;
final String _comment; final String _comment;
final DateTime _date; final DateTime _date;
final TimeOfDay _sleepedAt; final TimeOfDay _sleptAt;
final TimeOfDay _wokeUpAt; final TimeOfDay _wokeUpAt;
Sleep(this._sleepQualityValue, this._comment, this._date, this._sleepedAt, Sleep(this._sleepQualityValue, this._comment, this._date, this._sleptAt,
this._wokeUpAt); this._wokeUpAt);
@override @override
factory Sleep.fromDatabase(Map<String, dynamic> map) { factory Sleep.fromDatabase(Map<String, dynamic> map) {
DateTime date = DateTime.parse(map['date']); DateTime date = DateTime.parse(map['date']);
TimeOfDay sleepedAt = TimeOfDay sleptAt =
TimeOfDay(hour: map['sleepedAtHour'], minute: map['sleepedAtMinute']); TimeOfDay(hour: map['sleptAtHour'], minute: map['sleptAtMinute']);
TimeOfDay wokeUpAt = TimeOfDay wokeUpAt =
TimeOfDay(hour: map['wokeUpAtHour'], minute: map['wokeUpAtMinute']); 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 @override
String toCSV() { 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 @override
@ -32,8 +32,8 @@ class Sleep implements DatabaseRecord {
'value': _sleepQualityValue, 'value': _sleepQualityValue,
'comment': _comment, 'comment': _comment,
'date': _date.toIso8601String(), 'date': _date.toIso8601String(),
'sleepedAtHour': _sleepedAt.hour, 'sleptAtHour': _sleptAt.hour,
'sleepedAtMinute': _sleepedAt.minute, 'sleptAtMinute': _sleptAt.minute,
'wokeUpAtHour': _wokeUpAt.hour, 'wokeUpAtHour': _wokeUpAt.hour,
'wokeUpAtMinute': _wokeUpAt.minute, 'wokeUpAtMinute': _wokeUpAt.minute,
}; };

View File

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

View File

@ -1,12 +1,15 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.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 '../globals.dart' as globals; 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 TextEditingController _textController = TextEditingController(text: '');
final Map<String, TimeOfDay> _times = {}; final Map<String, TimeOfDay> _times = {
'wokeUpAt': const TimeOfDay(hour: 8, minute: 0),
'sleptAt': const TimeOfDay(hour: 22, minute: 0),
};
double get sliderValue => _sliderValue; double get sliderValue => _sliderValue;
TextEditingController get textController => _textController; TextEditingController get textController => _textController;
@ -16,17 +19,20 @@ class InputProvider extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
TimeOfDay? getTimeEntry(String key) { TimeOfDay getTimeEntry(String key) {
return _times[key]; return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
} }
void setTime(String key, TimeOfDay time) { void setTime(String key, TimeOfDay time) {
_times[key] = time; _times[key] = time;
notifyListeners();
} }
void _resetFields() { void _resetFields() {
_sliderValue = 50; _sliderValue = 50;
_textController.text = ''; _textController.text = '';
setTime('wokeUpAt', const TimeOfDay(hour: 8, minute: 0));
setTime('sleptAt', const TimeOfDay(hour: 22, minute: 0));
notifyListeners(); notifyListeners();
} }
@ -36,4 +42,11 @@ class InputProvider extends ChangeNotifier {
globals.databaseService.addMood(mood); globals.databaseService.addMood(mood);
_resetFields(); _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();
}
} }

View File

@ -79,8 +79,8 @@ String _createSleepTable = '''
value INTEGER, value INTEGER,
date TEXT, date TEXT,
comment TEXT, comment TEXT,
sleepedAtHour INTEGER, sleptAtHour INTEGER,
sleepedAtMinute INTEGER, sleptAtMinute INTEGER,
wokeUpAtHour INTEGER, wokeUpAtHour INTEGER,
wokeUpAtMinute INTEGER wokeUpAtMinute INTEGER
) )

View File

@ -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<DatabaseRecord> getWorkoutRecords() => _database.getWorkoutRecords();
List<DatabaseRecord> getMoodRecords() => _database.getMoodRecords();
List<DatabaseRecord> getSleepRecords() => _database.getSleepRecords();
String recordsToCSV(List<DatabaseRecord> records) {
String csv = "";
for (DatabaseRecord record in records) {
csv += '${record.toCSV()}\n';
}
return csv;
}
}

View File

@ -1,97 +1,51 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.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/elevated_card.dart';
import 'package:smoke_cess_app/widgets/slider.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/text_formfield.dart';
import 'package:smoke_cess_app/widgets/timepicker.dart'; import 'package:smoke_cess_app/widgets/timepicker.dart';
import '../providers/input_provider.dart'; import '../providers/input_provider.dart';
class SleepForm extends StatefulWidget { class SleepForm extends StatelessWidget {
const SleepForm({Key? key}) : super(key: key); const SleepForm({Key? key}) : super(key: key);
@override
State<SleepForm> createState() => _SleepFormState();
}
class _SleepFormState extends State<SleepForm> {
final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>();
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<InputProvider>();
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Form( InputProvider inputModel = context.watch<InputProvider>();
key: _sleepFormKey, String wokeUpKey = 'wokeUpAt';
child: Stack( String sleptKey = 'sleptAt';
children: [
SingleChildScrollView( return Column(
child: Column( mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch,
crossAxisAlignment: CrossAxisAlignment.stretch, children: [
children: [ ElevatedCard(
ElevatedCard( title: 'Einschlafzeit',
title: 'Einschlafzeit', child: TimePicker(sleptKey),
child: sleepTimePicker, ),
), const SizedBox(height: 16),
const SizedBox(height: 16), ElevatedCard(
ElevatedCard( title: 'Aufwachzeit',
title: 'Aufwachzeit', child: TimePicker(wokeUpKey),
child: wakeUpTimePicker, ),
), const SizedBox(height: 16),
const SizedBox(height: 16), const ElevatedCard(
ElevatedCard( title: 'Schlafbewertung',
title: 'Schlafbewertung', child: MySlider(),
child: slider, ),
), const SizedBox(height: 16),
const SizedBox(height: 16), const ElevatedCard(
const ElevatedCard( title: 'Schlafbeschreibung',
title: 'Schlafbeschreibung', child: MyTextFormField('Beschreibe deinen Schlaf'),
child: MyTextFormField('Beschreibe deinen Schlaf'), ),
), const SizedBox(
const SizedBox( height: 80,
height: 80, ),
), ElevatedButton(
], onPressed: () => inputModel.saveSleep(wokeUpKey, sleptKey),
), child: const Text('Speichern'))
), ],
Positioned(
bottom: 0,
right: 0,
child: SizedBox(
width: 140,
height: 80,
child: SubmitFormButton(() => submitForm(context)),
),
),
],
),
); );
} }
} }

View File

@ -3,11 +3,11 @@ import 'package:flutter/material.dart';
import 'package:smoke_cess_app/providers/input_provider.dart'; import 'package:smoke_cess_app/providers/input_provider.dart';
class MySlider extends StatelessWidget { class MySlider extends StatelessWidget {
MySlider({Key? key}) : super(key: key); const MySlider({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var inputModel = context.watch<InputProvider>(); InputProvider inputModel = context.watch<InputProvider>();
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,

View File

@ -1,25 +1,20 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/providers/input_provider.dart';
import 'package:provider/provider.dart';
class TimePicker extends StatefulWidget { class TimePicker extends StatelessWidget {
TimeOfDay _initialTime; final String keyMap;
TimePicker(this._initialTime, {Key? key});
TimeOfDay get getCurrentTime => _initialTime; const TimePicker(this.keyMap, {super.key});
@override
State<StatefulWidget> createState() => TimePickerState();
}
class TimePickerState extends State<TimePicker> {
TimePickerState();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
InputProvider inputModel = context.watch<InputProvider>();
return Center( return Center(
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [ child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text( 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), style: const TextStyle(fontSize: 22),
), ),
const SizedBox(width: 16), const SizedBox(width: 16),
@ -28,7 +23,7 @@ class TimePickerState extends State<TimePicker> {
//TODO auslagern //TODO auslagern
TimeOfDay? newTime = await showTimePicker( TimeOfDay? newTime = await showTimePicker(
context: context, context: context,
initialTime: widget._initialTime, initialTime: inputModel.getTimeEntry(keyMap),
builder: (context, child) { builder: (context, child) {
return MediaQuery( return MediaQuery(
data: MediaQuery.of(context) data: MediaQuery.of(context)
@ -38,9 +33,7 @@ class TimePickerState extends State<TimePicker> {
}, },
); );
if (newTime == null) return; if (newTime == null) return;
setState(() { inputModel.setTime(keyMap, newTime);
widget._initialTime = newTime;
});
}, },
child: const Text('Zeit einstellen')) child: const Text('Zeit einstellen'))
]), ]),