finished notification fix
This commit is contained in:
@@ -69,6 +69,7 @@ const userDataDB = DatabaseStruct("user_data", {
|
||||
// "subject_lesson_count": String, // non kreta data
|
||||
// notifications and surprise grades // non kreta data
|
||||
"last_seen_grade": int,
|
||||
"last_seen_surprisegrade": int,
|
||||
"last_seen_absence": int,
|
||||
"last_seen_message": int,
|
||||
"last_seen_lesson": int,
|
||||
@@ -134,10 +135,11 @@ Future<Database> initDB(DatabaseProvider database) async {
|
||||
// renamed teachers // non kreta data
|
||||
"renamed_teachers": "{}",
|
||||
// "subject_lesson_count": "{}", // non kreta data
|
||||
"last_seen_grade": 0,
|
||||
"last_seen_absence": 0,
|
||||
"last_seen_message": 0,
|
||||
"last_seen_lesson": 0,
|
||||
"last_seen_grade": DateTime.now().millisecondsSinceEpoch,
|
||||
"last_seen_surprisegrade": 0,
|
||||
"last_seen_absence": DateTime.now().millisecondsSinceEpoch,
|
||||
"last_seen_message": DateTime.now().millisecondsSinceEpoch,
|
||||
"last_seen_lesson": DateTime.now().millisecondsSinceEpoch,
|
||||
// goal planning // non kreta data
|
||||
"goal_plans": "{}",
|
||||
"goal_averages": "{}",
|
||||
@@ -215,3 +217,4 @@ Future<void> migrateDB(
|
||||
print("INFO: Database migrated");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +136,7 @@ class UserDatabaseStore {
|
||||
await db.update("user_data", {"last_seen_${category.name}": lastSeenDate},
|
||||
where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
|
||||
// renamed things
|
||||
Future<void> storeRenamedSubjects(Map<String, String> subjects,
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:refilc/api/providers/database_provider.dart';
|
||||
import 'package:refilc/api/providers/status_provider.dart';
|
||||
import 'package:refilc/api/providers/user_provider.dart';
|
||||
import 'package:refilc/models/settings.dart';
|
||||
import 'package:refilc/helpers/notification_helper.i18n.dart';
|
||||
import 'package:refilc/models/user.dart';
|
||||
import 'package:refilc/utils/navigation_service.dart';
|
||||
import 'package:refilc/utils/service_locator.dart';
|
||||
import 'package:refilc_kreta_api/client/api.dart';
|
||||
import 'package:refilc_kreta_api/client/client.dart';
|
||||
import 'package:refilc_kreta_api/models/absence.dart';
|
||||
@@ -18,8 +21,10 @@ import 'package:i18n_extension/i18n_widget.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:refilc_kreta_api/models/message.dart';
|
||||
|
||||
// if you want to add a new category, also add it to the DB or else the app will probably crash
|
||||
enum LastSeenCategory {
|
||||
grade,
|
||||
surprisegrade,
|
||||
absence,
|
||||
message,
|
||||
lesson
|
||||
@@ -91,20 +96,14 @@ class NotificationsHelper {
|
||||
.then((grades) async {
|
||||
DateTime lastSeenGrade = await database.userQuery.lastSeen(
|
||||
userId: currentuserProvider.id!, category: LastSeenCategory.grade);
|
||||
lastSeenGrade = lastSeenGrade.subtract(const Duration(minutes: 2)); // needed as lastSeenGrade somehow will be a bit in the future
|
||||
|
||||
// loop through grades and see which hasn't been seen yet
|
||||
for (Grade grade in grades) {
|
||||
// if grade is not a normal grade (1-5), don't show it
|
||||
if ([1, 2, 3, 4, 5].contains(grade.value.value)) {
|
||||
// if the grade was added over a week ago, don't show it to avoid notification spam
|
||||
// it worked in reverse, cuz someone added a * -1 to it, but it has been fixed now :D
|
||||
// old code below
|
||||
// if (grade.seenDate.isAfter(lastSeenGrade) &&
|
||||
// grade.date.difference(DateTime.now()).inDays * -1 < 7) {
|
||||
// new code from here :P
|
||||
if (grade.seenDate.isAfter(lastSeenGrade) &&
|
||||
grade.date.difference(DateTime.now()).inDays < 7) {
|
||||
if (grade.date.isAfter(lastSeenGrade) &&
|
||||
DateTime.now().difference(grade.date).inDays < 7) {
|
||||
// send notificiation about new grade
|
||||
AndroidNotificationDetails androidNotificationDetails =
|
||||
AndroidNotificationDetails(
|
||||
@@ -132,6 +131,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "grades"
|
||||
);
|
||||
} else {
|
||||
// multiple users are added, also display student name
|
||||
@@ -149,6 +149,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "grades"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -200,6 +201,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "absences"
|
||||
);
|
||||
} else {
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
@@ -217,6 +219,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "absences"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -271,6 +274,7 @@ class NotificationsHelper {
|
||||
message.author,
|
||||
message.content.replaceAll(RegExp(r'<[^>]*>'), ''),
|
||||
notificationDetails,
|
||||
payload: "messages",
|
||||
);
|
||||
} else {
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
@@ -278,6 +282,7 @@ class NotificationsHelper {
|
||||
"(${currentuserProvider.displayName!}) ${message.author}",
|
||||
message.content.replaceAll(RegExp(r'<[^>]*>'), ''),
|
||||
notificationDetails,
|
||||
payload: "messages",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -297,8 +302,12 @@ class NotificationsHelper {
|
||||
|
||||
DateTime lastSeenLesson = await database.userQuery.lastSeen(
|
||||
userId: currentuserProvider.id!, category: LastSeenCategory.lesson);
|
||||
Lesson? latestLesson;
|
||||
|
||||
for (Lesson lesson in apilessons) {
|
||||
if((lesson.status?.name != "Elmaradt" || lesson.substituteTeacher?.name != "") && lesson.date.isAfter(latestLesson?.start ?? DateTime(1970))) {
|
||||
latestLesson = lesson;
|
||||
}
|
||||
if (lesson.date.isAfter(lastSeenLesson)) {
|
||||
AndroidNotificationDetails androidNotificationDetails =
|
||||
AndroidNotificationDetails(
|
||||
@@ -329,6 +338,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable"
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -345,6 +355,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable"
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -361,11 +372,12 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable"
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (lesson.substituteTeacher?.name != "") {
|
||||
} else if (lesson.substituteTeacher?.name != "" && lesson.substituteTeacher != null) {
|
||||
switch (I18n.localeStr) {
|
||||
case "en_en":
|
||||
{
|
||||
@@ -383,6 +395,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -402,6 +415,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -421,6 +435,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -443,6 +458,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -460,6 +476,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -477,6 +494,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -500,6 +518,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -520,6 +539,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -540,6 +560,7 @@ class NotificationsHelper {
|
||||
],
|
||||
),
|
||||
notificationDetails,
|
||||
payload: "timetable",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -548,7 +569,44 @@ class NotificationsHelper {
|
||||
}
|
||||
}
|
||||
}
|
||||
await database.userStore.storeLastSeen(DateTime.now(),
|
||||
// lesson.date does not contain time, only the date
|
||||
await database.userStore.storeLastSeen(latestLesson?.start ?? DateTime.now(),
|
||||
userId: currentuserProvider.id!, category: LastSeenCategory.lesson);
|
||||
}
|
||||
|
||||
// Called when the user taps a notification
|
||||
void onDidReceiveNotificationResponse(NotificationResponse notificationResponse) async {
|
||||
final String? payload = notificationResponse.payload;
|
||||
if (notificationResponse.payload != null) {
|
||||
debugPrint('notification payload: $payload');
|
||||
}
|
||||
switch(payload) {
|
||||
case "timetable":
|
||||
locator<NavigationService>().navigateTo("timetable");
|
||||
break;
|
||||
case "grades":
|
||||
locator<NavigationService>().navigateTo("grades");
|
||||
break;
|
||||
case "messages":
|
||||
locator<NavigationService>().navigateTo("messages");
|
||||
break;
|
||||
case "absences":
|
||||
locator<NavigationService>().navigateTo("absences");
|
||||
break;
|
||||
case "settings":
|
||||
locator<NavigationService>().navigateTo("settings");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Set all notification categories to seen
|
||||
Future<void> setAllCategoriesSeen(UserProvider userProvider) async {
|
||||
if(userProvider.id != null) {
|
||||
for(LastSeenCategory category in LastSeenCategory.values) {
|
||||
await database.userStore.storeLastSeen(DateTime.now(), userId: userProvider.id!, category: category);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +47,9 @@ extension Localization on String {
|
||||
String get i18n {
|
||||
// very hacky way to get app language in notifications
|
||||
// i18n does not like being in background functions (it cannot retrieve locale sometimes)
|
||||
final DatabaseProvider _databaseProvider = DatabaseProvider();
|
||||
_databaseProvider.init().then((value) {
|
||||
_databaseProvider.query.getSettings(_databaseProvider).then((settings) {
|
||||
final DatabaseProvider databaseProvider = DatabaseProvider();
|
||||
databaseProvider.init().then((value) {
|
||||
databaseProvider.query.getSettings(databaseProvider).then((settings) {
|
||||
return localize(this, _t, locale: "${settings.language}_${settings.language.toUpperCase()}");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,6 +10,8 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:refilc/app.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:refilc/utils/navigation_service.dart';
|
||||
import 'package:refilc/utils/service_locator.dart';
|
||||
import 'package:refilc_mobile_ui/screens/error_screen.dart';
|
||||
import 'package:refilc_mobile_ui/screens/error_report_screen.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
@@ -24,19 +26,22 @@ void main() async {
|
||||
// ignore: deprecated_member_use
|
||||
binding.renderView.automaticSystemUiAdjustment = false;
|
||||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
||||
// navigation
|
||||
setupLocator();
|
||||
|
||||
// Startup
|
||||
Startup startup = Startup();
|
||||
await startup.start();
|
||||
|
||||
// Custom error page
|
||||
ErrorWidget.builder = errorBuilder;
|
||||
|
||||
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask);
|
||||
|
||||
// initialize stripe key
|
||||
stripe.Stripe.publishableKey =
|
||||
'pk_test_51Oo7iUBS0FxsTGxKjGZSQqzDKWHY5ZFYM9XeI0qSdIh2w8jWy6GhHlYpT7GLTzgpl1xhE5YP4BXpA4gMZqPmgMId00cGFYFzbh';
|
||||
|
||||
|
||||
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask);
|
||||
|
||||
// Run App
|
||||
runApp(App(
|
||||
database: startup.database,
|
||||
@@ -58,6 +63,9 @@ class Startup {
|
||||
settings = await database.query.getSettings(database);
|
||||
user = await database.query.getUsers(settings);
|
||||
|
||||
// Set all notification categories to seen to avoid having notifications that the user has already seen in the app
|
||||
NotificationsHelper().setAllCategoriesSeen(user);
|
||||
|
||||
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
|
||||
// Notifications setup
|
||||
if (!kIsWeb) {
|
||||
@@ -118,6 +126,7 @@ class Startup {
|
||||
// Initialize notifications
|
||||
await flutterLocalNotificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
onDidReceiveNotificationResponse: NotificationsHelper().onDidReceiveNotificationResponse,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
9
refilc/lib/utils/navigation_service.dart
Normal file
9
refilc/lib/utils/navigation_service.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class NavigationService {
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
Future<dynamic> navigateTo(String routeName) {
|
||||
return navigatorKey.currentState!.pushNamed(routeName);
|
||||
}
|
||||
}
|
||||
8
refilc/lib/utils/service_locator.dart
Normal file
8
refilc/lib/utils/service_locator.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:refilc/utils/navigation_service.dart';
|
||||
|
||||
GetIt locator = GetIt.instance;
|
||||
|
||||
void setupLocator() {
|
||||
locator.registerLazySingleton(() => NavigationService());
|
||||
}
|
||||
Reference in New Issue
Block a user