From 9d26bdee6015027c098cb4c44e0d8776b4997eae Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Fri, 3 Mar 2023 16:38:08 +0100 Subject: [PATCH] Added Timer to popup if task isnt due --- lib/providers/tasks_provider.dart | 7 ++++-- lib/services/date_service.dart | 41 +++++++++++++++++++++++++++++++ lib/utils/timer_util.dart | 12 ++++++++- lib/widgets/view_form_page.dart | 15 +++++++---- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/providers/tasks_provider.dart b/lib/providers/tasks_provider.dart index 590c4fc..d3fc042 100644 --- a/lib/providers/tasks_provider.dart +++ b/lib/providers/tasks_provider.dart @@ -40,12 +40,14 @@ class TasksProvider extends ChangeNotifier { } void initTasks() async { + DateTime now = DateTime.now(); 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); + tasks[Pages.mood] = + !isSameDay(moodToday, mood.date) && moodToday.isBefore(now); } } else { tasks[Pages.mood] = false; @@ -55,7 +57,8 @@ class TasksProvider extends ChangeNotifier { List sleepList = await globals.databaseService.getSleepRecords(); if (sleepList.isNotEmpty) { Sleep sleep = sleepList.last; - tasks[Pages.sleep] = !isSameDay(sleepToday, sleep.date); + tasks[Pages.sleep] = + !isSameDay(sleepToday, sleep.date) && sleepToday.isBefore(now); } } else { tasks[Pages.sleep] = false; diff --git a/lib/services/date_service.dart b/lib/services/date_service.dart index b5e254f..79bdd37 100644 --- a/lib/services/date_service.dart +++ b/lib/services/date_service.dart @@ -1,6 +1,8 @@ import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:timezone/timezone.dart'; +import 'pages_service.dart'; + const int trainingTime = 40; const weekDays = { @@ -47,6 +49,45 @@ Future getTodaySleep() async { return today.isNotEmpty ? today.first : null; } +Future getTimeTillNextMood() async { + List moodDates = await getDatesforMood(); + Iterable nextDate = + moodDates.where((element) => element.isAfter(DateTime.now())); + Duration duration = nextDate.isNotEmpty + ? nextDate.first.difference(DateTime.now()) + : const Duration(seconds: 0); + return duration; +} + +Future getTimeTillNextSleep() async { + List sleepDates = await getDatesforSleep(); + Iterable nextDate = + sleepDates.where((element) => element.isAfter(DateTime.now())); + Duration duration = nextDate.isNotEmpty + ? nextDate.first.difference(DateTime.now()) + : const Duration(seconds: 0); + return duration; +} + +Future getTimeTillNextWorkout() async { + DateTime now = DateTime.now(); + DateTime tomorrow = + DateTime(now.year, now.month, now.day).add(const Duration(days: 1)); + Duration duration = tomorrow.difference(now); + return duration; +} + +Future getTimeTill(Pages page) { + switch (page) { + case Pages.mood: + return getTimeTillNextMood(); + case Pages.sleep: + return getTimeTillNextSleep(); + default: + return getTimeTillNextWorkout(); + } +} + List createTZDateTimes( List? selectedDays, int? selectedHours, int? selectedMinutes) { final List tzDateTimes = []; diff --git a/lib/utils/timer_util.dart b/lib/utils/timer_util.dart index 58d9690..a63030f 100644 --- a/lib/utils/timer_util.dart +++ b/lib/utils/timer_util.dart @@ -1,4 +1,14 @@ String formatTime(int seconds) { Duration duration = Duration(seconds: seconds); - return '${duration.inMinutes.remainder(60).toString().padLeft(2, '0')}:${duration.inSeconds.remainder(60).toString().padLeft(2, '0')}'; + String formattedTime = ''; + String twoDigits(int n) => n.toString().padLeft(2, "0"); + String days = duration.inDays.toString(); + String hours = twoDigits(duration.inHours.remainder(24)); + String minutes = twoDigits(duration.inMinutes.remainder(60)); + String formattedSeconds = twoDigits(duration.inSeconds.remainder(60)); + if (duration.inDays != 0) formattedTime += '$days:'; + if (duration.inHours != 0) formattedTime += '$hours:'; + formattedTime += '$minutes:'; + formattedTime += formattedSeconds; + return formattedTime; } diff --git a/lib/widgets/view_form_page.dart b/lib/widgets/view_form_page.dart index 843f66e..a907cfb 100644 --- a/lib/widgets/view_form_page.dart +++ b/lib/widgets/view_form_page.dart @@ -2,10 +2,12 @@ import 'package:awesome_dialog/awesome_dialog.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:smoke_cess_app/services/pages_service.dart'; - import '../providers/input_provider.dart'; import '../providers/page_provider.dart'; import '../providers/tasks_provider.dart'; +import '../providers/timer_provider.dart'; +import '../services/date_service.dart'; +import 'timer_widget.dart'; class ViewFormPage extends StatelessWidget { final Widget form; @@ -14,12 +16,15 @@ class ViewFormPage extends StatelessWidget { const ViewFormPage( {super.key, required this.form, required this.view, required this.page}); - void showPopUp(BuildContext context) { + void showPopup(BuildContext context) async { + TimerProvider timerProvider = context.read(); + Duration duration = await getTimeTill(page); + timerProvider.startTimer(duration); AwesomeDialog( context: context, dialogType: DialogType.info, - title: 'Schon gemacht!', - desc: 'Der Eintrag wurde erfolgreich gespeichert', + title: 'Bald erst wieder', + body: TimerWidget(duration: duration), ).show(); } @@ -36,7 +41,7 @@ class ViewFormPage extends StatelessWidget { : const Icon(Icons.add_outlined, color: Colors.black), onPressed: tasksProvider.tasks[page] ?? true ? pageProvider.swap - : () => showPopUp(context), + : () => showPopup(context), ), ), pageProvider.showForm