Skip to content

Commit

Permalink
feat: allow banner to be updated after call show more than 1 time
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
distante committed Oct 8, 2020
1 parent d7f0340 commit e44821d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ public void initialize() {
}

public void showBanner(final PluginCall call) {
/**
* TODO: Allow the user to manually reload the ad? (ignore mAdView != null)
* Why? Well the user could remove their personalized ads consent and we need to update that!
*/
final AdOptions adOptions = AdOptions.getFactory().createBannerOptions(call);

if (mAdView != null) {
updateExistingAdView(adOptions);
return;
}

final AdOptions adOptions = AdOptions.getFactory().createBannerOptions(call);

// Why a try catch block?
try {
mAdView = new AdView(contextSupplier.get());
Expand Down Expand Up @@ -82,64 +79,7 @@ public void showBanner(final PluginCall call) {
int densityMargin = (int) (adOptions.margin * density);
mAdViewLayoutParams.setMargins(0, densityMargin, 0, densityMargin);

// Run AdMob In Main UI Thread
activitySupplier
.get()
.runOnUiThread(
() -> {
final AdRequest adRequest = RequestHelper.createRequest(adOptions);
// Assign the correct id needed
AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get());
// Add the AdView to the view hierarchy.
mAdViewLayout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequest);

mAdView.setAdListener(
new AdListener() {

@Override
public void onAdLoaded() {
notifyListenersFunction.accept("onAdLoaded", new JSObject().put("value", true));

JSObject ret = new JSObject();
ret.put("width", mAdView.getAdSize().getWidth());
ret.put("height", mAdView.getAdSize().getHeight());
notifyListeners("onAdSize", ret);

super.onAdLoaded();
}

@Override
public void onAdFailedToLoad(int i) {
notifyListeners("onAdFailedToLoad", new JSObject().put("errorCode", i));

JSObject ret = new JSObject();
ret.put("width", 0);
ret.put("height", 0);
notifyListeners("onAdSize", ret);

super.onAdFailedToLoad(i);
}

@Override
public void onAdOpened() {
notifyListeners("onAdOpened", new JSObject().put("value", true));
super.onAdOpened();
}

@Override
public void onAdClosed() {
notifyListeners("onAdClosed", new JSObject().put("value", true));
super.onAdClosed();
}
}
);

// Add AdViewLayout top of the WebView
mViewGroup.addView(mAdViewLayout);
}
);
createNewAdView(adOptions);

call.success(new JSObject().put("value", true));
} catch (Exception ex) {
Expand Down Expand Up @@ -220,4 +160,76 @@ public void removeBanner(final PluginCall call) {
call.error(ex.getLocalizedMessage(), ex);
}
}

private void updateExistingAdView(AdOptions adOptions) {
activitySupplier
.get()
.runOnUiThread(
() -> {
final AdRequest adRequest = RequestHelper.createRequest(adOptions);
mAdView.loadAd(adRequest);
}
);
}

private void createNewAdView(AdOptions adOptions) {
// Run AdMob In Main UI Thread
activitySupplier
.get()
.runOnUiThread(
() -> {
final AdRequest adRequest = RequestHelper.createRequest(adOptions);
// Assign the correct id needed
AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get());
// Add the AdView to the view hierarchy.
mAdViewLayout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequest);

mAdView.setAdListener(
new AdListener() {

@Override
public void onAdLoaded() {
notifyListenersFunction.accept("onAdLoaded", new JSObject().put("value", true));

JSObject ret = new JSObject();
ret.put("width", mAdView.getAdSize().getWidth());
ret.put("height", mAdView.getAdSize().getHeight());
notifyListeners("onAdSize", ret);

super.onAdLoaded();
}

@Override
public void onAdFailedToLoad(int i) {
notifyListeners("onAdFailedToLoad", new JSObject().put("errorCode", i));

JSObject ret = new JSObject();
ret.put("width", 0);
ret.put("height", 0);
notifyListeners("onAdSize", ret);

super.onAdFailedToLoad(i);
}

@Override
public void onAdOpened() {
notifyListeners("onAdOpened", new JSObject().put("value", true));
super.onAdOpened();
}

@Override
public void onAdClosed() {
notifyListeners("onAdClosed", new JSObject().put("value", true));
super.onAdClosed();
}
}
);

// Add AdViewLayout top of the WebView
mViewGroup.addView(mAdViewLayout);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
Expand All @@ -23,6 +24,7 @@
import com.getcapacitor.community.admob.models.AdOptions;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.common.util.BiConsumer;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -145,11 +147,16 @@ void showBannerUsesRequestHelper() {
}

@Test
@DisplayName("Updates the banner if more than 1 show request is done")
@DisplayName("Updates the banner if more than one show request is done")
void showBanner() {
PluginCall pluginCallMock = mock(PluginCall.class);

sut.showBanner(pluginCallMock);
sut.showBanner(pluginCallMock);

verify(activityMock, atLeast(1)).runOnUiThread(runnableArgumentCaptor.capture());
List<Runnable> uiThreadRunnableSecondCall = runnableArgumentCaptor.getAllValues();
uiThreadRunnableSecondCall.forEach(Runnable::run);

AdView adViewMocked = adViewMockedConstruction.constructed().get(0);
verify(adViewMocked, times(2)).loadAd(any());
Expand Down

0 comments on commit e44821d

Please sign in to comment.