Skip to content

Commit

Permalink
Merge pull request #125 from thekorn/fix-onbannerdoneloading-not-call…
Browse files Browse the repository at this point in the history
…ed-ios

fix: _onDoneloading callback not called iOS
  • Loading branch information
thekorn authored Jul 30, 2024
2 parents 57d1a34 + 93f3e44 commit f2df5ea
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 248 deletions.
2 changes: 2 additions & 0 deletions packages/xandr/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class _XandrExampleState extends State<XandrExample> {
customKeywords: const {
'kw': ['test-kw', 'demoads'],
},
onBannerFinishLoading: ({required success, height, width}) =>
debugPrint('on banner finish loading: success: $success'),
//resizeAdToFitContainer: false,
//enableLazyLoad: true,
),
Expand Down
1 change: 0 additions & 1 deletion packages/xandr/example/lib/main_fadeOnDone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 14 additions & 18 deletions packages/xandr/lib/ad_banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<bool> doneLoading = Completer();
Expand Down Expand Up @@ -179,13 +181,11 @@ class _AdBannerState extends State<AdBanner> {
}

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(() {
Expand Down Expand Up @@ -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,
Expand All @@ -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 = <String, dynamic>{
'placementID': placementID,
Expand Down Expand Up @@ -347,8 +348,7 @@ class _HostAdBannerView extends StatelessWidget {
static const StandardMessageCodec _decoder = StandardMessageCodec();
final Map<String, dynamic> creationParams;
final XandrController controller;
final BannerAdEventDelegate? delegate;
final _DoneLoadingCallback _onDoneLoading;
final DoneLoadingCallback _onDoneLoading;
final Completer<int> widgetId;

static const viewType = 'de.thekorn.xandr/ad_banner';
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/xandr_android/lib/src/messages.g.dart
Original file line number Diff line number Diff line change
@@ -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

Expand Down
111 changes: 0 additions & 111 deletions packages/xandr_android/lib/xandr_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Loading

0 comments on commit f2df5ea

Please sign in to comment.