react-native-applovin-max 4.1.7 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import React, {useContext, useRef, useEffect} from "react";
2
- import {findNodeHandle, UIManager, Text, Image, View, TouchableOpacity} from "react-native";
2
+ import {findNodeHandle, Text, Image, View, TouchableOpacity} from "react-native";
3
3
  import {NativeAdViewContext} from "./NativeAdViewProvider";
4
4
 
5
5
  export const TitleView = (props) => {
@@ -7,20 +7,19 @@ export const TitleView = (props) => {
7
7
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
8
8
 
9
9
  useEffect(() => {
10
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
10
+ if (!nativeAdView || !nativeAd.title) return;
11
11
 
12
12
  nativeAdView.setNativeProps({
13
13
  titleView: findNodeHandle(titleRef.current),
14
14
  });
15
15
  }, [nativeAd, nativeAdView]);
16
16
 
17
+ if (!nativeAdView) return null;
18
+
17
19
  return (
18
- nativeAdView ?
19
- <Text {...props} ref={titleRef}>
20
- {nativeAd.title || null}
21
- </Text>
22
- :
23
- null
20
+ <Text {...props} ref={titleRef}>
21
+ {nativeAd.title || null}
22
+ </Text>
24
23
  );
25
24
  };
26
25
 
@@ -29,20 +28,19 @@ export const AdvertiserView = (props) => {
29
28
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
30
29
 
31
30
  useEffect(() => {
32
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
31
+ if (!nativeAdView || !nativeAd.advertiser) return;
33
32
 
34
33
  nativeAdView.setNativeProps({
35
34
  advertiserView: findNodeHandle(advertiserRef.current),
36
35
  });
37
36
  }, [nativeAd, nativeAdView]);
38
37
 
38
+ if (!nativeAdView) return null;
39
+
39
40
  return (
40
- nativeAdView ?
41
- <Text {...props} ref={advertiserRef}>
42
- {nativeAd.advertiser || null}
43
- </Text>
44
- :
45
- null
41
+ <Text {...props} ref={advertiserRef}>
42
+ {nativeAd.advertiser || null}
43
+ </Text>
46
44
  );
47
45
  };
48
46
 
@@ -51,20 +49,19 @@ export const BodyView = (props) => {
51
49
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
52
50
 
53
51
  useEffect(() => {
54
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
52
+ if (!nativeAdView || !nativeAd.body) return;
55
53
 
56
54
  nativeAdView.setNativeProps({
57
55
  bodyView: findNodeHandle(bodyRef.current),
58
56
  });
59
57
  }, [nativeAd, nativeAdView]);
60
58
 
59
+ if (!nativeAdView) return null;
60
+
61
61
  return (
62
- nativeAdView ?
63
- <Text {...props} ref={bodyRef}>
64
- {nativeAd.body || null}
65
- </Text>
66
- :
67
- null
62
+ <Text {...props} ref={bodyRef}>
63
+ {nativeAd.body || null}
64
+ </Text>
68
65
  );
69
66
  };
70
67
 
@@ -73,22 +70,21 @@ export const CallToActionView = (props) => {
73
70
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
74
71
 
75
72
  useEffect(() => {
76
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
73
+ if (!nativeAdView || !nativeAd.callToAction) return;
77
74
 
78
75
  nativeAdView.setNativeProps({
79
76
  callToActionView: findNodeHandle(callToActionRef.current),
80
77
  });
81
78
  }, [nativeAd, nativeAdView]);
82
79
 
80
+ if (!nativeAdView) return null;
81
+
83
82
  return (
84
- nativeAdView ?
85
- <TouchableOpacity {...props}>
86
- <Text {...props} ref={callToActionRef}>
87
- {nativeAd.callToAction || null}
88
- </Text>
89
- </TouchableOpacity>
90
- :
91
- null
83
+ <TouchableOpacity {...props}>
84
+ <Text {...props} ref={callToActionRef}>
85
+ {nativeAd.callToAction || null}
86
+ </Text>
87
+ </TouchableOpacity>
92
88
  );
93
89
  };
94
90
 
@@ -98,18 +94,17 @@ export const IconView = (props) => {
98
94
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
99
95
 
100
96
  useEffect(() => {
101
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
97
+ if (!nativeAdView || !(nativeAd.url || nativeAd.image)) return;
102
98
 
103
99
  nativeAdView.setNativeProps({
104
100
  iconView: findNodeHandle(imageRef.current),
105
101
  });
106
102
  }, [nativeAd, nativeAdView]);
107
103
 
104
+ if (!nativeAdView) return null;
105
+
108
106
  return (
109
- nativeAdView ?
110
- <Image {...props} ref={imageRef} source={{uri: nativeAd.url || null}}/>
111
- :
112
- null
107
+ <Image {...props} ref={imageRef} source={{uri: nativeAd.url || null}}/>
113
108
  );
114
109
  };
115
110
 
@@ -119,18 +114,17 @@ export const OptionsView = (props) => {
119
114
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
120
115
 
121
116
  useEffect(() => {
122
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
117
+ if (!nativeAdView || !nativeAd.isOptionsViewAvailable) return;
123
118
 
124
119
  nativeAdView.setNativeProps({
125
120
  optionsView: findNodeHandle(viewRef.current),
126
121
  });
127
122
  }, [nativeAd, nativeAdView]);
128
123
 
124
+ if (!nativeAdView) return null;
125
+
129
126
  return (
130
- nativeAdView ?
131
- <View {...props} ref={viewRef}/>
132
- :
133
- null
127
+ <View {...props} ref={viewRef}/>
134
128
  );
135
129
  };
136
130
 
@@ -140,17 +134,16 @@ export const MediaView = (props) => {
140
134
  const {nativeAd, nativeAdView} = useContext(NativeAdViewContext);
141
135
 
142
136
  useEffect(() => {
143
- if (Object.keys(nativeAd).length === 0 || !nativeAdView) return;
137
+ if (!nativeAdView || !nativeAd.isMediaViewAvailable) return;
144
138
 
145
139
  nativeAdView.setNativeProps({
146
140
  mediaView: findNodeHandle(viewRef.current),
147
141
  });
148
142
  }, [nativeAd, nativeAdView]);
149
143
 
144
+ if (!nativeAdView) return null;
145
+
150
146
  return (
151
- nativeAdView ?
152
- <View {...props} ref={viewRef}/>
153
- :
154
- null
147
+ <View {...props} ref={viewRef}/>
155
148
  );
156
149
  };
@@ -1,40 +1,19 @@
1
- import React, { forwardRef, useContext, useImperativeHandle, useRef, useCallback } from "react";
1
+ import React, { forwardRef, useContext, useImperativeHandle, useRef, useState, useEffect } from "react";
2
2
  import PropTypes from "prop-types";
3
- import { NativeModules, requireNativeComponent, UIManager, findNodeHandle, View, StyleSheet } from "react-native";
3
+ import { NativeModules, requireNativeComponent, UIManager, findNodeHandle } from "react-native";
4
4
  import { NativeAdViewContext, NativeAdViewProvider } from "./NativeAdViewProvider";
5
5
  import { TitleView, AdvertiserView, BodyView, CallToActionView, IconView, OptionsView, MediaView } from "./NativeAdComponents";
6
6
 
7
7
  const { AppLovinMAX } = NativeModules;
8
8
 
9
- // Returns NativeAdView if AppLovinMAX has been initialized, or returns an empty black view if
10
- // AppLovinMAX has not been initialized
11
9
  const NativeAdViewWrapper = forwardRef((props, ref) => {
12
- const {style, ...rest} = props;
13
10
  return (
14
- AppLovinMAX.isInitialized() ?
15
- <NativeAdViewProvider>
16
- <NativeAdView {...props} ref={ref}/>
17
- </NativeAdViewProvider>
18
- :
19
- <View style={[styles.container, style]} {...rest}>
20
- {
21
- console.warn('[AppLovinSdk] [AppLovinMAX] <NativeAdView/> has been mounted before AppLovin initialization')
22
- }
23
- </View>
11
+ <NativeAdViewProvider>
12
+ <NativeAdView {...props} ref={ref}/>
13
+ </NativeAdViewProvider>
24
14
  );
25
15
  });
26
16
 
27
- const styles = StyleSheet.create({
28
- container: {
29
- flexDirection: 'column',
30
- justifyContent: 'flex-end',
31
- alignItems: 'center',
32
- backgroundColor: 'black',
33
- borderColor: 'whitesmoke',
34
- borderWidth: 1,
35
- },
36
- });
37
-
38
17
  const AppLovinMAXNativeAdView = requireNativeComponent('AppLovinMAXNativeAdView', NativeAdView);
39
18
 
40
19
  // NativeAdView renders itself multiple times:
@@ -44,6 +23,8 @@ const AppLovinMAXNativeAdView = requireNativeComponent('AppLovinMAXNativeAdView'
44
23
  // 3. update of the nativeAd context by onNativeAdLoaded, which renders the ad components with nativeAd
45
24
  const NativeAdView = forwardRef((props, ref) => {
46
25
 
26
+ const {extraParameters, localExtraParameters, ...otherProps} = props;
27
+
47
28
  // context from NativeAdViewProvider
48
29
  const {nativeAd, nativeAdView, setNativeAd, setNativeAdView} = useContext(NativeAdViewContext);
49
30
 
@@ -65,20 +46,59 @@ const NativeAdView = forwardRef((props, ref) => {
65
46
  useImperativeHandle(ref, () => ({ loadAd }), []);
66
47
 
67
48
  // save the DOM element via the ref callback
68
- const saveElement = useCallback((element) => {
49
+ const saveElement = (element) => {
69
50
  if (element) {
70
51
  nativeAdViewRef.current = element;
71
52
  setNativeAdView(element);
72
53
  }
73
- }, [nativeAdViewRef]);
54
+ };
55
+
56
+ const onAdLoadedEvent = (event) => {
57
+ setNativeAd(event.nativeEvent.nativeAd);
58
+ if (props.onAdLoaded) props.onAdLoaded(event.nativeEvent.adInfo);
59
+ };
60
+
61
+ const onAdLoadFailedEvent = (event) => {
62
+ if (props.onAdLoadFailed) props.onAdLoadFailed(event.nativeEvent);
63
+ };
64
+
65
+ const onAdClickedEvent = (event) => {
66
+ if (props.onAdClicked) props.onAdClicked(event.nativeEvent);
67
+ };
68
+
69
+ const onAdRevenuePaidEvent = (event) => {
70
+ if (props.onAdRevenuePaid) props.onAdRevenuePaid(event.nativeEvent);
71
+ };
72
+
73
+ const sanitizeExtraParameters = (name, params) => {
74
+ if (!params) return params;
75
+
76
+ for (const key in params) {
77
+ const value = params[key];
78
+
79
+ // `null` and `undefined` are valid values (e.g. for clearing previously-set values)
80
+ if (value == null || value == undefined) continue;
81
+
82
+ if (typeof value !== 'string') {
83
+ console.warn("AppLovinMAXNativeAdView only support string values: " + value + ", deleting value for key: " + key);
84
+ delete params[key];
85
+ }
86
+ }
74
87
 
75
- // callback from the native module to set a loaded ad
76
- const onAdLoaded = useCallback((event) => {
77
- setNativeAd(event.nativeEvent);
78
- }, []);
88
+ return params;
89
+ };
79
90
 
80
91
  return (
81
- <AppLovinMAXNativeAdView {...props} ref={saveElement} onAdLoaded={onAdLoaded}>
92
+ <AppLovinMAXNativeAdView
93
+ ref={saveElement}
94
+ extraParameters={sanitizeExtraParameters('extraParameters', extraParameters)}
95
+ localExtraParameters={sanitizeExtraParameters('localExtraParameters', localExtraParameters)}
96
+ onAdLoadedEvent={onAdLoadedEvent}
97
+ onAdLoadFailedEvent={onAdLoadFailedEvent}
98
+ onAdClickedEvent={onAdClickedEvent}
99
+ onAdRevenuePaidEvent={onAdRevenuePaidEvent}
100
+ {...otherProps}
101
+ >
82
102
  {props.children}
83
103
  </AppLovinMAXNativeAdView>
84
104
  );
@@ -101,9 +121,34 @@ NativeAdView.propTypes = {
101
121
  customData: PropTypes.string,
102
122
 
103
123
  /**
104
- * A dictionary value representing the extra parameters to set a list of key-value string pairs.
124
+ * A dictionary of extra parameters consisting of key-value string pairs.
105
125
  */
106
126
  extraParameters: PropTypes.object,
127
+
128
+ /**
129
+ * A dictionary of local extra parameters consisting of key-value string pairs.
130
+ */
131
+ localExtraParameters: PropTypes.object,
132
+
133
+ /**
134
+ * A callback fuction to be fired when a new ad has been loaded.
135
+ */
136
+ onAdLoaded: PropTypes.func,
137
+
138
+ /**
139
+ * A callback fuction to be fired when an ad could not be retrieved.
140
+ */
141
+ onAdLoadFailed: PropTypes.func,
142
+
143
+ /**
144
+ * A callback fuction to be fired when ad is clicked.
145
+ */
146
+ onAdClicked: PropTypes.func,
147
+
148
+ /**
149
+ * A callback fuction to be fired when the revenue event is detected.
150
+ */
151
+ onAdRevenuePaid: PropTypes.func,
107
152
  };
108
153
 
109
154
  // Add the child ad components
@@ -19,11 +19,17 @@ const UserGender = {
19
19
  const TargetingData = {
20
20
 
21
21
  set yearOfBirth(value) {
22
- AppLovinMAX.setTargetingDataYearOfBirth(value);
22
+ AppLovinMAX.setTargetingDataYearOfBirth(value ? value : 0);
23
+ },
24
+
25
+ get yearOfBirth() {
26
+ return AppLovinMAX.getTargetingDataYearOfBirth();
23
27
  },
24
28
 
25
29
  set gender(value) {
26
- if ( value === UserGender.Unknown ||
30
+ if ( !value ) {
31
+ AppLovinMAX.setTargetingDataGender(UserGender.Unknown);
32
+ } else if ( value === UserGender.Unknown ||
27
33
  value === UserGender.Female ||
28
34
  value === UserGender.Male ||
29
35
  value === UserGender.Other ) {
@@ -31,8 +37,14 @@ const TargetingData = {
31
37
  }
32
38
  },
33
39
 
40
+ get gender() {
41
+ return AppLovinMAX.getTargetingDataGender();
42
+ },
43
+
34
44
  set maximumAdContentRating(value) {
35
- if ( value === AdContentRating.None ||
45
+ if ( !value ) {
46
+ AppLovinMAX.setTargetingDataMaximumAdContentRating(AdContentRating.None);
47
+ } else if ( value === AdContentRating.None ||
36
48
  value === AdContentRating.AllAudiences ||
37
49
  value === AdContentRating.EveryoneOverTwelve ||
38
50
  value === AdContentRating.MatureAudiences ) {
@@ -40,22 +52,42 @@ const TargetingData = {
40
52
  }
41
53
  },
42
54
 
55
+ get maximumAdContentRating() {
56
+ return AppLovinMAX.getTargetingDataMaximumAdContentRating();
57
+ },
58
+
43
59
  set email(value) {
44
60
  AppLovinMAX.setTargetingDataEmail(value);
45
61
  },
46
62
 
63
+ get email() {
64
+ return AppLovinMAX.getTargetingDataEmail();
65
+ },
66
+
47
67
  set phoneNumber(value) {
48
68
  AppLovinMAX.setTargetingDataPhoneNumber(value);
49
69
  },
50
70
 
71
+ get phoneNumber() {
72
+ return AppLovinMAX.getTargetingDataPhoneNumber();
73
+ },
74
+
51
75
  set keywords(value) {
52
76
  AppLovinMAX.setTargetingDataKeywords(value);
53
77
  },
54
78
 
79
+ get keywords() {
80
+ return AppLovinMAX.getTargetingDataKeywords();
81
+ },
82
+
55
83
  set interests(value) {
56
84
  AppLovinMAX.setTargetingDataInterests(value);
57
85
  },
58
86
 
87
+ get interests() {
88
+ return AppLovinMAX.getTargetingDataInterests();
89
+ },
90
+
59
91
  clearAll() {
60
92
  AppLovinMAX.clearAllTargetingData();
61
93
  }