forked from firka/student-legacy
add toggle for notifications in settings
This commit is contained in:
parent
3eee2c7a55
commit
07bbafe7dd
@ -7,16 +7,14 @@ 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/theme/colors/accent.dart';
|
||||
import 'package:filcnaplo_kreta_api/client/client.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/category.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/grade.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/subject.dart';
|
||||
import 'package:filcnaplo_kreta_api/providers/grade_provider.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
class NotificationsHelper {
|
||||
void backgroundJob() async {
|
||||
// initialize providers
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
DatabaseProvider database = DatabaseProvider();
|
||||
@ -25,37 +23,53 @@ class NotificationsHelper {
|
||||
SettingsProvider settingsProvider =
|
||||
await database.query.getSettings(database);
|
||||
UserProvider userProvider = await database.query.getUsers(settingsProvider);
|
||||
if(userProvider.id != null) {
|
||||
final status = StatusProvider();
|
||||
final kretaClient = KretaClient(
|
||||
user: userProvider, settings: settingsProvider, status: status);
|
||||
kretaClient.refreshLogin();
|
||||
GradeProvider gradeProvider = GradeProvider(
|
||||
settings: settingsProvider,
|
||||
user: userProvider,
|
||||
database: database,
|
||||
kreta: kretaClient);
|
||||
gradeProvider.fetch();
|
||||
List<Grade> grades =
|
||||
await database.userQuery.getGrades(userId: userProvider.id ?? "");
|
||||
DateTime lastSeenGrade =
|
||||
await database.userQuery.lastSeenGrade(userId: userProvider.id ?? "");
|
||||
for (Grade grade in grades) {
|
||||
if (grade.seenDate.isAfter(lastSeenGrade)) {
|
||||
const AndroidNotificationDetails androidNotificationDetails =
|
||||
AndroidNotificationDetails('GRADES', 'Jegyek',
|
||||
channelDescription: 'Értesítés jegyek beírásakor',
|
||||
importance: Importance.max,
|
||||
priority: Priority.max,
|
||||
color: const Color(0xFF3D7BF4),
|
||||
ticker: 'ticker');
|
||||
const NotificationDetails notificationDetails =
|
||||
NotificationDetails(android: androidNotificationDetails);
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
Random().nextInt(432234*2), "title".i18n, "body".i18n.fill([grade.value.value.toString(), grade.subject.name.toString()]), notificationDetails);
|
||||
|
||||
if (userProvider.id != null && settingsProvider.notificationsEnabled) {
|
||||
// refresh grades
|
||||
final status = StatusProvider();
|
||||
final kretaClient = KretaClient(
|
||||
user: userProvider, settings: settingsProvider, status: status);
|
||||
kretaClient.refreshLogin();
|
||||
GradeProvider gradeProvider = GradeProvider(
|
||||
settings: settingsProvider,
|
||||
user: userProvider,
|
||||
database: database,
|
||||
kreta: kretaClient);
|
||||
gradeProvider.fetch();
|
||||
List<Grade> grades =
|
||||
await database.userQuery.getGrades(userId: userProvider.id ?? "");
|
||||
DateTime lastSeenGrade =
|
||||
await database.userQuery.lastSeenGrade(userId: userProvider.id ?? "");
|
||||
|
||||
// loop through grades and see which hasn't been seen yet
|
||||
for (Grade grade in grades) {
|
||||
if (grade.seenDate.isAfter(lastSeenGrade)) {
|
||||
// send notificiation about new grade
|
||||
const AndroidNotificationDetails androidNotificationDetails =
|
||||
AndroidNotificationDetails('GRADES', 'Jegyek',
|
||||
channelDescription: 'Értesítés jegyek beírásakor',
|
||||
importance: Importance.max,
|
||||
priority: Priority.max,
|
||||
color: const Color(0xFF3D7BF4),
|
||||
ticker: 'Jegyek');
|
||||
const NotificationDetails notificationDetails =
|
||||
NotificationDetails(android: androidNotificationDetails);
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
// probably shouldn't use a random int
|
||||
Random().nextInt(432234 * 2),
|
||||
"title".i18n,
|
||||
"body".i18n.fill([
|
||||
grade.value.value.toString(),
|
||||
grade.subject.isRenamed &&
|
||||
settingsProvider.renamedSubjectsEnabled
|
||||
? grade.subject.renamedTo!
|
||||
: grade.subject.name
|
||||
]),
|
||||
notificationDetails);
|
||||
}
|
||||
}
|
||||
}
|
||||
gradeProvider.seenAll();
|
||||
// set grade seen status
|
||||
gradeProvider.seenAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ class _SettingsScreenState extends State<SettingsScreen>
|
||||
void initState() {
|
||||
super.initState();
|
||||
Future.delayed(Duration.zero, () {
|
||||
futureRelease = Provider.of<UpdateProvider>(context).installedVersion();
|
||||
futureRelease = Provider.of<UpdateProvider>(context, listen: false).installedVersion();
|
||||
});
|
||||
_hideContainersController = AnimationController(
|
||||
vsync: this, duration: const Duration(milliseconds: 200));
|
||||
@ -434,6 +434,47 @@ class _SettingsScreenState extends State<SettingsScreen>
|
||||
activeColor: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
),
|
||||
PanelButton(
|
||||
padding: const EdgeInsets.only(left: 14.0),
|
||||
title: Row(children: [
|
||||
Text(
|
||||
"notifications".i18n,
|
||||
style: TextStyle(
|
||||
color: AppColors.of(context).text.withOpacity(
|
||||
settings.notificationsEnabled ? 1.0 : .5)),
|
||||
),
|
||||
SizedBox(
|
||||
width: 5,
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
child: AnimatedContainer(
|
||||
duration: Duration(milliseconds: 200),
|
||||
child: Padding(
|
||||
padding:
|
||||
const EdgeInsets.only(left: 10, right: 10),
|
||||
child: Center(
|
||||
child: Text("BETA",
|
||||
style: TextStyle(fontSize: 9.1, color: AppColors.of(context).text.withOpacity(settings.notificationsEnabled ? 1.0 : .5), overflow: TextOverflow.ellipsis))),
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.of(context).filc.withOpacity(settings.notificationsEnabled ? 1.0 : .5),
|
||||
borderRadius: BorderRadius.circular(40)),
|
||||
),
|
||||
)
|
||||
]),
|
||||
leading: settings.notificationsEnabled
|
||||
? const Icon(FeatherIcons.messageSquare)
|
||||
: Icon(FeatherIcons.messageSquare,
|
||||
color:
|
||||
AppColors.of(context).text.withOpacity(.25)),
|
||||
trailing: Switch(
|
||||
onChanged: (v) =>
|
||||
settings.update(notificationsEnabled: v),
|
||||
value: settings.notificationsEnabled,
|
||||
activeColor: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user