From c3fde53bec01c2612f5de33ada7489a79652b3fc Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Thu, 2 Mar 2023 19:31:47 +0100 Subject: [PATCH] added TasksProvider to MainPage --- assets/group3.json | 2 +- lib/main.dart | 4 +-- lib/pages/main_page.dart | 28 +++++++++++++--- lib/providers/task_provider.dart | 56 ------------------------------- lib/providers/tasks_provider.dart | 55 ++++++++++++++++++++++++++++++ lib/services/pages_service.dart | 9 ++--- 6 files changed, 84 insertions(+), 70 deletions(-) delete mode 100644 lib/providers/task_provider.dart create mode 100644 lib/providers/tasks_provider.dart diff --git a/assets/group3.json b/assets/group3.json index 55ce9e0..30e9886 100644 --- a/assets/group3.json +++ b/assets/group3.json @@ -7,7 +7,7 @@ }, "relapse_categories": ["App stresst mich", "langeweile", "lunge braucht es"], "mood_query": { - "days": ["Montag", "Freitag"], + "days": ["Montag", "Donnerstag"], "hours": 10, "minutes": 30 }, diff --git a/lib/main.dart b/lib/main.dart index e52b7e6..0c574ce 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.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:timezone/data/latest.dart' as tz; import 'globals.dart' as globals; @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget { home: MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => SettingsProvider()), - ChangeNotifierProvider(create: (context) => TaskProvider()), + ChangeNotifierProvider(create: (context) => TasksProvider()), ], child: const MyHomePage(), )); diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 1d55df5..13defdb 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -1,6 +1,7 @@ import 'package:awesome_dialog/awesome_dialog.dart'; import 'package:flutter/material.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/providers/settings_provider.dart'; @@ -31,12 +32,12 @@ class MyHomePageState extends State { @override Widget build(BuildContext context) { var settingsModel = context.watch(); - var group = settingsModel.settings?.group; + var tasksModel = context.watch(); _isConfigured = settingsModel.initialized; return Scaffold( appBar: AppBar( title: Text( - '${pages.values.elementAt(_selectedIndex)['title']} ${_isConfigured ? "Gruppe $group" : ""}')), + '${pages.values.elementAt(_selectedIndex)['title']} + ${_isConfigured ? "Gruppe ${settingsModel.settings?.group}" : ""}')), body: Center( child: SingleChildScrollView( child: pages.values.elementAt(_selectedIndex)['page'])), @@ -45,10 +46,29 @@ class MyHomePageState extends State { selectedIndex: _selectedIndex, destinations: pages.keys.map((key) { return NavigationDestination( - icon: pages[key]?['icon'] ?? - const Icon(Icons.disabled_by_default), + icon: tasksModel.tasks[key] ?? false + ? MyToDoIcon(pages[key]?['icon']) + : pages[key]!['icon'], label: pages[key]?['title']); }).toList()), ); } } + +class MyToDoIcon extends StatelessWidget { + final Icon _icon; + const MyToDoIcon(this._icon, {super.key}); + + @override + Widget build(BuildContext context) { + return Stack(children: [ + _icon, + const Positioned( + // draw a red marble + top: 0.0, + right: 0.0, + child: Icon(Icons.brightness_1, size: 10.0, color: Colors.redAccent), + ) + ]); + } +} diff --git a/lib/providers/task_provider.dart b/lib/providers/task_provider.dart deleted file mode 100644 index cf27964..0000000 --- a/lib/providers/task_provider.dart +++ /dev/null @@ -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 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 sleepList = - await globals.databaseService.getSleepRecords(); - Sleep sleep = sleepList.last; - pages[key]?['todo'] = !isSameDay(sleepToday, sleep.date); - } - break; - case Pages.timer: - { - List workoutList = - await globals.databaseService.getWorkoutRecords(); - Workout mood = workoutList.last; - pages[key]?['todo'] = !isSameDay(DateTime.now(), mood.date); - break; - } - default: - } - } - }); - notifyListeners(); - } -} diff --git a/lib/providers/tasks_provider.dart b/lib/providers/tasks_provider.dart new file mode 100644 index 0000000..182b8ec --- /dev/null +++ b/lib/providers/tasks_provider.dart @@ -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 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 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 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 workoutList = + await globals.databaseService.getWorkoutRecords(); + if (workoutList.isNotEmpty) { + Workout mood = workoutList.last; + tasks[Pages.timer] = isSameDay(DateTime.now(), mood.date); + } + notifyListeners(); + } +} diff --git a/lib/services/pages_service.dart b/lib/services/pages_service.dart index c29795a..0c19383 100644 --- a/lib/services/pages_service.dart +++ b/lib/services/pages_service.dart @@ -18,30 +18,25 @@ const Map> pages = { 'title': 'Stimmung', 'page': MoodPage(), 'icon': Icon(Icons.mood_outlined, color: Colors.black), - 'todo': false, }, Pages.sleep: { 'title': 'Schlaf', 'page': SleepPage(), 'icon': Icon(Icons.bedtime_outlined, color: Colors.black), - 'todo': false, }, - Pages.relapse: { + Pages.timer: { 'title': 'Timer', 'page': IntervalTimerPage(), 'icon': Icon(Icons.timer_outlined, color: Colors.black), - 'todo': false, }, - Pages.timer: { + Pages.relapse: { 'title': 'Rückfall', 'page': RelapsePage(), 'icon': Icon(Icons.smoke_free_outlined, color: Colors.black), - 'todo': false, }, Pages.settings: { 'title': 'Scanner', 'page': ScannerPage(), 'icon': Icon(Icons.camera_alt_outlined, color: Colors.black), - 'todo': false, }, };