diff --git a/packages/xandr/example/lib/main.dart b/packages/xandr/example/lib/main.dart index 4a1ad6d..d6c69b4 100644 --- a/packages/xandr/example/lib/main.dart +++ b/packages/xandr/example/lib/main.dart @@ -38,7 +38,7 @@ class _XandrExampleState extends State { late final XandrController _controller; final ScrollController _scrollController = ScrollController(); final StreamController _checkIfAdIsInViewport = - StreamController(); + StreamController.broadcast(); @override void dispose() { diff --git a/packages/xandr_ios/ios/Classes/models/FlutterState.swift b/packages/xandr_ios/ios/Classes/models/FlutterState.swift index b3886f8..fa67542 100644 --- a/packages/xandr_ios/ios/Classes/models/FlutterState.swift +++ b/packages/xandr_ios/ios/Classes/models/FlutterState.swift @@ -5,9 +5,17 @@ // Created by markus korn on 13.05.24. // +import AppNexusSDK import Flutter import Foundation +private func logResult(_ result: Any?) { + logger + .debug( + message: "flutter api: we got a result back from the flutter api \(String(describing: result))" + ) +} + public class FlutterState { public var isInitialized: Completer = .init() private var flutterAPI: XandrFlutterApi? @@ -63,4 +71,29 @@ public class FlutterState { throw XandrPluginError.runtimeError("Unable to find Banner for widgetId=\(id)") } } + + public func onAdLoadedAPI(viewId: Int64, width: CGFloat, height: CGFloat, creativeId: String, + adType: ANAdType, tagId: String, auctionId: String, cpm: Double, + memberId: Int) { + let adTypeIndentifier = switch adType { + case ANAdType.banner: "banner" + case ANAdType.native: "native" + case ANAdType.unknown: "unknown" + case ANAdType.video: "video" + @unknown default: "unknwon" + } + + flutterAPI?.onAdLoaded( + viewId: viewId, + width: Int64(width), + height: Int64(height), + creativeId: creativeId, + adType: adTypeIndentifier, + tagId: tagId, + auctionId: auctionId, + cpm: cpm, + memberId: Int64(memberId), + completion: logResult + ) + } } diff --git a/packages/xandr_ios/ios/Classes/models/ads/BannerAd.swift b/packages/xandr_ios/ios/Classes/models/ads/BannerAd.swift index 26dbce0..835b311 100644 --- a/packages/xandr_ios/ios/Classes/models/ads/BannerAd.swift +++ b/packages/xandr_ios/ios/Classes/models/ads/BannerAd.swift @@ -10,16 +10,20 @@ import Flutter import Foundation class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate { - private var banner: ANBannerAdView? + public var banner: ANBannerAdView? + public var state: FlutterState? + public var viewId: Int64 init(state: FlutterState, frame: CGRect, viewIdentifier viewId: Int64, args: Any?, binaryMessenger messenger: FlutterBinaryMessenger?) { + self.viewId = viewId super.init() // Do any additional setup after loading the view. ANSDKSettings.sharedInstance().enableOMIDOptimization = true logger.debug(message: "init banner") + self.state = state guard let arguments = args as? [String: Any] else { return @@ -45,6 +49,7 @@ class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate { inventoryCode: inventoryCode! ) banner?.delegate = self + // banner?.appEventDelegate = self customKeywords?.forEach { item in for value in item.value { banner?.addCustomKeyword(withKey: item.key, value: value) @@ -71,4 +76,31 @@ class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate { func loadAd() { banner?.loadAd() } + + public func adDidReceiveAd(_ ad: Any) { + if ad is ANBannerAdView { + let a = ad as? ANBannerAdView + if let info = a?.adResponseInfo { + state?.onAdLoadedAPI( + viewId: viewId, + width: a!.loadedAdSize.width, + height: (a?.loadedAdSize.height)!, + creativeId: info.creativeId!, + adType: info.adType, + tagId: "", + auctionId: info.auctionId!, + cpm: info.cpm!.doubleValue, + memberId: info.memberId + ) + + } else { + logger + .error( + message: "BannerAd.adDidRecieveAd: we did not get an adResponseInfo back, got \(String(describing: a))" + ) + } + } else { + logger.error(message: "BannerAd.adDidRecieveAd: recieved an unknown payload \(ad)") + } + } }