Solian/lib/widgets/notification_notifier.dart

75 lines
2.0 KiB
Dart
Raw Normal View History

2024-04-24 15:19:26 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart';
2024-05-12 12:15:12 +00:00
import 'package:solian/providers/keypair.dart';
2024-04-24 15:19:26 +00:00
import 'package:solian/providers/notify.dart';
import 'package:solian/router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:badges/badges.dart' as badge;
2024-05-12 12:15:12 +00:00
class NotificationButton extends StatefulWidget {
const NotificationButton({super.key});
2024-04-24 15:19:26 +00:00
@override
2024-05-12 12:15:12 +00:00
State<NotificationButton> createState() => _NotificationButtonState();
2024-04-24 15:19:26 +00:00
}
2024-05-12 12:15:12 +00:00
class _NotificationButtonState extends State<NotificationButton> {
2024-04-27 05:12:26 +00:00
void connect() async {
2024-05-12 12:15:12 +00:00
final auth = context.read<AuthProvider>();
final nty = context.read<NotifyProvider>();
final keypair = context.read<KeypairProvider>();
if (nty.isOpened) return;
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),
),
);
2024-04-27 05:12:26 +00:00
if (await auth.isAuthorized()) {
2024-05-11 15:22:35 +00:00
if (auth.client == null) {
await auth.loadClient();
}
2024-04-27 05:12:26 +00:00
nty.fetch(auth);
2024-05-12 12:15:12 +00:00
keypair.channel = await nty.connect(
auth,
onKexRequest: keypair.provideKeypair,
onKexProvide: keypair.receiveKeypair,
);
2024-04-27 05:12:26 +00:00
}
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() {
super.initState();
2024-05-12 12:15:12 +00:00
Future.delayed(Duration.zero, () => connect());
2024-04-24 15:19:26 +00:00
}
@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: () {
SolianRouter.router.pushNamed('notification');
2024-04-24 15:19:26 +00:00
},
),
);
}
}