|
| 1 | +/// SPEC-07 Slice 2 — push-token registration seam. |
| 2 | +/// |
| 3 | +/// [pushRegisterBody] is the pure `cmd` body the app sends after a successful |
| 4 | +/// (re)connect. [PushRegistrar] isolates native APNs/FCM token retrieval behind |
| 5 | +/// a platform channel/plugin; [NoopPushRegistrar] is the default (no token → |
| 6 | +/// the app never sends `push.register`, and the server falls back to Slice-1). |
| 7 | +library; |
| 8 | + |
| 9 | +import 'package:flutter/foundation.dart'; |
| 10 | +import 'package:flutter/services.dart'; |
| 11 | + |
| 12 | +import '../transport/protocol.dart'; |
| 13 | + |
| 14 | +/// Build the `push.register` command body. Pure + unit-tested. |
| 15 | +Map<String, dynamic> pushRegisterBody({ |
| 16 | + required String token, |
| 17 | + required String platform, |
| 18 | +}) => {'kind': CmdKind.registerPush.wire, 'token': token, 'platform': platform}; |
| 19 | + |
| 20 | +/// Native push-token provider seam. A real implementation wraps a platform |
| 21 | +/// channel (iOS `AppDelegate` forwards the APNs token; Android/FCM later). |
| 22 | +abstract class PushRegistrar { |
| 23 | + /// The routing platform for this registrar: "apns" | "fcm". |
| 24 | + String get platform; |
| 25 | + |
| 26 | + /// The current push token, or null when unavailable (permission declined, |
| 27 | + /// not yet retrieved, or no native provider wired). |
| 28 | + Future<String?> getToken(); |
| 29 | + |
| 30 | + /// Install a listener fired when a token first becomes (or newly becomes) |
| 31 | + /// available. The APNs token can arrive AFTER the socket connects, so the |
| 32 | + /// [ConnectionController] subscribes to this to send `push.register` late. |
| 33 | + /// Passing null detaches the listener. |
| 34 | + set onToken(void Function(String token)? listener); |
| 35 | +} |
| 36 | + |
| 37 | +/// Default registrar: no native provider, so no token. Keeps the composition |
| 38 | +/// root buildable without platform wiring; the app simply skips registration |
| 39 | +/// and the server stays on the Slice-1 fallback. |
| 40 | +class NoopPushRegistrar implements PushRegistrar { |
| 41 | + const NoopPushRegistrar(); |
| 42 | + |
| 43 | + @override |
| 44 | + String get platform => 'apns'; |
| 45 | + |
| 46 | + @override |
| 47 | + Future<String?> getToken() async => null; |
| 48 | + |
| 49 | + @override |
| 50 | + set onToken(void Function(String token)? listener) { |
| 51 | + // No native provider → a token never arrives, so nothing to notify. |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// A registrar whose token is provided by a caller (e.g. once the iOS |
| 56 | +/// `AppDelegate` channel delivers the APNs token). Kept trivial + testable. |
| 57 | +@immutable |
| 58 | +class ProvidedPushRegistrar implements PushRegistrar { |
| 59 | + const ProvidedPushRegistrar(this._token, {this.platform = 'apns'}); |
| 60 | + |
| 61 | + final String? _token; |
| 62 | + |
| 63 | + @override |
| 64 | + final String platform; |
| 65 | + |
| 66 | + @override |
| 67 | + Future<String?> getToken() async => _token; |
| 68 | + |
| 69 | + @override |
| 70 | + set onToken(void Function(String token)? listener) { |
| 71 | + // Token is fixed at construction, so it never "newly" arrives. |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Method-channel-backed registrar (SPEC-07 W4 Dart half). Listens on the |
| 76 | +/// `pino/push` channel that iOS `AppDelegate` invokes with the hex APNs token. |
| 77 | +/// |
| 78 | +/// The token can arrive AFTER the socket connects, so this stores the latest |
| 79 | +/// token AND fires [onToken] so the [ConnectionController] can send |
| 80 | +/// `push.register` mid-connection. [getToken] returns the stored token (null |
| 81 | +/// until the native `didRegister` call fires). |
| 82 | +class ChannelPushRegistrar implements PushRegistrar { |
| 83 | + ChannelPushRegistrar({MethodChannel? channel, this.platform = 'apns'}) |
| 84 | + : _channel = channel ?? const MethodChannel(pushChannelName) { |
| 85 | + _channel.setMethodCallHandler(_handle); |
| 86 | + } |
| 87 | + |
| 88 | + /// The native channel name (mirrors `AppDelegate.swift`). |
| 89 | + static const String pushChannelName = 'pino/push'; |
| 90 | + |
| 91 | + /// The native method name for a delivered APNs token (mirrors AppDelegate). |
| 92 | + static const String didRegisterMethod = 'didRegister'; |
| 93 | + |
| 94 | + final MethodChannel _channel; |
| 95 | + |
| 96 | + @override |
| 97 | + final String platform; |
| 98 | + |
| 99 | + String? _token; |
| 100 | + void Function(String token)? _onToken; |
| 101 | + |
| 102 | + @override |
| 103 | + Future<String?> getToken() async => _token; |
| 104 | + |
| 105 | + @override |
| 106 | + set onToken(void Function(String token)? listener) => _onToken = listener; |
| 107 | + |
| 108 | + Future<Object?> _handle(MethodCall call) async { |
| 109 | + if (call.method == didRegisterMethod) { |
| 110 | + final token = call.arguments; |
| 111 | + if (token is String && token.isNotEmpty) { |
| 112 | + _token = token; |
| 113 | + _onToken?.call(token); |
| 114 | + } |
| 115 | + } |
| 116 | + // `didFail` and unknown methods: ignore (best-effort seam; a missing token |
| 117 | + // simply leaves the app on the Slice-1 fallback). |
| 118 | + return null; |
| 119 | + } |
| 120 | +} |
0 commit comments