-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathLoadingPaywallView.swift
More file actions
302 lines (251 loc) · 9.05 KB
/
Copy pathLoadingPaywallView.swift
File metadata and controls
302 lines (251 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// LoadingPaywallView.swift
//
// Created by Nacho Soto on 7/21/23.
@_spi(Internal) import RevenueCat
import SwiftUI
#if !os(tvOS)
/// A `PaywallView` suitable to be displayed as a loading placeholder.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable, message: "Legacy paywalls are unavailable in macOS")
@available(tvOS, unavailable)
@MainActor
struct LoadingPaywallView: View {
var mode: PaywallViewMode
var displayCloseButton: Bool
var shimmer: Bool = true
var body: some View {
LoadedOfferingPaywallView(
offering: .init(
identifier: Self.offeringIdentifier,
serverDescription: "",
metadata: [:],
paywall: Self.defaultPaywall,
availablePackages: Self.packages,
webCheckoutUrl: nil
),
paywall: Self.defaultPaywall,
template: Self.template,
mode: self.mode,
fonts: DefaultPaywallFontProvider(),
displayCloseButton: self.displayCloseButton,
introEligibility: Self.introEligibility,
purchaseHandler: Self.purchaseHandler,
locale: Locale.current,
showZeroDecimalPlacePrices: true
)
.allowsHitTesting(false)
.redacted(reason: .placeholder)
.shimmering(enable: self.mode.shouldDisplayBackground && self.shimmer)
}
private static let template: PaywallTemplate = PaywallData.defaultTemplate
private static let defaultPaywall: PaywallData = .createDefault(with: Self.packages, locale: .current)
private static let packages: [Package] = [
Self.monthlyPackage,
Self.weeklyPackage,
Self.annualPackage
]
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable, message: "Legacy paywalls are unavailable in macOS")
@available(tvOS, unavailable)
private extension LoadingPaywallView {
static let introEligibility: TrialOrIntroEligibilityChecker = .init(checker: { packages in
return Dictionary(
uniqueKeysWithValues: packages.map { ($0, .unknown) }
)
})
static let purchases = LoadingPaywallPurchases()
static let purchaseHandler: PurchaseHandler = .init(purchases: purchases, eventTracker: .init(purchases: purchases))
static let offeringIdentifier = "offering"
static let weeklyPackage = Package(
identifier: "weekly",
packageType: .weekly,
storeProduct: Self.weeklyProduct.toStoreProduct(),
offeringIdentifier: Self.offeringIdentifier,
webCheckoutUrl: nil
)
static let monthlyPackage = Package(
identifier: "monthly",
packageType: .monthly,
storeProduct: Self.monthlyProduct.toStoreProduct(),
offeringIdentifier: Self.offeringIdentifier,
webCheckoutUrl: nil
)
static let annualPackage = Package(
identifier: "annual",
packageType: .annual,
storeProduct: Self.annualProduct.toStoreProduct(),
offeringIdentifier: Self.offeringIdentifier,
webCheckoutUrl: nil
)
static let weeklyProduct = TestStoreProduct(
localizedTitle: "Weekly",
price: 1.99,
currencyCode: "USD",
localizedPriceString: "$1.99",
productIdentifier: "com.revenuecat.product_1",
productType: .autoRenewableSubscription,
localizedDescription: "PRO weekly",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .week),
locale: Locale(identifier: "en_US")
)
static let monthlyProduct = TestStoreProduct(
localizedTitle: "Monthly",
price: 12.99,
currencyCode: "USD",
localizedPriceString: "$12.99",
productIdentifier: "com.revenuecat.product_2",
productType: .autoRenewableSubscription,
localizedDescription: "PRO monthly",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .month),
locale: Locale(identifier: "en_US")
)
static let annualProduct = TestStoreProduct(
localizedTitle: "Annual",
price: 69.49,
currencyCode: "USD",
localizedPriceString: "$69.49",
productIdentifier: "com.revenuecat.product_3",
productType: .autoRenewableSubscription,
localizedDescription: "PRO annual",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .year),
locale: Locale(identifier: "en_US")
)
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
private final class LoadingPaywallPurchases: PaywallPurchasesType {
var preferredLocales: [String] { Locale.preferredLanguages }
var preferredLocaleOverride: String? { nil }
var isUIPreviewMode: Bool { false }
var remoteConfigEnabled: Bool { false }
var purchasesAreCompletedBy: PurchasesAreCompletedBy {
get { return .myApp }
set { _ = newValue }
}
var subscriptionHistoryTracker: RevenueCat.SubscriptionHistoryTracker {
SubscriptionHistoryTracker()
}
func offerings() async throws -> Offerings { throw ErrorCode.configurationError }
var cachedOfferings: Offerings? { nil }
#if !os(tvOS)
func workflow(forOfferingIdentifier offeringID: String) async throws -> WorkflowDataResult {
throw ErrorCode.configurationError
}
func cachedWorkflow(forOfferingIdentifier offeringID: String) -> WorkflowDataResult? {
return nil
}
#endif
func customerInfo() async throws -> RevenueCat.CustomerInfo {
fatalError("Should not be able to purchase")
}
func purchase(
package: Package,
promotionalOffer: PromotionalOffer?,
paywallEvent: PaywallEvent?
) async throws -> PurchaseResultData {
fatalError("Should not be able to purchase")
}
func restorePurchases() async throws -> CustomerInfo {
fatalError("Should not be able to purchase")
}
func track(paywallEvent: PaywallEvent) async {
// Ignoring events from loading paywall view
}
func track(workflowEvent: WorkflowEvent) async {
// Ignoring events from loading paywall view
}
func cachePurchaseData(presentedOfferingContext: PresentedOfferingContext,
paywallEvent: PaywallEvent?,
productIdentifier: String) {
// No-op for loading paywall
}
func clearCachedPurchaseData(productIdentifier: String) {
// No-op for loading paywall
}
func invalidateCustomerInfoCache() {
// No-op, this is a mock implementation.
}
func failedToLoadFontWithConfig(_ fontConfig: RevenueCat.UIConfig.FontsConfig) {
// Ignoring font loading errors from paywall view
}
}
// MARK: - Shimmer
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
private struct Shimmer: ViewModifier {
var duration: CGFloat
var autoreverse: Bool
@State
private var initialState: Bool = true
func body(content: Content) -> some View {
content
.mask(
LinearGradient(
gradient: .init(colors: [
.black.opacity(0.3),
.black,
.black.opacity(0.3)
]),
startPoint: self.startPoint,
endPoint: self.endPoint
)
.edgesIgnoringSafeArea(.all)
)
.onAppear {
withAnimation(
.linear(duration: self.duration)
.delay(self.duration / 2.0)
.repeatForever(autoreverses: self.autoreverse)
) {
self.initialState.toggle()
}
}
}
private var startPoint: UnitPoint {
return self.initialState ? UnitPoint(x: -0.3, y: -0.3) : UnitPoint(x: 1, y: 1)
}
private var endPoint: UnitPoint {
return self.initialState ? UnitPoint(x: 0, y: 0) : UnitPoint(x: 1.3, y: 1.3)
}
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
private extension View {
@ViewBuilder
func shimmering(
enable: Bool,
duration: Double = 1.5,
autoreverse: Bool = false
) -> some View {
if enable {
self.modifier(Shimmer(duration: duration, autoreverse: autoreverse))
} else {
self
}
}
}
// MARK: -
#if DEBUG && !os(macOS) && !os(tvOS)
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
struct LoadingPaywallView_Previews: PreviewProvider {
static var previews: some View {
ForEach(PaywallViewMode.allCases, id: \.self) { mode in
LoadingPaywallView(mode: mode, displayCloseButton: true)
.previewDisplayName("\(mode)")
}
}
}
#endif
#endif