Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: TrialOrIntroEligibilityChecker.eligibility(for packages:) #2846

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RevenueCatUI/Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ internal enum TestData {
extension TrialOrIntroEligibilityChecker {

/// Creates a mock `TrialOrIntroEligibilityChecker` with a constant result.
static func producing(eligibility: IntroEligibilityStatus) -> Self {
static func producing(eligibility: @autoclosure @escaping () -> IntroEligibilityStatus) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows providing a different value for each call, like using Bool.random(), so it's not computed eagerly.

return .init { product in
return product.hasIntroDiscount
? eligibility
? eligibility()
: .noIntroOfferExists
}
}
Expand Down
19 changes: 19 additions & 0 deletions RevenueCatUI/Helpers/TrialOrIntroEligibilityChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ extension TrialOrIntroEligibilityChecker {
return await self.eligibility(for: package.storeProduct)
}

/// Computes eligibility for a list of packages in parallel, returning them all in a dictionary.
func eligibility(for packages: [Package]) async -> [Package: IntroEligibilityStatus] {
return await withTaskGroup(of: (Package, IntroEligibilityStatus).self) { group in
for package in packages {
group.addTask {
return (package, await self.eligibility(for: package))
}
}

var result: [Package: IntroEligibilityStatus] = [:]

for await (package, status) in group {
result[package] = status
}

return result
}
}

}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
Expand Down