InappifyBackendCapabilities.fromMap constructor

InappifyBackendCapabilities.fromMap(
  1. Map<String, dynamic> map
)

Decodes a capability map returned by JavaScript or MethodChannel.

Implementation

factory InappifyBackendCapabilities.fromMap(Map<String, dynamic> map) {
  final backendName = map['backendKind'];
  final protocolVersion = _exactInteger(map['protocolVersion']);
  final ownsSession = map['ownsSession'];
  final operationsValue = map['supportedOperations'];
  final sdkVersion = map['sdkVersion'];
  if (backendName is! String ||
      protocolVersion == null ||
      ownsSession is! bool ||
      (sdkVersion != null && sdkVersion is! String)) {
    throw const FormatException(
      'Backend capability fields have invalid types.',
    );
  }
  final operations = <InappifyOperation>{};

  if (operationsValue is List) {
    for (final value in operationsValue) {
      if (value is! String) {
        throw const FormatException(
          'Backend operation names must be strings.',
        );
      }
      final operation = _operationFromWireName(value);
      if (operation != null) operations.add(operation);
    }
  } else if (operationsValue is Map) {
    for (final entry in operationsValue.entries) {
      if (entry.value == true || entry.value == 1) {
        final operation = _operationFromWireName(entry.key.toString());
        if (operation != null) operations.add(operation);
      }
    }
  } else {
    throw const FormatException(
      'Backend supportedOperations must be a list or map.',
    );
  }

  return InappifyBackendCapabilities(
    backendKind: switch (backendName) {
      'dart' => InappifyBackendKind.dart,
      'webJavaScript' => InappifyBackendKind.webJavaScript,
      'methodChannel' => InappifyBackendKind.methodChannel,
      _ => InappifyBackendKind.unknown,
    },
    protocolVersion: protocolVersion,
    ownsSession: ownsSession,
    supportedOperations: Set<InappifyOperation>.unmodifiable(operations),
    sdkVersion: sdkVersion as String?,
  );
}