import 'dart:async'; import 'package:flutter/foundation.dart'; import 'notification_service_bridge.dart'; /// Settings' view of background delivery: the switch, the live status and the /// battery exemption, all read from the native service (never guessed). class NotificationServiceController extends ChangeNotifier { NotificationServiceController( this.bridge, { this.requestNotificationPermission, }) { _changes = bridge.changes.listen(_apply); } final NotificationServiceBridge bridge; /// Asks Android 13+ for POST_NOTIFICATIONS (the app's existing request). final Future Function()? requestNotificationPermission; late final StreamSubscription _changes; NotificationServiceStatus? _status; List _events = const []; bool _busy = false; String _error = ''; bool _disposed = false; NotificationServiceStatus? get status => _status; /// The service's connection log, newest first. List get events => _events; bool get busy => _busy; String get error => _error; bool _askedForExemption = false; Future load() => _run(bridge.status); /// Opens Settings: reads the status and, when delivery is already on but the /// battery exemption is missing (existing users, upgrades), asks for it once /// per controller. The warning row stays if the prompt is declined. Future open() async { await load(); final current = _status; if (current != null && current.enabled && !current.batteryExempt && !_askedForExemption) { _askedForExemption = true; await allowBackgroundDelivery(); } } /// Re-reads after returning from a system screen (permission, battery). /// Never prompts again: a declined prompt is shown as the warning row. Future refresh() => load(); /// Switching delivery on first asks for the battery exemption, so the /// service starts with the prompt answered: delivery should not pause while /// the phone is idle. Future setEnabled(bool enabled) => _run(() async { if (enabled) { if (_status?.notificationsAllowed == false) { await requestNotificationPermission?.call(); } if (_status?.batteryExempt == false) { await bridge.requestBatteryExemption(); } } return bridge.setEnabled(enabled); }); Future allowNotifications() => _run(() async { await requestNotificationPermission?.call(); return bridge.status(); }); Future allowBackgroundDelivery() => _run(() async { await bridge.requestBatteryExemption(); return bridge.status(); }); Future _run(Future Function() action) async { if (_busy) return; _busy = true; _error = ''; _notify(); try { _status = await action(); _events = await bridge.connectionEvents(); } catch (error) { _error = 'Background delivery is unavailable: $error'; } finally { _busy = false; _notify(); } } void _apply(NotificationServiceStatus status) { _status = status; _notify(); // The service logs its own transitions; re-read so the log stays current. unawaited(_refreshEvents()); } Future _refreshEvents() async { try { _events = await bridge.connectionEvents(); } catch (error) { _error = 'Background delivery is unavailable: $error'; } _notify(); } void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; unawaited(_changes.cancel()); super.dispose(); } }