Added Timer to popup if task isnt due

main
Kai Mannweiler 2023-03-03 16:38:08 +01:00
parent 37a63b0059
commit 9d26bdee60
4 changed files with 67 additions and 8 deletions

View File

@ -40,12 +40,14 @@ class TasksProvider extends ChangeNotifier {
} }
void initTasks() async { void initTasks() async {
DateTime now = DateTime.now();
TZDateTime? moodToday = await getTodayMood(); TZDateTime? moodToday = await getTodayMood();
if (moodToday != null) { if (moodToday != null) {
List<Mood> moodList = await globals.databaseService.getMoodRecords(); List<Mood> moodList = await globals.databaseService.getMoodRecords();
if (moodList.isNotEmpty) { if (moodList.isNotEmpty) {
Mood mood = moodList.last; Mood mood = moodList.last;
tasks[Pages.mood] = !isSameDay(moodToday, mood.date); tasks[Pages.mood] =
!isSameDay(moodToday, mood.date) && moodToday.isBefore(now);
} }
} else { } else {
tasks[Pages.mood] = false; tasks[Pages.mood] = false;
@ -55,7 +57,8 @@ class TasksProvider extends ChangeNotifier {
List<Sleep> sleepList = await globals.databaseService.getSleepRecords(); List<Sleep> sleepList = await globals.databaseService.getSleepRecords();
if (sleepList.isNotEmpty) { if (sleepList.isNotEmpty) {
Sleep sleep = sleepList.last; Sleep sleep = sleepList.last;
tasks[Pages.sleep] = !isSameDay(sleepToday, sleep.date); tasks[Pages.sleep] =
!isSameDay(sleepToday, sleep.date) && sleepToday.isBefore(now);
} }
} else { } else {
tasks[Pages.sleep] = false; tasks[Pages.sleep] = false;

View File

@ -1,6 +1,8 @@
import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart';
import 'package:timezone/timezone.dart'; import 'package:timezone/timezone.dart';
import 'pages_service.dart';
const int trainingTime = 40; const int trainingTime = 40;
const weekDays = { const weekDays = {
@ -47,6 +49,45 @@ Future<TZDateTime?> getTodaySleep() async {
return today.isNotEmpty ? today.first : null; return today.isNotEmpty ? today.first : null;
} }
Future<Duration> getTimeTillNextMood() async {
List<TZDateTime> moodDates = await getDatesforMood();
Iterable<TZDateTime> nextDate =
moodDates.where((element) => element.isAfter(DateTime.now()));
Duration duration = nextDate.isNotEmpty
? nextDate.first.difference(DateTime.now())
: const Duration(seconds: 0);
return duration;
}
Future<Duration> getTimeTillNextSleep() async {
List<TZDateTime> sleepDates = await getDatesforSleep();
Iterable<TZDateTime> nextDate =
sleepDates.where((element) => element.isAfter(DateTime.now()));
Duration duration = nextDate.isNotEmpty
? nextDate.first.difference(DateTime.now())
: const Duration(seconds: 0);
return duration;
}
Future<Duration> 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<Duration> getTimeTill(Pages page) {
switch (page) {
case Pages.mood:
return getTimeTillNextMood();
case Pages.sleep:
return getTimeTillNextSleep();
default:
return getTimeTillNextWorkout();
}
}
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

@ -1,4 +1,14 @@
String formatTime(int seconds) { String formatTime(int seconds) {
Duration duration = Duration(seconds: 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;
} }

View File

@ -2,10 +2,12 @@ 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/services/pages_service.dart'; import 'package:smoke_cess_app/services/pages_service.dart';
import '../providers/input_provider.dart'; import '../providers/input_provider.dart';
import '../providers/page_provider.dart'; import '../providers/page_provider.dart';
import '../providers/tasks_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 { class ViewFormPage extends StatelessWidget {
final Widget form; final Widget form;
@ -14,12 +16,15 @@ class ViewFormPage extends StatelessWidget {
const ViewFormPage( const ViewFormPage(
{super.key, required this.form, required this.view, required this.page}); {super.key, required this.form, required this.view, required this.page});
void showPopUp(BuildContext context) { void showPopup(BuildContext context) async {
TimerProvider timerProvider = context.read<TimerProvider>();
Duration duration = await getTimeTill(page);
timerProvider.startTimer(duration);
AwesomeDialog( AwesomeDialog(
context: context, context: context,
dialogType: DialogType.info, dialogType: DialogType.info,
title: 'Schon gemacht!', title: 'Bald erst wieder',
desc: 'Der Eintrag wurde erfolgreich gespeichert', body: TimerWidget(duration: duration),
).show(); ).show();
} }
@ -36,7 +41,7 @@ class ViewFormPage extends StatelessWidget {
: const Icon(Icons.add_outlined, color: Colors.black), : const Icon(Icons.add_outlined, color: Colors.black),
onPressed: tasksProvider.tasks[page] ?? true onPressed: tasksProvider.tasks[page] ?? true
? pageProvider.swap ? pageProvider.swap
: () => showPopUp(context), : () => showPopup(context),
), ),
), ),
pageProvider.showForm pageProvider.showForm