diff --git a/lib/entities/PizzaEvent.dart b/lib/entities/PizzaEvent.dart index 4acd0eb..197592c 100644 --- a/lib/entities/PizzaEvent.dart +++ b/lib/entities/PizzaEvent.dart @@ -39,7 +39,8 @@ class PizzaEvent extends HiveObject{ "PizzaEventChannel", "PizzaEventChannel", "PizzaPlanner notification channel", importance: Importance.max, priority: Priority.high, - ticker: "ticker" + ticker: "ticker", + fullScreenIntent: true, ); const platformChannelSpecific = NotificationDetails(android: androidPlatformChannelSpecifics); @@ -53,20 +54,22 @@ class PizzaEvent extends HiveObject{ final List pendingNotificationRequests = await flutterLocalNotificationsPlugin.pendingNotificationRequests(); int notificationId = pendingNotificationRequests.map((pendingNotification) => pendingNotification.id).fold(0, max); + int stepId = 0; for (var recipeStep in this.recipe.recipeSteps) { await flutterLocalNotificationsPlugin.zonedSchedule( - notificationId, + notificationId+stepId, recipeStep.name, null, stepTime, platformChannelSpecific, androidAllowWhileIdle: true, + payload: "${this.key}__$stepId", uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime ); recipeStep.notificationId = notificationId; stepTime = stepTime.add(Duration(seconds: recipeStep.getCurrentWaitInSeconds())); - notificationId++; + stepId++; } } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 83a4b66..a700283 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,17 +8,22 @@ import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart'; import 'package:pizzaplanner/pages/AddPizzaEventPage.dart'; import 'package:pizzaplanner/pages/PickPizzaRecipePage.dart'; +import 'package:pizzaplanner/pages/PizzaEventNotificationPage.dart'; import 'package:pizzaplanner/pages/PizzaEventPage.dart'; import 'package:pizzaplanner/pages/PizzaEventsPage.dart'; import 'package:hive/hive.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:pizzaplanner/util.dart'; +import 'package:rxdart/subjects.dart'; import 'package:timezone/data/latest.dart' as tz; import 'package:timezone/timezone.dart' as tz; final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); +final BehaviorSubject selectNotificationSubject = BehaviorSubject(); +String? selectedNotificationPayload; + void main() async { // hive init @@ -47,28 +52,51 @@ void main() async { iOS: initializationSettingsIOS, macOS: initializationSettingsMacOS ); - await flutterLocalNotificationsPlugin.initialize(initializationSettings, - onSelectNotification: selectNotification); + await flutterLocalNotificationsPlugin.initialize( + initializationSettings, + onSelectNotification: (String? payload) async { + selectedNotificationPayload = payload; + selectNotificationSubject.add(payload); + }); // init timezones properly tz.initializeTimeZones(); final String? timeZoneName = await FlutterNativeTimezone.getLocalTimezone(); tz.setLocalLocation(tz.getLocation(timeZoneName!)); - runApp(PizzaPlanner()); + runApp( + MaterialApp( + title: "PizzaPlanner", + home: PizzaPlanner(), + onGenerateRoute: RouteGenerator.generateRoute, + ) + ); //await Hive.close(); } -class PizzaPlanner extends StatelessWidget { - // This widget is the root of your application. +class PizzaPlanner extends StatefulWidget { + @override + PizzaPlannerState createState() => PizzaPlannerState(); +} + +class PizzaPlannerState extends State { + + @override + void initState(){ + super.initState(); + this._configureSelectNotificationSubject(); + } + @override Widget build(BuildContext context) { - return MaterialApp( - title: "PizzaPlanner", - home: PizzaEventsPage(), - onGenerateRoute: RouteGenerator.generateRoute, - ); + return PizzaEventsPage(); + } + + void _configureSelectNotificationSubject() { + selectNotificationSubject.stream.listen((String? payload) async { + await Navigator.pushNamed(context, '/event/notification', arguments: payload); + }); } } @@ -87,6 +115,9 @@ class RouteGenerator { case "/event/view": { return MaterialPageRoute(builder: (context) => PizzaEventPage(settings.arguments as PizzaEvent)); } + case "/event/notification": { + return MaterialPageRoute(builder: (context) => PizzaEventNotificationPage(settings.arguments as String?)); + } default: { return MaterialPageRoute(builder: (context) => PizzaEventsPage()); @@ -95,10 +126,3 @@ class RouteGenerator { } } -Future selectNotification(String? payload) async { - if (payload == null) { - print("Payload: null"); - return; - } - print("Payload: $payload"); -} \ No newline at end of file diff --git a/lib/pages/PizzaEventNotificationPage.dart b/lib/pages/PizzaEventNotificationPage.dart new file mode 100644 index 0000000..0b26504 --- /dev/null +++ b/lib/pages/PizzaEventNotificationPage.dart @@ -0,0 +1,55 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:hive/hive.dart'; +import 'package:pizzaplanner/entities/PizzaEvent.dart'; +import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart'; + +class PizzaEventNotificationPage extends StatefulWidget { + final String? payload; + + PizzaEventNotificationPage(this.payload); + + @override + PizzaEventNotificationState createState() => PizzaEventNotificationState(); +} + +class PizzaEventNotificationState extends State { + late final PizzaEvent pizzaEvent; + late final RecipeStep recipeStep; + + @override + void initState() { + super.initState(); + if (this.widget.payload == null){ + print("Redirected to notification page but no payload... Popping"); + Navigator.pop(context); + } + var split = this.widget.payload!.split("__"); + var pizzaEventId = int.parse(split[0]); + var recipeStepId = int.parse(split[1]); + + var pizzaEventsBox = Hive.box("PizzaEvents"); + + pizzaEvent = pizzaEventsBox.get(pizzaEventId)!; + recipeStep = pizzaEvent.recipe.recipeSteps[recipeStepId]; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("From notification"), + ), + resizeToAvoidBottomInset: false, + body: Container( + padding: EdgeInsets.fromLTRB(40, 10, 40, 10), + child: Column( + children: [ + Text(pizzaEvent.name), + Text(recipeStep.name) + ] + ) + ) + ); + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index acd583c..b1a37e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,8 @@ dependencies: hive: ^2.0.4 hive_flutter: ^1.1.0 + rxdart: ^0.27.1 + timezone: ^0.7.0 flutter_native_timezone: ^2.0.0 flutter_local_notifications: ^8.0.0