showTemplates static method

void showTemplates(
  1. BuildContext context,
  2. List<Template> templates,
  3. TemplateService service
)

Implementation

static void showTemplates(BuildContext context, List<Template> templates, TemplateService service) {
  if (templates.isEmpty) return;

  void showNextTemplate(int index) {
    if (index >= templates.length) return;

    Template template = templates[index];
    Widget templateWidget;

    // Track delivery event when template is received
    service.trackEvent(TrackingEvent(
      templateId: template.id,
      eventType: TrackingEventType.delivery,
    ));

    debugPrint('Showing template type: ${template.type}');
    debugPrint('Showing video : ${template}');

    void onClose(bool isUserInitiated) {
      // Track close event only if user explicitly clicked close
      if (isUserInitiated) {
        service.trackEvent(TrackingEvent(
          templateId: template.id,
          eventType: TrackingEventType.close,
        ));
      }
      showNextTemplate(index + 1);
    }

    switch (template.type) {
      case 'nudge':
        templateWidget = TemplateNudge(
          template: template,
          onClose: () => onClose(true), // Pass true for user-initiated close
        );
        break;
      case 'picture-in-picture':
        templateWidget = TemplatePIP(
          template: template,
          onClose: () => onClose(true),
        );
        break;
      case 'roadblock':
        debugPrint('Within Roadblock');
        templateWidget = TemplateWebView(
          template: template,
          onClose: () => onClose(true),
        );
        break;
      default:
        debugPrint('Unhandled template type: ${template.type}');
        onClose(false); // Pass false for system-initiated close
        return;
    }

    // Track read event when template is shown
    service.trackEvent(TrackingEvent(
      templateId: template.id,
      eventType: TrackingEventType.read,
    ));

    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => WillPopScope(
        onWillPop: () async {
          onClose(false); // Pass false for system-initiated close
          return true;
        },
        child: templateWidget,
      ),
    );
  }

  showNextTemplate(0);
}