mnemo_cards/mnemo_cards_web_v2/web/foos.js
AI Agent 311894318d feat(web_v2): Complete Ads Reward Flow - Integrate Adsgram SDK
Task ID: WEB-001
Priority: high

Changes:

Completed by: AI Agent
Duration: 1604716ms
2025-11-21 03:02:05 +00:00

157 lines
No EOL
5.2 KiB
JavaScript

// Adsgram SDK integration for rewarded ads
// This file provides a bridge between Dart code and the Adsgram JavaScript SDK
// Check if Adsgram SDK is loaded
if (typeof window.Adsgram === 'undefined') {
console.error('Adsgram SDK not loaded. Make sure sad.min.js is included in index.html');
}
// Store callbacks for ad lifecycle events
let rewardCallback = null;
let errorCallback = null;
let currentAdController = null;
// Default block ID (can be overridden)
const DEFAULT_BLOCK_ID = "16505";
/**
* Initialize Adsgram ad controller with a specific block ID
* @param {string} blockId - The Adsgram block ID
* @returns {object} Adsgram ad controller instance
*/
function initAdController(blockId) {
if (typeof window.Adsgram === 'undefined') {
throw new Error('Adsgram SDK not available');
}
try {
return window.Adsgram.init({ blockId: blockId });
} catch (error) {
console.error('Failed to initialize Adsgram controller:', error);
throw error;
}
}
/**
* Show a rewarded ad using the default block ID
* Called from Dart code via js_util.callMethod
* @returns {Promise} Promise that resolves when ad completes or rejects on error
*/
function showAd() {
return showAdWithBlockId(DEFAULT_BLOCK_ID);
}
/**
* Show a rewarded ad with a specific block ID
* Called from Dart code via js_util.callMethod
* @param {string} blockId - The Adsgram block ID to use
* @returns {Promise} Promise that resolves when ad completes or rejects on error
*/
function showAdWithBlockId(blockId) {
if (!blockId || typeof blockId !== 'string') {
const error = 'Invalid block ID provided';
console.error(error);
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(error);
}
return Promise.reject(new Error(error));
}
try {
// Initialize controller for this specific block
currentAdController = initAdController(blockId);
// Show the ad
return currentAdController.show().then((result) => {
// Ad completed successfully - user watched till the end
console.log('Ad completed successfully', result);
// Call reward callback if set
if (rewardCallback && typeof rewardCallback === 'function') {
try {
rewardCallback();
} catch (callbackError) {
console.error('Error in reward callback:', callbackError);
}
}
return result;
}).catch((error) => {
// Ad failed or was closed early
console.error('Ad failed or was closed:', error);
// Format error message
let errorMessage = 'Ad failed';
if (error && typeof error === 'object') {
try {
errorMessage = JSON.stringify(error);
} catch (e) {
errorMessage = error.toString();
}
} else if (error) {
errorMessage = error.toString();
}
// Call error callback if set
if (errorCallback && typeof errorCallback === 'function') {
try {
errorCallback(errorMessage);
} catch (callbackError) {
console.error('Error in error callback:', callbackError);
}
}
throw error;
});
} catch (error) {
// Initialization or show() call failed
const errorMessage = error ? error.toString() : 'Failed to show ad';
console.error('Failed to show ad:', errorMessage);
if (errorCallback && typeof errorCallback === 'function') {
try {
errorCallback(errorMessage);
} catch (callbackError) {
console.error('Error in error callback:', callbackError);
}
}
return Promise.reject(error);
}
}
/**
* Set the reward callback function
* Called from Dart code to register callback for successful ad completion
* @param {Function} callback - Function to call when ad completes successfully
*/
function setRewardCallback(callback) {
if (callback && typeof callback === 'function') {
rewardCallback = callback;
console.log('Reward callback registered');
} else {
console.warn('Invalid reward callback provided');
rewardCallback = null;
}
}
/**
* Set the error callback function
* Called from Dart code to register callback for ad errors
* @param {Function} callback - Function to call when ad fails (takes error message as parameter)
*/
function setErrorCallback(callback) {
if (callback && typeof callback === 'function') {
errorCallback = callback;
console.log('Error callback registered');
} else {
console.warn('Invalid error callback provided');
errorCallback = null;
}
}
// Make functions available globally for Dart interop
window.showAd = showAd;
window.showAdWithBlockId = showAdWithBlockId;
window.setRewardCallback = setRewardCallback;
window.setErrorCallback = setErrorCallback;