VesperPlayerEvent.fromMap constructor

VesperPlayerEvent.fromMap(
  1. Map<Object?, Object?> map
)

Implementation

factory VesperPlayerEvent.fromMap(Map<Object?, Object?> map) {
  final type = map['type'] as String? ?? 'snapshot';
  final playerId = map['playerId'] as String? ?? '';

  switch (type) {
    case 'pictureInPicture':
      final errorMap = vesperDecodeMap(map['error']);
      final rawState = map['state'];
      final state = _decodePictureInPictureStatus(rawState);
      return VesperPlayerPictureInPictureEvent(
        playerId: playerId,
        state: state,
        stateRawValue:
            rawState is String && rawState != state.name ? rawState : null,
        isActive: _decodeEventBool(map['isActive']),
        source: map['source'] as String? ?? 'system',
        error: errorMap.isNotEmpty
            ? VesperPictureInPictureError.fromMap(errorMap)
            : null,
        canAutoEnter: _decodeNullableEventBool(map['canAutoEnter']),
        diagnostics: vesperDecodeMap(map['diagnostics']),
      );
    case 'error':
      final rawError = map['error'];
      final errorMap = vesperDecodeMap(rawError);
      final error = errorMap.isNotEmpty
          ? VesperPlayerError.fromMap(errorMap)
          : const VesperPlayerError(
              message: 'Unknown Vesper player error.',
              code: VesperPlayerErrorCode.backendFailure,
              category: VesperPlayerErrorCategory.platform,
              retriable: false,
            );
      final rawSnapshot = map['snapshot'];
      final snapshotMap = vesperDecodeMap(rawSnapshot);
      return VesperPlayerErrorEvent(
        playerId: playerId,
        error: error,
        snapshot: snapshotMap.isNotEmpty
            ? VesperPlayerSnapshot.fromMap(snapshotMap)
            : null,
      );
    case 'disposed':
      return VesperPlayerDisposedEvent(playerId: playerId);
    case 'warning':
      final rawWarning = map['warning'];
      final warningMap = vesperDecodeMap(rawWarning);
      return VesperPlayerWarningEvent(
        playerId: playerId,
        warning: VesperRuntimeWarning.fromMap(warningMap),
      );
    case 'pipelineEventHookReports':
      return VesperPlayerPipelineEventHookReportsEvent(
        playerId: playerId,
        reports: VesperPipelineEventHookReportBatch.fromMap(
          vesperDecodeMap(map['reports']),
        ),
      );
    case 'snapshot':
      final rawSnapshot = map['snapshot'];
      final snapshotMap = vesperDecodeMap(rawSnapshot);
      final snapshot = snapshotMap.isNotEmpty
          ? VesperPlayerSnapshot.fromMap(snapshotMap)
          : const VesperPlayerSnapshot.initial();
      return VesperPlayerSnapshotEvent(
        playerId: playerId,
        snapshot: snapshot,
      );
    default:
      return VesperPlayerUnknownEvent(
        playerId: playerId,
        type: type,
        payload: vesperDecodeMap(map),
      );
  }
}