Skip to content

Commit b74d4b1

Browse files
committed
fixed types, fixed paywall logs methods
1 parent 7c589cc commit b74d4b1

File tree

10 files changed

+164
-34
lines changed

10 files changed

+164
-34
lines changed

ApphudPaywallsHelper.swift

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ApphudSDK
2+
import Combine
3+
4+
final class ApphudPaywallsHelper {
5+
static func getPaywall(paywallIdentifier:String?, placementIdentifier: String?) async -> ApphudPaywall? {
6+
if paywallIdentifier == nil, placementIdentifier == nil {
7+
return nil
8+
}
9+
10+
var paywall:ApphudPaywall?
11+
12+
if(placementIdentifier != nil) {
13+
let placements = await Apphud.placements()
14+
paywall = placements
15+
.first(where: {pl in pl.identifier == placementIdentifier})?.paywall
16+
} else {
17+
paywall = await withCheckedContinuation { @MainActor continuation in
18+
Apphud.paywallsDidLoadCallback{ paywalls, error in
19+
paywall = paywalls
20+
.first(where: { pw in return pw.identifier == paywallIdentifier })
21+
continuation.resume(returning: paywall)
22+
}
23+
}
24+
}
25+
return paywall
26+
}
27+
28+
static func getPaywalls() async -> [ApphudPaywall] {
29+
return await withCheckedContinuation { @MainActor continuation in
30+
Apphud
31+
.paywallsDidLoadCallback{
32+
pwls,
33+
error in continuation.resume(returning: pwls)
34+
}
35+
}
36+
}
37+
}

android/src/main/java/com/reactnativeapphudsdk/ApphudDataTransformer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal fun ApphudPaywall.toMap(): WritableNativeMap {
121121

122122
result.putString("name", name)
123123
result.putString("identifier", identifier)
124-
result.putBoolean("default", default)
124+
result.putBoolean("isDefault", default)
125125

126126
json?.let {
127127
result.putMap("json", it.toWritableNativeMap())

android/src/main/java/com/reactnativeapphudsdk/ApphudSdkModule.kt

+11-8
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,28 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) :
8080
}
8181

8282
@ReactMethod
83-
fun paywallShown(identifier: String) {
84-
Apphud.paywallsDidLoadCallback { apphudPaywalls, _ ->
85-
val paywall = apphudPaywalls.firstOrNull { it.identifier == identifier }
83+
fun paywallShown(options: ReadableMap) {
84+
val paywallIdentifier = options.getString("paywallIdentifier")
85+
val placementIdentifier = options.getString("placementIdentifier")
8686

87+
Utils.paywall(paywallIdentifier, placementIdentifier) { paywall ->
8788
paywall?.let {
88-
Apphud.paywallShown(it)
89+
Apphud.paywallShown(paywall)
8990
}
9091
}
9192
}
9293

9394
@ReactMethod
94-
fun paywallClosed(identifier: String) {
95-
Apphud.paywallsDidLoadCallback { apphudPaywalls, _ ->
96-
val paywall = apphudPaywalls.firstOrNull { it.identifier == identifier }
95+
fun paywallClosed(options: ReadableMap) {
96+
val paywallIdentifier = options.getString("paywallIdentifier")
97+
val placementIdentifier = options.getString("placementIdentifier")
9798

99+
Utils.paywall(paywallIdentifier, placementIdentifier) { paywall ->
98100
paywall?.let {
99-
Apphud.paywallClosed(it)
101+
Apphud.paywallClosed(paywall)
100102
}
101103
}
104+
102105
}
103106

104107
@ReactMethod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.reactnativeapphudsdk
2+
3+
import com.apphud.sdk.Apphud
4+
import com.apphud.sdk.domain.ApphudPaywall
5+
6+
object Utils {
7+
fun paywall(
8+
paywallIdentifier: String?,
9+
placementIdentifier: String?,
10+
cb: (ApphudPaywall?) -> Unit
11+
) {
12+
13+
if (paywallIdentifier == null && placementIdentifier == null) {
14+
cb(null)
15+
return
16+
}
17+
18+
placementIdentifier?.let { pId ->
19+
Apphud.fetchPlacements { apphudPlacements, _ ->
20+
cb(apphudPlacements.find { it.identifier == pId }?.paywall)
21+
}
22+
} ?: run {
23+
Apphud.paywallsDidLoadCallback { apphudPaywalls, _ ->
24+
val paywall = apphudPaywalls.firstOrNull { it.identifier == paywallIdentifier }
25+
cb(paywall)
26+
}
27+
}
28+
}
29+
}

example/src/screens/PaywallScreen.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,18 @@ export default function PaywallScreen({
107107

108108
React.useEffect(() => {
109109
const findPaywall = async () => {
110-
const paywalls = await ApphudSdk.paywalls();
110+
const paywalls = (await ApphudSdk.placements())
111+
.map((x) => x.paywall)
112+
.filter((x) => x) as ApphudPaywall[];
113+
111114
for (const paywall of paywalls) {
112115
if (paywall.identifier === route.params.paywallId) {
113116
setCurrentPaywall(paywall);
114-
ApphudSdk.paywallShown(paywall.identifier);
117+
118+
ApphudSdk.paywallShown({
119+
paywallIdentifier: paywall.identifier,
120+
placementIdentifier: paywall.placementIdentifier,
121+
});
115122

116123
setProductsProps(prepareProducts(paywall.products));
117124
return paywall;

ios/ApphudSdk.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ @interface RCT_EXTERN_MODULE(ApphudSdk, NSObject)
5050

5151
RCT_EXTERN_METHOD(collectDeviceIdentifiers)
5252
RCT_EXTERN_METHOD(setDeviceIdentifiers:(NSDictionary*)options)
53-
RCT_EXTERN_METHOD(paywallShown:(NSString*)identifier)
54-
RCT_EXTERN_METHOD(paywallClosed:(NSString*)identifier)
53+
RCT_EXTERN_METHOD(paywallShown:(NSDictionary*)options)
54+
RCT_EXTERN_METHOD(paywallClosed:(NSDictionary*)options)
5555
RCT_EXTERN_METHOD(optOutOfTracking)
5656
RCT_EXTERN_METHOD(enableDebugLogs)
5757
RCT_EXTERN_METHOD(logout: (RCTPromiseResolveBlock)resolve

ios/ApphudSdk.swift

+22-9
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,36 @@ class ApphudSdk: NSObject {
192192
}
193193

194194
@objc(paywallShown:)
195-
func paywallShown(identifier: String) {
196-
print("Paywall Shown: \(identifier)")
195+
func paywallShown(options: [AnyHashable : Any]) {
196+
let placementIdentifier = options["placementIdentifier"] as? String
197+
let paywallIdentifier = options["paywallIdentifier"] as? String
198+
199+
if placementIdentifier == nil && paywallIdentifier == nil {
200+
return
201+
}
202+
197203
Task {
198-
if let paywall = await paywalls().first(
199-
where: { $0.identifier == identifier
200-
}) {
204+
var paywall = await ApphudPaywallsHelper.getPaywall(paywallIdentifier: paywallIdentifier, placementIdentifier: placementIdentifier)
205+
206+
if let paywall {
201207
Apphud.paywallShown(paywall)
202208
}
203209
}
204210
}
205211

206212
@objc(paywallClosed:)
207-
func paywallClosed(identifier: String) {
213+
func paywallClosed(options: [AnyHashable : Any]) {
214+
let placementIdentifier = options["placementIdentifier"] as? String
215+
let paywallIdentifier = options["paywallIdentifier"] as? String
216+
217+
if placementIdentifier == nil && paywallIdentifier == nil {
218+
return
219+
}
220+
208221
Task {
209-
if let paywall = await paywalls().first(
210-
where: { $0.identifier == identifier
211-
}) {
222+
var paywall = await ApphudPaywallsHelper.getPaywall(paywallIdentifier: paywallIdentifier, placementIdentifier: placementIdentifier)
223+
224+
if let paywall {
212225
Apphud.paywallClosed(paywall)
213226
}
214227
}

ios/DataTransformer.swift

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extension ApphudProduct : RNAdapter {
5656
map["store"] = store
5757
map["skProduct"] = skProduct?.toMap()
5858
map["paywallIdentifier"] = paywallIdentifier
59+
map["placementIdentifier"] = placementIdentifier
5960

6061
return map as NSDictionary;
6162
}

src/module/ApphudSdk.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
ApphudUser,
1515
ApphudPlacement,
1616
Identifiers,
17+
PaywallLogsInfo,
1718
} from './types';
1819

1920
interface IApphudSdk {
@@ -42,24 +43,34 @@ interface IApphudSdk {
4243
/**
4344
* Available on iOS and Android.
4445
*
46+
* Returns the placements from Product Hub > Placements, potentially altered based on the user's involvement in A/ B testing, if applicable
47+
*
48+
*/
49+
placements(): Promise<ApphudPlacement[]>;
50+
51+
/**
52+
* Available on iOS and Android.
53+
*
54+
* @deprecated Will be removed in the future
55+
*
4556
* Each paywall contains an array of `ApphudProduct` objects that you use for purchase.
4657
* `ApphudProduct` is Apphud's wrapper around `SKProduct`/ `ProductDetails` models.
4758
* Method returns immediately if paywalls are cached or already loaded.
4859
* @returns paywalls configured in Apphud Dashboard > Product Hub > Paywalls.
4960
*/
50-
paywalls(): Promise<Array<ApphudPaywall>>;
61+
paywalls(): Promise<ApphudPaywall[]>;
5162

5263
/**
5364
* Available on iOS and Android
5465
* Logs a "Paywall Shown" (Paywall View) event which is required for A/B Testing Analytics.
5566
*/
56-
paywallShown(identifier: string): void;
67+
paywallShown(options: PaywallLogsInfo): void;
5768

5869
/**
5970
* Available on iOS and Android
6071
* Logs a "Paywall Closed" event. Optional.
6172
*/
62-
paywallClosed(identifier: string): void;
73+
paywallClosed(options: PaywallLogsInfo): void;
6374

6475
/**
6576
* Available on iOS and Android.
@@ -273,14 +284,6 @@ interface IApphudSdk {
273284
*/
274285
handlePushNotification(payload: any): void;
275286

276-
/**
277-
* Available on iOS and Android.
278-
*
279-
* Returns the placements from Product Hub > Placements, potentially altered based on the user's involvement in A/ B testing, if applicable
280-
*
281-
*/
282-
placements(): Promise<ApphudPlacement[]>;
283-
284287
/**
285288
* Available on iOS
286289
*

src/module/types.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,36 @@ export interface ApphudPaywall {
319319
*/
320320
identifier: string;
321321

322+
/**
323+
* Available on Android
324+
*
325+
* Paywall name, from Apphud Dashboard.
326+
*
327+
*/
328+
name?: string;
329+
322330
/**
323331
* A/B experiment name, if any.
324332
*/
325333
experimentName?: string;
326334

335+
/**
336+
* It's possible to make a paywall default – it's a special alias name, that can be assigned to only ONE paywall at a time.
337+
* There can be no default paywalls at all. It's up to you whether you want to have them or not.
338+
*/
339+
isDefault: boolean;
340+
327341
/**
328342
* A/B Experiment Variation Name
329343
*/
330344
variationName?: string;
331345

346+
/**
347+
* Represents the identifier of a parent paywall from which an experiment variation was derived in A/B Experiments.
348+
* This property is populated only if the 'Use existing paywall' option was selected during the setup of the experiment variation.
349+
*/
350+
parentPaywallIdentifier?: string;
351+
332352
/**
333353
* Custom JSON data from Paywall.
334354
*/
@@ -338,6 +358,11 @@ export interface ApphudPaywall {
338358
* Array of products from Paywall.
339359
*/
340360
products: ApphudProduct[];
361+
362+
/**
363+
* Current paywall's placement identifier, if available.
364+
*/
365+
placementIdentifier?: string;
341366
}
342367

343368
/**
@@ -415,8 +440,15 @@ export interface ApphudProduct {
415440
*/
416441
productDetails?: ProductDetails;
417442

418-
/** Paywall Identifier */
443+
/**
444+
* Paywall Identifier
445+
*/
419446
paywallIdentifier?: string;
447+
448+
/**
449+
* Placement Identifier, if any.
450+
*/
451+
placementIdentifier?: string;
420452
}
421453

422454
/** Available on Android only
@@ -576,3 +608,8 @@ export interface Identifiers {
576608
*/
577609
idfv: string;
578610
}
611+
612+
export interface PaywallLogsInfo {
613+
placementIdentifier?: string;
614+
paywallIdentifier?: string;
615+
}

0 commit comments

Comments
 (0)