PaywallModels.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(from product: Product?, config: PaywallConfig) -> String {
  26. guard let product else { return config.loadingPrice }
  27. let copy = planCopy(from: config)
  28. return copy.subtitleTemplate.replacingOccurrences(of: "{price}", with: product.displayPrice)
  29. }
  30. func localizedCTATitle(from product: Product?, config: PaywallConfig) -> String {
  31. guard let product else { return config.loadingCTA }
  32. let copy = planCopy(from: config)
  33. return copy.ctaTemplate.replacingOccurrences(of: "{price}", with: product.displayPrice)
  34. }
  35. func localizedRenewalDisclosure(from product: Product?, config: PaywallConfig) -> String {
  36. switch self {
  37. case .lifetime:
  38. return config.lifetimeDisclosure
  39. case .monthly, .yearly:
  40. guard let product else { return config.loadingDisclosure }
  41. let price = product.displayPrice
  42. let period = product.subscription.map {
  43. config.subscriptionPeriodDescription(from: $0.subscriptionPeriod)
  44. } ?? config.messages.periodFallback
  45. return config.subscriptionDisclosureTemplate
  46. .replacingOccurrences(of: "{price}", with: price)
  47. .replacingOccurrences(of: "{period}", with: period)
  48. }
  49. }
  50. func fallbackBillingDescription(config: PaywallConfig) -> String {
  51. let copy = planCopy(from: config)
  52. return copy.fallbackBillingDescription
  53. .replacingOccurrences(of: "{price}", with: copy.fallbackPrice)
  54. }
  55. }