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 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<String, dynamic> 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,
};

View File

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

View File

@ -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<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;
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();
}
}

View File

@ -79,8 +79,8 @@ String _createSleepTable = '''
value INTEGER,
date TEXT,
comment TEXT,
sleepedAtHour INTEGER,
sleepedAtMinute INTEGER,
sleptAtHour INTEGER,
sleptAtMinute INTEGER,
wokeUpAtHour 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,74 +1,38 @@
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<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
Widget build(BuildContext context) {
return Form(
key: _sleepFormKey,
child: Stack(
children: [
SingleChildScrollView(
child: Column(
InputProvider inputModel = context.watch<InputProvider>();
String wokeUpKey = 'wokeUpAt';
String sleptKey = 'sleptAt';
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedCard(
title: 'Einschlafzeit',
child: sleepTimePicker,
child: TimePicker(sleptKey),
),
const SizedBox(height: 16),
ElevatedCard(
title: 'Aufwachzeit',
child: wakeUpTimePicker,
child: TimePicker(wokeUpKey),
),
const SizedBox(height: 16),
ElevatedCard(
const ElevatedCard(
title: 'Schlafbewertung',
child: slider,
child: MySlider(),
),
const SizedBox(height: 16),
const ElevatedCard(
@ -78,20 +42,10 @@ class _SleepFormState extends State<SleepForm> {
const SizedBox(
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';
class MySlider extends StatelessWidget {
MySlider({Key? key}) : super(key: key);
const MySlider({super.key});
@override
Widget build(BuildContext context) {
var inputModel = context.watch<InputProvider>();
InputProvider inputModel = context.watch<InputProvider>();
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,

View File

@ -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<StatefulWidget> createState() => TimePickerState();
}
class TimePickerState extends State<TimePicker> {
TimePickerState();
const TimePicker(this.keyMap, {super.key});
@override
Widget build(BuildContext context) {
InputProvider inputModel = context.watch<InputProvider>();
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<TimePicker> {
//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<TimePicker> {
},
);
if (newTime == null) return;
setState(() {
widget._initialTime = newTime;
});
inputModel.setTime(keyMap, newTime);
},
child: const Text('Zeit einstellen'))
]),