import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../notification_service/notification_service_bridge.dart'; import '../notification_service/notification_service_controller.dart'; import 'settings_group.dart'; import 'settings_row.dart'; import 'settings_theme.dart'; const deviceDeliveryTitle = 'Notifications in Pipelines'; const deviceDeliverySwitchLabel = 'Receive notifications in Pipelines'; const deviceDeliveryNote = 'Pipelines keeps its own connection to the server and shows its ' 'notifications, so the ntfy app is no longer needed. Android shows a ' 'quiet "Pipelines is connected" notification while it runs.'; const deviceDeliveryBatteryLabel = 'Allow background delivery'; const deviceDeliveryNotificationsLabel = 'Allow notifications'; abstract final class DeviceDeliveryKeys { static const toggle = ValueKey('settings.deviceDelivery.toggle'); static const status = ValueKey('settings.deviceDelivery.status'); static const battery = ValueKey('settings.deviceDelivery.battery'); static const permission = ValueKey( 'settings.deviceDelivery.permission', ); static const error = ValueKey('settings.deviceDelivery.error'); } /// Android background delivery in Settings: the switch, the service's live /// status, and the two system grants it relies on (notification permission and /// the battery-optimisation exemption). Absent where there is no service. class SettingsDeviceDeliverySection extends StatefulWidget { const SettingsDeviceDeliverySection({ super.key, required this.bridge, this.controller, this.requestNotificationPermission, }); final NotificationServiceBridge bridge; final NotificationServiceController? controller; final Future Function()? requestNotificationPermission; @override State createState() => _SettingsDeviceDeliverySectionState(); } class _SettingsDeviceDeliverySectionState extends State { late final NotificationServiceController _controller = widget.controller ?? NotificationServiceController( widget.bridge, requestNotificationPermission: widget.requestNotificationPermission, ); bool get _ownsController => widget.controller == null; // System prompts (permission, battery) return by resuming the app. late final AppLifecycleListener _lifecycle = AppLifecycleListener( onResume: () => unawaited(_controller.refresh()), ); @override void initState() { super.initState(); _lifecycle; unawaited(_controller.load()); } @override void dispose() { _lifecycle.dispose(); if (_ownsController) _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) => ListenableBuilder( listenable: _controller, builder: (context, _) => _body(context), ); Widget _body(BuildContext context) { final cupertino = usesCupertinoControls(context); final status = _controller.status; final enabled = status?.enabled ?? false; final onChanged = status == null || _controller.busy ? null : (bool value) => unawaited(_controller.setEnabled(value)); return SettingsGroup( title: deviceDeliveryTitle, footer: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text(deviceDeliveryNote), if (_controller.error.isNotEmpty) Text( _controller.error, key: DeviceDeliveryKeys.error, style: TextStyle(color: settingsErrorColor(context)), ), ], ), children: [ if (cupertino) CupertinoListTile( key: DeviceDeliveryKeys.toggle, title: const Text(deviceDeliverySwitchLabel), trailing: CupertinoSwitch(value: enabled, onChanged: onChanged), onTap: onChanged == null ? null : () => onChanged(!enabled), ) else SwitchListTile( key: DeviceDeliveryKeys.toggle, title: const Text(deviceDeliverySwitchLabel), value: enabled, onChanged: onChanged, ), SettingsRow( key: DeviceDeliveryKeys.status, title: const Text('Status'), subtitle: Text(status == null ? 'Loading…' : status.label), ), if (status != null && enabled && !status.notificationsAllowed) SettingsRow( key: DeviceDeliveryKeys.permission, title: const Text(deviceDeliveryNotificationsLabel), subtitle: const Text('Android is blocking Pipelines notifications'), action: true, enabled: !_controller.busy, onTap: () => unawaited(_controller.allowNotifications()), ), if (status != null && enabled) SettingsRow( key: DeviceDeliveryKeys.battery, title: const Text(deviceDeliveryBatteryLabel), subtitle: Text( status.batteryExempt ? 'Allowed: battery optimisation will not pause delivery' : 'Keeps notifications instant while the phone is idle', ), action: !status.batteryExempt, enabled: !status.batteryExempt && !_controller.busy, onTap: () => unawaited(_controller.allowBackgroundDelivery()), ), ], ); } }