/// Real Adsgram SDK interface using JavaScript interop /// This provides a Dart interface to the Adsgram JavaScript SDK import 'dart:js_util' as js_util; /// Configuration for Adsgram ad display class AdsgramAd { final String blockId; final int rewardAmount; final void Function()? onReward; final void Function(String error)? onError; const AdsgramAd({ required this.blockId, required this.rewardAmount, this.onReward, this.onError, }); } /// Adsgram SDK interface for showing rewarded ads class Adsgram { static final Adsgram _instance = Adsgram._internal(); static Adsgram get instance => _instance; Adsgram._internal(); /// Show a rewarded ad using the Adsgram JavaScript SDK /// This calls the JavaScript showAd() function defined in web/foos.js Future showRewardedAd(AdsgramAd adConfig) async { try { // Set up callbacks in JavaScript before showing ad await _setupCallbacks(adConfig); // Call the JavaScript showAd function // The function is defined in web/foos.js and handles the Adsgram SDK await js_util.callMethod>( js_util.globalThis, 'showAd', [], ); // Note: The actual reward/error dispatch logic is handled in the JavaScript // The callbacks are triggered from JavaScript when ad completes or fails } catch (e) { // If JavaScript call fails, call error callback adConfig.onError?.call('Failed to show ad: $e'); } } /// Set up JavaScript callbacks for ad completion Future _setupCallbacks(AdsgramAd adConfig) async { // Create JavaScript functions that will call the Dart callbacks final rewardJsFunction = js_util.jsify(() { adConfig.onReward?.call(); }); final errorJsFunction = js_util.jsify((String error) { adConfig.onError?.call(error); }); // Set the callbacks in JavaScript await js_util.callMethod>( js_util.globalThis, 'setRewardCallback', [rewardJsFunction], ); await js_util.callMethod>( js_util.globalThis, 'setErrorCallback', [errorJsFunction], ); } /// Alternative method to show ad with specific block ID /// This allows dynamic block ID configuration Future showAdWithBlockId(String blockId) async { try { await js_util.callMethod>( js_util.globalThis, 'showAdWithBlockId', [blockId], ); } catch (e) { throw Exception('Failed to show ad with block ID $blockId: $e'); } } }