1
1
package com.reactnativeapphudsdk
2
2
import android.util.Log
3
- import androidx.annotation.RequiresPermission.Read
4
3
import com.apphud.sdk.Apphud
5
4
import com.apphud.sdk.ApphudAttributionProvider
6
5
import com.apphud.sdk.ApphudUserPropertyKey
6
+ import com.apphud.sdk.ApphudUtils
7
7
import com.apphud.sdk.domain.ApphudProduct
8
8
import com.apphud.sdk.managers.HeadersInterceptor
9
9
import com.facebook.react.bridge.*
10
10
import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
11
- import java.lang.Exception
12
11
13
12
class ApphudSdkModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
14
13
@@ -22,28 +21,31 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
22
21
23
22
init {
24
23
HeadersInterceptor .X_SDK = " reactnative"
25
- HeadersInterceptor .X_SDK_VERSION = " 2.1 .0"
24
+ HeadersInterceptor .X_SDK_VERSION = " 2.2 .0"
26
25
listener = ApphudListenerHandler (reactContext)
27
26
listener?.let { Apphud .setListener(it) }
28
27
}
29
28
30
29
@ReactMethod
31
- fun start (options : ReadableMap ) {
32
- startManually(options)
30
+ fun start (options : ReadableMap , promise : Promise ) {
31
+ startManually(options, promise )
33
32
}
34
33
35
34
@ReactMethod
36
- fun startManually (options : ReadableMap ) {
35
+ fun startManually (options : ReadableMap , promise : Promise ) {
37
36
val apiKey = options.getString(" apiKey" )
38
37
val userId = options.getString(" userId" )
39
38
val deviceId = options.getString(" deviceId" )
40
39
41
40
if (apiKey.isNullOrEmpty()) {
41
+ promise.reject(" Error" , " apiKey not set" )
42
42
return
43
43
}
44
44
45
45
runOnUiThread {
46
- Apphud .start(this .reactApplicationContext, apiKey!! , userId, deviceId)
46
+ Apphud .start(this .reactApplicationContext, apiKey, userId, deviceId) { _ ->
47
+ promise.resolve(null )
48
+ }
47
49
}
48
50
}
49
51
@@ -64,9 +66,14 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
64
66
65
67
@ReactMethod
66
68
fun paywalls (promise : Promise ) {
67
- Apphud .paywallsDidLoadCallback {
69
+ Apphud .paywallsDidLoadCallback { list, error ->
70
+ if (error != null ) {
71
+ promise.reject(error)
72
+ return @paywallsDidLoadCallback
73
+ }
74
+
68
75
val result = WritableNativeArray ()
69
- for (paywall in it ) {
76
+ for (paywall in list ) {
70
77
result.pushMap(ApphudDataTransformer .getApphudPaywallMap(paywall))
71
78
}
72
79
promise.resolve(result)
@@ -75,18 +82,24 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
75
82
76
83
@ReactMethod
77
84
fun paywallShown (identifier : String ) {
78
- val paywall = Apphud .paywalls().firstOrNull { it.identifier == identifier }
85
+ Apphud .paywallsDidLoadCallback { apphudPaywalls, _ ->
86
+ val paywall = apphudPaywalls.firstOrNull { it.identifier == identifier }
87
+
79
88
paywall?.let {
80
- Apphud .paywallShown(it)
89
+ Apphud .paywallShown(it)
81
90
}
91
+ }
82
92
}
83
93
84
94
@ReactMethod
85
95
fun paywallClosed (identifier : String ) {
86
- val paywall = Apphud .paywalls().firstOrNull { it.identifier == identifier }
96
+ Apphud .paywallsDidLoadCallback { apphudPaywalls, _ ->
97
+ val paywall = apphudPaywalls.firstOrNull { it.identifier == identifier }
98
+
87
99
paywall?.let {
88
- Apphud .paywallClosed(it)
100
+ Apphud .paywallClosed(it)
89
101
}
102
+ }
90
103
}
91
104
92
105
@ReactMethod
@@ -100,35 +113,40 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
100
113
val paywallId = args.getString(" paywallId" )
101
114
102
115
var product: ApphudProduct ? = null
103
- val paywalls = Apphud .paywalls()
104
116
105
- for (paywall in paywalls) {
106
- if (product == null ) {
107
- product = paywall.products?.firstOrNull { p ->
108
- p.product_id == productId && (paywallId.isNullOrEmpty() || p.paywall_identifier == paywallId)
117
+ Apphud .paywallsDidLoadCallback { paywalls, apphudError ->
118
+ if (apphudError != null ) {
119
+ promise.reject(apphudError)
120
+
121
+ return @paywallsDidLoadCallback
122
+ }
123
+
124
+ for (paywall in paywalls) {
125
+ if (product == null ) {
126
+ product = paywall.products?.firstOrNull { p ->
127
+ p.productId == productId && (paywallId.isNullOrEmpty() || p.paywallIdentifier == paywallId)
128
+ }
109
129
}
110
130
}
111
- }
112
131
113
- val isSub = product?.productDetails?.productType?.lowercase() == " subs"
114
- val offerToken = args.getString(" offerToken" )
115
- val isConsumable = if (args.hasKey(" isConsumable" )) args.getBoolean(" isConsumable" ) else false
132
+ val isSub = product?.productDetails?.productType?.lowercase() == " subs"
133
+ val offerToken = args.getString(" offerToken" )
134
+ val isConsumable = if (args.hasKey(" isConsumable" )) args.getBoolean(" isConsumable" ) else false
116
135
117
- if (product == null ) {
118
- promise.reject(" Error" , " Product not found" )
119
- return
120
- }
136
+ if (product == null ) {
137
+ promise.reject(" Error" , " Product not found" )
138
+ return @paywallsDidLoadCallback
139
+ }
121
140
122
- if (isSub && offerToken.isNullOrEmpty()) {
123
- promise.reject(" Error" , " Offer Token not found" )
124
- } else if (! offerToken.isNullOrEmpty()) {
125
- purchaseSubscription(product, offerToken, promise)
126
- } else {
127
- purchaseOneTimeProduct(product, isConsumable, promise)
141
+ if (isSub || product?.productDetails == null ) {
142
+ purchaseSubscription(product!! , offerToken, promise)
143
+ } else {
144
+ purchaseOneTimeProduct(product!! , isConsumable, promise)
145
+ }
128
146
}
129
147
}
130
148
131
- private fun purchaseSubscription (product : ApphudProduct , offerToken : String , promise : Promise ) {
149
+ private fun purchaseSubscription (product : ApphudProduct , offerToken : String? , promise : Promise ) {
132
150
this .currentActivity?.let {
133
151
Apphud .purchase(it, product, offerToken) { res ->
134
152
promise.resolve(ApphudDataTransformer .getPurchaseMap(res))
@@ -173,6 +191,32 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
173
191
}
174
192
}
175
193
194
+ @ReactMethod
195
+ fun attributeFromWeb (options : ReadableMap , promise : Promise ) {
196
+ val data = options.toHashMap().let {
197
+ val result = mutableMapOf<String , Any >()
198
+
199
+ for ((key, value) in it) {
200
+ value?.let { x ->
201
+ result[key] = x
202
+ }
203
+ }
204
+
205
+ return @let result
206
+ }
207
+
208
+ Apphud .attributeFromWeb(data) { success, user ->
209
+ val result = WritableNativeMap ()
210
+
211
+ user?.userId?.let {
212
+ result.putString(" user_id" , it)
213
+ }
214
+ result.putBoolean(" is_premium" , Apphud .hasPremiumAccess())
215
+ result.putBoolean(" result" , success)
216
+ promise.resolve(result)
217
+ }
218
+ }
219
+
176
220
private fun stringToApphudAttributionProvider (value : String ): ApphudAttributionProvider ? {
177
221
return enumValues<ApphudAttributionProvider >().find {
178
222
it.name == value
@@ -300,7 +344,7 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
300
344
301
345
@ReactMethod
302
346
fun enableDebugLogs () {
303
- Apphud .enableDebugLogs ()
347
+ ApphudUtils .enableAllLogs ()
304
348
}
305
349
306
350
@ReactMethod
@@ -314,8 +358,9 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
314
358
}
315
359
316
360
@ReactMethod
317
- fun logout () {
361
+ fun logout (promise : Promise ) {
318
362
Apphud .logout()
363
+ promise.resolve(null )
319
364
}
320
365
321
366
private fun getUserPropertyKey (key : String ): ApphudUserPropertyKey {
0 commit comments