diff --git a/lib/entities/PizzaEvent.dart b/lib/entities/PizzaEvent.dart index 932dfa4..3188ac9 100644 --- a/lib/entities/PizzaEvent.dart +++ b/lib/entities/PizzaEvent.dart @@ -1,6 +1,10 @@ +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:hive/hive.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; +import 'package:pizzaplanner/main.dart'; + +import 'package:timezone/timezone.dart' as tz; part 'PizzaEvent.g.dart'; @@ -28,4 +32,35 @@ class PizzaEvent extends HiveObject{ this.doughBallSize, this.dateTime ); + + Future createPizzaEventNotifications() async { + const androidPlatformChannelSpecifics = AndroidNotificationDetails( + "PizzaEventChannel", "PizzaEventChannel", "PizzaPlanner notification channel", + importance: Importance.max, + priority: Priority.high, + ticker: "ticker" + ); + + const platformChannelSpecific = NotificationDetails(android: androidPlatformChannelSpecifics); + + var stepTime = tz.TZDateTime.from(dateTime, tz.local); + var durationToFirstStep = Duration(seconds: this.recipe.recipeSteps + .map((recipeStep) => recipeStep.getCurrentWaitInSeconds()) + .fold(0, (a, b) => a+b)); + stepTime = stepTime.subtract(durationToFirstStep); + + for (var recipeStep in this.recipe.recipeSteps) { + await flutterLocalNotificationsPlugin.zonedSchedule( + "${name}_${recipeStep.name}_${dateTime.millisecondsSinceEpoch}".hashCode, + recipeStep.name, + null, + stepTime, + platformChannelSpecific, + androidAllowWhileIdle: true, + uiLocalNotificationDateInterpretation: + UILocalNotificationDateInterpretation.absoluteTime + ); + stepTime = stepTime.add(Duration(seconds: recipeStep.getCurrentWaitInSeconds())); + } + } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 731659e..83a4b66 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:flutter_native_timezone/flutter_native_timezone.dart'; import 'package:pizzaplanner/entities/PizzaEvent.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; @@ -14,6 +15,9 @@ import 'package:hive/hive.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:pizzaplanner/util.dart'; +import 'package:timezone/data/latest.dart' as tz; +import 'package:timezone/timezone.dart' as tz; + final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); void main() async { @@ -46,6 +50,11 @@ void main() async { await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: selectNotification); + // init timezones properly + tz.initializeTimeZones(); + final String? timeZoneName = await FlutterNativeTimezone.getLocalTimezone(); + tz.setLocalLocation(tz.getLocation(timeZoneName!)); + runApp(PizzaPlanner()); //await Hive.close(); diff --git a/lib/pages/AddPizzaEventPage.dart b/lib/pages/AddPizzaEventPage.dart index e186d15..fa866ea 100644 --- a/lib/pages/AddPizzaEventPage.dart +++ b/lib/pages/AddPizzaEventPage.dart @@ -1,6 +1,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:pizzaplanner/entities/PizzaEvent.dart'; @@ -66,6 +67,7 @@ class AddPizzaEventPageState extends State { Icon(FontAwesome5.hashtag), Expanded( child: Slider( + value: pizzaCount.toDouble(), min: 1, max: 20, @@ -164,16 +166,19 @@ class AddPizzaEventPageState extends State { if (eventTime == null){ return; } - var pizzaEventsBox = await Hive.box("PizzaEvents"); - pizzaEventsBox.add( - PizzaEvent( - this.name, - this.widget.pizzaRecipe, - this.pizzaCount, - this.doughBallSize, - eventTime - ) + + var pizzaEventsBox = Hive.box("PizzaEvents"); + PizzaEvent pizzaEvent = PizzaEvent( + this.name, + this.widget.pizzaRecipe, + this.pizzaCount, + this.doughBallSize, + eventTime ); + await pizzaEventsBox.add(pizzaEvent); + + pizzaEvent.createPizzaEventNotifications(); + Navigator.pop(context); Navigator.pop(context); // two times because of the pick recipe page }, diff --git a/pubspec.yaml b/pubspec.yaml index b62cbf4..acd583c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,8 @@ dependencies: hive: ^2.0.4 hive_flutter: ^1.1.0 + timezone: ^0.7.0 + flutter_native_timezone: ^2.0.0 flutter_local_notifications: ^8.0.0 dev_dependencies: