| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986 |
- import Cocoa
- import StoreKit
- // MARK: - Plan Model
- enum PaywallPlan: CaseIterable {
- case monthly
- case yearly
- case lifetime
- var productID: String {
- switch self {
- case .monthly: StoreProductID.monthly
- case .yearly: StoreProductID.yearly
- case .lifetime: StoreProductID.lifetime
- }
- }
- func planCopy(from config: PaywallConfig) -> PaywallConfig.PlanCopy {
- switch self {
- case .monthly: config.plans.monthly
- case .yearly: config.plans.yearly
- case .lifetime: config.plans.lifetime
- }
- }
- func localizedPrice(from product: Product?, config: PaywallConfig) -> String {
- product?.displayPrice ?? config.loadingPrice
- }
- func localizedSubtitle(
- from product: Product?,
- config: PaywallConfig,
- introOffer: Product.SubscriptionOffer? = nil
- ) -> String {
- guard let product else { return config.loadingPrice }
- let copy = planCopy(from: config)
- let price = product.displayPrice
- if self == .yearly, let introOffer, let trialTemplate = copy.trialSubtitleTemplate {
- let duration = config.trialDurationDescription(from: introOffer, lowercase: true)
- return trialTemplate
- .replacingOccurrences(of: "{trial}", with: duration)
- .replacingOccurrences(of: "{price}", with: price)
- }
- return copy.subtitleTemplate.replacingOccurrences(of: "{price}", with: price)
- }
- func localizedCTATitle(
- from product: Product?,
- config: PaywallConfig,
- introOffer: Product.SubscriptionOffer? = nil
- ) -> String {
- guard let product else { return config.loadingCTA }
- let copy = planCopy(from: config)
- let price = product.displayPrice
- if self == .yearly, let introOffer {
- return config.trialCTATitle(from: introOffer)
- }
- return copy.ctaTemplate.replacingOccurrences(of: "{price}", with: price)
- }
- func localizedRenewalDisclosure(
- from product: Product?,
- config: PaywallConfig,
- introOffer: Product.SubscriptionOffer? = nil
- ) -> String {
- switch self {
- case .lifetime:
- return config.lifetimeDisclosure
- case .monthly, .yearly:
- 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 self == .yearly, let introOffer {
- let trial = config.trialDurationDescription(from: introOffer, lowercase: true)
- return config.trialDisclosureTemplate
- .replacingOccurrences(of: "{trial}", with: trial)
- .replacingOccurrences(of: "{price}", with: price)
- .replacingOccurrences(of: "{period}", with: period)
- }
- return config.subscriptionDisclosureTemplate
- .replacingOccurrences(of: "{price}", with: price)
- .replacingOccurrences(of: "{period}", with: period)
- }
- }
- }
- // MARK: - StoreKit
- enum StoreProductID {
- static let monthly = AppStoreConfig.ProductID.monthly
- static let yearly = AppStoreConfig.ProductID.yearly
- static let lifetime = AppStoreConfig.ProductID.lifetime
- static let all = AppStoreConfig.ProductID.all
- }
- enum PremiumAccessKind: String {
- case none
- case subscription
- case lifetime
- }
- enum StoreError: Error {
- case productNotFound
- case failedVerification
- }
- enum PurchaseOutcome: Sendable {
- case purchased
- case cancelled
- case pending
- }
- @MainActor
- final class StoreManager {
- static let shared = StoreManager()
- private static let cachedPremiumKey = "StoreManager.cachedIsPremium"
- private static let cachedPremiumKindKey = "StoreManager.cachedPremiumAccessKind"
- private(set) var products: [Product] = []
- private(set) var isPremium = UserDefaults.standard.bool(forKey: cachedPremiumKey)
- private(set) var premiumAccessKind: PremiumAccessKind = {
- guard let raw = UserDefaults.standard.string(forKey: cachedPremiumKindKey),
- let kind = PremiumAccessKind(rawValue: raw) else {
- return .none
- }
- return kind
- }()
- var isPro: Bool { isPremium }
- var hasActiveSubscription: Bool { premiumAccessKind == .subscription }
- var hasLifetimeAccess: Bool { premiumAccessKind == .lifetime }
- private(set) var isLoadingProducts = false
- private(set) var productLoadError: String?
- private(set) var isPurchasing = false
- private(set) var isResolvingEntitlements = true
- private(set) var introOfferEligibleByProductID: [String: Bool] = [:]
- private var transactionListener: Task<Void, Never>?
- private var initialEntitlementTask: Task<Void, Never>?
- private var hasStarted = false
- private init() {}
- func start() {
- guard !hasStarted else { return }
- hasStarted = true
- transactionListener = Task { [weak self] in
- for await update in Transaction.updates {
- await self?.handleTransactionUpdate(update)
- }
- }
- initialEntitlementTask = Task { [weak self] in
- guard let self else { return }
- await self.refreshEntitlementsOnly()
- self.isResolvingEntitlements = false
- NotificationCenter.default.post(name: .entitlementsDidResolve, object: nil)
- await self.loadProducts()
- await self.refreshPremiumStatus()
- }
- }
- func ensureEntitlementsResolved() async {
- await initialEntitlementTask?.value
- }
- func product(for plan: PaywallPlan) -> Product? {
- products.first { $0.id == plan.productID }
- }
- func eligibleIntroOffer(for plan: PaywallPlan) -> Product.SubscriptionOffer? {
- guard let product = product(for: plan) else { return nil }
- return eligibleIntroOffer(for: product)
- }
- func eligibleIntroOffer(for product: Product) -> Product.SubscriptionOffer? {
- guard introOfferEligibleByProductID[product.id] == true,
- let offer = product.subscription?.introductoryOffer,
- offer.paymentMode == .freeTrial else {
- return nil
- }
- return offer
- }
- func loadProducts() async {
- isLoadingProducts = true
- productLoadError = nil
- postStoreStateDidChange()
- defer {
- isLoadingProducts = false
- postStoreStateDidChange()
- }
- do {
- let loaded = try await Product.products(for: StoreProductID.all)
- guard !loaded.isEmpty else {
- productLoadError = PaywallConfigService.shared.config.messages.noPlansAvailable
- products = []
- return
- }
- products = loaded.sorted { lhs, rhs in
- productSortOrder(for: lhs.id) < productSortOrder(for: rhs.id)
- }
- productLoadError = nil
- await refreshIntroOfferEligibility()
- NotificationCenter.default.post(name: .storeProductsDidUpdate, object: nil)
- } catch {
- productLoadError = PaywallConfigService.shared.config.messages.plansLoadFailed
- NSLog("Failed to load products: \(error.localizedDescription)")
- }
- }
- @discardableResult
- func purchase(plan: PaywallPlan) async throws -> PurchaseOutcome {
- if products.isEmpty {
- await loadProducts()
- }
- guard let product = product(for: plan) else {
- throw StoreError.productNotFound
- }
- isPurchasing = true
- postStoreStateDidChange()
- defer {
- isPurchasing = false
- postStoreStateDidChange()
- }
- let result = try await product.purchase()
- switch result {
- case .success(let verification):
- let transaction = try checkVerified(verification)
- await transaction.finish()
- applyPremiumStatus(premiumAccessKind(for: transaction.productID))
- await refreshPremiumStatus()
- return .purchased
- case .userCancelled:
- return .cancelled
- case .pending:
- return .pending
- @unknown default:
- return .cancelled
- }
- }
- @discardableResult
- func restorePurchases() async throws -> Bool {
- isPurchasing = true
- postStoreStateDidChange()
- defer {
- isPurchasing = false
- postStoreStateDidChange()
- }
- try await AppStore.sync()
- await refreshPremiumStatus()
- return isPremium
- }
- func showAlert(title: String, message: String, on window: NSWindow?) {
- let alert = NSAlert()
- alert.messageText = title
- alert.informativeText = message
- alert.alertStyle = .informational
- alert.addButton(withTitle: PaywallConfigService.shared.config.messages.alertOK)
- if let window {
- alert.beginSheetModal(for: window)
- } else {
- alert.runModal()
- }
- }
- func showManageSubscriptions() {
- let urlString = PaywallConfigService.shared.config.urls.manageSubscriptions
- guard let url = URL(string: urlString) else { return }
- NSWorkspace.shared.open(url)
- }
- func showPurchaseError(_ error: Error, on window: NSWindow?) {
- let messages = PaywallConfigService.shared.config.messages
- switch error {
- case StoreError.productNotFound:
- showAlert(title: messages.purchaseFailedTitle, message: messages.productNotFound, on: window)
- case StoreError.failedVerification:
- showAlert(title: messages.purchaseFailedTitle, message: messages.failedVerification, on: window)
- default:
- if let storeKitError = error as? StoreKitError, case .userCancelled = storeKitError {
- return
- }
- showAlert(title: messages.purchaseFailedTitle, message: error.localizedDescription, on: window)
- }
- }
- private func handleTransactionUpdate(_ update: VerificationResult<Transaction>) async {
- do {
- let transaction = try checkVerified(update)
- await transaction.finish()
- await refreshPremiumStatus()
- } catch {
- NSLog("Transaction verification failed: \(error.localizedDescription)")
- }
- }
- func refreshEntitlementsOnly() async {
- applyPremiumStatus(await resolvePremiumAccessKind())
- }
- func refreshPremiumStatus() async {
- await refreshIntroOfferEligibility()
- applyPremiumStatus(await resolvePremiumAccessKind())
- }
- private func applyPremiumStatus(_ kind: PremiumAccessKind) {
- let hasPremium = kind != .none
- let didChange = hasPremium != isPremium || kind != premiumAccessKind
- isPremium = hasPremium
- premiumAccessKind = hasPremium ? kind : .none
- UserDefaults.standard.set(hasPremium, forKey: Self.cachedPremiumKey)
- UserDefaults.standard.set(premiumAccessKind.rawValue, forKey: Self.cachedPremiumKindKey)
- if didChange {
- NotificationCenter.default.post(name: .premiumStatusDidChange, object: nil)
- }
- postStoreStateDidChange()
- }
- private func resolvePremiumAccessKind() async -> PremiumAccessKind {
- if await hasActiveEntitlement(for: StoreProductID.lifetime) {
- return .lifetime
- }
- if await hasActiveEntitlement(for: StoreProductID.monthly) {
- return .subscription
- }
- if await hasActiveEntitlement(for: StoreProductID.yearly) {
- return .subscription
- }
- if await hasEntitlementFromSubscriptionStatus() {
- return .subscription
- }
- return .none
- }
- private func premiumAccessKind(for productID: String) -> PremiumAccessKind {
- if productID == StoreProductID.lifetime {
- return .lifetime
- }
- if productID == StoreProductID.monthly || productID == StoreProductID.yearly {
- return .subscription
- }
- return .none
- }
- private func hasActiveEntitlement(for productID: String) async -> Bool {
- for await result in Transaction.currentEntitlements {
- guard let transaction = try? checkVerified(result) else { continue }
- if transaction.productID == productID, isActivePremiumTransaction(transaction) {
- return true
- }
- }
- guard let result = await Transaction.latest(for: productID),
- let transaction = try? checkVerified(result) else {
- return false
- }
- return isActivePremiumTransaction(transaction)
- }
- private func hasEntitlementFromSubscriptionStatus() async -> Bool {
- for product in products where product.subscription != nil {
- guard StoreProductID.all.contains(product.id) else { continue }
- guard let statuses = try? await product.subscription?.status else { continue }
- for status in statuses {
- switch status.state {
- case .subscribed, .inGracePeriod, .inBillingRetryPeriod:
- return true
- default:
- continue
- }
- }
- }
- return false
- }
- private func isActivePremiumTransaction(_ transaction: Transaction) -> Bool {
- guard StoreProductID.all.contains(transaction.productID) else { return false }
- guard transaction.revocationDate == nil else { return false }
- if let expirationDate = transaction.expirationDate {
- return expirationDate > Date()
- }
- return true
- }
- private func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
- switch result {
- case .unverified:
- throw StoreError.failedVerification
- case .verified(let safe):
- return safe
- }
- }
- private func productSortOrder(for productID: String) -> Int {
- switch productID {
- case StoreProductID.monthly: 0
- case StoreProductID.yearly: 1
- case StoreProductID.lifetime: 2
- default: 99
- }
- }
- private func refreshIntroOfferEligibility() async {
- var eligibility: [String: Bool] = [:]
- for product in products {
- guard let subscription = product.subscription,
- subscription.introductoryOffer != nil else {
- continue
- }
- let groupID = subscription.subscriptionGroupID
- eligibility[product.id] = await Product.SubscriptionInfo.isEligibleForIntroOffer(for: groupID)
- }
- introOfferEligibleByProductID = eligibility
- }
- private func postStoreStateDidChange() {
- NotificationCenter.default.post(name: .storeStateDidChange, object: nil)
- }
- }
- extension Notification.Name {
- static let premiumStatusDidChange = Notification.Name("premiumStatusDidChange")
- static let entitlementsDidResolve = Notification.Name("entitlementsDidResolve")
- static let storeProductsDidUpdate = Notification.Name("storeProductsDidUpdate")
- static let storeStateDidChange = Notification.Name("storeStateDidChange")
- }
- // MARK: - Premium Access
- enum PremiumAccess {
- @MainActor
- static func canAccess(_ feature: AppFeature) -> Bool {
- let store = StoreManager.shared
- if store.isResolvingEntitlements {
- return FreeTierManager.canAccess(feature, isPremium: false)
- }
- return FreeTierManager.canAccess(feature, isPremium: store.isPremium)
- }
- @MainActor
- @discardableResult
- static func require(
- feature: AppFeature,
- from window: NSWindow?,
- returnToHomeOnDismiss: Bool = false
- ) -> Bool {
- guard canAccess(feature) else {
- presentPaywallOrLimitAlert(for: feature, from: window, returnToHomeOnDismiss: returnToHomeOnDismiss)
- return false
- }
- return true
- }
- @MainActor
- @discardableResult
- static func require(from window: NSWindow?, returnToHomeOnDismiss: Bool = false) -> Bool {
- require(feature: .documentImport, from: window, returnToHomeOnDismiss: returnToHomeOnDismiss)
- }
- @MainActor
- private static func presentPaywallOrLimitAlert(
- for feature: AppFeature,
- from window: NSWindow?,
- returnToHomeOnDismiss: Bool
- ) {
- if StoreManager.shared.isResolvingEntitlements {
- Task { @MainActor in
- await StoreManager.shared.ensureEntitlementsResolved()
- guard !canAccess(feature) else { return }
- presentPaywallOrLimitAlert(for: feature, from: window, returnToHomeOnDismiss: returnToHomeOnDismiss)
- }
- return
- }
- guard let viewController = mainViewController(from: window) else { return }
- viewController.presentPaywall(returnToHomeOnDismiss: returnToHomeOnDismiss)
- }
- @MainActor
- static func mainViewController(from window: NSWindow?) -> ViewController? {
- if let viewController = window?.contentViewController as? ViewController {
- return viewController
- }
- if let viewController = NSApp.keyWindow?.contentViewController as? ViewController {
- return viewController
- }
- for appWindow in NSApp.windows where appWindow.canBecomeMain {
- if let viewController = appWindow.contentViewController as? ViewController {
- return viewController
- }
- }
- return nil
- }
- }
- // MARK: - Left Panel
- private final class PaywallLeftPanelView: NSView, AppearanceRefreshable {
- private let gradientLayer = CAGradientLayer()
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- wantsLayer = true
- gradientLayer.startPoint = CGPoint(x: 0.5, y: 1)
- gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
- layer?.insertSublayer(gradientLayer, at: 0)
- refreshAppearance()
- }
- func refreshAppearance() {
- gradientLayer.colors = AppTheme.paywallLeftGradientColors.map(\.cgColor)
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- override func layout() {
- super.layout()
- gradientLayer.frame = bounds
- let mask = CAShapeLayer()
- mask.path = CGPath(
- roundedRect: bounds,
- cornerWidth: 20,
- cornerHeight: 20,
- transform: nil
- )
- layer?.mask = mask
- }
- }
- // MARK: - Badge
- private final class PaywallBadgeView: NSView {
- private let label: NSTextField
- init(text: String, iconName: String, background: NSColor, foreground: NSColor) {
- label = NSTextField(labelWithString: text)
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- wantsLayer = true
- layer?.backgroundColor = background.cgColor
- layer?.cornerRadius = 10
- layer?.masksToBounds = true
- let icon = NSImageView()
- icon.translatesAutoresizingMaskIntoConstraints = false
- if let image = NSImage(systemSymbolName: iconName, accessibilityDescription: nil) {
- let config = NSImage.SymbolConfiguration(pointSize: 9, weight: .semibold)
- icon.image = image.withSymbolConfiguration(config)
- }
- icon.contentTintColor = foreground
- label.font = AppTheme.semiboldFont(size: 10)
- label.textColor = foreground
- label.translatesAutoresizingMaskIntoConstraints = false
- addSubview(icon)
- addSubview(label)
- NSLayoutConstraint.activate([
- heightAnchor.constraint(equalToConstant: 20),
- icon.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
- icon.centerYAnchor.constraint(equalTo: centerYAnchor),
- icon.widthAnchor.constraint(equalToConstant: 12),
- icon.heightAnchor.constraint(equalToConstant: 12),
- label.leadingAnchor.constraint(equalTo: icon.trailingAnchor, constant: 4),
- label.centerYAnchor.constraint(equalTo: centerYAnchor),
- label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
- ])
- }
- func updateText(_ text: String) {
- label.stringValue = text
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- }
- // MARK: - Feature Row
- private final class PaywallFeatureRow: NSView, AppearanceRefreshable {
- private let label: NSTextField
- init(text: String) {
- label = NSTextField.themeLabel(text, style: .primary, font: AppTheme.regularFont(size: 14))
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- let checkContainer = NSView()
- checkContainer.translatesAutoresizingMaskIntoConstraints = false
- checkContainer.wantsLayer = true
- checkContainer.layer?.backgroundColor = AppTheme.green.cgColor
- checkContainer.layer?.cornerRadius = 10
- checkContainer.layer?.masksToBounds = true
- let checkIcon = NSImageView()
- checkIcon.translatesAutoresizingMaskIntoConstraints = false
- if let image = NSImage(systemSymbolName: "checkmark", accessibilityDescription: nil) {
- let config = NSImage.SymbolConfiguration(pointSize: 9, weight: .bold)
- checkIcon.image = image.withSymbolConfiguration(config)
- }
- checkIcon.contentTintColor = .white
- label.translatesAutoresizingMaskIntoConstraints = false
- addSubview(checkContainer)
- checkContainer.addSubview(checkIcon)
- addSubview(label)
- NSLayoutConstraint.activate([
- heightAnchor.constraint(equalToConstant: 28),
- checkContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
- checkContainer.centerYAnchor.constraint(equalTo: centerYAnchor),
- checkContainer.widthAnchor.constraint(equalToConstant: 20),
- checkContainer.heightAnchor.constraint(equalToConstant: 20),
- checkIcon.centerXAnchor.constraint(equalTo: checkContainer.centerXAnchor),
- checkIcon.centerYAnchor.constraint(equalTo: checkContainer.centerYAnchor),
- checkIcon.widthAnchor.constraint(equalToConstant: 12),
- checkIcon.heightAnchor.constraint(equalToConstant: 12),
- label.leadingAnchor.constraint(equalTo: checkContainer.trailingAnchor, constant: 12),
- label.centerYAnchor.constraint(equalTo: centerYAnchor),
- label.trailingAnchor.constraint(equalTo: trailingAnchor),
- ])
- }
- func updateText(_ text: String) {
- label.stringValue = text
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func refreshAppearance() {
- label.refreshThemeLabelColor()
- }
- }
- // MARK: - Plan Card
- private final class PaywallPlanCard: NSControl, AppearanceRefreshable {
- var onSelect: (() -> Void)?
- private let plan: PaywallPlan
- private let titleLabel = NSTextField(labelWithString: "")
- private let subtitleLabel = NSTextField(labelWithString: "")
- private let priceLabel = NSTextField(labelWithString: "")
- private var badgeView: PaywallBadgeView?
- private var trialBadgeView: PaywallBadgeView?
- private var hoverTracker: HoverTracker?
- private var isHovered = false
- var isChosen: Bool = false {
- didSet { updateAppearance() }
- }
- init(plan: PaywallPlan) {
- self.plan = plan
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- wantsLayer = true
- layer?.cornerRadius = 12
- layer?.masksToBounds = false
- let config = PaywallConfigService.shared.config
- titleLabel.stringValue = plan.planCopy(from: config).title
- titleLabel.font = AppTheme.semiboldFont(size: 15)
- titleLabel.translatesAutoresizingMaskIntoConstraints = false
- subtitleLabel.stringValue = config.loadingPrice
- subtitleLabel.font = AppTheme.regularFont(size: 11)
- subtitleLabel.themeLabelStyle = .secondary
- subtitleLabel.textColor = AppTheme.textSecondary
- subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
- priceLabel.stringValue = config.loadingPrice
- priceLabel.font = AppTheme.semiboldFont(size: 15)
- priceLabel.alignment = .right
- priceLabel.translatesAutoresizingMaskIntoConstraints = false
- addSubview(titleLabel)
- addSubview(subtitleLabel)
- addSubview(priceLabel)
- NSLayoutConstraint.activate([
- heightAnchor.constraint(equalToConstant: 86),
- titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
- titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 24),
- subtitleLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
- subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 3),
- subtitleLabel.trailingAnchor.constraint(lessThanOrEqualTo: priceLabel.leadingAnchor, constant: -12),
- priceLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
- priceLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
- ])
- if plan == .lifetime, let badgeText = plan.planCopy(from: config).badge {
- let badge = PaywallBadgeView(
- text: badgeText,
- iconName: "star.fill",
- background: AppTheme.paywallGold,
- foreground: AppTheme.paywallGoldText
- )
- badgeView = badge
- addSubview(badge)
- NSLayoutConstraint.activate([
- badge.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
- badge.topAnchor.constraint(equalTo: topAnchor, constant: 8),
- ])
- }
- applyCardShadow()
- updateAppearance()
- hoverTracker = HoverTracker(view: self) { [weak self] hovering in
- self?.setHovered(hovering)
- }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func updateDisplay(
- product: Product?,
- config: PaywallConfig,
- introOffer: Product.SubscriptionOffer? = nil
- ) {
- titleLabel.stringValue = plan.planCopy(from: config).title
- subtitleLabel.stringValue = plan.localizedSubtitle(from: product, config: config, introOffer: introOffer)
- priceLabel.stringValue = plan.localizedPrice(from: product, config: config)
- updateLifetimeBadge(config: config)
- updateTrialBadge(introOffer: introOffer)
- }
- private func updateLifetimeBadge(config: PaywallConfig) {
- guard plan == .lifetime else { return }
- if let badgeText = plan.planCopy(from: config).badge {
- if let badgeView {
- badgeView.updateText(badgeText)
- badgeView.isHidden = false
- }
- } else {
- badgeView?.isHidden = true
- }
- }
- private func updateTrialBadge(introOffer: Product.SubscriptionOffer?) {
- guard plan == .yearly else { return }
- if let introOffer {
- let text = PaywallConfigService.shared.config.trialBadgeText(from: introOffer)
- if let trialBadgeView {
- trialBadgeView.updateText(text)
- trialBadgeView.isHidden = false
- } else {
- let badge = PaywallBadgeView(
- text: text,
- iconName: "calendar",
- background: AppTheme.paywallPink,
- foreground: AppTheme.paywallPinkText
- )
- trialBadgeView = badge
- addSubview(badge)
- NSLayoutConstraint.activate([
- badge.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
- badge.topAnchor.constraint(equalTo: topAnchor, constant: 8),
- ])
- }
- } else {
- trialBadgeView?.isHidden = true
- }
- }
- func refreshAppearance() {
- updateAppearance()
- subtitleLabel.refreshThemeLabelColor()
- if isHovered {
- applyHoverLift(true)
- }
- }
- private func setHovered(_ hovering: Bool) {
- isHovered = hovering
- applyHoverLift(hovering)
- updateAppearance()
- }
- private func updateAppearance() {
- let titleColor = isChosen && plan == .yearly ? AppTheme.green : AppTheme.paywallAccent
- titleLabel.textColor = titleColor
- priceLabel.textColor = titleColor
- layer?.backgroundColor = AppTheme.paywallTrustBackground.cgColor
- if isChosen {
- layer?.borderWidth = 2
- layer?.borderColor = AppTheme.green.cgColor
- } else if isHovered {
- layer?.borderWidth = 2
- let hoverBorder = AppTheme.paywallBorder.blended(withFraction: 0.35, of: AppTheme.paywallAccent)
- ?? AppTheme.paywallBorder
- layer?.borderColor = hoverBorder.cgColor
- } else {
- layer?.borderWidth = 1.5
- layer?.borderColor = AppTheme.paywallBorder.cgColor
- }
- }
- override func mouseUp(with event: NSEvent) {
- guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
- onSelect?()
- }
- override func resetCursorRects() {
- addCursorRect(bounds, cursor: .pointingHand)
- }
- }
- // MARK: - Footer Link
- private final class PaywallFooterLink: NSButton, AppearanceRefreshable {
- private var hoverTracker: HoverTracker?
- private var isHovered = false
- init(title: String) {
- super.init(frame: .zero)
- updateTitle(title)
- isBordered = false
- bezelStyle = .inline
- font = AppTheme.regularFont(size: 11)
- translatesAutoresizingMaskIntoConstraints = false
- refreshAppearance()
- hoverTracker = HoverTracker(view: self) { [weak self] hovering in
- self?.isHovered = hovering
- self?.refreshAppearance()
- }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func updateTitle(_ title: String) {
- self.title = title
- invalidateIntrinsicContentSize()
- needsDisplay = true
- }
- func refreshAppearance() {
- contentTintColor = isHovered ? AppTheme.textPrimary : AppTheme.textSecondary
- }
- override func resetCursorRects() {
- addCursorRect(bounds, cursor: .pointingHand)
- }
- }
- // MARK: - Footer Trust Item
- private final class PaywallTrustItemView: NSView, AppearanceRefreshable {
- private let iconContainer = NSView()
- private let icon = NSImageView()
- private let titleLabel: NSTextField
- private let subtitleLabel: NSTextField
- init(iconName: String, title: String, subtitle: String) {
- titleLabel = NSTextField.themeLabel(title, style: .primary, font: AppTheme.semiboldFont(size: 11))
- subtitleLabel = NSTextField.themeLabel(subtitle, style: .secondary, font: AppTheme.regularFont(size: 9))
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- iconContainer.translatesAutoresizingMaskIntoConstraints = false
- iconContainer.wantsLayer = true
- iconContainer.layer?.cornerRadius = 10
- iconContainer.layer?.masksToBounds = true
- icon.translatesAutoresizingMaskIntoConstraints = false
- if let image = NSImage(systemSymbolName: iconName, accessibilityDescription: nil) {
- let config = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
- icon.image = image.withSymbolConfiguration(config)
- }
- titleLabel.lineBreakMode = .byTruncatingTail
- titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
- titleLabel.translatesAutoresizingMaskIntoConstraints = false
- subtitleLabel.lineBreakMode = .byTruncatingTail
- subtitleLabel.maximumNumberOfLines = 2
- subtitleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
- subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
- addSubview(iconContainer)
- iconContainer.addSubview(icon)
- addSubview(titleLabel)
- addSubview(subtitleLabel)
- NSLayoutConstraint.activate([
- heightAnchor.constraint(equalToConstant: Self.preferredHeight),
- iconContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
- iconContainer.topAnchor.constraint(equalTo: topAnchor),
- iconContainer.widthAnchor.constraint(equalToConstant: 20),
- iconContainer.heightAnchor.constraint(equalToConstant: 20),
- icon.centerXAnchor.constraint(equalTo: iconContainer.centerXAnchor),
- icon.centerYAnchor.constraint(equalTo: iconContainer.centerYAnchor),
- icon.widthAnchor.constraint(equalToConstant: 12),
- icon.heightAnchor.constraint(equalToConstant: 12),
- titleLabel.leadingAnchor.constraint(equalTo: iconContainer.trailingAnchor, constant: 8),
- titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 1),
- titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
- subtitleLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
- subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 2),
- subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
- subtitleLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
- ])
- setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
- setContentHuggingPriority(.defaultLow, for: .horizontal)
- setContentHuggingPriority(.required, for: .vertical)
- setContentCompressionResistancePriority(.required, for: .vertical)
- refreshAppearance()
- }
- fileprivate static let preferredHeight: CGFloat = 34
- func update(iconName: String, title: String, subtitle: String) {
- titleLabel.stringValue = title
- subtitleLabel.stringValue = subtitle
- if let image = NSImage(systemSymbolName: iconName, accessibilityDescription: nil) {
- let symbolConfig = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
- icon.image = image.withSymbolConfiguration(symbolConfig)
- }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func refreshAppearance() {
- iconContainer.layer?.backgroundColor = AppTheme.paywallTrustIconBackground.cgColor
- icon.contentTintColor = AppTheme.paywallIconAccent
- titleLabel.refreshThemeLabelColor()
- subtitleLabel.refreshThemeLabelColor()
- }
- }
- // MARK: - Products Error
- private final class PaywallProductsErrorView: NSView, AppearanceRefreshable {
- var onRetry: (() -> Void)?
- private let messageLabel = NSTextField(wrappingLabelWithString: "")
- private let retryButton = PaywallFooterLink(title: "")
- init() {
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- messageLabel.font = AppTheme.regularFont(size: 12)
- messageLabel.textColor = AppTheme.textSecondary
- messageLabel.alignment = .center
- messageLabel.maximumNumberOfLines = 3
- messageLabel.lineBreakMode = .byWordWrapping
- messageLabel.cell?.wraps = true
- messageLabel.cell?.isScrollable = false
- messageLabel.translatesAutoresizingMaskIntoConstraints = false
- retryButton.target = self
- retryButton.action = #selector(retryTapped)
- addSubview(messageLabel)
- addSubview(retryButton)
- NSLayoutConstraint.activate([
- messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
- messageLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
- messageLabel.topAnchor.constraint(equalTo: topAnchor),
- retryButton.centerXAnchor.constraint(equalTo: centerXAnchor),
- retryButton.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 8),
- retryButton.bottomAnchor.constraint(equalTo: bottomAnchor),
- ])
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func update(message: String, isRetrying: Bool) {
- let messages = PaywallConfigService.shared.config.messages
- messageLabel.stringValue = message
- retryButton.isHidden = false
- retryButton.isEnabled = !isRetrying
- retryButton.updateTitle(isRetrying ? messages.retrying : messages.retry)
- }
- func refreshAppearance() {
- messageLabel.textColor = AppTheme.textSecondary
- retryButton.refreshAppearance()
- }
- @objc private func retryTapped() {
- onRetry?()
- }
- }
- // MARK: - Close Button
- private final class PaywallCloseButton: NSButton, AppearanceRefreshable {
- private var hoverTracker: HoverTracker?
- private var isHovered = false
- init() {
- super.init(frame: .zero)
- isBordered = false
- bezelStyle = .inline
- translatesAutoresizingMaskIntoConstraints = false
- wantsLayer = true
- layer?.cornerRadius = 15
- layer?.borderWidth = 1.5
- if let image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Close") {
- let config = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
- self.image = image.withSymbolConfiguration(config)
- }
- refreshAppearance()
- hoverTracker = HoverTracker(view: self) { [weak self] hovering in
- self?.setHovered(hovering)
- }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func refreshAppearance() {
- updateAppearance()
- }
- private func setHovered(_ hovering: Bool) {
- isHovered = hovering
- animateHover {
- updateAppearance()
- layer?.transform = hovering
- ? CATransform3DMakeScale(1.06, 1.06, 1)
- : CATransform3DIdentity
- }
- }
- private func updateAppearance() {
- if isHovered {
- layer?.backgroundColor = AppTheme.elevatedBackground.cgColor
- layer?.borderColor = AppTheme.paywallAccent.withAlphaComponent(0.45).cgColor
- contentTintColor = AppTheme.textPrimary
- } else {
- layer?.backgroundColor = AppTheme.paywallTrustBackground.cgColor
- layer?.borderColor = AppTheme.paywallBorder.cgColor
- contentTintColor = AppTheme.textSecondary
- }
- }
- override func resetCursorRects() {
- addCursorRect(bounds, cursor: .pointingHand)
- }
- }
- // MARK: - CTA Button
- private final class PaywallCTAButton: NSButton, AppearanceRefreshable {
- private var hoverTracker: HoverTracker?
- init() {
- super.init(frame: .zero)
- isBordered = false
- wantsLayer = true
- layer?.cornerRadius = 12
- font = AppTheme.semiboldFont(size: 15)
- translatesAutoresizingMaskIntoConstraints = false
- refreshAppearance()
- hoverTracker = HoverTracker(view: self) { [weak self] hovering in
- self?.setHovered(hovering)
- }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- func refreshAppearance() {
- layer?.backgroundColor = AppTheme.paywallCTABackground.cgColor
- contentTintColor = AppTheme.paywallCTAForeground
- }
- private func setHovered(_ hovering: Bool) {
- let base = AppTheme.paywallCTABackground
- let color = hovering ? base.blended(withFraction: 0.12, of: .black) ?? base : base
- animateHover {
- layer?.backgroundColor = color.cgColor
- layer?.transform = hovering
- ? CATransform3DMakeScale(1.02, 1.02, 1)
- : CATransform3DIdentity
- }
- }
- override func resetCursorRects() {
- addCursorRect(bounds, cursor: .pointingHand)
- }
- }
- // MARK: - Main Paywall Card
- final class PaywallView: NSView, AppearanceRefreshable {
- var onClose: (() -> Void)?
- private var selectedPlan: PaywallPlan = .yearly
- private var planCards: [PaywallPlan: PaywallPlanCard] = [:]
- private let ctaButton = PaywallCTAButton()
- private let renewalDisclosureLabel = NSTextField(wrappingLabelWithString: "")
- private let continueFreePlanLink = PaywallFooterLink(title: "")
- private let manageSubscriptionLink = PaywallFooterLink(title: "")
- private let restoreLink = PaywallFooterLink(title: "")
- private let privacyLink = PaywallFooterLink(title: "")
- private let termsLink = PaywallFooterLink(title: "")
- private let supportLink = PaywallFooterLink(title: "")
- private let premiumCloseButton = PaywallCloseButton()
- private var primaryFooterLinkCell: NSView?
- private var leftPanelTitle: NSTextField!
- private var rightTitle: NSTextField!
- private var rightSubtitle: NSTextField!
- private var featuresStack: NSStackView!
- private var featureRows: [PaywallFeatureRow] = []
- private var trustItemViews: [PaywallTrustItemView] = []
- private var trustStack: NSStackView!
- private var productsErrorView: PaywallProductsErrorView!
- private var plansStack: NSStackView!
- private var plansStackHeightConstraint: NSLayoutConstraint!
- private var productsErrorHeightConstraint: NSLayoutConstraint!
- private var storeObservers: [NSObjectProtocol] = []
- init() {
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- wantsLayer = true
- layer?.cornerRadius = 0
- setup()
- observeStoreUpdates()
- refreshConfigDisplay()
- refreshProductDisplay()
- refreshAppearance()
- }
- deinit {
- storeObservers.forEach { NotificationCenter.default.removeObserver($0) }
- }
- private func observeStoreUpdates() {
- let center = NotificationCenter.default
- storeObservers = [
- center.addObserver(forName: .storeProductsDidUpdate, object: nil, queue: .main) { [weak self] _ in
- self?.refreshProductDisplay()
- },
- center.addObserver(forName: .storeStateDidChange, object: nil, queue: .main) { [weak self] _ in
- self?.refreshProductDisplay()
- },
- center.addObserver(forName: .premiumStatusDidChange, object: nil, queue: .main) { [weak self] _ in
- self?.refreshProductDisplay()
- },
- center.addObserver(forName: .paywallConfigDidUpdate, object: nil, queue: .main) { [weak self] _ in
- self?.refreshConfigDisplay()
- self?.refreshProductDisplay()
- },
- ]
- }
- private var paywallConfig: PaywallConfig {
- PaywallConfigService.shared.config
- }
- private func refreshConfigDisplay() {
- let config = paywallConfig
- leftPanelTitle?.stringValue = config.leftPanelTitle
- rightTitle?.stringValue = config.rightTitle
- rightSubtitle?.stringValue = config.rightSubtitle
- syncFeatureRows(with: config.features)
- syncTrustItems(with: config.trustItems)
- continueFreePlanLink.updateTitle(config.footer.continueFree)
- manageSubscriptionLink.updateTitle(config.footer.manageSubscription)
- restoreLink.updateTitle(config.footer.restorePurchase)
- privacyLink.updateTitle(config.footer.privacyPolicy)
- termsLink.updateTitle(config.footer.termsOfService)
- supportLink.updateTitle(config.footer.support)
- }
- private func syncFeatureRows(with features: [String]) {
- guard let featuresStack else { return }
- while featureRows.count < features.count {
- let row = PaywallFeatureRow(text: "")
- featureRows.append(row)
- featuresStack.addArrangedSubview(row)
- }
- while featureRows.count > features.count {
- let row = featureRows.removeLast()
- featuresStack.removeArrangedSubview(row)
- row.removeFromSuperview()
- }
- for (index, text) in features.enumerated() {
- featureRows[index].updateText(text)
- }
- }
- private func syncTrustItems(with items: [PaywallConfig.TrustItem]) {
- for (index, item) in items.enumerated() where index < trustItemViews.count {
- trustItemViews[index].update(
- iconName: item.icon,
- title: item.title,
- subtitle: item.subtitle
- )
- }
- }
- private func refreshProductDisplay() {
- let store = StoreManager.shared
- let config = paywallConfig
- for (plan, card) in planCards {
- let product = store.product(for: plan)
- card.updateDisplay(
- product: product,
- config: config,
- introOffer: store.eligibleIntroOffer(for: plan)
- )
- }
- ctaButton.title = selectedPlan.localizedCTATitle(
- from: store.product(for: selectedPlan),
- config: config,
- introOffer: store.eligibleIntroOffer(for: selectedPlan)
- )
- updateRenewalDisclosure(
- selectedPlan.localizedRenewalDisclosure(
- from: store.product(for: selectedPlan),
- config: config,
- introOffer: store.eligibleIntroOffer(for: selectedPlan)
- )
- )
- refreshTrustItemsForSelectedPlan()
- refreshProductsErrorState()
- refreshPurchaseState()
- }
- private func refreshProductsErrorState() {
- let store = StoreManager.shared
- let hasLoadFailure = store.productLoadError != nil && store.products.isEmpty
- let isRetrying = hasLoadFailure && store.isLoadingProducts
- productsErrorView.isHidden = !hasLoadFailure
- plansStack.isHidden = hasLoadFailure
- plansStackHeightConstraint.constant = hasLoadFailure ? 0 : plansAreaHeight
- productsErrorHeightConstraint.constant = hasLoadFailure ? productsErrorAreaHeight : 0
- if hasLoadFailure, let error = store.productLoadError {
- productsErrorView.update(message: error, isRetrying: isRetrying)
- }
- }
- private var plansAreaHeight: CGFloat {
- let cardHeight: CGFloat = 86
- let spacing: CGFloat = 12
- let planCount = CGFloat(PaywallPlan.allCases.count)
- return planCount * cardHeight + max(0, planCount - 1) * spacing
- }
- private var productsErrorAreaHeight: CGFloat { 86 }
- private func refreshPurchaseState() {
- let store = StoreManager.shared
- let hasProductLoadFailure = store.productLoadError != nil && store.products.isEmpty
- let isProductReady = store.product(for: selectedPlan) != nil
- let isBusy = store.isPurchasing || store.isLoadingProducts
- let isDisabled = isBusy || !isProductReady || hasProductLoadFailure
- ctaButton.isEnabled = !isDisabled
- ctaButton.alphaValue = isDisabled ? 0.65 : 1
- refreshPrimaryFooterLink()
- }
- private func refreshPrimaryFooterLink() {
- let store = StoreManager.shared
- let showContinue = !store.isPro
- let showManage = store.hasActiveSubscription
- continueFreePlanLink.isHidden = !showContinue
- manageSubscriptionLink.isHidden = !showManage
- premiumCloseButton.isHidden = !store.isPro
- primaryFooterLinkCell?.isHidden = !showContinue && !showManage
- primaryFooterLinkCell?.needsLayout = true
- primaryFooterLinkCell?.layoutSubtreeIfNeeded()
- }
- private func refreshTrustItemsForSelectedPlan() {
- guard trustItemViews.count > 1, paywallConfig.trustItems.count > 1 else { return }
- let cancelItem = paywallConfig.trustItems[1]
- if selectedPlan == .lifetime {
- let item = paywallConfig.lifetimeTrustItem
- trustItemViews[1].update(
- iconName: item.icon,
- title: item.title,
- subtitle: item.subtitle
- )
- } else {
- trustItemViews[1].update(
- iconName: cancelItem.icon,
- title: cancelItem.title,
- subtitle: cancelItem.subtitle
- )
- }
- }
- func refreshStoreState() {
- refreshProductDisplay()
- }
- override func layout() {
- super.layout()
- let width = renewalDisclosureLabel.bounds.width
- guard width > 0, renewalDisclosureLabel.preferredMaxLayoutWidth != width else { return }
- renewalDisclosureLabel.preferredMaxLayoutWidth = width
- renewalDisclosureLabel.invalidateIntrinsicContentSize()
- }
- private func updateRenewalDisclosure(_ text: String) {
- renewalDisclosureLabel.stringValue = text
- renewalDisclosureLabel.invalidateIntrinsicContentSize()
- needsLayout = true
- }
- func refreshAppearance() {
- layer?.backgroundColor = AppTheme.paywallBackground.cgColor
- leftPanelTitle?.refreshThemeLabelColor()
- rightTitle?.refreshThemeLabelColor()
- rightSubtitle?.refreshThemeLabelColor()
- renewalDisclosureLabel.textColor = AppTheme.textSecondary
- trustStack?.layer?.backgroundColor = AppTheme.paywallTrustBackground.cgColor
- trustStack?.layer?.borderColor = AppTheme.paywallBorder.cgColor
- ctaButton.refreshAppearance()
- subviews.forEach { $0.refreshAppearanceRecursively() }
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- private func setup() {
- let leftPanel = makeLeftPanel()
- let rightPanel = makeRightPanel()
- premiumCloseButton.target = self
- premiumCloseButton.action = #selector(premiumCloseTapped)
- premiumCloseButton.isHidden = true
- addSubview(leftPanel)
- addSubview(rightPanel)
- addSubview(premiumCloseButton)
- NSLayoutConstraint.activate([
- leftPanel.leadingAnchor.constraint(equalTo: leadingAnchor),
- leftPanel.topAnchor.constraint(equalTo: topAnchor),
- leftPanel.bottomAnchor.constraint(equalTo: bottomAnchor),
- leftPanel.widthAnchor.constraint(equalToConstant: 320),
- rightPanel.leadingAnchor.constraint(equalTo: leftPanel.trailingAnchor),
- rightPanel.trailingAnchor.constraint(equalTo: trailingAnchor),
- rightPanel.topAnchor.constraint(equalTo: topAnchor),
- rightPanel.bottomAnchor.constraint(equalTo: bottomAnchor),
- premiumCloseButton.topAnchor.constraint(equalTo: topAnchor, constant: 16),
- premiumCloseButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
- premiumCloseButton.widthAnchor.constraint(equalToConstant: 30),
- premiumCloseButton.heightAnchor.constraint(equalToConstant: 30),
- ])
- }
- private func makeLeftPanel() -> NSView {
- let panel = PaywallLeftPanelView()
- panel.translatesAutoresizingMaskIntoConstraints = false
- let config = paywallConfig
- let title = NSTextField.themeLabel(
- config.leftPanelTitle,
- style: .primary,
- font: AppTheme.semiboldFont(size: 22)
- )
- title.maximumNumberOfLines = 2
- title.translatesAutoresizingMaskIntoConstraints = false
- leftPanelTitle = title
- let featuresStack = NSStackView()
- featuresStack.orientation = .vertical
- featuresStack.spacing = 6
- featuresStack.alignment = .leading
- featuresStack.translatesAutoresizingMaskIntoConstraints = false
- self.featuresStack = featuresStack
- for feature in config.features {
- let row = PaywallFeatureRow(text: feature)
- featureRows.append(row)
- featuresStack.addArrangedSubview(row)
- }
- panel.addSubview(title)
- panel.addSubview(featuresStack)
- NSLayoutConstraint.activate([
- title.leadingAnchor.constraint(equalTo: panel.leadingAnchor, constant: 28),
- title.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -20),
- title.topAnchor.constraint(equalTo: panel.topAnchor, constant: 48),
- featuresStack.leadingAnchor.constraint(equalTo: title.leadingAnchor),
- featuresStack.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -20),
- featuresStack.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 28),
- ])
- return panel
- }
- private func makeRightPanel() -> NSView {
- let panel = NSView()
- panel.translatesAutoresizingMaskIntoConstraints = false
- let config = paywallConfig
- let title = NSTextField.themeLabel(
- config.rightTitle,
- style: .primary,
- font: AppTheme.semiboldFont(size: 26)
- )
- title.alignment = .center
- title.translatesAutoresizingMaskIntoConstraints = false
- rightTitle = title
- let subtitle = NSTextField.themeLabel(
- config.rightSubtitle,
- style: .secondary,
- font: AppTheme.regularFont(size: 13)
- )
- subtitle.alignment = .center
- subtitle.maximumNumberOfLines = 2
- subtitle.translatesAutoresizingMaskIntoConstraints = false
- rightSubtitle = subtitle
- let plansStack = NSStackView()
- plansStack.orientation = .vertical
- plansStack.spacing = 12
- plansStack.translatesAutoresizingMaskIntoConstraints = false
- self.plansStack = plansStack
- let plansAreaView = NSView()
- plansAreaView.translatesAutoresizingMaskIntoConstraints = false
- let productsErrorView = PaywallProductsErrorView()
- productsErrorView.isHidden = true
- productsErrorView.onRetry = { [weak self] in
- self?.retryProductLoad()
- }
- self.productsErrorView = productsErrorView
- for plan in PaywallPlan.allCases {
- let card = PaywallPlanCard(plan: plan)
- card.isChosen = plan == selectedPlan
- card.onSelect = { [weak self] in self?.selectPlan(plan) }
- planCards[plan] = card
- plansStack.addArrangedSubview(card)
- }
- ctaButton.title = selectedPlan.localizedCTATitle(
- from: StoreManager.shared.product(for: selectedPlan),
- config: config,
- introOffer: StoreManager.shared.eligibleIntroOffer(for: selectedPlan)
- )
- ctaButton.target = self
- ctaButton.action = #selector(purchaseTapped)
- ctaButton.translatesAutoresizingMaskIntoConstraints = false
- renewalDisclosureLabel.font = AppTheme.regularFont(size: 10)
- renewalDisclosureLabel.textColor = AppTheme.textSecondary
- renewalDisclosureLabel.alignment = .center
- renewalDisclosureLabel.maximumNumberOfLines = 3
- renewalDisclosureLabel.lineBreakMode = .byWordWrapping
- renewalDisclosureLabel.cell?.wraps = true
- renewalDisclosureLabel.cell?.isScrollable = false
- renewalDisclosureLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
- renewalDisclosureLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
- renewalDisclosureLabel.setContentHuggingPriority(.required, for: .vertical)
- renewalDisclosureLabel.translatesAutoresizingMaskIntoConstraints = false
- updateRenewalDisclosure(
- selectedPlan.localizedRenewalDisclosure(
- from: StoreManager.shared.product(for: selectedPlan),
- config: config,
- introOffer: StoreManager.shared.eligibleIntroOffer(for: selectedPlan)
- )
- )
- let trustRow = makeTrustRow()
- let footerLinks = makeFooterLinks()
- let footerSpacer = NSView()
- footerSpacer.translatesAutoresizingMaskIntoConstraints = false
- footerSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
- footerSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
- plansAreaView.addSubview(plansStack)
- plansAreaView.addSubview(productsErrorView)
- plansStackHeightConstraint = plansStack.heightAnchor.constraint(equalToConstant: plansAreaHeight)
- productsErrorHeightConstraint = productsErrorView.heightAnchor.constraint(equalToConstant: 0)
- panel.addSubview(title)
- panel.addSubview(subtitle)
- panel.addSubview(plansAreaView)
- panel.addSubview(trustRow)
- panel.addSubview(ctaButton)
- panel.addSubview(renewalDisclosureLabel)
- panel.addSubview(footerSpacer)
- panel.addSubview(footerLinks)
- NSLayoutConstraint.activate([
- title.leadingAnchor.constraint(equalTo: panel.leadingAnchor, constant: 28),
- title.topAnchor.constraint(equalTo: panel.topAnchor, constant: 40),
- title.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -28),
- subtitle.leadingAnchor.constraint(equalTo: title.leadingAnchor),
- subtitle.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -28),
- subtitle.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 6),
- plansAreaView.leadingAnchor.constraint(equalTo: title.leadingAnchor),
- plansAreaView.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -28),
- plansAreaView.topAnchor.constraint(equalTo: subtitle.bottomAnchor, constant: 24),
- plansAreaView.heightAnchor.constraint(equalToConstant: plansAreaHeight),
- plansStack.leadingAnchor.constraint(equalTo: plansAreaView.leadingAnchor),
- plansStack.trailingAnchor.constraint(equalTo: plansAreaView.trailingAnchor),
- plansStack.topAnchor.constraint(equalTo: plansAreaView.topAnchor),
- plansStackHeightConstraint,
- productsErrorView.leadingAnchor.constraint(equalTo: plansAreaView.leadingAnchor),
- productsErrorView.trailingAnchor.constraint(equalTo: plansAreaView.trailingAnchor),
- productsErrorView.topAnchor.constraint(equalTo: plansAreaView.topAnchor),
- productsErrorHeightConstraint,
- trustRow.leadingAnchor.constraint(equalTo: plansAreaView.leadingAnchor),
- trustRow.trailingAnchor.constraint(equalTo: plansAreaView.trailingAnchor),
- trustRow.topAnchor.constraint(equalTo: plansAreaView.bottomAnchor, constant: 18),
- ctaButton.leadingAnchor.constraint(equalTo: plansAreaView.leadingAnchor),
- ctaButton.trailingAnchor.constraint(equalTo: plansAreaView.trailingAnchor),
- ctaButton.topAnchor.constraint(equalTo: trustRow.bottomAnchor, constant: 16),
- ctaButton.heightAnchor.constraint(equalToConstant: 48),
- renewalDisclosureLabel.leadingAnchor.constraint(equalTo: plansAreaView.leadingAnchor),
- renewalDisclosureLabel.trailingAnchor.constraint(equalTo: plansAreaView.trailingAnchor),
- renewalDisclosureLabel.topAnchor.constraint(equalTo: ctaButton.bottomAnchor, constant: 10),
- footerSpacer.leadingAnchor.constraint(equalTo: panel.leadingAnchor),
- footerSpacer.trailingAnchor.constraint(equalTo: panel.trailingAnchor),
- footerSpacer.topAnchor.constraint(equalTo: renewalDisclosureLabel.bottomAnchor, constant: 16),
- footerSpacer.bottomAnchor.constraint(equalTo: footerLinks.topAnchor),
- footerLinks.leadingAnchor.constraint(equalTo: panel.leadingAnchor, constant: 28),
- footerLinks.trailingAnchor.constraint(equalTo: panel.trailingAnchor, constant: -28),
- footerLinks.bottomAnchor.constraint(equalTo: panel.bottomAnchor, constant: -16),
- ])
- return panel
- }
- private func makeTrustRow() -> NSView {
- let config = paywallConfig
- trustItemViews = config.trustItems.map { item in
- PaywallTrustItemView(
- iconName: item.icon,
- title: item.title,
- subtitle: item.subtitle
- )
- }
- let trustStack = NSStackView(views: trustItemViews)
- trustStack.orientation = .horizontal
- trustStack.distribution = .fillEqually
- trustStack.spacing = 16
- trustStack.alignment = .top
- trustStack.translatesAutoresizingMaskIntoConstraints = false
- trustStack.wantsLayer = true
- trustStack.layer?.cornerRadius = 12
- trustStack.layer?.borderWidth = 1.5
- trustStack.layer?.masksToBounds = true
- trustStack.edgeInsets = NSEdgeInsets(top: 12, left: 14, bottom: 12, right: 14)
- trustStack.translatesAutoresizingMaskIntoConstraints = false
- trustStack.setContentHuggingPriority(.required, for: .vertical)
- trustStack.setContentCompressionResistancePriority(.required, for: .vertical)
- trustStack.heightAnchor.constraint(
- equalToConstant: PaywallTrustItemView.preferredHeight + trustStack.edgeInsets.top + trustStack.edgeInsets.bottom
- ).isActive = true
- self.trustStack = trustStack
- return trustStack
- }
- private func makeFooterLinks() -> NSView {
- let container = NSView()
- container.translatesAutoresizingMaskIntoConstraints = false
- container.setContentHuggingPriority(.required, for: .vertical)
- container.setContentCompressionResistancePriority(.required, for: .vertical)
- let config = paywallConfig
- let separator = NSView()
- separator.translatesAutoresizingMaskIntoConstraints = false
- separator.wantsLayer = true
- separator.layer?.backgroundColor = AppTheme.paywallBorder.cgColor
- continueFreePlanLink.updateTitle(config.footer.continueFree)
- continueFreePlanLink.target = self
- continueFreePlanLink.action = #selector(continueWithFreePlanTapped)
- manageSubscriptionLink.updateTitle(config.footer.manageSubscription)
- manageSubscriptionLink.target = self
- manageSubscriptionLink.action = #selector(manageSubscriptionTapped)
- let primaryCell = makePrimaryFooterLinkCell()
- primaryFooterLinkCell = primaryCell
- refreshPrimaryFooterLink()
- restoreLink.updateTitle(config.footer.restorePurchase)
- restoreLink.target = self
- restoreLink.action = #selector(restoreTapped)
- privacyLink.updateTitle(config.footer.privacyPolicy)
- privacyLink.target = self
- privacyLink.action = #selector(privacyPolicyTapped)
- termsLink.updateTitle(config.footer.termsOfService)
- termsLink.target = self
- termsLink.action = #selector(termsOfServiceTapped)
- supportLink.updateTitle(config.footer.support)
- supportLink.target = self
- supportLink.action = #selector(supportTapped)
- let linkCells = [
- primaryCell,
- makeFooterLinkCell(link: restoreLink),
- makeFooterLinkCell(link: privacyLink),
- makeFooterLinkCell(link: termsLink),
- makeFooterLinkCell(link: supportLink),
- ]
- let linksStack = NSStackView(views: linkCells)
- linksStack.orientation = .horizontal
- linksStack.spacing = 0
- linksStack.alignment = .centerY
- linksStack.distribution = .fillEqually
- linksStack.translatesAutoresizingMaskIntoConstraints = false
- container.addSubview(separator)
- container.addSubview(linksStack)
- NSLayoutConstraint.activate([
- separator.leadingAnchor.constraint(equalTo: container.leadingAnchor),
- separator.trailingAnchor.constraint(equalTo: container.trailingAnchor),
- separator.topAnchor.constraint(equalTo: container.topAnchor),
- separator.heightAnchor.constraint(equalToConstant: 1),
- linksStack.leadingAnchor.constraint(equalTo: container.leadingAnchor),
- linksStack.trailingAnchor.constraint(equalTo: container.trailingAnchor),
- linksStack.topAnchor.constraint(equalTo: separator.bottomAnchor, constant: 10),
- linksStack.bottomAnchor.constraint(equalTo: container.bottomAnchor),
- ])
- return container
- }
- private func makePrimaryFooterLinkCell() -> NSView {
- let cell = NSView()
- cell.translatesAutoresizingMaskIntoConstraints = false
- cell.setContentHuggingPriority(.required, for: .vertical)
- cell.setContentCompressionResistancePriority(.required, for: .vertical)
- for link in [continueFreePlanLink, manageSubscriptionLink] {
- cell.addSubview(link)
- NSLayoutConstraint.activate([
- link.centerXAnchor.constraint(equalTo: cell.centerXAnchor),
- link.centerYAnchor.constraint(equalTo: cell.centerYAnchor),
- link.topAnchor.constraint(equalTo: cell.topAnchor),
- link.bottomAnchor.constraint(equalTo: cell.bottomAnchor),
- ])
- }
- return cell
- }
- private func makeFooterLinkCell(link: PaywallFooterLink) -> NSView {
- let cell = NSView()
- cell.translatesAutoresizingMaskIntoConstraints = false
- cell.setContentHuggingPriority(.required, for: .vertical)
- cell.setContentCompressionResistancePriority(.required, for: .vertical)
- cell.addSubview(link)
- NSLayoutConstraint.activate([
- link.centerXAnchor.constraint(equalTo: cell.centerXAnchor),
- link.centerYAnchor.constraint(equalTo: cell.centerYAnchor),
- link.topAnchor.constraint(equalTo: cell.topAnchor),
- link.bottomAnchor.constraint(equalTo: cell.bottomAnchor),
- ])
- return cell
- }
- private func selectPlan(_ plan: PaywallPlan) {
- selectedPlan = plan
- for (key, card) in planCards {
- card.isChosen = key == plan
- }
- ctaButton.title = plan.localizedCTATitle(
- from: StoreManager.shared.product(for: plan),
- config: paywallConfig,
- introOffer: StoreManager.shared.eligibleIntroOffer(for: plan)
- )
- updateRenewalDisclosure(
- plan.localizedRenewalDisclosure(
- from: StoreManager.shared.product(for: plan),
- config: paywallConfig,
- introOffer: StoreManager.shared.eligibleIntroOffer(for: plan)
- )
- )
- refreshTrustItemsForSelectedPlan()
- refreshPurchaseState()
- }
- private func retryProductLoad() {
- Task { @MainActor in
- refreshProductsErrorState()
- refreshPurchaseState()
- await StoreManager.shared.loadProducts()
- refreshStoreState()
- }
- }
- @objc private func purchaseTapped() {
- Task { @MainActor in
- refreshPurchaseState()
- do {
- let outcome = try await StoreManager.shared.purchase(plan: selectedPlan)
- refreshStoreState()
- switch outcome {
- case .purchased, .cancelled:
- break
- case .pending:
- let messages = paywallConfig.messages
- StoreManager.shared.showAlert(
- title: messages.pendingPurchaseTitle,
- message: messages.pendingPurchaseMessage,
- on: window
- )
- }
- } catch {
- refreshPurchaseState()
- StoreManager.shared.showPurchaseError(error, on: window)
- }
- }
- }
- @objc private func restoreTapped() {
- Task { @MainActor in
- refreshPurchaseState()
- do {
- let restored = try await StoreManager.shared.restorePurchases()
- refreshStoreState()
- if !restored {
- let messages = paywallConfig.messages
- StoreManager.shared.showAlert(
- title: messages.restoreNotFoundTitle,
- message: messages.restoreNotFoundMessage,
- on: window
- )
- }
- } catch {
- refreshPurchaseState()
- StoreManager.shared.showPurchaseError(error, on: window)
- }
- }
- }
- @objc private func continueWithFreePlanTapped() {
- onClose?()
- }
- @objc private func premiumCloseTapped() {
- onClose?()
- }
- @objc private func manageSubscriptionTapped() {
- StoreManager.shared.showManageSubscriptions()
- }
- @objc private func privacyPolicyTapped() {
- openExternalLink(paywallConfig.urls.privacy)
- }
- @objc private func termsOfServiceTapped() {
- openExternalLink(paywallConfig.urls.terms)
- }
- @objc private func supportTapped() {
- openExternalLink(paywallConfig.urls.support)
- }
- private func openExternalLink(_ urlString: String) {
- guard let url = URL(string: urlString) else { return }
- NSWorkspace.shared.open(url)
- }
- }
- // MARK: - Overlay Presenter
- final class PaywallOverlayView: NSView, AppearanceRefreshable {
- var onDismiss: (() -> Void)?
- private let paywallView: PaywallView
- private let blurView = NSVisualEffectView()
- private let backdrop = NSView()
- private let pattern = WavePatternView()
- private var acceptsHitTesting = false
- init() {
- paywallView = PaywallView()
- super.init(frame: .zero)
- translatesAutoresizingMaskIntoConstraints = false
- setup()
- refreshAppearance()
- }
- func refreshAppearance() {
- backdrop.layer?.backgroundColor = AppTheme.paywallOverlayBackdrop.cgColor
- blurView.material = AppSettings.darkModeEnabled ? .hudWindow : .underWindowBackground
- pattern.refreshAppearance()
- paywallView.refreshAppearance()
- }
- func refreshStoreState() {
- paywallView.refreshStoreState()
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) { nil }
- override func hitTest(_ point: NSPoint) -> NSView? {
- guard acceptsHitTesting else { return nil }
- return super.hitTest(point)
- }
- private func setup() {
- blurView.translatesAutoresizingMaskIntoConstraints = false
- blurView.material = .underWindowBackground
- blurView.blendingMode = .withinWindow
- blurView.state = .active
- backdrop.translatesAutoresizingMaskIntoConstraints = false
- backdrop.wantsLayer = true
- backdrop.layer?.backgroundColor = NSColor(calibratedWhite: 0.15, alpha: 0.22).cgColor
- pattern.translatesAutoresizingMaskIntoConstraints = false
- pattern.alphaValue = 0.35
- paywallView.translatesAutoresizingMaskIntoConstraints = false
- paywallView.onClose = { [weak self] in self?.dismiss() }
- addSubview(blurView)
- addSubview(backdrop)
- backdrop.addSubview(pattern)
- addSubview(paywallView)
- NSLayoutConstraint.activate([
- blurView.leadingAnchor.constraint(equalTo: leadingAnchor),
- blurView.trailingAnchor.constraint(equalTo: trailingAnchor),
- blurView.topAnchor.constraint(equalTo: topAnchor),
- blurView.bottomAnchor.constraint(equalTo: bottomAnchor),
- backdrop.leadingAnchor.constraint(equalTo: leadingAnchor),
- backdrop.trailingAnchor.constraint(equalTo: trailingAnchor),
- backdrop.topAnchor.constraint(equalTo: topAnchor),
- backdrop.bottomAnchor.constraint(equalTo: bottomAnchor),
- pattern.leadingAnchor.constraint(equalTo: backdrop.leadingAnchor),
- pattern.trailingAnchor.constraint(equalTo: backdrop.trailingAnchor),
- pattern.topAnchor.constraint(equalTo: backdrop.topAnchor),
- pattern.bottomAnchor.constraint(equalTo: backdrop.bottomAnchor),
- paywallView.leadingAnchor.constraint(equalTo: leadingAnchor),
- paywallView.trailingAnchor.constraint(equalTo: trailingAnchor),
- paywallView.topAnchor.constraint(equalTo: topAnchor),
- paywallView.bottomAnchor.constraint(equalTo: bottomAnchor),
- ])
- }
- func present(in parent: NSView) {
- guard superview == nil else { return }
- if let window = parent.window,
- let windowFrameView = window.contentView?.superview {
- windowFrameView.addSubview(self)
- NSLayoutConstraint.activate([
- leadingAnchor.constraint(equalTo: windowFrameView.leadingAnchor),
- trailingAnchor.constraint(equalTo: windowFrameView.trailingAnchor),
- topAnchor.constraint(equalTo: windowFrameView.topAnchor),
- bottomAnchor.constraint(equalTo: windowFrameView.bottomAnchor),
- ])
- } else {
- parent.addSubview(self)
- NSLayoutConstraint.activate([
- leadingAnchor.constraint(equalTo: parent.leadingAnchor),
- trailingAnchor.constraint(equalTo: parent.trailingAnchor),
- topAnchor.constraint(equalTo: parent.topAnchor),
- bottomAnchor.constraint(equalTo: parent.bottomAnchor),
- ])
- }
- alphaValue = 1
- acceptsHitTesting = true
- paywallView.refreshStoreState()
- Task { @MainActor in
- if StoreManager.shared.isResolvingEntitlements {
- await StoreManager.shared.ensureEntitlementsResolved()
- paywallView.refreshStoreState()
- }
- if StoreManager.shared.products.isEmpty, !StoreManager.shared.isLoadingProducts {
- await StoreManager.shared.loadProducts()
- paywallView.refreshStoreState()
- }
- }
- Task { @MainActor in
- await PaywallConfigService.shared.refreshFromRemote()
- }
- }
- func dismiss() {
- acceptsHitTesting = false
- NSAnimationContext.runAnimationGroup({ context in
- context.duration = 0.15
- animator().alphaValue = 0
- }, completionHandler: { [weak self] in
- self?.removeFromSuperview()
- self?.onDismiss?()
- })
- }
- }
|