Skip to content

Commit

Permalink
version 2.11.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vladd-g committed Aug 19, 2024
2 parents ab5b41c + f958b80 commit 65509c6
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 23 deletions.
15 changes: 15 additions & 0 deletions lib/android/src/main/kotlin/com/adapty/react/AdaptyCallHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class AdaptyCallHandler(
when (ctx.methodName) {
MethodName.ACTIVATE -> handleActivate(ctx)
MethodName.GET_PAYWALL -> handleGetPaywall(ctx)
MethodName.GET_PAYWALL_FOR_DEFAULT_AUDIENCE -> handleGetPaywallForDefaultAudience(ctx)
MethodName.GET_PAYWALL_PRODUCTS -> handleGetPaywallProducts(ctx)
MethodName.GET_PROFILE -> handleGetProfile(ctx)
MethodName.IDENTIFY -> handleIdentify(ctx)
Expand Down Expand Up @@ -113,6 +114,20 @@ internal class AdaptyCallHandler(
}
}

@Throws(BridgeError.TypeMismatch::class)
private fun handleGetPaywallForDefaultAudience(ctx: AdaptyContext) {
val placementId: String = ctx.params.getRequiredValue(ParamKey.PLACEMENT_ID)
val locale: String? = ctx.params.getOptionalValue(ParamKey.LOCALE)
val fetchPolicy: AdaptyPaywall.FetchPolicy = ctx.params.getDecodedValue(ParamKey.FETCH_POLICY)

Adapty.getPaywallForDefaultAudience(placementId, locale, fetchPolicy) { result ->
when (result) {
is AdaptyResult.Success -> ctx.resolve(result.value)
is AdaptyResult.Error -> ctx.forwardError(result.error)
}
}
}

@Throws(BridgeError.TypeMismatch::class)
private fun handleGetPaywallProducts(ctx: AdaptyContext) {
val paywall: AdaptyPaywall = ctx.params.getDecodedValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class MethodName(val value: String) {
ACTIVATE("activate"),
UPDATE_ATTRIBUTION("update_attribution"),
GET_PAYWALL("get_paywall"),
GET_PAYWALL_FOR_DEFAULT_AUDIENCE("get_paywall_for_default_audience"),
GET_PAYWALL_PRODUCTS("get_paywall_products"),
// GET_PRODUCTS_INTRODUCTORY_OFFER_ELIGIBILITY("get_products_introductory_offer_eligibility"),
LOG_SHOW_ONBOARDING("log_show_onboarding"),
Expand Down
1 change: 1 addition & 0 deletions lib/ios/RNAConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum MethodName: String {
case activate = "activate"
case updateAttribution = "update_attribution"
case getPaywall = "get_paywall"
case getPaywallForDefaultAudience = "get_paywall_for_default_audience"
case getPaywallProducts = "get_paywall_products"
case getProductsIntroductoryOfferEligibility = "get_products_introductory_offer_eligibility"
case logShowOnboarding = "log_show_onboarding"
Expand Down
20 changes: 20 additions & 0 deletions lib/ios/RNAdapty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class RNAdapty: RCTEventEmitter, AdaptyDelegate {
case .activate: try handleActivate(ctx)
case .updateAttribution: try handleUpdateAttribution(ctx)
case .getPaywall: try handleGetPaywall(ctx)
case .getPaywallForDefaultAudience: try handleGetPaywallForDefaultAudience(ctx)
case .getPaywallProducts: try handleGetPaywallProducts(ctx)
case .getProductsIntroductoryOfferEligibility: try handleGetProductsIntroductoryOfferEligibility(ctx)
case .logShowOnboarding: try handleLogShowOnboarding(ctx)
Expand Down Expand Up @@ -285,6 +286,25 @@ class RNAdapty: RCTEventEmitter, AdaptyDelegate {
}
}

private func handleGetPaywallForDefaultAudience(_ ctx: AdaptyContext) throws {

let placementId: String = try ctx.params.getRequiredValue(for: .placementId)
let locale: String? = ctx.params.getOptionalValue(for: .locale)
let fetchPolicy: AdaptyPaywall.FetchPolicy = try ctx.params.getDecodedValue(
for: .fetchPolicy,
jsonDecoder: AdaptyContext.jsonDecoder
)

Adapty.getPaywallForDefaultAudience(placementId: placementId, locale: locale, fetchPolicy: fetchPolicy) { result in
switch result {
case let .success(paywall):
ctx.resolve(with: paywall)
case let .failure(error):
ctx.forwardError(error)
}
}
}

private func handleGetPaywallProducts(_ ctx: AdaptyContext) throws {
let paywall: AdaptyPaywall = try ctx.params.getDecodedValue(
for: .paywall,
Expand Down
90 changes: 83 additions & 7 deletions lib/src/adapty-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { AddListenerFn, MethodName } from '@/types/bridge';
export class Adapty {
private resolveHeldActivation?: (() => Promise<void>) | null = null;
private activating: Promise<void> | null = null;
private nonWaitingMethods: MethodName[] = ['activate', 'get_paywall_for_default_audience']

// Middleware to call native handle
async handle<T>(
Expand All @@ -34,7 +35,7 @@ export class Adapty {
*
* Not applicable for activate method ofc
*/
if (this.resolveHeldActivation && method !== 'activate') {
if (this.resolveHeldActivation && !this.nonWaitingMethods.includes(method)) {
log.wait({});
await this.resolveHeldActivation();
this.resolveHeldActivation = null;
Expand All @@ -44,7 +45,7 @@ export class Adapty {
* wait until activate call is resolved before calling native methods
* Not applicable for activate method ofc
*/
if (this.activating && method !== 'activate') {
if (this.activating && !this.nonWaitingMethods.includes(method)) {
log.wait({});
await this.activating;
log.waitComplete({});
Expand Down Expand Up @@ -169,13 +170,14 @@ export class Adapty {
}

/**
* Fetches a paywall by its developer ID.
* Fetches the paywall by the specified placement.
*
* @remarks
* Adapty allows you remotely configure the products
* that will be displayed in your app.
* This way you don’t have to hardcode the products
* and can dynamically change offers or run A/B tests without app releases.
* With Adapty, you can remotely configure the products and offers in your app
* by simply adding them to paywalls – no need for hardcoding them.
* The only thing you hardcode is the placement ID.
* This flexibility allows you to easily update paywalls, products, and offers,
* or run A/B tests, all without the need for a new app release.
*
* @param {string} placementId - The identifier of the desired placement.
* This is the value you specified when you created the placement
Expand Down Expand Up @@ -238,6 +240,80 @@ export class Adapty {
return result;
}

/**
* Fetches the paywall of the specified placement for the **All Users** audience.
*
* @remarks
* With Adapty, you can remotely configure the products and offers in your app
* by simply adding them to paywalls – no need for hardcoding them.
* The only thing you hardcode is the placement ID.
* This flexibility allows you to easily update paywalls, products, and offers,
* or run A/B tests, all without the need for a new app release.
*
* However, it’s crucial to understand that the recommended approach is to fetch the paywall
* through the placement ID by the {@link getPaywall} method.
* The `getPaywallForDefaultAudience` method should be a last resort due to its significant drawbacks.
* See docs for more details
*
* @param {string} placementId - The identifier of the desired placement.
* This is the value you specified when you created the placement
* in the Adapty Dashboard.
* @param {string | undefined} [locale] - The locale of the desired paywall.
* @param {Input.GetPaywallForDefaultAudienceParamsInput} [params] - Additional parameters for retrieving paywall.
* @returns {Promise<Model.AdaptyPaywall>}
* A promise that resolves with a requested paywall.
*
* @throws {@link AdaptyError}
* Throws an error:
* 1. if the paywall with the specified ID is not found
* 2. if your bundle ID does not match with your Adapty Dashboard setup
*/
public async getPaywallForDefaultAudience(
placementId: string,
locale?: string,
params: Input.GetPaywallForDefaultAudienceParamsInput = {
fetchPolicy: Input.FetchPolicy.ReloadRevalidatingCacheData,
},
): Promise<Model.AdaptyPaywall> {
const ctx = new LogContext();
const log = ctx.call({ methodName: 'getPaywallForDefaultAudience' });

log.start({ placementId, locale, params });

const body = new ParamMap();
body.set('placement_id', placementId);
if (locale) {
body.set('locale', locale);
}

if (params.fetchPolicy !== 'return_cache_data_if_not_expired_else_load') {
body.set(
'fetch_policy',
JSON.stringify({
type:
params.fetchPolicy ?? Input.FetchPolicy.ReloadRevalidatingCacheData,
} satisfies Schema['InOutput.AdaptyPaywallFetchPolicy']),
);
} else {
body.set(
'fetch_policy',
JSON.stringify({
type: params.fetchPolicy,
max_age: params.maxAgeSeconds,
} satisfies Schema['InOutput.AdaptyPaywallFetchPolicy']),
);
}

const result = await this.handle<Model.AdaptyPaywall>(
'get_paywall_for_default_audience',
body,
ctx,
log,
);

return result;
}

/**
* Fetches a list of products associated with a provided paywall.
*
Expand Down
1 change: 1 addition & 0 deletions lib/src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type ParamKey = (typeof ParamKeys)[number];
export const MethodNames = [
'activate',
'get_paywall',
'get_paywall_for_default_audience',
'get_paywall_products',
'get_products_introductory_offer_eligibility',
'get_profile',
Expand Down
26 changes: 12 additions & 14 deletions lib/src/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ export const FetchPolicy = Object.freeze({
export type FetchPolicy = (typeof FetchPolicy)[keyof typeof FetchPolicy];

export type GetPaywallParamsInput =
& GetPaywallForDefaultAudienceParamsInput
& {
/**
* This value limits the timeout (in milliseconds) for this method.
*
* @remarks
* If the timeout is reached, cached data or local fallback will be returned.
*/
loadTimeoutMs?: number;
};

export type GetPaywallForDefaultAudienceParamsInput =
| {
/**
* Fetch policy
Expand All @@ -63,13 +75,6 @@ export type GetPaywallParamsInput =
FetchPolicy,
'return_cache_data_if_not_expired_else_load'
>;
/**
* This value limits the timeout (in milliseconds) for this method.
*
* @remarks
* If the timeout is reached, cached data or local fallback will be returned.
*/
loadTimeoutMs?: number;
}
| {
/**
Expand All @@ -90,13 +95,6 @@ export type GetPaywallParamsInput =
* Max time (in seconds) the cache is valid in case of `'return_cache_data_if_not_expired_else_load'` fetch policy.
*/
maxAgeSeconds: number;
/**
* This value limits the timeout (in milliseconds) for this method.
*
* @remarks
* If the timeout is reached, cached data or local fallback will be returned.
*/
loadTimeoutMs?: number;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "react-native-adapty",
"version": "2.11.1",
"version": "2.11.2",
"description": "Adapty React Native SDK",
"license": "MIT",
"author": "Vanya Dorofeyev <[email protected]> (https://github.com/divanc)",
Expand Down
2 changes: 1 addition & 1 deletion react-native-adapty-sdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Pod::Spec.new do |s|
s.resources = "lib/ios/**/*.{plist}"
s.requires_arc = true

s.dependency "Adapty", "2.11.1"
s.dependency "Adapty", "2.11.2"
s.dependency "React"
end

0 comments on commit 65509c6

Please sign in to comment.