Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interstitials and rewarded ads #18

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions android/src/main/java/io/invertase/googleads/ReactNativeAppModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package io.invertase.googleads;
mikehardy marked this conversation as resolved.
Show resolved Hide resolved

/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import io.invertase.googleads.common.ReactNativeApp;
import io.invertase.googleads.common.RCTConvert;
import io.invertase.googleads.common.ReactNativeEvent;
import io.invertase.googleads.common.ReactNativeEventEmitter;
import io.invertase.googleads.common.ReactNativeJSON;
import io.invertase.googleads.common.ReactNativeMeta;
import io.invertase.googleads.common.ReactNativePreferences;
import io.invertase.googleads.common.ReactNativeModule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReactNativeAppModule extends ReactNativeModule {
private static final String TAG = "RNAppModule";

ReactNativeAppModule(ReactApplicationContext reactContext) {
super(reactContext, TAG);
}

@Override
public void initialize() {
super.initialize();
ReactNativeEventEmitter.getSharedInstance().attachReactContext(getContext());
}

@ReactMethod
public void initializeApp(ReadableMap options, ReadableMap appConfig, Promise promise) {
// ReactNativeApp reactNativeApp =
// RCTConvertFirebase.readableMapToFirebaseApp(options, appConfig, getContext());

// WritableMap reactNativeAppMap = RCTConvertFirebase.reactNativeAppToWritableMap(reactNativeApp);
// promise.resolve(reactNativeAppMap);
promise.resolve(options);
}

@ReactMethod
public void setAutomaticDataCollectionEnabled(String appName, Boolean enabled) {
// ReactNativeApp reactNativeApp = ReactNativeApp.getInstance(appName);
// reactNativeApp.setDataCollectionDefaultEnabled(enabled);
}

@ReactMethod
public void deleteApp(String appName, Promise promise) {
// ReactNativeApp reactNativeApp = ReactNativeApp.getInstance(appName);

// if (reactNativeApp != null) {
// reactNativeApp.delete();
// }

promise.resolve(null);
}

@ReactMethod
public void eventsNotifyReady(Boolean ready) {
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance();
emitter.notifyJsReady(ready);
}

@ReactMethod
public void eventsGetListeners(Promise promise) {
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance();
promise.resolve(emitter.getListenersMap());
}

@ReactMethod
public void eventsPing(String eventName, ReadableMap eventBody, Promise promise) {
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance();
emitter.sendEvent(
new ReactNativeEvent(
eventName, RCTConvert.readableMapToWritableMap(eventBody)));
promise.resolve(RCTConvert.readableMapToWritableMap(eventBody));
}

@ReactMethod
public void eventsAddListener(String eventName) {
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance();
emitter.addListener(eventName);
}

@ReactMethod
public void eventsRemoveListener(String eventName, Boolean all) {
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance();
emitter.removeListener(eventName, all);
}

@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
}

@ReactMethod
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}

/** ------------------ META ------------------ */
@ReactMethod
public void metaGetAll(Promise promise) {
promise.resolve(ReactNativeMeta.getSharedInstance().getAll());
}

/** ------------------ JSON ------------------ */
@ReactMethod
public void jsonGetAll(Promise promise) {
promise.resolve(ReactNativeJSON.getSharedInstance().getAll());
}

/** ------------------ PREFERENCES ------------------ */
@ReactMethod
public void preferencesSetBool(String key, boolean value, Promise promise) {
ReactNativePreferences.getSharedInstance().setBooleanValue(key, value);
promise.resolve(null);
}

@ReactMethod
public void preferencesSetString(String key, String value, Promise promise) {
ReactNativePreferences.getSharedInstance().setStringValue(key, value);
promise.resolve(null);
}

@ReactMethod
public void preferencesGetAll(Promise promise) {
promise.resolve(ReactNativePreferences.getSharedInstance().getAll());
}

@ReactMethod
public void preferencesClearAll(Promise promise) {
ReactNativePreferences.getSharedInstance().clearAll();
promise.resolve(null);
}

@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
// List<Map<String, Object>> appsList = new ArrayList<>();
// List<ReactNativeApp> reactNativeApps = ReactNativeApp.getApps(getReactApplicationContext());

// for (ReactNativeApp app : reactNativeApps) {
// appsList.add(RCTConvertFirebase.reactNativeAppToMap(app));
// }

// constants.put("NATIVE_FIREBASE_APPS", appsList);

// constants.put("FIREBASE_RAW_JSON", ReactNativeJSON.getSharedInstance().getRawJSON());

return constants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.List;

public class ReactNativeGoogleAdsConsentModule extends ReactNativeModule {
private static final String TAG = "GoogleAdsConsent";
private static final String TAG = "RNGoogleAdsConsentModule";
private ConsentInformation consentInformation;
private ConsentForm consentForm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import javax.annotation.Nullable;

public class ReactNativeGoogleAdsInterstitialModule extends ReactNativeModule {
private static final String SERVICE = "GoogleAdsInterstitial";
private static final String SERVICE = "RNGoogleAdsInterstitialModule";
private static SparseArray<InterstitialAd> interstitialAdArray = new SparseArray<>();

public ReactNativeGoogleAdsInterstitialModule(ReactApplicationContext reactContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Objects;

public class ReactNativeGoogleAdsModule extends ReactNativeModule {
private static final String SERVICE = "GoogleAds";
private static final String SERVICE = "RNGoogleAdsModule";

ReactNativeGoogleAdsModule(ReactApplicationContext reactContext) {
super(reactContext, SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ReactNativeGoogleAdsPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ReactNativeAppModule(reactContext));
modules.add(new ReactNativeGoogleAdsModule(reactContext));
modules.add(new ReactNativeGoogleAdsConsentModule(reactContext));
modules.add(new ReactNativeGoogleAdsInterstitialModule(reactContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.invertase.googleads.common.ReactNativeModule;

public class ReactNativeGoogleAdsRewardedModule extends ReactNativeModule {
private static final String SERVICE = "GoogleAdsRewarded";
private static final String SERVICE = "RNGoogleAdsRewardedModule";
private static SparseArray<RewardedAd> rewardedAdArray = new SparseArray<>();

public ReactNativeGoogleAdsRewardedModule(ReactApplicationContext reactContext) {
Expand Down
118 changes: 118 additions & 0 deletions android/src/main/java/io/invertase/googleads/common/RCTConvert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package io.invertase.googleads.common;

/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import android.content.Context;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/**
* Utilities to convert to and from React Native bridge formats.
*/
public class RCTConvert {
private static String TAG = "RCTConvert";

/**
* Takes a value and calls the appropriate setter for its type on the target map + key
*
* @param key String key to set on target map
* @param value Object value to set on target map
* @param map WritableMap target map to write the value to
*/
@SuppressWarnings("unchecked")
public static WritableMap mapPutValue(String key, @Nullable Object value, WritableMap map) {
if (value == null) {
map.putNull(key);
return map;
}

String type = value.getClass().getName();

switch (type) {
case "java.lang.Boolean":
map.putBoolean(key, (Boolean) value);
break;
case "java.lang.Long":
Long longVal = (Long) value;
map.putDouble(key, (double) longVal);
break;
case "java.lang.Float":
float floatVal = (float) value;
map.putDouble(key, (double) floatVal);
break;
case "java.lang.Double":
map.putDouble(key, (Double) value);
break;
case "java.lang.Integer":
map.putInt(key, (int) value);
break;
case "java.lang.String":
map.putString(key, (String) value);
break;
case "org.json.JSONObject$1":
map.putString(key, value.toString());
break;
default:
if (List.class.isAssignableFrom(value.getClass())) {
map.putArray(key, Arguments.makeNativeArray((List<Object>) value));
} else if (Map.class.isAssignableFrom(value.getClass())) {
WritableMap childMap = Arguments.createMap();
Map<String, Object> valueMap = (Map<String, Object>) value;

for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
mapPutValue(entry.getKey(), entry.getValue(), childMap);
}

map.putMap(key, childMap);
} else {
Log.d(TAG, "utils:mapPutValue:unknownType:" + type);
map.putNull(key);
}
}

return map;
}

// TODO Remove me - also in SharedUtils
public static WritableMap readableMapToWritableMap(ReadableMap map) {
WritableMap writableMap = Arguments.createMap();
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java#L58
writableMap.merge(map);
return writableMap;
}

public static Map<String, Object> toHashMap(ReadableMap readableMap) {
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java#L263
return readableMap.toHashMap();
}

public static List<Object> toArrayList(ReadableArray readableArray) {
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java#L140
return readableArray.toArrayList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private boolean emit(final NativeEvent event) {
try {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(event.getEventName(), event.getEventBody());
.emit("rnapp_" + event.getEventName(), event.getEventBody());
} catch (Exception e) {
Log.wtf("RN_EVENT_EMITTER", "Error sending Event " + event.getEventName(), e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Activity getActivity() {
@Nonnull
@Override
public String getName() {
return "RNAdMob" + moduleName + "Module";
return moduleName;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion ios/RNGoogleAds/common/RNRCTEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ - (void)notifyJsReady:(BOOL)jsReady {
- (void)sendEventWithName:(NSString *)eventName body:(id)body {
@synchronized(self.jsListeners) {
if (self.bridge && self.isObserving && self.jsListeners[eventName] != nil) {
NSString *prefixedEventName = [@"rnfb_" stringByAppendingString:eventName];
NSString *prefixedEventName = [@"rnapp_" stringByAppendingString:eventName];
[self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[ prefixedEventName, body ] : @[ prefixedEventName ]
Expand Down
Loading