This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSubscriptionCard.swift
More file actions
302 lines (277 loc) · 14.6 KB
/
SubscriptionCard.swift
File metadata and controls
302 lines (277 loc) · 14.6 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
import SwiftUI
import OpenIAP
struct SubscriptionCard: View {
let productId: String
let product: OpenIapProduct?
let purchase: OpenIapPurchase?
let isSubscribed: Bool
let isCancelled: Bool
let isLoading: Bool
var upgradeInfo: UpgradeInfo? = nil
let onSubscribe: () -> Void
let onManage: () -> Void
private var expirationInfo: (date: Date, formattedString: String, isExpiringSoon: Bool)? {
guard let purchase = purchase,
let expirationTime = purchase.expirationDateIOS else { return nil }
let expirationDate = Date(timeIntervalSince1970: expirationTime / 1000)
let formatter: DateFormatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
let timeInterval = expirationDate.timeIntervalSinceNow
let isExpiringSoon = timeInterval < 86400 && timeInterval > 0
return (expirationDate, formatter.string(from: expirationDate), isExpiringSoon)
}
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
VStack(alignment: .leading, spacing: 4) {
HStack {
Text(product?.title ?? productId)
.font(.headline)
if isSubscribed {
Label("Subscribed", systemImage: "checkmark.seal.fill")
.font(.caption)
.foregroundColor(AppColors.success)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(AppColors.success.opacity(0.2))
.cornerRadius(4)
} else if let upgradeInfo = upgradeInfo, upgradeInfo.canUpgrade {
Label("Upgrade", systemImage: "arrow.up.circle.fill")
.font(.caption)
.foregroundColor(AppColors.primary)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(AppColors.primary.opacity(0.2))
.cornerRadius(4)
} else if let upgradeInfo = upgradeInfo, upgradeInfo.isDowngrade {
Label("Downgrade", systemImage: "arrow.down.circle")
.font(.caption)
.foregroundColor(.orange)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(Color.orange.opacity(0.2))
.cornerRadius(4)
}
}
Text(productId)
.font(.caption)
.font(.system(.body, design: .monospaced))
.foregroundColor(.secondary)
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
Text(product.map { $0.displayPrice } ?? "--")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(AppColors.secondary)
Label(product.map { $0.typeIOS.rawValue } ?? "subscription", systemImage: "repeat")
.font(.caption2)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(AppColors.secondary.opacity(0.15))
.cornerRadius(4)
}
}
if isSubscribed {
VStack(alignment: .leading, spacing: 10) {
if let purchase = purchase {
VStack(alignment: .leading, spacing: 6) {
if let info = expirationInfo {
HStack {
Label("Expires", systemImage: "calendar")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
Text(info.formattedString)
.font(.caption)
.fontWeight(.medium)
.foregroundColor(info.isExpiringSoon ? AppColors.warning : AppColors.success)
}
}
if let purchaseDate = Date(timeIntervalSince1970: purchase.transactionDate / 1000) as Date? {
HStack {
Label("Started", systemImage: "clock")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
Text(DateFormatter.localizedString(from: purchaseDate, dateStyle: .medium, timeStyle: .short))
.font(.caption)
.fontWeight(.medium)
.foregroundColor(.secondary)
}
}
if let isPaused = purchase.isAutoRenewing as Bool? {
HStack {
Label("Auto-Renew", systemImage: isPaused ? "arrow.triangle.2.circlepath" : "pause")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
Text(isPaused ? "Enabled" : "Disabled")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(isPaused ? AppColors.success : AppColors.warning)
}
}
}
.padding(12)
.background(Color.gray.opacity(0.05))
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.gray.opacity(0.2), lineWidth: 1)
)
.cornerRadius(6)
}
if isCancelled {
Button(action: onSubscribe) {
HStack {
if isLoading {
ProgressView()
.scaleEffect(0.8)
.tint(.white)
} else {
Image(systemName: "arrow.clockwise.circle")
}
Text(isLoading ? "Reactivating..." : "Reactivate Subscription")
.fontWeight(.medium)
Spacer()
Text(product?.displayPrice ?? "--")
.fontWeight(.semibold)
}
.padding()
.background(isLoading ? AppColors.secondary.opacity(0.7) : AppColors.secondary)
.foregroundColor(.white)
.cornerRadius(8)
}
.disabled(isLoading)
Text("Subscription will remain active until expiry")
.font(.caption2)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .center)
} else {
Button(action: onManage) {
HStack {
Image(systemName: "gear")
.font(.system(size: 14))
Text("Manage Subscription")
.fontWeight(.medium)
Spacer()
Image(systemName: "chevron.right")
.font(.caption)
}
.padding(.horizontal, 12)
.padding(.vertical, 10)
.background(Color.gray.opacity(0.15))
.foregroundColor(AppColors.primaryText)
.cornerRadius(8)
}
Text("Cancel anytime in Settings")
.font(.caption2)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .center)
}
}
} else {
// Show upgrade info message if available
if let upgradeInfo = upgradeInfo, let currentTier = upgradeInfo.currentTier {
VStack(spacing: 8) {
// Check if this is a pending upgrade
if let message = upgradeInfo.message, message.contains("pending") {
// Show pending upgrade status
HStack {
Image(systemName: "clock.arrow.circlepath")
.foregroundColor(.orange)
Text("Upgrade pending from \(currentTier)")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(Color.orange.opacity(0.1))
.cornerRadius(6)
Text("This plan will activate on your next renewal date")
.font(.caption2)
.foregroundColor(.secondary)
.padding(.horizontal, 12)
.frame(maxWidth: .infinity, alignment: .leading)
} else {
// Show regular upgrade/switch option
HStack {
Image(systemName: upgradeInfo.canUpgrade ? "arrow.up.circle.fill" : "info.circle.fill")
.foregroundColor(upgradeInfo.canUpgrade ? AppColors.primary : .orange)
Text(upgradeInfo.canUpgrade ? "Upgrade from \(currentTier)" : "Currently subscribed to \(currentTier)")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(Color.gray.opacity(0.1))
.cornerRadius(6)
Button(action: onSubscribe) {
HStack {
if isLoading {
ProgressView()
.scaleEffect(0.8)
.tint(.white)
} else {
Image(systemName: upgradeInfo.canUpgrade ? "arrow.up.circle" : "repeat.circle")
}
Text(isLoading ? "Processing..." : (upgradeInfo.canUpgrade ? "Upgrade Now" : "Switch Plan"))
.fontWeight(.medium)
Spacer()
if !isLoading {
VStack(alignment: .trailing, spacing: 2) {
Text(product?.displayPrice ?? "--")
.fontWeight(.semibold)
if upgradeInfo.canUpgrade {
Text("Pro-rated")
.font(.caption2)
.opacity(0.8)
}
}
}
}
.padding()
.background(isLoading ? AppColors.secondary.opacity(0.7) : (upgradeInfo.canUpgrade ? AppColors.primary : AppColors.secondary))
.foregroundColor(.white)
.cornerRadius(8)
}
.disabled(isLoading)
}
}
} else {
Button(action: onSubscribe) {
HStack {
if isLoading {
ProgressView()
.scaleEffect(0.8)
.tint(.white)
} else {
Image(systemName: "repeat.circle")
}
Text(isLoading ? "Processing..." : "Subscribe")
.fontWeight(.medium)
Spacer()
if !isLoading {
Text(product?.displayPrice ?? "--")
.fontWeight(.semibold)
}
}
.padding()
.background(isLoading ? AppColors.secondary.opacity(0.7) : AppColors.secondary)
.foregroundColor(.white)
.cornerRadius(8)
}
.disabled(isLoading)
}
}
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal)
}
}