add UnifiedPushAllowedController

This commit is contained in:
Henry Hiles 2026-08-23 14:26:51 -04:00
commit 7218eb5195
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
3 changed files with 67 additions and 16 deletions

View file

@ -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,
);
},
),

View file

@ -78,8 +78,9 @@ class UnifiedPushController extends AsyncNotifier<bool> {
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(

View file

@ -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<String?> {
@override
Future<String?> 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, String?>(
UnifiedPushAllowedController.new,
);
}