fromJson static method

GamificationConfig? fromJson(
  1. Map<String, dynamic> campaignJson,
  2. String campaignKey
)

Implementation

static GamificationConfig? fromJson(
    Map<String, dynamic> campaignJson, String campaignKey) {
  GamificationConfig? drop(String what) {
    _log.e('Dropped — gamification campaign has no $what',
        campaign: campaignKey,
        stage: TimelineStage.parse,
        reason: TimelineReason.malformedCampaignSkipped);
    return null;
  }

  final template = optMap(campaignJson, 'templateConfig');
  if (template == null) return drop('templateConfig');
  final skinId = _nonBlank(template['skinId']);
  if (skinId == null) return drop('skinId');
  final skinVersion = _intOrNull(template['skinVersion']);
  if (skinVersion == null) return drop('skinVersion');
  final bundleUrl = _nonBlank(template['bundleUrl']) ??
      _nonBlank(campaignJson['bundleUrl']);
  if (bundleUrl == null) return drop('bundleUrl');

  final config = optMap(template, 'config');
  final surface = optMap(template, 'surface');
  final theme = optMap(template, 'theme') ??
      (config == null ? null : optMap(config, 'theme'));
  return GamificationConfig(
    format: GamificationFormat.parseOrUnknown(
        _nonBlank(template['templateType'])),
    skinId: skinId,
    skinVersion: skinVersion,
    bridgeVersion: _intOrNull(template['bridgeVersion']),
    values: (config == null ? null : optMap(config, 'values')) ?? {},
    slots: _slotsOf(config, campaignKey),
    entry: GamificationEntryKind.parseOrUnknown(
        _nonBlank(optMap(template, 'entry')?['kind'])),
    slotKey: _nonBlank(template['slotKey']),
    teaser: optMap(template, 'teaser'),
    // The dashboard saves `showMute`/`showClose` at the top level;
    // `surface.*` is the fallback. Both default to visible: hiding chrome
    // is opt-in. A JSON null counts as absent, like a missing key.
    showMute: template.containsKey('showMute') && template['showMute'] != null
        ? optBool(template, 'showMute', false)
        : (surface != null && optBool(surface, 'showMute', false)),
    showClose:
        template.containsKey('showClose') && template['showClose'] != null
            ? optBool(template, 'showClose', true)
            : (surface == null || optBool(surface, 'showClose', true)),
    // `dismissAfterPlayed` defaults to visible-hide: absent means the played
    // floating entry comes down, matching the long-standing behaviour.
    // A JSON null counts as absent, like a missing key.
    dismissAfterPlayed: template.containsKey('dismissAfterPlayed') &&
            template['dismissAfterPlayed'] != null
        ? optBool(template, 'dismissAfterPlayed', true)
        : (surface == null || optBool(surface, 'dismissAfterPlayed', true)),
    bundleUrl: bundleUrl,
    sensors: _sensorsOf(template, campaignJson),
    minSdkVersion: _nonBlank((config == null
            ? null
            : optMap(config, 'minSdkVersion'))?['flutter']) ??
        _nonBlank(optMap(template, 'minSdkVersion')?['flutter']),
    themeFontFamily: _nonBlank(theme?['fontFamily']),
    themeFonts: _themeFontsOf(theme),
  );
}