Merge branch 'krille/fix-desktop-notifications' into 'master'

fix: Desktop notifications

See merge request ChristianPauly/fluffychat-flutter!139
This commit is contained in:
Christian Pauly 2020-08-22 13:30:05 +00:00
commit 7c980d9474

View file

@ -102,6 +102,8 @@ class MatrixState extends State<Matrix> {
StreamSubscription onKeyVerificationRequestSub; StreamSubscription onKeyVerificationRequestSub;
StreamSubscription onJitsiCallSub; StreamSubscription onJitsiCallSub;
StreamSubscription onNotification; StreamSubscription onNotification;
StreamSubscription<html.Event> onFocusSub;
StreamSubscription<html.Event> onBlurSub;
void onJitsiCall(EventUpdate eventUpdate) { void onJitsiCall(EventUpdate eventUpdate) {
final event = Event.fromJson( final event = Event.fromJson(
@ -158,7 +160,10 @@ class MatrixState extends State<Matrix> {
return; return;
} }
bool webHasFocus = true;
void _showWebNotification(EventUpdate eventUpdate) async { void _showWebNotification(EventUpdate eventUpdate) async {
if (webHasFocus && activeRoomId == eventUpdate.roomID) return;
final room = client.getRoomById(eventUpdate.roomID); final room = client.getRoomById(eventUpdate.roomID);
if (room.notificationCount == 0) return; if (room.notificationCount == 0) return;
final event = Event.fromJson(eventUpdate.content, room); final event = Event.fromJson(eventUpdate.content, room);
@ -261,11 +266,13 @@ class MatrixState extends State<Matrix> {
}); });
} }
if (kIsWeb) { if (kIsWeb) {
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
client.onSync.stream.first.then((s) { client.onSync.stream.first.then((s) {
html.Notification.requestPermission(); html.Notification.requestPermission();
onNotification ??= client.onEvent.stream onNotification ??= client.onEvent.stream
.where((e) => .where((e) =>
e.roomID != activeRoomId &&
e.type == 'timeline' && e.type == 'timeline' &&
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted] [EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(e.eventType) && .contains(e.eventType) &&
@ -282,6 +289,8 @@ class MatrixState extends State<Matrix> {
onKeyVerificationRequestSub?.cancel(); onKeyVerificationRequestSub?.cancel();
onJitsiCallSub?.cancel(); onJitsiCallSub?.cancel();
onNotification?.cancel(); onNotification?.cancel();
onFocusSub?.cancel();
onBlurSub?.cancel();
super.dispose(); super.dispose();
} }