PaywallModels.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import Foundation
  2. import StoreKit
  3. enum PaywallPlan: String, CaseIterable, Identifiable {
  4. case monthly
  5. case yearly
  6. case lifetime
  7. var id: String { rawValue }
  8. var productID: String {
  9. switch self {
  10. case .monthly: SubscriptionProductID.monthly
  11. case .yearly: SubscriptionProductID.yearly
  12. case .lifetime: SubscriptionProductID.lifetime
  13. }
  14. }
  15. func planCopy(from config: PaywallConfig) -> PaywallConfig.PlanCopy {
  16. switch self {
  17. case .monthly: config.plans.monthly
  18. case .yearly: config.plans.yearly
  19. case .lifetime: config.plans.lifetime
  20. }
  21. }
  22. func localizedPrice(from product: Product?, config: PaywallConfig) -> String {
  23. product?.displayPrice ?? planCopy(from: config).fallbackPrice
  24. }
  25. func localizedSubtitle(
  26. from product: Product?,
  27. config: PaywallConfig,
  28. introOffer: Product.SubscriptionOffer? = nil
  29. ) -> String {
  30. guard let product else { return config.loadingPrice }
  31. let copy = planCopy(from: config)
  32. let price = product.displayPrice
  33. if self == .yearly, let introOffer, let trialTemplate = copy.trialSubtitleTemplate {
  34. let duration = config.trialDurationDescription(from: introOffer, lowercase: true)
  35. return trialTemplate
  36. .replacingOccurrences(of: "{trial}", with: duration)
  37. .replacingOccurrences(of: "{price}", with: price)
  38. }
  39. return copy.subtitleTemplate.replacingOccurrences(of: "{price}", with: price)
  40. }
  41. func localizedCTATitle(
  42. from product: Product?,
  43. config: PaywallConfig,
  44. introOffer: Product.SubscriptionOffer? = nil
  45. ) -> String {
  46. guard let product else { return config.loadingCTA }
  47. let copy = planCopy(from: config)
  48. let price = product.displayPrice
  49. if self == .yearly, let introOffer {
  50. return config.trialCTATitle(from: introOffer)
  51. }
  52. return copy.ctaTemplate.replacingOccurrences(of: "{price}", with: price)
  53. }
  54. func localizedRenewalDisclosure(
  55. from product: Product?,
  56. config: PaywallConfig,
  57. introOffer: Product.SubscriptionOffer? = nil
  58. ) -> String {
  59. switch self {
  60. case .lifetime:
  61. return config.lifetimeDisclosure
  62. case .monthly, .yearly:
  63. guard let product else { return config.loadingDisclosure }
  64. let price = product.displayPrice
  65. let period = product.subscription.map {
  66. config.subscriptionPeriodDescription(from: $0.subscriptionPeriod)
  67. } ?? config.messages.periodFallback
  68. if self == .yearly, let introOffer {
  69. let trial = config.trialDurationDescription(from: introOffer, lowercase: true)
  70. return config.trialDisclosureTemplate
  71. .replacingOccurrences(of: "{trial}", with: trial)
  72. .replacingOccurrences(of: "{price}", with: price)
  73. .replacingOccurrences(of: "{period}", with: period)
  74. }
  75. return config.subscriptionDisclosureTemplate
  76. .replacingOccurrences(of: "{price}", with: price)
  77. .replacingOccurrences(of: "{period}", with: period)
  78. }
  79. }
  80. func fallbackBillingDescription(config: PaywallConfig) -> String {
  81. let copy = planCopy(from: config)
  82. return copy.fallbackBillingDescription
  83. .replacingOccurrences(of: "{price}", with: copy.fallbackPrice)
  84. }
  85. }