refactor: cleanup notifications bbutton signer requests

This commit is contained in:
2025-05-28 13:17:45 +01:00
parent 74c3ad9b9c
commit 0295d49077
2 changed files with 80 additions and 43 deletions

View File

@ -1,4 +1,3 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:zap_stream_flutter/notifications.dart';
import 'package:zap_stream_flutter/theme.dart';
@ -16,47 +15,33 @@ class NotificationsButtonWidget extends StatefulWidget {
class _NotificationsButtonWidget extends State<NotificationsButtonWidget> {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: () async {
if (notifications?.authorizationStatus ==
AuthorizationStatus.authorized) {
final n = getNotificationService();
return await n?.getWatchedKeys();
} else {
return null;
}
}(),
builder: (context, state) {
if (state.data == null) {
return SizedBox.shrink();
} else {
final isNotified = (state.data ?? []).contains(
widget.stream.info.host,
);
return IconButton(
iconSize: 20,
onPressed: () async {
final n = getNotificationService();
if (n == null) return;
return ValueListenableBuilder(
valueListenable: notifications,
builder: (context, state, _) {
final isNotified = (state?.notifyKeys ?? []).contains(
widget.stream.info.host,
);
return IconButton(
iconSize: 20,
onPressed: () async {
final n = getNotificationService();
if (n == null) return;
if (isNotified) {
await n.removeWatchPubkey(widget.stream.info.host);
} else {
await n.watchPubkey(widget.stream.info.host, [30311]);
}
setState(() {
// reload widget
});
},
style: ButtonStyle(
padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
backgroundColor: WidgetStateColor.resolveWith((_) => LAYER_2),
),
icon: Icon(
isNotified ? Icons.notifications_off : Icons.notification_add,
),
);
}
if (isNotified) {
await n.removeWatchPubkey(widget.stream.info.host);
} else {
await n.watchPubkey(widget.stream.info.host, [30311]);
}
await notifications.reload();
},
style: ButtonStyle(
padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
backgroundColor: WidgetStateColor.resolveWith((_) => LAYER_2),
),
icon: Icon(
isNotified ? Icons.notifications_off : Icons.notification_add,
),
);
},
);
}