Gate push toggle on backend notification method

Add a GET /api/config endpoint exposing the front-end-facing backend
configuration (currently the notification delivery method, grouped under
a nested "notifications" object so the shape can grow). The settings
screen fetches it on init and only shows the per-device push toggle when
the backend method is "push" (and the platform supports push, which keeps
it off the browser).

Also fold in related push-notifications cleanups: fix the Android app
label ("frontend" -> "OOTT") so the notification permission dialog reads
correctly, remove the completed push_notifications.md plan, and update
TODO.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-08 17:26:53 -04:00
co-authored by Claude Opus 4.8
parent bef1a2987d
commit 1d85ec6f83
14 changed files with 209 additions and 299 deletions
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="frontend"
android:label="OOTT"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
+16
View File
@@ -0,0 +1,16 @@
/// Front-end-facing backend configuration, served by `GET /api/config`.
///
/// Only the settings the UI needs to adapt itself are exposed. Today that is the
/// notification delivery method, which the settings screen uses to decide
/// whether the per-device push toggle applies.
class AppConfig {
AppConfig({required this.notificationMethod});
/// The backend's configured delivery method (e.g. `push`, `pushover`, `none`).
final String notificationMethod;
factory AppConfig.fromJson(Map<String, dynamic> json) {
final notifications = json['notifications'] as Map<String, dynamic>;
return AppConfig(notificationMethod: notifications['method'] as String);
}
}
+18 -1
View File
@@ -32,6 +32,9 @@ class _SettingsState extends State<Settings> {
late final PushService _pushService;
bool _pushEnabled = false;
bool _pushBusy = false;
// Whether the backend delivers notifications via push. The per-device push
// toggle only makes sense then, so it stays hidden until this is confirmed.
bool _pushMethodActive = false;
final _formKey = GlobalKey<FormState>();
@@ -46,6 +49,20 @@ class _SettingsState extends State<Settings> {
_selectedTheme = context.read<AppState>().themeKey;
_pushService = widget.pushService ?? FirebasePushService();
_pushEnabled = PrefUtil.getValue('push_enabled', false) as bool;
_loadConfig();
}
// Learn the backend's notification method so the push toggle is only shown
// when the backend actually delivers via push. Failures (e.g. the backend is
// unreachable, as on first run) just leave the toggle hidden.
Future<void> _loadConfig() async {
try {
final config = await BackendAPI.instance.getConfig();
if (!mounted) return;
setState(() => _pushMethodActive = config.notificationMethod == 'push');
} catch (e) {
debugPrint('Failed to load backend config: $e');
}
}
@override
@@ -245,7 +262,7 @@ class _SettingsState extends State<Settings> {
if (value != null) setState(() => _selectedTheme = value);
},
),
if (_pushService.isSupported) ...[
if (_pushService.isSupported && _pushMethodActive) ...[
const SizedBox(height: Insets.sm),
SwitchListTile(
contentPadding: EdgeInsets.zero,
@@ -0,0 +1,9 @@
part of '../oott_api.dart';
/// Front-end configuration endpoint: the subset of backend settings the UI needs
/// to adapt itself (currently the notification delivery method).
extension ConfigApi on BackendAPI {
/// Fetches the backend's UI-facing configuration. Used by the settings screen
/// to show push controls only when the backend delivers via push.
Future<AppConfig> getConfig() => _getModel('/config', AppConfig.fromJson);
}
+2
View File
@@ -9,6 +9,7 @@ import 'api/dio_config.dart';
import 'backend_reachability.dart';
import 'pref_utils.dart';
import '../model/active_scanner_status.dart';
import '../model/app_config.dart';
import '../model/passive_scanner_status.dart';
import '../model/device.dart';
import '../model/device_event.dart';
@@ -18,6 +19,7 @@ import '../model/notification.dart';
export 'api/api_error.dart';
part 'api/oott_api_config.dart';
part 'api/oott_api_devices.dart';
part 'api/oott_api_scanners.dart';
part 'api/oott_api_notifications.dart';
+26
View File
@@ -0,0 +1,26 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/utils/oott_api.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import '../helpers/backend_test_harness.dart';
void main() {
late DioAdapter adapter;
setUp(() async {
adapter = await setUpBackendForTest();
});
test('getConfig GETs /config and decodes the notification method', () async {
adapter.onGet(
'/config',
(server) => server.reply(200, {
'notifications': {'method': 'push'},
}),
);
final config = await BackendAPI.instance.getConfig();
expect(config.notificationMethod, 'push');
});
}
+53 -8
View File
@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/settings/settings.dart';
import 'package:frontend/utils/pref_utils.dart';
import 'package:frontend/utils/push_service.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import '../helpers/backend_test_harness.dart';
import '../helpers/pump_app.dart';
@@ -35,8 +36,10 @@ class _FakePushService implements PushService {
const _toggleText = 'Push notifications on this device';
void main() {
late DioAdapter adapter;
setUp(() async {
await setUpBackendForTest(
adapter = await setUpBackendForTest(
prefs: {
'base_url': 'http://my.server/api',
'api_key': XOR().xorEncode('topsecret'),
@@ -48,24 +51,59 @@ void main() {
await PrefUtil.setValue('push_enabled', false);
});
testWidgets('shows the push toggle on a supported platform', (tester) async {
// Stubs the backend config endpoint the settings screen reads on load.
void stubNotificationMethod(String method) {
adapter.onGet(
'/config',
(server) => server.reply(200, {
'notifications': {'method': method},
}),
);
}
// Pumps frames so the async config load (resolved via Dio's zero-duration
// timer) settles, for assertions that expect the toggle to be absent.
Future<void> settleConfig(WidgetTester tester) async {
for (var i = 0; i < 10; i++) {
await tester.pump(const Duration(milliseconds: 10));
}
}
testWidgets('shows the push toggle when the backend method is push', (
tester,
) async {
stubNotificationMethod('push');
await pumpScreen(tester, Settings(pushService: _FakePushService()));
await pumpUntilFound(tester, find.text(_toggleText));
expect(find.text(_toggleText), findsOneWidget);
});
testWidgets('hides the push toggle when push is unsupported', (tester) async {
stubNotificationMethod('push');
await pumpScreen(
tester,
Settings(pushService: _FakePushService(supported: false)),
);
await settleConfig(tester);
expect(find.text(_toggleText), findsNothing);
});
testWidgets('hides the push toggle when the backend method is not push', (
tester,
) async {
stubNotificationMethod('pushover');
await pumpScreen(tester, Settings(pushService: _FakePushService()));
await settleConfig(tester);
expect(find.text(_toggleText), findsNothing);
});
testWidgets('enabling push registers and shows a success message', (
tester,
) async {
stubNotificationMethod('push');
final service = _FakePushService(enableResult: true);
await pumpScreen(tester, Settings(pushService: service));
await pumpUntilFound(tester, find.byType(SwitchListTile));
await tester.tap(find.byType(SwitchListTile));
await pumpUntilFound(
@@ -75,31 +113,38 @@ void main() {
expect(service.enableCalls, 1);
expect(PrefUtil.getValue('push_enabled', false), isTrue);
expect(tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value, isTrue);
expect(
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
isTrue,
);
});
testWidgets('a declined permission leaves the toggle off with an error', (
tester,
) async {
stubNotificationMethod('push');
final service = _FakePushService(enableResult: false);
await pumpScreen(tester, Settings(pushService: service));
await pumpUntilFound(tester, find.byType(SwitchListTile));
await tester.tap(find.byType(SwitchListTile));
await pumpUntilFound(
tester,
find.textContaining('Could not enable push'),
);
await pumpUntilFound(tester, find.textContaining('Could not enable push'));
expect(service.enableCalls, 1);
expect(tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value, isFalse);
expect(
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
isFalse,
);
});
testWidgets('disabling push unregisters and shows a success message', (
tester,
) async {
stubNotificationMethod('push');
await PrefUtil.setValue('push_enabled', true);
final service = _FakePushService();
await pumpScreen(tester, Settings(pushService: service));
await pumpUntilFound(tester, find.byType(SwitchListTile));
// Starts on because the stored intent is enabled.
expect(
+13 -1
View File
@@ -9,17 +9,27 @@ import '../helpers/pump_app.dart';
void main() {
setUp(() async {
await setUpBackendForTest(
final adapter = await setUpBackendForTest(
prefs: {
'base_url': 'http://my.server/api',
'api_key': XOR().xorEncode('topsecret'),
'theme': 'catppuccin_mocha',
},
);
// Settings loads the backend config on init; these tests don't exercise the
// push toggle, so report a non-push method to keep it hidden.
adapter.onGet(
'/config',
(server) => server.reply(200, {
'notifications': {'method': 'none'},
}),
);
});
testWidgets('prefills the form from stored preferences', (tester) async {
await pumpScreen(tester, const Settings());
// Let the on-init config request resolve so its timer doesn't leak.
await tester.pump(const Duration(milliseconds: 10));
expect(find.text('http://my.server/api'), findsOneWidget);
expect(find.text('Catppuccin Mocha'), findsOneWidget);
@@ -78,6 +88,7 @@ void main() {
) async {
await PrefUtil.setValue('base_url', '');
await pumpScreen(tester, const Settings());
await tester.pump(const Duration(milliseconds: 10));
expect(find.textContaining('Welcome to OOTT'), findsOneWidget);
});
@@ -87,6 +98,7 @@ void main() {
) async {
await PrefUtil.setValue('base_url', 'http://my.server/api');
await pumpScreen(tester, const Settings());
await tester.pump(const Duration(milliseconds: 10));
expect(find.textContaining('Welcome to OOTT'), findsNothing);
});