66
77import com .facebook .react .bridge .Arguments ;
88import com .facebook .react .bridge .Callback ;
9+ import com .facebook .react .bridge .Promise ;
910import com .facebook .react .bridge .ReactApplicationContext ;
1011import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1112import com .facebook .react .bridge .ReactMethod ;
@@ -26,8 +27,8 @@ public class RNAdMobInterstitialAdModule extends ReactContextBaseJavaModule {
2627 InterstitialAd mInterstitialAd ;
2728 String adUnitID ;
2829 String [] testDevices ;
29- Callback requestAdCallback ;
30- Callback showAdCallback ;
30+
31+ private Promise mRequestAdPromise ;
3132
3233 @ Override
3334 public String getName () {
@@ -45,7 +46,6 @@ public void run() {
4546 @ Override
4647 public void onAdClosed () {
4748 sendEvent ("interstitialDidClose" , null );
48- showAdCallback .invoke ();
4949 }
5050 @ Override
5151 public void onAdFailedToLoad (int errorCode ) {
@@ -65,9 +65,10 @@ public void onAdFailedToLoad(int errorCode) {
6565 errorString = "ERROR_CODE_NO_FILL" ;
6666 break ;
6767 }
68- event .putString ("error" , errorString );
68+
69+ event .putString ("message" , errorString );
6970 sendEvent ("interstitialDidFailToLoad" , event );
70- requestAdCallback . invoke ( errorString );
71+ mRequestAdPromise . reject ( errorString , errorString );
7172 }
7273 @ Override
7374 public void onAdLeftApplication () {
@@ -76,7 +77,7 @@ public void onAdLeftApplication() {
7677 @ Override
7778 public void onAdLoaded () {
7879 sendEvent ("interstitialDidLoad" , null );
79- requestAdCallback . invoke ( );
80+ mRequestAdPromise . resolve ( null );
8081 }
8182 @ Override
8283 public void onAdOpened () {
@@ -103,14 +104,14 @@ public void setTestDevices(ReadableArray testDevices) {
103104 }
104105
105106 @ ReactMethod
106- public void requestAd (final Callback callback ) {
107+ public void requestAd (final Promise promise ) {
107108 new Handler (Looper .getMainLooper ()).post (new Runnable () {
108109 @ Override
109110 public void run () {
110111 if (mInterstitialAd .isLoaded () || mInterstitialAd .isLoading ()) {
111- callback . invoke ( " Ad is already loaded." ); // TODO: make proper error
112+ promise . reject ( "E_AD_ALREADY_LOADED" , " Ad is already loaded." );
112113 } else {
113- requestAdCallback = callback ;
114+ mRequestAdPromise = promise ;
114115 AdRequest .Builder adRequestBuilder = new AdRequest .Builder ();
115116 if (testDevices != null ) {
116117 for (int i = 0 ; i < testDevices .length ; i ++) {
@@ -125,15 +126,15 @@ public void run () {
125126 }
126127
127128 @ ReactMethod
128- public void showAd (final Callback callback ) {
129+ public void showAd (final Promise promise ) {
129130 new Handler (Looper .getMainLooper ()).post (new Runnable () {
130131 @ Override
131132 public void run () {
132133 if (mInterstitialAd .isLoaded ()) {
133- showAdCallback = callback ;
134134 mInterstitialAd .show ();
135+ promise .resolve (null );
135136 } else {
136- callback . invoke ( " Ad is not ready." ); // TODO: make proper error
137+ promise . reject ( "E_AD_NOT_READY" , " Ad is not ready." );
137138 }
138139 }
139140 });
0 commit comments