Add actionable notifications

This commit is contained in:
2022-07-01 08:42:28 -04:00
parent 24de5c9b9a
commit 7af98910ec
4 changed files with 111 additions and 1 deletions

View File

@ -7,14 +7,38 @@ import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:scheduled_timer/scheduled_timer.dart';
void main() {
AwesomeNotifications().initialize(
// set the icon to null if you want to use the default app icon
null,
[
NotificationChannel(
channelGroupKey: 'reminder_channel_group',
channelKey: 'reminder_channel',
channelName: 'Check-In Reminder notifications',
channelDescription: 'Notification channel for Check-In Reminders',
defaultColor: Colors.cyan.shade900,
ledColor: Colors.white)
],
// Channel groups are only visual and are not required
channelGroups: [
NotificationChannelGroup(
channelGroupkey: 'reminder_channel_group',
channelGroupName: 'Reminder group')
],
debug: true
);
runApp(const TBDoctor());
}
class TBDoctor extends StatelessWidget {
const TBDoctor({Key? key}) : super(key: key);
get id => null;
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
@ -115,6 +139,7 @@ class _TBDoctorHomePageState extends State<TBDoctorHomePage> {
// Required for program
late SharedPreferences _prefs;
late ScheduledTimer checkinReminder;
void _setCalcs() async {
if (_inOfficeHoursToDate == 0.0 && _tbdHoursToDate == 0.0 ||
@ -313,6 +338,24 @@ class _TBDoctorHomePageState extends State<TBDoctorHomePage> {
],
);
}
void _showReminderNotification() {
DateTime now = DateTime.now();
if (now.weekday == DateTime.saturday || now.weekday == DateTime.sunday) {
return;
}
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 10,
channelKey: 'reminder_channel',
title: 'Check-In Reminder',
body: 'Are you remote, in office, or out today?'),
actionButtons: <NotificationActionButton>[
NotificationActionButton(key: 'remote', label: 'Remote'),
NotificationActionButton(key: 'office', label: 'In Office'),
NotificationActionButton(key: 'no', label: 'Not Today'),
],
);
}
@override
void initState() {
@ -324,6 +367,57 @@ class _TBDoctorHomePageState extends State<TBDoctorHomePage> {
_setCalcs();
_populateSelf();
});
DateTime now = DateTime.now();
checkinReminder = ScheduledTimer(
id: "checkinReminder",
onExecute: _showReminderNotification,
defaultScheduledTime: DateTime(now.year, now.month, now.day + 1, 7, 0),
onMissedSchedule: _showReminderNotification,
);
AwesomeNotifications().actionStream.listen((receivedNotification) {
DateTime now = DateTime.now();
double hours = 7.5;
if (now.weekday == DateTime.saturday || now.weekday == DateTime.sunday) {
debugPrint("Detected weekend, no work allowed");
return;
}
if (now.isAfter(DateTime(now.year, 06, 23)) || now.isBefore(DateTime(now.year, 09, 14))) {
debugPrint("Detected summer hours");
hours = 8;
if (now.weekday == DateTime.friday) {
debugPrint("Detected summer hours, half day friday");
hours = 4.5;
}
}
SharedPreferences.getInstance().then((SharedPreferences p) {
_prefs = p;
switch(receivedNotification.buttonKeyPressed) {
case "remote":
debugPrint("Modifying TBD since user responded 'remote'");
_tbdHoursToDate += hours;
_prefs.setDouble("_tbdHoursToDate", _tbdHoursToDate);
break;
case "office":
debugPrint("Modifying in-office since user responded 'office'");
_inOfficeHoursToDate += hours;
_prefs.setDouble("_tbdHoursToDate", _tbdHoursToDate);
break;
default:
debugPrint("Didn't care.");
break;
}
// Schedule the next one?
DateTime now = DateTime.now();
checkinReminder = ScheduledTimer(
id: "checkinReminder",
onExecute: _showReminderNotification,
defaultScheduledTime: DateTime(now.year, now.month, now.day + 1, 7, 0),
onMissedSchedule: _showReminderNotification,
);
_setCalcs();
});
});
super.initState();
}