added working notifications to the app for recipe steps

This commit is contained in:
broodjeaap89 2021-08-15 14:52:41 +02:00
parent 1ce594c5df
commit 3f5b537ef0
4 changed files with 60 additions and 9 deletions

View file

@ -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<void> 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()));
}
}
}

View file

@ -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();

View file

@ -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<AddPizzaEventPage> {
Icon(FontAwesome5.hashtag),
Expanded(
child: Slider(
value: pizzaCount.toDouble(),
min: 1,
max: 20,
@ -164,16 +166,19 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
if (eventTime == null){
return;
}
var pizzaEventsBox = await Hive.box<PizzaEvent>("PizzaEvents");
pizzaEventsBox.add(
PizzaEvent(
this.name,
this.widget.pizzaRecipe,
this.pizzaCount,
this.doughBallSize,
eventTime
)
var pizzaEventsBox = Hive.box<PizzaEvent>("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
},

View file

@ -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: