add notification for absences

This commit is contained in:
hihihaha
2023-08-29 13:45:52 +02:00
parent ec04a0ff81
commit bc4e4e9b5a
7 changed files with 296 additions and 98 deletions

View File

@@ -20,7 +20,7 @@ const settingsDB = DatabaseStruct("settings", {
"grade_color4": int, "grade_color5": int, // grade colors
"vibration_strength": int, "ab_weeks": int, "swap_ab_weeks": int,
"notifications": int, "notifications_bitfield": int,
"notification_poll_interval": int, // notifications
"notification_poll_interval": int, "notifications_grades":int, "notifications_absences":int, // notifications
"x_filc_id": String, "graph_class_avg": int, "presentation_mode": int,
"bell_delay": int, "bell_delay_enabled": int,
"grade_opening_fun": int, "icon_pack": String, "premium_scopes": String,

View File

@@ -3,6 +3,7 @@ import 'dart:ui';
import 'package:filcnaplo/api/providers/database_provider.dart';
import 'package:filcnaplo/api/providers/status_provider.dart';
import 'package:filcnaplo/api/providers/user_provider.dart';
import 'package:filcnaplo/database/init.dart';
import 'package:filcnaplo/models/settings.dart';
import 'package:filcnaplo/helpers/notification_helper.i18n.dart';
import 'package:filcnaplo_kreta_api/client/api.dart';
@@ -19,12 +20,36 @@ class NotificationsHelper {
late UserProvider userProvider;
late KretaClient kretaClient;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
List<T> combineLists<T, K>(List<T> list1, List<T> list2, K Function(T) keyExtractor,) {
Set<K> uniqueKeys = Set<K>();
List<T> combinedList = [];
for (T item in list1) {
K key = keyExtractor(item);
if (!uniqueKeys.contains(key)) {
uniqueKeys.add(key);
combinedList.add(item);
}
}
for (T item in list2) {
K key = keyExtractor(item);
if (!uniqueKeys.contains(key)) {
uniqueKeys.add(key);
combinedList.add(item);
}
}
return combinedList;
}
@pragma('vm:entry-point')
void backgroundJob() async {
// initialize providers
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
DatabaseProvider database = DatabaseProvider();
database = DatabaseProvider();
var db = await initDB(database);
await database.init();
settingsProvider =
await database.query.getSettings(database);
@@ -36,8 +61,8 @@ class NotificationsHelper {
kretaClient = KretaClient(
user: userProvider, settings: settingsProvider, status: status);
kretaClient.refreshLogin();
gradeNotification();
absenceNotification();
if(settingsProvider.notificationsGradesEnabled) gradeNotification();
if(settingsProvider.notificationsAbsencesEnabled) absenceNotification();
}
}
@@ -70,31 +95,31 @@ class NotificationsHelper {
ticker: 'Jegyek');
const NotificationDetails notificationDetails = NotificationDetails(android: androidNotificationDetails);
if(userProvider.getUsers().length == 1) {
await flutterLocalNotificationsPlugin.show(
grade.id.hashCode,
"title_grade".i18n,
"body_grade".i18n.fill([
grade.value.value.toString(),
grade.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? grade.subject.renamedTo!
: grade.subject.name
]),
notificationDetails);
} else { // multiple users are added, also display student name
await flutterLocalNotificationsPlugin.show(
grade.id.hashCode,
"title_grade".i18n,
"body_grade_multiuser".i18n.fill([
userProvider.displayName!,
grade.value.value.toString(),
grade.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? grade.subject.renamedTo!
: grade.subject.name
]),
notificationDetails);
}
await flutterLocalNotificationsPlugin.show(
grade.id.hashCode,
"title_grade".i18n,
"body_grade".i18n.fill([
grade.value.value.toString(),
grade.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? grade.subject.renamedTo!
: grade.subject.name
]),
notificationDetails);
} else { // multiple users are added, also display student name
await flutterLocalNotificationsPlugin.show(
grade.id.hashCode,
"title_grade".i18n,
"body_grade_multiuser".i18n.fill([
userProvider.displayName!,
grade.value.value.toString(),
grade.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? grade.subject.renamedTo!
: grade.subject.name
]),
notificationDetails);
}
}
}
}
@@ -102,50 +127,72 @@ class NotificationsHelper {
gradeProvider.seenAll();
}
void absenceNotification() async {
// get absences from api
List? absenceJson = await kretaClient.getAPI(KretaAPI.absences(userProvider.instituteCode ?? ""));
List<Absence> storedAbsences = await database.userQuery.getAbsences(userId: userProvider.id!);
if(absenceJson == null) {
return;
}
List<Absence> absences = absenceJson.map((e) => Absence.fromJson(e)).toList();
// format api absences to correct format while preserving hasSeen value
List<Absence> absences = absenceJson.map((e) {
Absence apiAbsence = Absence.fromJson(e);
Absence storedAbsence = storedAbsences.firstWhere(
(stored) => stored.id == apiAbsence.id,
orElse: () => apiAbsence);
apiAbsence.hasSeen = storedAbsence.hasSeen;
return apiAbsence;
}).toList();
List<Absence> modifiedAbsences = [];
if(absences != storedAbsences) {
// remove absences that are not new
absences.removeWhere((element) => storedAbsences.contains(element));
for(Absence absence in absences) {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('ABSENCES', 'Hiányzások',
channelDescription: 'Értesítés hiányzások beírásakor',
importance: Importance.max,
priority: Priority.max,
color: const Color(0xFF3D7BF4),
ticker: 'Hiányzások');
const NotificationDetails notificationDetails = NotificationDetails(android: androidNotificationDetails);
if(userProvider.getUsers().length == 1) {
await flutterLocalNotificationsPlugin.show(
absence.id.hashCode,
"title_absence".i18n,
"body_absence".i18n.fill([
DateFormat("yyyy-MM-dd").format(absence.date),
absence.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? absence.subject.renamedTo!
: absence.subject.name
]),
notificationDetails);
} else {
await flutterLocalNotificationsPlugin.show(
absence.id.hashCode,
"title_absence".i18n,
"body_absence_multiuser".i18n.fill([
userProvider.displayName!,
DateFormat("yyyy-MM-dd").format(absence.date),
absence.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? absence.subject.renamedTo!
: absence.subject.name
]),
notificationDetails);
if(!absence.hasSeen) {
absence.hasSeen = true;
modifiedAbsences.add(absence);
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('ABSENCES', 'Hiányzások',
channelDescription: 'Értesítés hiányzások beírásakor',
importance: Importance.max,
priority: Priority.max,
color: const Color(0xFF3D7BF4),
ticker: 'Hiányzások');
const NotificationDetails notificationDetails = NotificationDetails(android: androidNotificationDetails);
if(userProvider.getUsers().length == 1) {
await flutterLocalNotificationsPlugin.show(
absence.id.hashCode,
"title_absence".i18n,
"body_absence".i18n.fill([
DateFormat("yyyy-MM-dd").format(absence.date),
absence.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? absence.subject.renamedTo!
: absence.subject.name
]),
notificationDetails);
} else {
await flutterLocalNotificationsPlugin.show(
absence.id.hashCode,
"title_absence".i18n,
"body_absence_multiuser".i18n.fill([
userProvider.displayName!,
DateFormat("yyyy-MM-dd").format(absence.date),
absence.subject.isRenamed &&
settingsProvider.renamedSubjectsEnabled
? absence.subject.renamedTo!
: absence.subject.name
]),
notificationDetails);
}
}
}
}
// combine modified absences and storedabsences list and save them to the database
List<Absence> combinedAbsences = combineLists(
modifiedAbsences,
storedAbsences,
(Absence absence) => absence.id,
);
await database.userStore.storeAbsences(combinedAbsences, userId: userProvider.id!);
}
}

View File

@@ -31,6 +31,8 @@ class SettingsProvider extends ChangeNotifier {
bool _newsEnabled;
String _seenNews;
bool _notificationsEnabled;
bool _notificationsGradesEnabled;
bool _notificationsAbsencesEnabled;
/*
notificationsBitfield values:
@@ -84,6 +86,8 @@ class SettingsProvider extends ChangeNotifier {
required bool newsEnabled,
required String seenNews,
required bool notificationsEnabled,
required bool notificationsGradesEnabled,
required bool notificationsAbsencesEnabled,
required int notificationsBitfield,
required bool developerMode,
required int notificationPollInterval,
@@ -122,6 +126,8 @@ class SettingsProvider extends ChangeNotifier {
_newsEnabled = newsEnabled,
_seenNews = seenNews,
_notificationsEnabled = notificationsEnabled,
_notificationsGradesEnabled = notificationsGradesEnabled,
_notificationsAbsencesEnabled = notificationsAbsencesEnabled,
_notificationsBitfield = notificationsBitfield,
_developerMode = developerMode,
_notificationPollInterval = notificationPollInterval,
@@ -178,6 +184,8 @@ class SettingsProvider extends ChangeNotifier {
newsEnabled: map["news"] == 1,
seenNews: map["seen_news"],
notificationsEnabled: map["notifications"] == 1,
notificationsGradesEnabled: map["notifications_grades"] == 1,
notificationsAbsencesEnabled: map["notifications_absences"] == 1,
notificationsBitfield: map["notifications_bitfield"],
notificationPollInterval: map["notification_poll_interval"],
developerMode: map["developer_mode"] == 1,
@@ -220,6 +228,8 @@ class SettingsProvider extends ChangeNotifier {
"news": _newsEnabled ? 1 : 0,
"seen_news": _seenNews,
"notifications": _notificationsEnabled ? 1 : 0,
"notifications_grades": _notificationsGradesEnabled ? 1 : 0,
"notifications_absences": _notificationsAbsencesEnabled ? 1 : 0,
"notifications_bitfield": _notificationsBitfield,
"developer_mode": _developerMode ? 1 : 0,
"grade_color1": _gradeColors[0].value,
@@ -273,6 +283,8 @@ class SettingsProvider extends ChangeNotifier {
newsEnabled: true,
seenNews: '',
notificationsEnabled: true,
notificationsGradesEnabled: true,
notificationsAbsencesEnabled: true,
notificationsBitfield: 255,
developerMode: false,
notificationPollInterval: 1,
@@ -314,6 +326,8 @@ class SettingsProvider extends ChangeNotifier {
bool get newsEnabled => _newsEnabled;
List<String> get seenNews => _seenNews.split(',');
bool get notificationsEnabled => _notificationsEnabled;
bool get notificationsGradesEnabled => _notificationsGradesEnabled;
bool get notificationsAbsencesEnabled => _notificationsAbsencesEnabled;
int get notificationsBitfield => _notificationsBitfield;
bool get developerMode => _developerMode;
int get notificationPollInterval => _notificationPollInterval;
@@ -357,6 +371,8 @@ class SettingsProvider extends ChangeNotifier {
bool? newsEnabled,
String? seenNewsId,
bool? notificationsEnabled,
bool? notificationsGradesEnabled,
bool? notificationsAbsencesEnabled,
int? notificationsBitfield,
bool? developerMode,
int? notificationPollInterval,
@@ -408,6 +424,14 @@ class SettingsProvider extends ChangeNotifier {
notificationsEnabled != _notificationsEnabled) {
_notificationsEnabled = notificationsEnabled;
}
if (notificationsGradesEnabled != null &&
notificationsGradesEnabled != _notificationsGradesEnabled) {
_notificationsGradesEnabled = notificationsGradesEnabled;
}
if (notificationsAbsencesEnabled != null &&
notificationsAbsencesEnabled != _notificationsAbsencesEnabled) {
_notificationsAbsencesEnabled = notificationsAbsencesEnabled;
}
if (notificationsBitfield != null &&
notificationsBitfield != _notificationsBitfield) {
_notificationsBitfield = notificationsBitfield;