| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import Foundation
- import StoreKit
- struct PaywallTrialDisplay: Equatable, Sendable {
- let badgeText: String
- let durationLowercased: String
- let ctaText: String
- static func from(offer: Product.SubscriptionOffer, config: PaywallConfig) -> PaywallTrialDisplay {
- let duration = config.trialDurationDescription(from: offer)
- let durationLowercased = config.trialDurationDescription(from: offer, lowercase: true)
- return PaywallTrialDisplay(
- badgeText: config.trialBadgeText(from: offer),
- durationLowercased: durationLowercased,
- ctaText: config.trialCTATitle(from: offer)
- )
- }
- static func from(fallback: PaywallConfig.TrialFallbackDuration, config: PaywallConfig) -> PaywallTrialDisplay? {
- guard let duration = config.trialDurationDescription(from: fallback),
- let durationLowercased = config.trialDurationDescription(from: fallback, lowercase: true) else {
- return nil
- }
- return PaywallTrialDisplay(
- badgeText: config.trialBadgeText(duration: duration),
- durationLowercased: durationLowercased,
- ctaText: config.trialCTATitle(duration: duration)
- )
- }
- }
- struct PaywallPlan: Identifiable, Hashable, Sendable {
- let productID: String
- var id: String { productID }
- var planKey: String {
- SubscriptionProductID.planKey(for: productID)
- }
- init(productID: String) {
- self.productID = productID
- }
- static func from(rawValue: String) -> PaywallPlan? {
- if rawValue.contains(".") {
- return PaywallPlan(productID: rawValue)
- }
- switch rawValue {
- case "monthly": return PaywallPlan(productID: SubscriptionProductID.monthly)
- case "yearly": return PaywallPlan(productID: SubscriptionProductID.yearly)
- case "lifetime": return PaywallPlan(productID: SubscriptionProductID.lifetime)
- default: return nil
- }
- }
- func planCopy(from config: PaywallConfig, product: Product? = nil) -> PaywallConfig.PlanCopy {
- if let configured = config.plans.planCopy(for: planKey) {
- return configured
- }
- if let product {
- return config.generatedPlanCopy(for: product)
- }
- return config.fallbackPlanCopy
- }
- func localizedPrice(from product: Product?, config: PaywallConfig) -> String {
- product?.displayPrice ?? planCopy(from: config, product: product).fallbackPrice
- }
- func localizedSubtitle(
- from product: Product?,
- config: PaywallConfig,
- trial: PaywallTrialDisplay? = nil
- ) -> String {
- guard let product else { return "" }
- let copy = planCopy(from: config, product: product)
- if let trial, let template = copy.trialSubtitleTemplate {
- return template
- .replacingOccurrences(of: "{price}", with: product.displayPrice)
- .replacingOccurrences(of: "{trial}", with: trial.durationLowercased)
- }
- return copy.subtitleTemplate.replacingOccurrences(of: "{price}", with: product.displayPrice)
- }
- func localizedCTATitle(
- from product: Product?,
- config: PaywallConfig,
- trial: PaywallTrialDisplay? = nil
- ) -> String {
- guard let product else { return config.loadingCTA }
- if let trial {
- return trial.ctaText
- }
- let copy = planCopy(from: config, product: product)
- return copy.ctaTemplate.replacingOccurrences(of: "{price}", with: product.displayPrice)
- }
- func localizedRenewalDisclosure(
- from product: Product?,
- config: PaywallConfig,
- trial: PaywallTrialDisplay? = nil
- ) -> String {
- if product?.type == .nonConsumable {
- return config.lifetimeDisclosure
- }
- guard let product else { return config.loadingDisclosure }
- let price = product.displayPrice
- let period = product.subscription.map {
- config.subscriptionPeriodDescription(from: $0.subscriptionPeriod)
- } ?? config.messages.periodFallback
- if let trial {
- return config.trialDisclosureTemplate
- .replacingOccurrences(of: "{trial}", with: trial.durationLowercased)
- .replacingOccurrences(of: "{price}", with: price)
- .replacingOccurrences(of: "{period}", with: period)
- }
- return config.subscriptionDisclosureTemplate
- .replacingOccurrences(of: "{price}", with: price)
- .replacingOccurrences(of: "{period}", with: period)
- }
- func fallbackBillingDescription(config: PaywallConfig, product: Product? = nil) -> String {
- let copy = planCopy(from: config, product: product)
- return copy.fallbackBillingDescription
- .replacingOccurrences(of: "{price}", with: copy.fallbackPrice)
- }
- var isLifetime: Bool {
- productID == SubscriptionProductID.lifetime
- }
- }
- extension PaywallPlan {
- static func sortOrder(_ lhs: PaywallPlan, _ rhs: PaywallPlan) -> Bool {
- let order = [
- SubscriptionProductID.monthly,
- SubscriptionProductID.yearly,
- SubscriptionProductID.lifetime,
- ]
- let leftIndex = order.firstIndex(of: lhs.productID) ?? Int.max
- let rightIndex = order.firstIndex(of: rhs.productID) ?? Int.max
- if leftIndex != rightIndex {
- return leftIndex < rightIndex
- }
- return lhs.productID < rhs.productID
- }
- }
|