/// One entry of the background service's connection log, as Settings shows /// it for diagnosis. The service keeps the newest events first. class NotificationConnectionEvent { const NotificationConnectionEvent({ required this.time, required this.event, this.error, this.code, }); final DateTime time; /// The service's wire name for what happened, e.g. `failed`. final String event; final String? error; /// The HTTP status of a failed handshake, when there was one. final int? code; factory NotificationConnectionEvent.fromMap(Map map) => NotificationConnectionEvent( time: DateTime.fromMillisecondsSinceEpoch( (map['time'] as num?)?.toInt() ?? 0, ), event: map['event'] as String? ?? 'unknown', error: map['error'] as String?, code: (map['code'] as num?)?.toInt(), ); /// A short, plain name for the event. String get title => switch (event) { 'connecting' => 'Opening socket', 'connected' => 'Socket opened', 'closed' => 'Socket closed', 'failed' => 'Socket failed', 'retry_scheduled' => 'Retry scheduled', 'sign_in_required' => 'Session refused', 'refused' => 'Subscribe refused', 'network_available' => 'Network available', 'network_lost' => 'Network lost', 'invalid_endpoint' => 'Invalid address', 'watchdog_restart' => 'Watchdog restart', 'task_removed' => 'App closed from recents', 'stopped' => 'Stopped', _ => event, }; /// The HTTP code and error text, when there are any. String? get detail { final parts = [ if (code != null) 'HTTP $code', if (error != null && error!.isNotEmpty) error!, ]; return parts.isEmpty ? null : parts.join(' ยท '); } /// Local time as `MM-DD HH:MM:SS`. String get timeLabel { String two(int value) => value.toString().padLeft(2, '0'); final local = time.toLocal(); return '${two(local.month)}-${two(local.day)} ' '${two(local.hour)}:${two(local.minute)}:${two(local.second)}'; } }