Skip to content

Commit

Permalink
Merge branch 'develop' into v4-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftor committed Jan 30, 2025
2 parents b34cf12 + 13f4c70 commit 5d6a5d2
Show file tree
Hide file tree
Showing 3 changed files with 480 additions and 298 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ The changelog for `SuperwallKit`. Also see the [releases](https://github.com/sup
- Adds StoreKit 2 observer mode. This can be enabled by setting the `SuperwallOptions` `shouldObservePurchases` to `true` and `storeKitVersion` to `.storeKit2` (which is the default value). Note that this is only available with apps running iOS 17.2+.
- Adds `products(for:)` which gets the ``StoreProduct`s for given product identifiers.

## 3.12.4

### Fixes

- Simplifies and corrects logic for choosing paywall variants.

## 3.12.3

### Fixes
Expand Down
59 changes: 30 additions & 29 deletions Sources/SuperwallKit/Config/ConfigLogic.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Yusuf Tör on 21/07/2022.
//
Expand Down Expand Up @@ -28,36 +28,29 @@ enum ConfigLogic {
}

// If there's only one variant, return it.
// This could have a zero percentage.
if variants.count == 1,
let variant = variants.first {
let variant = variants.first
{

Check warning on line 33 in Sources/SuperwallKit/Config/ConfigLogic.swift

View workflow job for this annotation

GitHub Actions / Package-SwiftLint

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
return variant.toExperimentVariant()
}

// Calculate the total sum of variant percentages.
let totalPercentage = variants.reduce(0) { $0 + $1.percentage }
let variantSum = variants.reduce(0) { $0 + $1.percentage }

// Something went wrong on the dashboard, where all variants
// have 0% set. Choose a random one.
if totalPercentage == 0 {
// Choose a random variant
// If all variants have 0% set, choose a random one.
if variantSum == 0 {
let randomIndex = randomiser(0..<variants.count)
return variants[randomIndex].toExperimentVariant()
}

// Choose a random percentage e.g. 21
let randomPercentage = randomiser(0..<totalPercentage)

// Select the variant based on cumulative percentage
var cumulativePercentage = 0
// Choose a random threshold within the total sum.
let randomThreshold = randomiser(0..<variantSum)
var cumulativeSum = 0

// Iterate through variants and return the first that crosses the threshold.
for variant in variants {
// Add to total variant percentage
cumulativePercentage += variant.percentage

// See if selected is less than total. If it is then return.
// e.g. Loop 1: 21 < (0 + 20) = nope, Loop 2: 21 < (20 + 30) = match
if randomPercentage < cumulativePercentage {
cumulativeSum += variant.percentage
if randomThreshold < cumulativeSum {
return variant.toExperimentVariant()
}
}
Expand Down Expand Up @@ -179,15 +172,19 @@ enum ConfigLogic {

for assignment in assignments {
// Get the trigger with the matching experiment ID
guard let trigger = triggers.first(
where: { $0.audiences.contains(where: { $0.experiment.id == assignment.experimentId }) }
) else {
guard
let trigger = triggers.first(
where: { $0.audiences.contains(where: { $0.experiment.id == assignment.experimentId }) }
)
else {
continue
}
// Get the variant with the matching variant ID
guard let variantOption = trigger.audiences.compactMap({
$0.experiment.variants.first { $0.id == assignment.variantId }
}).first else {
guard
let variantOption = trigger.audiences.compactMap({
$0.experiment.variants.first { $0.id == assignment.variantId }
}).first
else {
continue
}

Expand Down Expand Up @@ -295,7 +292,8 @@ enum ConfigLogic {

for variant in preloadableVariants {
if variant.type == .treatment,
let paywallId = variant.paywallId {
let paywallId = variant.paywallId
{

Check warning on line 296 in Sources/SuperwallKit/Config/ConfigLogic.swift

View workflow job for this annotation

GitHub Actions / Package-SwiftLint

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
identifiers.insert(paywallId)
}
}
Expand All @@ -318,7 +316,8 @@ enum ConfigLogic {
continue
}
if variant.type == .treatment,
let paywallId = variant.paywallId {
let paywallId = variant.paywallId
{

Check warning on line 320 in Sources/SuperwallKit/Config/ConfigLogic.swift

View workflow job for this annotation

GitHub Actions / Package-SwiftLint

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
identifiers.insert(paywallId)
}
}
Expand Down Expand Up @@ -347,12 +346,14 @@ enum ConfigLogic {
let newPaywallIds = Set(newPaywalls.map { $0.identifier })

// Create dictionary for quick lookup of cacheKeys
let oldPaywallCacheKeys = Dictionary(uniqueKeysWithValues: oldPaywalls.map { ($0.identifier, $0.cacheKey) })
let oldPaywallCacheKeys = Dictionary(
uniqueKeysWithValues: oldPaywalls.map { ($0.identifier, $0.cacheKey) })

let removedPaywallIds = oldPaywallIds.subtracting(newPaywallIds)

// Find identifiers that are no longer in the new configuration or whose cacheKey has changed
let removedOrChangedPaywallIds = removedPaywallIds
let removedOrChangedPaywallIds =
removedPaywallIds
.union(
newPaywalls.filter { paywall in
let cacheKeyExists = oldPaywallCacheKeys[paywall.identifier] != nil
Expand Down
Loading

0 comments on commit 5d6a5d2

Please sign in to comment.