This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/background_fetch.dart

259 lines
9.9 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:intl/intl.dart';
import 'package:meincantor/timetable.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:meincantor/networking.dart';
Future<void> backgroundFetchTimetable() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? todayTimetable = prefs.getString("todayTimetable");
String? tomorrowTimetable = prefs.getString("tomorrowTimetable");
http.Response todayResponse = await fetchClassTimetable(
"/${DateFormat("yyyyMMdd").format(DateTime.now())}", null);
http.Response tomorrowResponse = await fetchClassTimetable(
"/${DateFormat("yyyyMMdd").format(DateTime.now().add(const Duration(days: 1)))}",
null);
if (todayResponse.statusCode == 200) {
if (todayTimetable != todayResponse.body) {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const AndroidNotificationDetails
androidPlatformChannelSpecificsTodayPlan = AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.today.plan',
'Vertretungsplan für heute',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsTodayPlan =
NotificationDetails(
android: androidPlatformChannelSpecificsTodayPlan);
const AndroidNotificationDetails
androidPlatformChannelSpecificsTodayInfo = AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.today.info',
'Informationen für heute',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsTodayInfo =
NotificationDetails(
android: androidPlatformChannelSpecificsTodayInfo);
List<TimetableLesson> lessonsList =
ClassTimetable.fromJson(jsonDecode(todayResponse.body)).timetable;
List<int> changedLessons = [];
for (var element in lessonsList) {
if (element.info.isNotEmpty) {
changedLessons.add(element.count);
}
}
String subtitle;
if (changedLessons.isNotEmpty && changedLessons.length > 1) {
subtitle = "Änderungen in den Stunden ";
for (var i in changedLessons) {
subtitle += "$i";
if (changedLessons.indexOf(i) != changedLessons.length - 1) {
subtitle += ", ";
}
}
} else if (changedLessons.isNotEmpty && changedLessons.length == 1) {
subtitle = "Änderungen in Stunde ${changedLessons[0]}";
} else {
subtitle = "Keine Änderungen im Plan gefunden!";
}
await flutterLocalNotificationsPlugin.show(
0,
'Neuer Vertretungsplan für heute geladen!',
subtitle,
platformChannelSpecificsTodayPlan,
payload: 'item x');
if ((jsonDecode(todayResponse.body)["info"] as String).isNotEmpty) {
await flutterLocalNotificationsPlugin.show(
1,
'Informationen für heute',
(jsonDecode(todayResponse.body)["info"] as String),
platformChannelSpecificsTodayInfo,
payload: 'item x');
}
prefs.setString("todayTimetable", todayResponse.body);
}
}
if (tomorrowResponse.statusCode == 200) {
if (tomorrowTimetable != tomorrowResponse.body) {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const AndroidNotificationDetails
androidPlatformChannelSpecificsTomorrowPlan =
AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.tomorrow.plan',
'Vertretungsplan für morgen',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsTomorrowPlan =
NotificationDetails(
android: androidPlatformChannelSpecificsTomorrowPlan);
const AndroidNotificationDetails
androidPlatformChannelSpecificsTomorrowInfo =
AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.tomorrow.info',
'Informationen für morgen',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsTomorrowInfo =
NotificationDetails(
android: androidPlatformChannelSpecificsTomorrowInfo);
List<TimetableLesson> lessonsList =
ClassTimetable.fromJson(jsonDecode(tomorrowResponse.body)).timetable;
List<int> changedLessons = [];
for (var element in lessonsList) {
if (element.info.isNotEmpty) {
changedLessons.add(element.count);
}
}
String subtitle;
if (changedLessons.isNotEmpty && changedLessons.length > 1) {
subtitle = "Änderungen in den Stunden ";
for (var i in changedLessons) {
subtitle += "$i";
if (changedLessons.indexOf(i) != changedLessons.length - 1) {
subtitle += ", ";
}
}
} else if (changedLessons.isNotEmpty && changedLessons.length == 1) {
subtitle = "Änderungen in Stunde ${changedLessons[0]}";
} else {
subtitle = "Keine Änderungen im Plan gefunden!";
}
await flutterLocalNotificationsPlugin.show(
2,
'Neuer Vertretungsplan für morgen geladen!',
subtitle,
platformChannelSpecificsTomorrowPlan,
payload: 'item x');
if ((jsonDecode(tomorrowResponse.body)["info"] as String).isNotEmpty) {
await flutterLocalNotificationsPlugin.show(
3,
'Informationen für morgen',
(jsonDecode(tomorrowResponse.body)["info"] as String),
platformChannelSpecificsTomorrowInfo,
payload: 'item x');
}
prefs.setString("tomorrowTimetable", tomorrowResponse.body);
}
}
}
Future<void> backgroundFetchArticles() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? articles = prefs.getString("articles");
String? news = prefs.getString("news");
http.Response fetchedArticles = await getArticles();
http.Response fetchedNews = await getNews();
if (fetchedArticles.statusCode == 200) {
if (articles != fetchedArticles.body) {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const AndroidNotificationDetails androidPlatformChannelSpecificsSZ =
AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.sz', 'Schülerzeitung',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsSZ =
NotificationDetails(android: androidPlatformChannelSpecificsSZ);
String subtitle;
List listFetchedArticles = jsonDecode(fetchedArticles.body)["data"];
if (articles != null && articles.isNotEmpty) {
List listSavedArticles = jsonDecode(articles)["data"];
int diff = listFetchedArticles.length - listSavedArticles.length;
diff == 1
? subtitle = "1 neuer Artikel!"
: diff > 1
? subtitle = "$diff neue Artikel!"
: subtitle = "Fehler beim Ermitteln der Änderungen!";
} else {
int len = listFetchedArticles.length;
len == 1
? subtitle = "1 neuer Artikel!"
: len > 1
? subtitle = "$len neue Artikel!"
: subtitle = "Fehler beim Ermitteln der Änderungen!";
}
await flutterLocalNotificationsPlugin.show(
4,
'Neuer Inhalt von der Schülerzeitung verfügbar!',
subtitle,
platformChannelSpecificsSZ,
payload: 'item x');
prefs.setString("articles", fetchedArticles.body);
}
}
if (fetchedNews.statusCode == 200) {
if (news != fetchedNews.body) {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const AndroidNotificationDetails androidPlatformChannelSpecificsNews =
AndroidNotificationDetails(
'de.cantorgymnasium.meincantor.news', 'Aktuelles',
channelDescription: '',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecificsNews =
NotificationDetails(android: androidPlatformChannelSpecificsNews);
String subtitle;
List listFetchedNews = jsonDecode(fetchedNews.body)["data"];
if (news != null && news.isNotEmpty) {
List listSavedNews = jsonDecode(news)["data"];
int diff = listFetchedNews.length - listSavedNews.length;
diff == 1
? subtitle = "1 neuer Artikel!"
: diff > 1
? subtitle = "$diff neue Artikel!"
: subtitle = "Fehler beim Ermitteln der Änderungen!";
} else {
int len = listFetchedNews.length;
len == 1
? subtitle = "1 neuer Artikel!"
: len > 1
? subtitle = "$len neue Artikel!"
: subtitle = "Fehler beim Ermitteln der Änderungen!";
}
await flutterLocalNotificationsPlugin.show(
5,
'Neue Informationen verfügbar!',
subtitle,
platformChannelSpecificsNews,
payload: 'item x');
prefs.setString("news", fetchedNews.body);
}
}
}