handlePopupReceived method

void handlePopupReceived(
  1. Map<String, dynamic> json, {
  2. bool isCached = false,
})

Implementation

void handlePopupReceived(Map<String, dynamic> json, {bool isCached = false}) {
  if (json != null && json.containsKey('title') && json['title'] != null) {
    if (isCached == true) {
      print('jsonform cache ${json}');
    }
    String popupId = json['popupId'] ?? '';
    if (!isCached && (popupId.isEmpty || shownPopupIds.contains(popupId))) {
      print("Popup already shown or invalid popupId: $popupId");
      return;
    }

    // Add popupId to shownPopupIds if not cached
    if (!isCached) {
      shownPopupIds.add(popupId);
    }

    print("Received popup ID: $popupId");
    int priority = json['priority'] ?? -1;
    List<String> emojiList = List<String>.from(json['emojiList'] ?? []);
    List<dynamic> components = json['components'] ?? [];

    String titleOfPopup = json['titleOfPopup']?['label'] ?? json['title'];
    if (titleOfPopup.isEmpty) {
      print("Popup title is empty for ID: $popupId");
      return;
    }

    String extractVideoLink(List<dynamic> components) {
      for (var component in components) {
        if (component is Map<String, dynamic> &&
            component['type'] == 'video') {
          return component['value'] ?? DEFAULT_VIDE_LINK;
        }
      }
      return DEFAULT_VIDE_LINK;
    }

    var popupTimeValue = json["popupTime"];
    if (popupTimeValue is String) {
      popupTimeValue = int.tryParse(popupTimeValue) ?? 0;
    } else if (popupTimeValue is! int) {
      popupTimeValue = 0;
    }

    if (primaryPeriority <= priority) {
      PopupWidget.showPopup(
          json['popupBackgroundImage'],
          json['popupBackground'],
          titleOfPopup,
          json['titleOfPopup']?['fontSize'] ?? 14,
          apiUrl,
          clientApiUrl,
          json['titleOfPopup']?['fontFamily'] ?? 'Arial',
          json['titleOfPopup']?['color'] ?? 'defaultColor',
          json['titleOfPopup']?['fontWeight'] ?? 'normal',
          json['status'],
          components.whereType<Map<String, dynamic>>().toList(),
          // Safe cast
          emojiList,
          json['interaction'],
          popupId,
          username,
          json['popupToken'],
          userId,
          components,
          extractVideoLink(components),
          popupTimeValue,
          priority,
          context,
          database,
        appId
     );
    }

      primaryPeriority = priority;

  }
}