add toggle for notifications in settings

This commit is contained in:
hihihaha 2023-06-10 20:34:01 +02:00
parent 3eee2c7a55
commit 07bbafe7dd
2 changed files with 89 additions and 34 deletions

View File

@ -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,7 +23,9 @@ class NotificationsHelper {
SettingsProvider settingsProvider =
await database.query.getSettings(database);
UserProvider userProvider = await database.query.getUsers(settingsProvider);
if(userProvider.id != null) {
if (userProvider.id != null && settingsProvider.notificationsEnabled) {
// refresh grades
final status = StatusProvider();
final kretaClient = KretaClient(
user: userProvider, settings: settingsProvider, status: status);
@ -40,21 +40,35 @@ class NotificationsHelper {
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: 'ticker');
ticker: 'Jegyek');
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);
// 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);
}
}
// set grade seen status
gradeProvider.seenAll();
}
}

View File

@ -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,
),
),
],
),
),