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: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());

View File

@ -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<DatabaseRecord> _moodRecords = [];
final List<DatabaseRecord> _sleepRecords = [];
class DatabaseMock implements DatabaseService {
static final DatabaseMock _databaseMock = DatabaseMock._internal();
factory DatabaseMock() {
return _databaseMock;
}
DatabaseMock._internal();
final List<Mood> _moodRecords = [];
final List<Sleep> _sleepRecords = [];
final List<DatabaseRecord> _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<int> addMood(Mood mood) {
_moodRecords.add(mood);
return Future.value(1);
}
List<DatabaseRecord> getMoodRecords() => _moodRecords;
List<DatabaseRecord> getSleepRecords() => _sleepRecords;
List<DatabaseRecord> getWorkoutRecords() => _workoutRecords;
@override
Future<int> addSleep(Sleep sleep) {
_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> {
int _selectedIndex = 4;
int _selectedIndex = 0;
int? _gruppe;
final List<String> _titles = [

View File

@ -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<ScannerPage> {
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () async {
List<Mood> moods = await DatabaseService.instance.getMoodRecords();
List<Mood> moods = await globals.databaseService.getMoodRecords();
List<Sleep> sleeps =
await DatabaseService.instance.getSleepRecords();
await globals.databaseService.getSleepRecords();
for (Mood mood in moods) {
print(mood.toCSV());
}

View File

@ -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<String, TimeOfDay> _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();
}
}

View File

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

View File

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

View File

@ -71,10 +71,9 @@ class _SleepFormState extends State<SleepForm> {
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,

View File

@ -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<InputProvider>();
return TextFormField(
onSaved: onSaveAction,
controller: inputProvider.textController,
decoration: InputDecoration(hintText: _description),
validator: (String? value) =>
value == null || value.isEmpty ? 'Text eingeben' : null,