diff --git a/lib/controllers/settings_sections.dart b/lib/controllers/settings_sections.dart index fea0d26..8d67435 100644 --- a/lib/controllers/settings_sections.dart +++ b/lib/controllers/settings_sections.dart @@ -11,6 +11,7 @@ import "package:nexus/controllers/client_state.dart"; import "package:nexus/controllers/settings.dart"; import "package:nexus/controllers/unified_push.dart"; import "package:nexus/controllers/spec_versions.dart"; +import "package:nexus/controllers/unified_push_allowed.dart"; import "package:nexus/models/account_data.dart"; import "package:nexus/models/settings_category.dart"; import "package:nexus/main.dart"; @@ -100,7 +101,6 @@ class SettingsSectionsController title: "Notifications", icon: Icons.notifications, settings: .new([ - // TODO: Disable if not supported .new( title: "Push notifications", description: "Enable push notifications using Web Push", @@ -109,26 +109,43 @@ class SettingsSectionsController final pusherRegistered = ref.watch( UnifiedPushController.provider, ); + final unifiedPushAllowed = ref.watch( + UnifiedPushAllowedController.provider, + ); return SwitchListTile( title: Text(title), - subtitle: Text(description), + subtitle: Text( + unifiedPushAllowed.maybeWhen( + data: (data) => data, + orElse: () => null, + ) ?? + description, + ), secondary: Icon(icon), value: pusherRegistered.maybeWhen( data: (value) => value, orElse: () => false, ), - onChanged: pusherRegistered.maybeWhen( - data: (_) => - (value) => value - ? ref - .watch( - UnifiedPushController.provider.notifier, - ) - .register() - .onError(showError) - : /*deregeister*/ null, - orElse: () => null, - ), + onChanged: + unifiedPushAllowed.maybeWhen( + data: (data) => data == null, + orElse: () => false, + ) + ? pusherRegistered.maybeWhen( + data: (_) => + (value) => value + ? ref + .watch( + UnifiedPushController + .provider + .notifier, + ) + .register() + .onError(showError) + : /*deregeister*/ null, + orElse: () => null, + ) + : null, ); }, ), diff --git a/lib/controllers/unified_push.dart b/lib/controllers/unified_push.dart index c7a6cde..b4aa107 100644 --- a/lib/controllers/unified_push.dart +++ b/lib/controllers/unified_push.dart @@ -78,8 +78,9 @@ class UnifiedPushController extends AsyncNotifier { if (clientState?.deviceId == null) ref.invalidateSelf(); final alreadyRegistered = early ? true : await future; - final client = ref.watch(ClientController.provider.notifier); - final capabilities = await client.getCapabilities(); + final capabilities = await ref + .watch(ClientController.provider.notifier) + .getCapabilities(); if (capabilities.webpush?.vapid == null) { throw UnsupportedError( diff --git a/lib/controllers/unified_push_allowed.dart b/lib/controllers/unified_push_allowed.dart new file mode 100644 index 0000000..4f925cf --- /dev/null +++ b/lib/controllers/unified_push_allowed.dart @@ -0,0 +1,33 @@ +import "dart:io"; + +import "package:flutter_riverpod/flutter_riverpod.dart"; +import "package:nexus/controllers/client.dart"; +import "package:unifiedpush/unifiedpush.dart"; + +class UnifiedPushAllowedController extends AsyncNotifier { + @override + Future build() async { + if (!await UnifiedPush.tryUseCurrentOrDefaultDistributor()) { + return "No valid distributors found. Try installing ${Platform.isLinux + ? "KUnifiedPush" + : Platform.isAndroid + ? "FCM or NTFY" + : "NTFY"}."; + } + + final capabilities = await ref + .watch(ClientController.provider.notifier) + .getCapabilities(); + + if (capabilities.webpush?.vapid == null) { + return "Your homeserver does not support MSC4174 (Web Push), and therefore cannot send notifications to Nexus."; + } + + return null; + } + + static final provider = + AsyncNotifierProvider.autoDispose( + UnifiedPushAllowedController.new, + ); +}