Notifications

This commit is contained in:
2024-04-24 23:19:26 +08:00
parent 0d96a6f9ac
commit 3a2894b533
13 changed files with 362 additions and 10 deletions

View File

@ -31,7 +31,7 @@ class _ChatMaintainerState extends State<ChatMaintainer> {
final notify = ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.connectingServer),
duration: const Duration(days: 1),
duration: const Duration(minutes: 1),
),
);
@ -55,6 +55,7 @@ class _ChatMaintainerState extends State<ChatMaintainer> {
}
},
onError: (_, __) => connect(),
onDone: () => connect(),
);
notify.close();
@ -72,6 +73,8 @@ class _ChatMaintainerState extends State<ChatMaintainer> {
@override
Widget build(BuildContext context) {
ScaffoldMessenger.of(context).clearSnackBars();
return widget.child;
}
}

View File

@ -36,7 +36,7 @@ class _ChatMessageEditorState extends State<ChatMessageEditor> {
builder: (context) => AttachmentEditor(
provider: 'messaging',
current: _attachments,
onUpdate: (value) => _attachments = value,
onUpdate: (value) => setState(() => _attachments = value),
),
);
}

View File

@ -0,0 +1,90 @@
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> {
void connect() {
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>();
nty.fetch(auth);
nty.connect(auth).then((snapshot) {
snapshot!.stream.listen(
(event) {
final result = model.Notification.fromJson(jsonDecode(event));
nty.onRemoteMessage(result);
},
onError: (_, __) => connect(),
onDone: () => connect(),
);
notify.close();
});
}
@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(
showBadge: nty.notifications.isNotEmpty,
position: badge.BadgePosition.custom(top: -2, end: 8),
badgeContent: Text(
nty.notifications.length.toString(),
style: const TextStyle(color: Colors.white),
),
child: IconButton(
icon: const Icon(Icons.notifications),
onPressed: () {
router.pushNamed("notification");
},
),
);
}
}