From f1c06226d052e2d55de42ed86483bf4ba5a5d269 Mon Sep 17 00:00:00 2001 From: Massimiliano Massaro Date: Tue, 30 Jul 2024 11:24:06 +0200 Subject: [PATCH 1/2] fix: _onDoneLoading callback not called on ios --- .../main/kotlin/de/thekorn/xandr/Xandr.g.kt | 4 +- .../thekorn/xandr/listeners/AdInitListener.kt | 2 +- .../de/thekorn/xandr/models/ads/BannerAd.kt | 7 +- .../xandr_android/lib/src/messages.g.dart | 2 +- packages/xandr_android/lib/xandr_android.dart | 111 ------------------ packages/xandr_ios/lib/xandr_ios.dart | 111 ------------------ .../lib/src/types.dart | 111 ++++++++++++++++++ 7 files changed, 119 insertions(+), 229 deletions(-) diff --git a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/Xandr.g.kt b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/Xandr.g.kt index 5d355c6..a748c32 100644 --- a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/Xandr.g.kt +++ b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/Xandr.g.kt @@ -1,9 +1,9 @@ -// Autogenerated from Pigeon (v21.0.0), do not edit directly. +// Autogenerated from Pigeon (v21.1.0), do not edit directly. // See also: https://pub.dev/packages/pigeon @file:Suppress("UNCHECKED_CAST", "ArrayInDataClass") -import io.flutter.Log +import android.util.Log import io.flutter.plugin.common.BasicMessageChannel import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.MessageCodec diff --git a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/listeners/AdInitListener.kt b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/listeners/AdInitListener.kt index d12dd2d..b5f45f6 100644 --- a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/listeners/AdInitListener.kt +++ b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/listeners/AdInitListener.kt @@ -1,9 +1,9 @@ package de.thekorn.xandr.listeners -import io.flutter.Log import com.appnexus.opensdk.InitListener import com.appnexus.opensdk.SDKSettings import de.thekorn.xandr.models.FlutterState +import io.flutter.Log class AdInitListener(private val flutterState: FlutterState) : InitListener { override fun onInitFinished(success: Boolean) { diff --git a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/models/ads/BannerAd.kt b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/models/ads/BannerAd.kt index 9175be8..1fc9593 100644 --- a/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/models/ads/BannerAd.kt +++ b/packages/xandr_android/android/src/main/kotlin/de/thekorn/xandr/models/ads/BannerAd.kt @@ -4,7 +4,6 @@ import android.annotation.SuppressLint import android.app.Activity import android.app.Application import android.os.Bundle -import io.flutter.Log import androidx.core.content.ContextCompat import androidx.lifecycle.DefaultLifecycleObserver import com.appnexus.opensdk.ANClickThroughAction @@ -13,20 +12,22 @@ import de.thekorn.xandr.listeners.XandrBannerAdListener import de.thekorn.xandr.models.BannerViewOptions import de.thekorn.xandr.models.FlutterState import de.thekorn.xandr.models.MultiAdRequestRegistry +import io.flutter.Log @SuppressLint("ViewConstructor") class BannerAd( private var activity: Activity, private var state: FlutterState, private var widgetId: Int -) : BannerAdView(activity), DefaultLifecycleObserver, Application.ActivityLifecycleCallbacks { +) : BannerAdView(activity), + DefaultLifecycleObserver, + Application.ActivityLifecycleCallbacks { init { activity.application.registerActivityLifecycleCallbacks(this) } fun configure(bannerViewOptions: BannerViewOptions) { - bannerViewOptions.let { it.adSizes?.let { adSizes -> this.adSizes = adSizes diff --git a/packages/xandr_android/lib/src/messages.g.dart b/packages/xandr_android/lib/src/messages.g.dart index 7be380e..a68f629 100644 --- a/packages/xandr_android/lib/src/messages.g.dart +++ b/packages/xandr_android/lib/src/messages.g.dart @@ -1,4 +1,4 @@ -// Autogenerated from Pigeon (v21.0.0), do not edit directly. +// Autogenerated from Pigeon (v21.1.0), do not edit directly. // See also: https://pub.dev/packages/pigeon // ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import, no_leading_underscores_for_local_identifiers diff --git a/packages/xandr_android/lib/xandr_android.dart b/packages/xandr_android/lib/xandr_android.dart index 86e3e6b..2598bc8 100644 --- a/packages/xandr_android/lib/xandr_android.dart +++ b/packages/xandr_android/lib/xandr_android.dart @@ -235,114 +235,3 @@ class XandrEventHandler implements messages.XandrFlutterApi { .add(NativeBannerAdLoadedErrorEvent(viewId: viewId, reason: reason)); } } - -/// Represents an error event for a banner ad. -/// This class is an abstract class that extends [BannerAdEvent]. -abstract class BannerAdErrorEvent extends BannerAdEvent { - /// Represents an event that occurs when a banner ad encounters an error. - /// - /// This event provides information about the error that occurred. - BannerAdErrorEvent({ - required super.viewId, - required this.reason, - }); - - /// The reason describing the error. - final String reason; -} - -/// Represents an event that is triggered when a banner ad is successfully -/// loaded. -class BannerAdLoadedEvent extends BannerAdEvent { - /// Represents an event indicating that a banner ad has been loaded. - /// - /// This event is triggered when a banner ad is successfully loaded and ready - /// to be displayed. - - BannerAdLoadedEvent({ - required super.viewId, - required this.width, - required this.height, - required this.creativeId, - required this.adType, - required this.tagId, - required this.auctionId, - required this.cpm, - required this.memberId, - }); - - /// The width of the object. - final int width; - - /// The height of the widget. - final int height; - - /// The ID of the creative. - final String creativeId; - - /// The type of the ad. - final String adType; - - /// The tag ID for Xandr Android. - final String tagId; - - /// The unique identifier for the auction. - final String auctionId; - - /// The cost per thousand impressions (CPM) for the Xandr Android package. - final double cpm; - - /// The member ID. - final int memberId; -} - -/// Represents an event that occurs when a banner ad fails to load. -/// This event is a subclass of [BannerAdErrorEvent]. -class BannerAdLoadedErrorEvent extends BannerAdErrorEvent { - /// Represents an event that occurs when a banner ad fails to load. - /// - /// This event is triggered when there is an error while loading a banner ad. - /// It provides information about the error that occurred. - BannerAdLoadedErrorEvent({ - required super.viewId, - required super.reason, - }); -} - -/// Represents an event indicating that a native banner ad has been loaded. -/// This event is a subclass of [BannerAdEvent]. -class NativeBannerAdLoadedEvent extends BannerAdEvent { - /// Represents an event indicating that a native banner ad has been loaded. - /// - /// This event is triggered when a native banner ad is successfully loaded - /// and ready to be displayed. - NativeBannerAdLoadedEvent({ - required super.viewId, - required this.title, - required this.description, - required this.imageUrl, - }); - - /// The title which should be shown within the native ad. - final String title; - - /// The description which should be shown within the native ad. - final String description; - - /// The URL of the main image within in the native ad. - final String imageUrl; -} - -/// Represents an event that is triggered when a native banner ad fails to load. -/// Extends the [BannerAdErrorEvent] class. -class NativeBannerAdLoadedErrorEvent extends BannerAdErrorEvent { - /// Represents an event that occurs when a native banner ad fails to load. - /// - /// This event is triggered when there is an error while loading a native - /// banner ad. - /// It provides information about the error that occurred. - NativeBannerAdLoadedErrorEvent({ - required super.viewId, - required super.reason, - }); -} diff --git a/packages/xandr_ios/lib/xandr_ios.dart b/packages/xandr_ios/lib/xandr_ios.dart index 2808a73..18feebc 100644 --- a/packages/xandr_ios/lib/xandr_ios.dart +++ b/packages/xandr_ios/lib/xandr_ios.dart @@ -235,114 +235,3 @@ class XandrEventHandler implements messages.XandrFlutterApi { .add(NativeBannerAdLoadedErrorEvent(viewId: viewId, reason: reason)); } } - -/// Represents an error event for a banner ad. -/// This class is an abstract class that extends [BannerAdEvent]. -abstract class BannerAdErrorEvent extends BannerAdEvent { - /// Represents an event that occurs when a banner ad encounters an error. - /// - /// This event provides information about the error that occurred. - BannerAdErrorEvent({ - required super.viewId, - required this.reason, - }); - - /// The reason describing the error. - final String reason; -} - -/// Represents an event that is triggered when a banner ad is successfully -/// loaded. -class BannerAdLoadedEvent extends BannerAdEvent { - /// Represents an event indicating that a banner ad has been loaded. - /// - /// This event is triggered when a banner ad is successfully loaded and ready - /// to be displayed. - - BannerAdLoadedEvent({ - required super.viewId, - required this.width, - required this.height, - required this.creativeId, - required this.adType, - required this.tagId, - required this.auctionId, - required this.cpm, - required this.memberId, - }); - - /// The width of the object. - final int width; - - /// The height of the widget. - final int height; - - /// The ID of the creative. - final String creativeId; - - /// The type of the ad. - final String adType; - - /// The tag ID for Xandr Android. - final String tagId; - - /// The unique identifier for the auction. - final String auctionId; - - /// The cost per thousand impressions (CPM) for the Xandr Android package. - final double cpm; - - /// The member ID. - final int memberId; -} - -/// Represents an event that occurs when a banner ad fails to load. -/// This event is a subclass of [BannerAdErrorEvent]. -class BannerAdLoadedErrorEvent extends BannerAdErrorEvent { - /// Represents an event that occurs when a banner ad fails to load. - /// - /// This event is triggered when there is an error while loading a banner ad. - /// It provides information about the error that occurred. - BannerAdLoadedErrorEvent({ - required super.viewId, - required super.reason, - }); -} - -/// Represents an event indicating that a native banner ad has been loaded. -/// This event is a subclass of [BannerAdEvent]. -class NativeBannerAdLoadedEvent extends BannerAdEvent { - /// Represents an event indicating that a native banner ad has been loaded. - /// - /// This event is triggered when a native banner ad is successfully loaded - /// and ready to be displayed. - NativeBannerAdLoadedEvent({ - required super.viewId, - required this.title, - required this.description, - required this.imageUrl, - }); - - /// The title which should be shown within the native ad. - final String title; - - /// The description which should be shown within the native ad. - final String description; - - /// The URL of the main image within in the native ad. - final String imageUrl; -} - -/// Represents an event that is triggered when a native banner ad fails to load. -/// Extends the [BannerAdErrorEvent] class. -class NativeBannerAdLoadedErrorEvent extends BannerAdErrorEvent { - /// Represents an event that occurs when a native banner ad fails to load. - /// - /// This event is triggered when there is an error while loading a native - /// banner ad. - /// It provides information about the error that occurred. - NativeBannerAdLoadedErrorEvent({ - required super.viewId, - required super.reason, - }); -} diff --git a/packages/xandr_platform_interface/lib/src/types.dart b/packages/xandr_platform_interface/lib/src/types.dart index 5bbe3d6..42cba44 100644 --- a/packages/xandr_platform_interface/lib/src/types.dart +++ b/packages/xandr_platform_interface/lib/src/types.dart @@ -10,6 +10,117 @@ abstract class BannerAdEvent { final int viewId; } +/// Represents an error event for a banner ad. +/// This class is an abstract class that extends [BannerAdEvent]. +abstract class BannerAdErrorEvent extends BannerAdEvent { + /// Represents an event that occurs when a banner ad encounters an error. + /// + /// This event provides information about the error that occurred. + BannerAdErrorEvent({ + required super.viewId, + required this.reason, + }); + + /// The reason describing the error. + final String reason; +} + +/// Represents an event that is triggered when a banner ad is successfully +/// loaded. +class BannerAdLoadedEvent extends BannerAdEvent { + /// Represents an event indicating that a banner ad has been loaded. + /// + /// This event is triggered when a banner ad is successfully loaded and ready + /// to be displayed. + + BannerAdLoadedEvent({ + required super.viewId, + required this.width, + required this.height, + required this.creativeId, + required this.adType, + required this.tagId, + required this.auctionId, + required this.cpm, + required this.memberId, + }); + + /// The width of the object. + final int width; + + /// The height of the widget. + final int height; + + /// The ID of the creative. + final String creativeId; + + /// The type of the ad. + final String adType; + + /// The tag ID for Xandr Android. + final String tagId; + + /// The unique identifier for the auction. + final String auctionId; + + /// The cost per thousand impressions (CPM) for the Xandr Android package. + final double cpm; + + /// The member ID. + final int memberId; +} + +/// Represents an event that occurs when a banner ad fails to load. +/// This event is a subclass of [BannerAdErrorEvent]. +class BannerAdLoadedErrorEvent extends BannerAdErrorEvent { + /// Represents an event that occurs when a banner ad fails to load. + /// + /// This event is triggered when there is an error while loading a banner ad. + /// It provides information about the error that occurred. + BannerAdLoadedErrorEvent({ + required super.viewId, + required super.reason, + }); +} + +/// Represents an event indicating that a native banner ad has been loaded. +/// This event is a subclass of [BannerAdEvent]. +class NativeBannerAdLoadedEvent extends BannerAdEvent { + /// Represents an event indicating that a native banner ad has been loaded. + /// + /// This event is triggered when a native banner ad is successfully loaded + /// and ready to be displayed. + NativeBannerAdLoadedEvent({ + required super.viewId, + required this.title, + required this.description, + required this.imageUrl, + }); + + /// The title which should be shown within the native ad. + final String title; + + /// The description which should be shown within the native ad. + final String description; + + /// The URL of the main image within in the native ad. + final String imageUrl; +} + +/// Represents an event that is triggered when a native banner ad fails to load. +/// Extends the [BannerAdErrorEvent] class. +class NativeBannerAdLoadedErrorEvent extends BannerAdErrorEvent { + /// Represents an event that occurs when a native banner ad fails to load. + /// + /// This event is triggered when there is an error while loading a native + /// banner ad. + /// It provides information about the error that occurred. + NativeBannerAdLoadedErrorEvent({ + required super.viewId, + required super.reason, + }); +} + /// A typedef representing a map of custom keywords. /// /// The keys are strings representing the keyword names, From 93f3e4446c45ac7d0b53cb49edac5b425346bb9f Mon Sep 17 00:00:00 2001 From: Massimiliano Massaro Date: Tue, 30 Jul 2024 11:30:29 +0200 Subject: [PATCH 2/2] fix: lint warnings --- packages/xandr/example/lib/main.dart | 2 ++ .../xandr/example/lib/main_fadeOnDone.dart | 1 - packages/xandr/lib/ad_banner.dart | 32 ++++++++----------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/xandr/example/lib/main.dart b/packages/xandr/example/lib/main.dart index 35f3344..6639f1e 100644 --- a/packages/xandr/example/lib/main.dart +++ b/packages/xandr/example/lib/main.dart @@ -93,6 +93,8 @@ class _XandrExampleState extends State { customKeywords: const { 'kw': ['test-kw', 'demoads'], }, + onBannerFinishLoading: ({required success, height, width}) => + debugPrint('on banner finish loading: success: $success'), //resizeAdToFitContainer: false, //enableLazyLoad: true, ), diff --git a/packages/xandr/example/lib/main_fadeOnDone.dart b/packages/xandr/example/lib/main_fadeOnDone.dart index f94684c..d649661 100644 --- a/packages/xandr/example/lib/main_fadeOnDone.dart +++ b/packages/xandr/example/lib/main_fadeOnDone.dart @@ -3,7 +3,6 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:xandr/ad_banner.dart'; import 'package:xandr/ad_size.dart'; -import 'package:xandr/load_mode.dart'; import 'package:xandr/xandr.dart'; void main() { diff --git a/packages/xandr/lib/ad_banner.dart b/packages/xandr/lib/ad_banner.dart index 16e9dbe..7fe5ca7 100644 --- a/packages/xandr/lib/ad_banner.dart +++ b/packages/xandr/lib/ad_banner.dart @@ -7,7 +7,6 @@ import 'package:flutter/widgets.dart'; import 'package:xandr/ad_size.dart'; import 'package:xandr/load_mode.dart'; import 'package:xandr/xandr.dart'; -import 'package:xandr_android/xandr_android.dart'; /// A widget that displays an banner advertisement. class AdBanner extends StatefulWidget { @@ -107,7 +106,10 @@ class AdBanner extends StatefulWidget { /// The controller for managing multi ad requests. final MultiAdRequestController? multiAdRequestController; - final _DoneLoadingCallback? onBannerFinishLoading; + /// Callback called when the ad finishes loading + /// whether it's success or failure + /// This also provides, if success, width and height of the ad + final DoneLoadingCallback? onBannerFinishLoading; /// A completer that indicates when loading is done. final Completer doneLoading = Completer(); @@ -179,13 +181,11 @@ class _AdBannerState extends State { } void onDoneLoading({required bool success, int? width, int? height}) { - if (widget.onBannerFinishLoading != null) { - widget.onBannerFinishLoading!( - success: success, - width: width, - height: height, - ); - } + widget.onBannerFinishLoading?.call( + success: success, + width: width, + height: height, + ); debugPrint('>>>> onDoneLoading: $success'); setState(() { @@ -285,7 +285,9 @@ enum ClickThroughAction { } } -typedef _DoneLoadingCallback = void Function({ +/// Represents a callback which is called when an ad is either loaded or +/// throws an error +typedef DoneLoadingCallback = void Function({ required bool success, int? width, int? height, @@ -305,14 +307,13 @@ class _HostAdBannerView extends StatelessWidget { required int layoutWidth, required bool resizeAdToFitContainer, required LoadMode loadMode, - required _DoneLoadingCallback onDoneLoading, + required DoneLoadingCallback onDoneLoading, required this.widgetId, required String? multiAdRequestId, ClickThroughAction? clickThroughAction, bool? loadsInBackground, bool? shouldServePSAs, bool? enableLazyLoad, - this.delegate, }) : _onDoneLoading = onDoneLoading, creationParams = { 'placementID': placementID, @@ -347,8 +348,7 @@ class _HostAdBannerView extends StatelessWidget { static const StandardMessageCodec _decoder = StandardMessageCodec(); final Map creationParams; final XandrController controller; - final BannerAdEventDelegate? delegate; - final _DoneLoadingCallback _onDoneLoading; + final DoneLoadingCallback _onDoneLoading; final Completer widgetId; static const viewType = 'de.thekorn.xandr/ad_banner'; @@ -390,16 +390,12 @@ class _HostAdBannerView extends StatelessWidget { debugPrint('>>>> controller listen: $event'); if (event is BannerAdLoadedEvent) { _onDoneLoading(success: true, width: event.width, height: event.height); - delegate?.onBannerAdLoaded?.call(event); } else if (event is BannerAdLoadedErrorEvent) { _onDoneLoading(success: false); - delegate?.onBannerAdLoadedError?.call(event); } else if (event is NativeBannerAdLoadedEvent) { _onDoneLoading(success: true); - delegate?.onNativeBannerAdLoaded?.call(event); } else if (event is NativeBannerAdLoadedErrorEvent) { _onDoneLoading(success: false); - delegate?.onNativeBannerAdLoadedError?.call(event); } }); }