setupEventsChannel static method

void setupEventsChannel()

Implementation

static void setupEventsChannel() {
  _eventChannel.receiveBroadcastStream().listen((event) {
    Map<String, dynamic> map = jsonDecode(event);
    var name = map['name'];
    var payload = map['payload'];

    switch (name) {
      case 'onClose':
        GlassfyTransaction? transaction;
        try {
          transaction = GlassfyTransaction.fromJson(payload['transaction']);
        } catch (_) {}
        var error = payload['error'];
        _listener?.onClose(transaction, error);
        GlassfyPaywall.close();
        break;

      case 'onPurchase':
        try {
          var sku = GlassfySku.fromJson(payload['sku']);
          _listener?.onPurchase(sku);
        } catch (_) {}
        break;

      case 'onLink':
        try {
          Uri url = Uri.parse(payload['url']);
          _listener?.onLink(url);
        } catch (_) {}
        break;

      case 'onRestore':
        _listener?.onRestore();
        break;

      default:
        debugPrint('PAYWALL - Received unknwon event $name');
    }
  });
}