Added TaskProvider and Pages enum

main
Kai Mannweiler 2023-03-02 18:35:03 +01:00
parent 4bde30f00e
commit 175735f4b9
10 changed files with 135 additions and 18 deletions

View File

@ -1,6 +1,7 @@
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/pages/main_page.dart'; import 'package:smoke_cess_app/pages/main_page.dart';
import 'package:smoke_cess_app/providers/task_provider.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; import 'globals.dart' as globals;
@ -25,8 +26,11 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: _title, title: _title,
home: ChangeNotifierProvider( home: MultiProvider(
create: (context) => SettingsProvider(), providers: [
ChangeNotifierProvider(create: (context) => SettingsProvider()),
ChangeNotifierProvider(create: (context) => TaskProvider()),
],
child: const MyHomePage(), child: const MyHomePage(),
)); ));
} }

View File

@ -1,4 +1,3 @@
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/relapse.dart'; import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/sleep.dart'; import 'package:smoke_cess_app/models/sleep.dart';
@ -17,7 +16,7 @@ class DatabaseMock implements DatabaseService {
final List<Mood> _moodRecords = []; final List<Mood> _moodRecords = [];
final List<Sleep> _sleepRecords = []; final List<Sleep> _sleepRecords = [];
final List<Relapse> _relapseRecords = []; final List<Relapse> _relapseRecords = [];
final List<DatabaseRecord> _workoutRecords = []; final List<Workout> _workoutRecords = [];
@override @override
Future<int> addMood(Mood mood) { Future<int> addMood(Mood mood) {
@ -60,4 +59,9 @@ class DatabaseMock implements DatabaseService {
Future<List<Relapse>> getRelapseRecords() { Future<List<Relapse>> getRelapseRecords() {
return Future.value(_relapseRecords); return Future.value(_relapseRecords);
} }
@override
Future<List<Workout>> getWorkoutRecords() {
return Future.value(_workoutRecords);
}
} }

View File

@ -7,6 +7,8 @@ class Mood implements DatabaseRecord {
Mood(this._moodValue, this._comment, this._date); Mood(this._moodValue, this._comment, this._date);
DateTime get date => _date;
@override @override
factory Mood.fromDatabase(Map<String, dynamic> map) { factory Mood.fromDatabase(Map<String, dynamic> map) {
DateTime date = DateTime.parse(map['date']); DateTime date = DateTime.parse(map['date']);

View File

@ -11,6 +11,8 @@ class Sleep implements DatabaseRecord {
Sleep(this._sleepQualityValue, this._comment, this._date, this._sleptAt, Sleep(this._sleepQualityValue, this._comment, this._date, this._sleptAt,
this._wokeUpAt); this._wokeUpAt);
DateTime get date => _date;
@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']);

View File

@ -7,8 +7,10 @@ class Workout implements DatabaseRecord {
Workout(this._motivationBefore, this._motivationAfter, this._workoutDate); Workout(this._motivationBefore, this._motivationAfter, this._workoutDate);
DateTime get date => _workoutDate;
@override @override
factory Workout.fromMap(Map<String, dynamic> map) { factory Workout.fromDatabase(Map<String, dynamic> map) {
return Workout(map['_workoutDuration'], map['_motivationBefore'], return Workout(map['_workoutDuration'], map['_motivationBefore'],
map['_motivationAfter']); map['_motivationAfter']);
} }

View File

@ -36,7 +36,7 @@ class MyHomePageState extends State<MyHomePage> {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text( title: Text(
'${pages.keys.elementAt(_selectedIndex)} ${_isConfigured ? "Gruppe $group" : ""}')), '${pages.values.elementAt(_selectedIndex)['title']} ${_isConfigured ? "Gruppe $group" : ""}')),
body: Center( body: Center(
child: SingleChildScrollView( child: SingleChildScrollView(
child: pages.values.elementAt(_selectedIndex)['page'])), child: pages.values.elementAt(_selectedIndex)['page'])),
@ -45,9 +45,9 @@ class MyHomePageState extends State<MyHomePage> {
selectedIndex: _selectedIndex, selectedIndex: _selectedIndex,
destinations: pages.keys.map((key) { destinations: pages.keys.map((key) {
return NavigationDestination( return NavigationDestination(
icon: pages[key]!['icon'] ?? icon: pages[key]?['icon'] ??
const Icon(Icons.disabled_by_default), const Icon(Icons.disabled_by_default),
label: key); label: pages[key]?['title']);
}).toList()), }).toList()),
); );
} }

View File

@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/sleep.dart';
import 'package:smoke_cess_app/models/workout.dart';
import 'package:smoke_cess_app/services/date_service.dart';
import 'package:smoke_cess_app/services/pages_service.dart';
import 'package:timezone/browser.dart';
import '../globals.dart' as globals;
import '../models/mood.dart';
class TaskProvider extends ChangeNotifier {
TaskProvider() {
initTasks();
}
void setTaksDone(Pages taskName) {
pages[taskName]?['todo'] = false;
notifyListeners();
}
void initTasks() {
pages.forEach((key, value) async {
if (!value['todo']) {
switch (key) {
case Pages.mood:
TZDateTime? moodToday = await getTodayMood();
if (moodToday != null) {
List<Mood> moodList =
await globals.databaseService.getMoodRecords();
Mood mood = moodList.last;
pages[key]?['todo'] = !isSameDay(moodToday, mood.date);
}
break;
case Pages.sleep:
TZDateTime? sleepToday = await getTodayMood();
if (sleepToday != null) {
List<Sleep> sleepList =
await globals.databaseService.getSleepRecords();
Sleep sleep = sleepList.last;
pages[key]?['todo'] = !isSameDay(sleepToday, sleep.date);
}
break;
case Pages.timer:
{
List<Workout> workoutList =
await globals.databaseService.getWorkoutRecords();
Workout mood = workoutList.last;
pages[key]?['todo'] = !isSameDay(DateTime.now(), mood.date);
break;
}
default:
}
}
});
notifyListeners();
}
}

View File

@ -63,6 +63,15 @@ class DatabaseService {
return relapseList; return relapseList;
} }
Future<List<Workout>> getWorkoutRecords() async {
Database db = await instance.database;
var workoutRecords = await db.query('workout');
List<Workout> workoutList = workoutRecords.isNotEmpty
? workoutRecords.map((e) => Workout.fromDatabase(e)).toList()
: [];
return workoutList;
}
Future<int> addMood(Mood mood) async { Future<int> addMood(Mood mood) async {
Database db = await instance.database; Database db = await instance.database;
return await db.insert('mood', mood.toMap()); return await db.insert('mood', mood.toMap());

View File

@ -13,6 +13,12 @@ const weekDays = {
"Sonntag": 7, "Sonntag": 7,
}; };
bool isSameDay(DateTime? dateA, DateTime? dateB) {
return dateA?.year == dateB?.year &&
dateA?.month == dateB?.month &&
dateA?.day == dateB?.day;
}
Future<List<TZDateTime>> getDatesforMood() async { Future<List<TZDateTime>> getDatesforMood() async {
final List<String>? selectedDays = await getMoodQueryDaysCategories(); final List<String>? selectedDays = await getMoodQueryDaysCategories();
final int? selectedHours = await getMoodQueryHours(); final int? selectedHours = await getMoodQueryHours();
@ -27,6 +33,20 @@ Future<List<TZDateTime>> getDatesforSleep() async {
return createTZDateTimes(selectedDays, selectedHours, selectedMinutes); return createTZDateTimes(selectedDays, selectedHours, selectedMinutes);
} }
Future<TZDateTime?> getTodayMood() async {
List<TZDateTime> moodDates = await getDatesforMood();
Iterable<TZDateTime> today =
moodDates.where((element) => isSameDay(element, DateTime.now()));
return today.isNotEmpty ? today.first : null;
}
Future<TZDateTime?> getTodaySleep() async {
List<TZDateTime> sleepDates = await getDatesforSleep();
Iterable<TZDateTime> today =
sleepDates.where((element) => isSameDay(element, DateTime.now()));
return today.isNotEmpty ? today.first : null;
}
List<TZDateTime> createTZDateTimes( List<TZDateTime> createTZDateTimes(
List<String>? selectedDays, int? selectedHours, int? selectedMinutes) { List<String>? selectedDays, int? selectedHours, int? selectedMinutes) {
final List<TZDateTime> tzDateTimes = []; final List<TZDateTime> tzDateTimes = [];

View File

@ -5,25 +5,43 @@ import '../pages/relapse_page.dart';
import '../pages/scanner_page.dart'; import '../pages/scanner_page.dart';
import '../pages/sleep_page.dart'; import '../pages/sleep_page.dart';
const pages = { enum Pages {
'Stimmung': { mood,
sleep,
relapse,
timer,
settings,
}
const Map<Pages, Map<String, dynamic>> pages = {
Pages.mood: {
'title': 'Stimmung',
'page': MoodPage(), 'page': MoodPage(),
'icon': Icon(Icons.mood_outlined, color: Colors.black) 'icon': Icon(Icons.mood_outlined, color: Colors.black),
'todo': false,
}, },
'Schlaf': { Pages.sleep: {
'title': 'Schlaf',
'page': SleepPage(), 'page': SleepPage(),
'icon': Icon(Icons.bedtime_outlined, color: Colors.black) 'icon': Icon(Icons.bedtime_outlined, color: Colors.black),
'todo': false,
}, },
'Timer': { Pages.relapse: {
'title': 'Timer',
'page': IntervalTimerPage(), 'page': IntervalTimerPage(),
'icon': Icon(Icons.timer_outlined, color: Colors.black) 'icon': Icon(Icons.timer_outlined, color: Colors.black),
'todo': false,
}, },
'Rückfall': { Pages.timer: {
'title': 'Rückfall',
'page': RelapsePage(), 'page': RelapsePage(),
'icon': Icon(Icons.smoke_free_outlined, color: Colors.black), 'icon': Icon(Icons.smoke_free_outlined, color: Colors.black),
'todo': false,
}, },
'Scanner': { Pages.settings: {
'title': 'Scanner',
'page': ScannerPage(), 'page': ScannerPage(),
'icon': Icon(Icons.camera_alt_outlined, color: Colors.black) 'icon': Icon(Icons.camera_alt_outlined, color: Colors.black),
'todo': false,
}, },
}; };