2024-04-24 15:19:26 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/notify.dart';
|
|
|
|
import 'package:solian/router.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:solian/models/notification.dart' as model;
|
|
|
|
import 'package:badges/badges.dart' as badge;
|
|
|
|
|
|
|
|
class NotificationNotifier extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const NotificationNotifier({super.key, required this.child});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<NotificationNotifier> createState() => _NotificationNotifierState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NotificationNotifierState extends State<NotificationNotifier> {
|
2024-04-27 05:12:26 +00:00
|
|
|
void connect() async {
|
2024-04-24 15:19:26 +00:00
|
|
|
final notify = ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.connectingServer),
|
|
|
|
duration: const Duration(minutes: 1),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
final nty = context.read<NotifyProvider>();
|
|
|
|
|
2024-04-27 05:12:26 +00:00
|
|
|
if (await auth.isAuthorized()) {
|
|
|
|
nty.fetch(auth);
|
|
|
|
nty.connect(auth).then((snapshot) {
|
|
|
|
snapshot!.stream.listen(
|
|
|
|
(event) {
|
|
|
|
final result = model.Notification.fromJson(jsonDecode(event));
|
|
|
|
nty.onRemoteMessage(result);
|
2024-04-28 13:49:03 +00:00
|
|
|
nty.notifyMessage(result.subject, result.content);
|
2024-04-27 05:12:26 +00:00
|
|
|
},
|
|
|
|
onError: (_, __) => connect(),
|
|
|
|
onDone: () => connect(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2024-04-24 15:19:26 +00:00
|
|
|
|
2024-04-27 05:12:26 +00:00
|
|
|
notify.close();
|
2024-04-24 15:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
connect();
|
|
|
|
});
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return widget.child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationButton extends StatefulWidget {
|
|
|
|
const NotificationButton({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<NotificationButton> createState() => _NotificationButtonState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NotificationButtonState extends State<NotificationButton> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final nty = context.watch<NotifyProvider>();
|
|
|
|
|
|
|
|
return badge.Badge(
|
2024-04-28 13:49:03 +00:00
|
|
|
showBadge: nty.unreadAmount > 0,
|
2024-04-24 15:19:26 +00:00
|
|
|
position: badge.BadgePosition.custom(top: -2, end: 8),
|
|
|
|
badgeContent: Text(
|
2024-04-28 13:49:03 +00:00
|
|
|
nty.unreadAmount.toString(),
|
2024-04-24 15:19:26 +00:00
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
child: IconButton(
|
|
|
|
icon: const Icon(Icons.notifications),
|
|
|
|
onPressed: () {
|
|
|
|
router.pushNamed("notification");
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|