Skip to content

Commit

Permalink
use promises instead of callbacks for request and show (Android)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenpunt committed Aug 2, 2017
1 parent a28d0ca commit 239cbf7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
Expand Up @@ -6,6 +6,7 @@

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -26,8 +27,8 @@ public class RNAdMobInterstitialAdModule extends ReactContextBaseJavaModule {
InterstitialAd mInterstitialAd;
String adUnitID;
String[] testDevices;
Callback requestAdCallback;
Callback showAdCallback;

private Promise mRequestAdPromise;

@Override
public String getName() {
Expand All @@ -45,7 +46,6 @@ public void run() {
@Override
public void onAdClosed() {
sendEvent("interstitialDidClose", null);
showAdCallback.invoke();
}
@Override
public void onAdFailedToLoad(int errorCode) {
Expand All @@ -65,9 +65,10 @@ public void onAdFailedToLoad(int errorCode) {
errorString = "ERROR_CODE_NO_FILL";
break;
}
event.putString("error", errorString);

event.putString("message", errorString);
sendEvent("interstitialDidFailToLoad", event);
requestAdCallback.invoke(errorString);
mRequestAdPromise.reject(errorString, errorString);
}
@Override
public void onAdLeftApplication() {
Expand All @@ -76,7 +77,7 @@ public void onAdLeftApplication() {
@Override
public void onAdLoaded() {
sendEvent("interstitialDidLoad", null);
requestAdCallback.invoke();
mRequestAdPromise.resolve(null);
}
@Override
public void onAdOpened() {
Expand All @@ -103,14 +104,14 @@ public void setTestDevices(ReadableArray testDevices) {
}

@ReactMethod
public void requestAd(final Callback callback) {
public void requestAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
if (mInterstitialAd.isLoaded() || mInterstitialAd.isLoading()) {
callback.invoke("Ad is already loaded."); // TODO: make proper error
promise.reject("E_AD_ALREADY_LOADED", "Ad is already loaded.");
} else {
requestAdCallback = callback;
mRequestAdPromise = promise;
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (testDevices != null) {
for (int i = 0; i < testDevices.length; i++) {
Expand All @@ -125,15 +126,15 @@ public void run () {
}

@ReactMethod
public void showAd(final Callback callback) {
public void showAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
if (mInterstitialAd.isLoaded()) {
showAdCallback = callback;
mInterstitialAd.show();
promise.resolve(null);
} else {
callback.invoke("Ad is not ready."); // TODO: make proper error
promise.reject("E_AD_NOT_READY", "Ad is not ready.");
}
}
});
Expand Down
Expand Up @@ -6,6 +6,7 @@

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -25,8 +26,8 @@ public class RNAdMobRewardedVideoAdModule extends ReactContextBaseJavaModule imp
RewardedVideoAd mRewardedVideoAd;
String adUnitID;
String[] testDevices;
Callback requestAdCallback;
Callback showAdCallback;

private Promise mRequestAdPromise;

@Override
public String getName() {
Expand All @@ -50,7 +51,7 @@ public void onRewarded(RewardItem rewardItem) {
@Override
public void onRewardedVideoAdLoaded() {
sendEvent("rewardedVideoDidLoad", null);
requestAdCallback.invoke();
mRequestAdPromise.resolve(null);
}

@Override
Expand Down Expand Up @@ -93,9 +94,9 @@ public void onRewardedVideoAdFailedToLoad(int errorCode) {
break;
}

event.putString("error", errorString);
event.putString("message", errorString);
sendEvent("rewardedVideoDidFailToLoad", event);
requestAdCallback.invoke(errorString);
mRequestAdPromise.reject(errorString, errorString);
}

private void sendEvent(String eventName, @Nullable WritableMap params) {
Expand All @@ -115,7 +116,7 @@ public void setTestDevices(ReadableArray testDevices) {
}

@ReactMethod
public void requestAd(final Callback callback) {
public void requestAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
Expand All @@ -124,9 +125,9 @@ public void run () {
RNAdMobRewardedVideoAdModule.this.mRewardedVideoAd.setRewardedVideoAdListener(RNAdMobRewardedVideoAdModule.this);

if (mRewardedVideoAd.isLoaded()) {
callback.invoke("Ad is already loaded."); // TODO: make proper error
promise.reject("E_AD_ALREADY_LOADED", "Ad is already loaded.");
} else {
requestAdCallback = callback;
mRequestAdPromise = promise;

AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

Expand All @@ -144,15 +145,15 @@ public void run () {
}

@ReactMethod
public void showAd(final Callback callback) {
public void showAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
if (mRewardedVideoAd.isLoaded()) {
showAdCallback = callback;
mRewardedVideoAd.show();
promise.resolve(null);
} else {
callback.invoke("Ad is not ready."); // TODO: make proper error
promise.reject("E_AD_NOT_READY", "Ad is not ready.");
}
}
});
Expand Down

0 comments on commit 239cbf7

Please sign in to comment.