Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new notification for classes #1420

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher.dart';
import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher_new_api.dart';
import 'package:uni/controller/local_storage/database/app_lectures_database.dart';
import 'package:uni/model/entities/lecture.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/providers/state_providers.dart';
import 'package:uni/session/flows/base/session.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tzData;

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();


void initializeNotifications() {
const androidInitialization = AndroidInitializationSettings('app_icon');
const iOSInitialization = DarwinInitializationSettings();
final initializationSettings = InitializationSettings(
android: androidInitialization,
iOS: iOSInitialization,
);

flutterLocalNotificationsPlugin.initialize(initializationSettings);

tzData.initializeTimeZones();
}


class LectureProvider extends StateProviderNotifier<List<Lecture>> {
LectureProvider() : super(cacheDuration: const Duration(hours: 6));


@override
Future<List<Lecture>> loadFromStorage(StateProviders stateProviders) async {
final db = AppLecturesDatabase();
return db.lectures();
}

@override
Future<List<Lecture>> loadFromRemote(StateProviders stateProviders) async {
return fetchUserLectures(
stateProviders.sessionProvider.state!,
);
}

Future<List<Lecture>> fetchUserLectures(
Session session, {
ScheduleFetcher? fetcher,
}) async {
final lectures = await getLecturesFromFetcherOrElse(fetcher, session);

final db = AppLecturesDatabase();
await db.saveIfPersistentSession(lectures);

return lectures;
}

Future<List<Lecture>> getLecturesFromFetcherOrElse(
ScheduleFetcher? fetcher,
Session session,
) =>
fetcher?.getLectures(session) ?? getLectures(session);

Future<List<Lecture>> getLectures(Session session) {
return ScheduleFetcherNewApi().getLectures(session).catchError(
(e) => <Lecture>[],
);
}
}

void scheduleLectureNotifications(List<Lecture> lectures, FlutterLocalNotificationsPlugin localNotificationsPlugin) {
for (Lecture lecture in lectures) {
DateTime lectureStart = lecture.startTime;
DateTime notificationTime = lectureStart.subtract(Duration(minutes: 15));

if (notificationTime.isAfter(DateTime.now())) {
_scheduleNotification(notificationTime, lecture, localNotificationsPlugin);
}
}
}


Future<void> _scheduleNotification(DateTime notificationTime, Lecture lecture, FlutterLocalNotificationsPlugin localNotificationsPlugin) async {
final location = tz.getLocation('Europe/Lisbon');
final zonedNotificationTime = tz.TZDateTime.from(notificationTime, location);

const androidDetails = AndroidNotificationDetails(
'lecture_notification_channel',
'Lecture Notifications',
importance: Importance.high,
priority: Priority.high,
showWhen: false
);
const iOSDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
interruptionLevel: InterruptionLevel.active,
);

const notificationDetails = NotificationDetails(
android: androidDetails,
iOS: iOSDetails,
);

await localNotificationsPlugin.zonedSchedule(
0,
'Upcoming Lecture: ${lecture.subject}',
'Your lecture ${lecture.subject} starts in 15 minutes!',
zonedNotificationTime,
notificationDetails,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.wallClockTime,
);
}


void onLecturesFetched(List<Lecture> lectures) {
scheduleLectureNotifications(lectures, flutterLocalNotificationsPlugin);
}
1 change: 1 addition & 0 deletions packages/uni_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies:
upgrader: ^10.3.0
url_launcher: ^6.2.2
workmanager: ^0.5.2
timezone: ^0.9.0

dev_dependencies:
build_runner: ^2.4.8
Expand Down
Loading