Files
pipelines/app/lib/features/settings/settings_device_delivery_section.dart
alan a7c41063fa Remove Cupertino branches from Settings
Keep the Material-only settings implementation and its focused tests while preserving public cupertino parameters for later cleanup. This removes unreachable platform-specific widgets and test variants without changing the Material path.
2026-11-11 05:05:57 +00:00

143 lines
5.0 KiB
Dart

import 'dart:async';
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<String>('settings.deviceDelivery.toggle');
static const status = ValueKey<String>('settings.deviceDelivery.status');
static const battery = ValueKey<String>('settings.deviceDelivery.battery');
static const permission = ValueKey<String>(
'settings.deviceDelivery.permission',
);
static const error = ValueKey<String>('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<void> Function()? requestNotificationPermission;
@override
State<SettingsDeviceDeliverySection> createState() =>
_SettingsDeviceDeliverySectionState();
}
class _SettingsDeviceDeliverySectionState
extends State<SettingsDeviceDeliverySection> {
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 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: <Widget>[
const Text(deviceDeliveryNote),
if (_controller.error.isNotEmpty)
Text(
_controller.error,
key: DeviceDeliveryKeys.error,
style: TextStyle(color: settingsErrorColor(context)),
),
],
),
children: <Widget>[
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()),
),
],
);
}
}