mirror of
https://github.com/nostrlabs-io/zap-stream-flutter.git
synced 2025-06-18 04:38:50 +00:00
refactor: cleanup notifications bbutton signer requests
This commit is contained in:
@ -6,6 +6,7 @@ import 'package:convert/convert.dart';
|
|||||||
import 'package:crypto/crypto.dart';
|
import 'package:crypto/crypto.dart';
|
||||||
import 'package:firebase_core/firebase_core.dart';
|
import 'package:firebase_core/firebase_core.dart';
|
||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:ndk/ndk.dart';
|
import 'package:ndk/ndk.dart';
|
||||||
@ -136,7 +137,54 @@ Notepush? getNotificationService() {
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationSettings? notifications;
|
class NotificationsState {
|
||||||
|
final AuthorizationStatus status;
|
||||||
|
final List<String> notifyKeys;
|
||||||
|
|
||||||
|
NotificationsState({required this.status, required this.notifyKeys});
|
||||||
|
|
||||||
|
NotificationsState copyWith({
|
||||||
|
AuthorizationStatus? newStatus,
|
||||||
|
List<String>? newNotifyKeys,
|
||||||
|
}) {
|
||||||
|
return NotificationsState(
|
||||||
|
status: newStatus ?? status,
|
||||||
|
notifyKeys: newNotifyKeys ?? notifyKeys,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<NotificationsState> init(AuthorizationStatus status) async {
|
||||||
|
if (status == AuthorizationStatus.authorized) {
|
||||||
|
final svc = getNotificationService();
|
||||||
|
if (svc != null) {
|
||||||
|
try {
|
||||||
|
final keys = await svc.getWatchedKeys();
|
||||||
|
return NotificationsState(status: status, notifyKeys: keys);
|
||||||
|
} catch (e) {
|
||||||
|
developer.log("Failed to init NotificationsState: $e");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NotificationsState(status: status, notifyKeys: []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NotificationsStore extends ValueNotifier<NotificationsState?> {
|
||||||
|
NotificationsStore(super.value);
|
||||||
|
|
||||||
|
Future<void> reload() async {
|
||||||
|
if (value != null && value!.status == AuthorizationStatus.authorized) {
|
||||||
|
final svc = getNotificationService();
|
||||||
|
if (svc != null) {
|
||||||
|
final keys = await svc.getWatchedKeys();
|
||||||
|
value = value!.copyWith(newNotifyKeys: keys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// global notifications store
|
||||||
|
final notifications = NotificationsStore(null);
|
||||||
|
|
||||||
Future<void> setupNotifications() async {
|
Future<void> setupNotifications() async {
|
||||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||||
@ -177,7 +225,7 @@ Future<void> setupNotifications() async {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
notifications = await fbase.requestPermission(provisional: true);
|
final settings = await fbase.requestPermission(provisional: true);
|
||||||
await fbase.setAutoInitEnabled(true);
|
await fbase.setAutoInitEnabled(true);
|
||||||
await fbase.setForegroundNotificationPresentationOptions(
|
await fbase.setForegroundNotificationPresentationOptions(
|
||||||
alert: true,
|
alert: true,
|
||||||
@ -209,5 +257,9 @@ Future<void> setupNotifications() async {
|
|||||||
}
|
}
|
||||||
await pusher.register(fcmToken);
|
await pusher.register(fcmToken);
|
||||||
await pusher.setNotificationSettings(fcmToken, [30_311]);
|
await pusher.setNotificationSettings(fcmToken, [30_311]);
|
||||||
|
|
||||||
|
notifications.value = await NotificationsState.init(
|
||||||
|
settings.authorizationStatus,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:zap_stream_flutter/notifications.dart';
|
import 'package:zap_stream_flutter/notifications.dart';
|
||||||
import 'package:zap_stream_flutter/theme.dart';
|
import 'package:zap_stream_flutter/theme.dart';
|
||||||
@ -16,21 +15,10 @@ class NotificationsButtonWidget extends StatefulWidget {
|
|||||||
class _NotificationsButtonWidget extends State<NotificationsButtonWidget> {
|
class _NotificationsButtonWidget extends State<NotificationsButtonWidget> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder(
|
return ValueListenableBuilder(
|
||||||
future: () async {
|
valueListenable: notifications,
|
||||||
if (notifications?.authorizationStatus ==
|
builder: (context, state, _) {
|
||||||
AuthorizationStatus.authorized) {
|
final isNotified = (state?.notifyKeys ?? []).contains(
|
||||||
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,
|
widget.stream.info.host,
|
||||||
);
|
);
|
||||||
return IconButton(
|
return IconButton(
|
||||||
@ -44,9 +32,7 @@ class _NotificationsButtonWidget extends State<NotificationsButtonWidget> {
|
|||||||
} else {
|
} else {
|
||||||
await n.watchPubkey(widget.stream.info.host, [30311]);
|
await n.watchPubkey(widget.stream.info.host, [30311]);
|
||||||
}
|
}
|
||||||
setState(() {
|
await notifications.reload();
|
||||||
// reload widget
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
style: ButtonStyle(
|
style: ButtonStyle(
|
||||||
padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
|
padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
|
||||||
@ -56,7 +42,6 @@ class _NotificationsButtonWidget extends State<NotificationsButtonWidget> {
|
|||||||
isNotified ? Icons.notifications_off : Icons.notification_add,
|
isNotified ? Icons.notifications_off : Icons.notification_add,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user