added TasksProvider to MainPage

main
Kai Mannweiler 2023-03-02 19:31:47 +01:00
parent 175735f4b9
commit c3fde53bec
6 changed files with 84 additions and 70 deletions

View File

@ -7,7 +7,7 @@
}, },
"relapse_categories": ["App stresst mich", "langeweile", "lunge braucht es"], "relapse_categories": ["App stresst mich", "langeweile", "lunge braucht es"],
"mood_query": { "mood_query": {
"days": ["Montag", "Freitag"], "days": ["Montag", "Donnerstag"],
"hours": 10, "hours": 10,
"minutes": 30 "minutes": 30
}, },

View File

@ -1,7 +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/providers/tasks_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;
@ -29,7 +29,7 @@ class MyApp extends StatelessWidget {
home: MultiProvider( home: MultiProvider(
providers: [ providers: [
ChangeNotifierProvider(create: (context) => SettingsProvider()), ChangeNotifierProvider(create: (context) => SettingsProvider()),
ChangeNotifierProvider(create: (context) => TaskProvider()), ChangeNotifierProvider(create: (context) => TasksProvider()),
], ],
child: const MyHomePage(), child: const MyHomePage(),
)); ));

View File

@ -1,6 +1,7 @@
import 'package:awesome_dialog/awesome_dialog.dart'; import 'package:awesome_dialog/awesome_dialog.dart';
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/providers/tasks_provider.dart';
import 'package:smoke_cess_app/services/pages_service.dart'; import 'package:smoke_cess_app/services/pages_service.dart';
import 'package:smoke_cess_app/providers/settings_provider.dart'; import 'package:smoke_cess_app/providers/settings_provider.dart';
@ -31,12 +32,12 @@ class MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var settingsModel = context.watch<SettingsProvider>(); var settingsModel = context.watch<SettingsProvider>();
var group = settingsModel.settings?.group; var tasksModel = context.watch<TasksProvider>();
_isConfigured = settingsModel.initialized; _isConfigured = settingsModel.initialized;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text( title: Text(
'${pages.values.elementAt(_selectedIndex)['title']} ${_isConfigured ? "Gruppe $group" : ""}')), '${pages.values.elementAt(_selectedIndex)['title']} + ${_isConfigured ? "Gruppe ${settingsModel.settings?.group}" : ""}')),
body: Center( body: Center(
child: SingleChildScrollView( child: SingleChildScrollView(
child: pages.values.elementAt(_selectedIndex)['page'])), child: pages.values.elementAt(_selectedIndex)['page'])),
@ -45,10 +46,29 @@ 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: tasksModel.tasks[key] ?? false
const Icon(Icons.disabled_by_default), ? MyToDoIcon(pages[key]?['icon'])
: pages[key]!['icon'],
label: pages[key]?['title']); label: pages[key]?['title']);
}).toList()), }).toList()),
); );
} }
} }
class MyToDoIcon extends StatelessWidget {
final Icon _icon;
const MyToDoIcon(this._icon, {super.key});
@override
Widget build(BuildContext context) {
return Stack(children: <Widget>[
_icon,
const Positioned(
// draw a red marble
top: 0.0,
right: 0.0,
child: Icon(Icons.brightness_1, size: 10.0, color: Colors.redAccent),
)
]);
}
}

View File

@ -1,56 +0,0 @@
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

@ -0,0 +1,55 @@
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 TasksProvider extends ChangeNotifier {
Map<Pages, bool> tasks = {
Pages.mood: true,
Pages.sleep: true,
Pages.timer: true,
};
TasksProvider() {
initTasks();
}
void setTaksDone(Pages taskName) {
tasks[taskName] = false;
notifyListeners();
}
void initTasks() async {
TZDateTime? moodToday = await getTodayMood();
if (moodToday != null) {
List<Mood> moodList = await globals.databaseService.getMoodRecords();
if (moodList.isNotEmpty) {
Mood mood = moodList.last;
tasks[Pages.mood] = isSameDay(moodToday, mood.date);
}
} else {
tasks[Pages.mood] = false;
}
TZDateTime? sleepToday = await getTodayMood();
if (sleepToday != null) {
List<Sleep> sleepList = await globals.databaseService.getSleepRecords();
if (sleepList.isNotEmpty) {
Sleep sleep = sleepList.last;
tasks[Pages.sleep] = isSameDay(sleepToday, sleep.date);
}
} else {
tasks[Pages.sleep] = false;
}
List<Workout> workoutList =
await globals.databaseService.getWorkoutRecords();
if (workoutList.isNotEmpty) {
Workout mood = workoutList.last;
tasks[Pages.timer] = isSameDay(DateTime.now(), mood.date);
}
notifyListeners();
}
}

View File

@ -18,30 +18,25 @@ const Map<Pages, Map<String, dynamic>> pages = {
'title': 'Stimmung', 'title': 'Stimmung',
'page': MoodPage(), 'page': MoodPage(),
'icon': Icon(Icons.mood_outlined, color: Colors.black), 'icon': Icon(Icons.mood_outlined, color: Colors.black),
'todo': false,
}, },
Pages.sleep: { Pages.sleep: {
'title': 'Schlaf', 'title': 'Schlaf',
'page': SleepPage(), 'page': SleepPage(),
'icon': Icon(Icons.bedtime_outlined, color: Colors.black), 'icon': Icon(Icons.bedtime_outlined, color: Colors.black),
'todo': false,
}, },
Pages.relapse: { Pages.timer: {
'title': 'Timer', 'title': 'Timer',
'page': IntervalTimerPage(), 'page': IntervalTimerPage(),
'icon': Icon(Icons.timer_outlined, color: Colors.black), 'icon': Icon(Icons.timer_outlined, color: Colors.black),
'todo': false,
}, },
Pages.timer: { Pages.relapse: {
'title': 'Rückfall', '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,
}, },
Pages.settings: { Pages.settings: {
'title': 'Scanner', '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,
}, },
}; };