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 pathAvailablePurchasesScreen.swift
More file actions
604 lines (538 loc) · 19.9 KB
/
AvailablePurchasesScreen.swift
File metadata and controls
604 lines (538 loc) · 19.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import SwiftUI
import OpenIAP
@available(iOS 15.0, *)
struct AvailablePurchasesScreen: View {
@StateObject private var store = StoreViewModel()
var body: some View {
ScrollView {
VStack(spacing: 24) {
availablePurchasesSection
purchaseHistorySection
}
.padding(.vertical)
}
.background(AppColors.background)
.navigationTitle("Purchases")
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
Task {
await store.loadPurchases()
}
}) {
Image(systemName: "arrow.clockwise")
}
}
}
.alert("Error", isPresented: $store.showError) {
Button("OK") {}
} message: {
Text(store.errorMessage)
}
.onAppear {
Task {
await store.loadPurchases()
}
}
}
// MARK: - Available Purchases Section (Currently Owned/Active)
private var availablePurchasesSection: some View {
VStack(alignment: .leading, spacing: 16) {
SectionHeaderView(
title: "Available Purchases",
subtitle: "Your active subscriptions and unconsumed items",
icon: "checkmark.seal.fill"
)
availablePurchasesContent
}
}
private var uniqueActivePurchases: [IapPurchase] {
let allActivePurchases = store.purchases.filter { purchase in
// Show unconsumed consumables and active subscriptions
purchase.purchaseState == .purchased && (
// Unconsumed consumables
(!purchase.productId.contains("premium") && purchase.acknowledgementState != .acknowledged) ||
// Active subscriptions
(purchase.productId.contains("premium") && (purchase.isAutoRenewing ||
(purchase.expiryTime != nil && purchase.expiryTime! > Date())))
)
}
// Group by productId and take only the latest purchase for each subscription
var uniquePurchases: [IapPurchase] = []
var seenSubscriptions: Set<String> = []
for purchase in allActivePurchases.sorted(by: { $0.purchaseTime > $1.purchaseTime }) {
if purchase.productId.contains("premium") {
// For subscriptions, only add if we haven't seen this productId yet
if !seenSubscriptions.contains(purchase.productId) {
uniquePurchases.append(purchase)
seenSubscriptions.insert(purchase.productId)
}
} else {
// For consumables, add all unconsumed items
uniquePurchases.append(purchase)
}
}
return uniquePurchases
}
@ViewBuilder
private var availablePurchasesContent: some View {
if uniqueActivePurchases.isEmpty {
EmptyStateCard(
icon: "bag.circle",
title: "No active purchases",
subtitle: "Your active subscriptions and items will appear here"
)
} else {
VStack(spacing: 12) {
ForEach(uniqueActivePurchases, id: \.transactionId) { purchase in
ActivePurchaseCard(purchase: purchase) {
Task {
await store.finishPurchase(purchase)
}
}
}
}
.padding(.horizontal)
}
}
// MARK: - Purchase History Section (All Past Purchases)
private var purchaseHistorySection: some View {
VStack(alignment: .leading, spacing: 16) {
SectionHeaderView(
title: "Purchase History",
subtitle: "All your past purchases",
icon: "clock.arrow.circlepath"
)
purchaseHistoryContent
}
}
@ViewBuilder
private var purchaseHistoryContent: some View {
if store.purchases.isEmpty {
EmptyStateCard(
icon: "clock",
title: "No purchase history",
subtitle: "Your purchase history will appear here"
)
} else {
VStack(spacing: 12) {
ForEach(store.purchases.sorted { $0.purchaseTime > $1.purchaseTime }, id: \.transactionId) { purchase in
PurchaseHistoryCard(purchase: purchase)
}
}
.padding(.horizontal)
}
}
}
// MARK: - Active Purchase Card (For Available Purchases)
struct ActivePurchaseCard: View {
let purchase: IapPurchase
let onConsume: () -> Void
private var isSubscription: Bool {
purchase.productId.contains("premium") || purchase.isAutoRenewing
}
var body: some View {
HStack(spacing: 16) {
// Status Icon
Image(systemName: isSubscription ? "crown.fill" : "checkmark.seal.fill")
.font(.system(size: 24))
.foregroundColor(isSubscription ? AppColors.warning : AppColors.success)
.frame(width: 44, height: 44)
.background((isSubscription ? AppColors.warning : AppColors.success).opacity(0.1))
.cornerRadius(12)
// Purchase Info
VStack(alignment: .leading, spacing: 4) {
Text(purchase.productId)
.font(.headline)
.fontWeight(.semibold)
if isSubscription && purchase.isAutoRenewing {
Label("Auto-renewable", systemImage: "arrow.triangle.2.circlepath")
.font(.caption)
.foregroundColor(AppColors.primary)
}
if let expiryTime = purchase.expiryTime {
Text("Expires: \(expiryTime, style: .relative)")
.font(.caption)
.foregroundColor(.secondary)
}
}
Spacer()
// Action Button
if !isSubscription && purchase.acknowledgementState != .acknowledged {
Button(action: onConsume) {
Text("Consume")
.font(.system(size: 14, weight: .medium))
.foregroundColor(.white)
.padding(.horizontal, 16)
.padding(.vertical, 6)
.background(AppColors.primary)
.cornerRadius(8)
}
} else if isSubscription {
Text("Active")
.font(.caption)
.fontWeight(.medium)
.padding(.horizontal, 12)
.padding(.vertical, 4)
.background(AppColors.success.opacity(0.2))
.foregroundColor(AppColors.success)
.cornerRadius(6)
}
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
}
}
// MARK: - Purchase History Card
struct PurchaseHistoryCard: View {
let purchase: IapPurchase
private var statusColor: Color {
switch purchase.purchaseState {
case .purchased: return AppColors.success
case .pending: return AppColors.warning
case .failed: return AppColors.error
case .restored: return AppColors.primary
case .deferred: return AppColors.secondary
}
}
private var statusText: String {
switch purchase.purchaseState {
case .purchased: return "Purchased"
case .pending: return "Pending"
case .failed: return "Failed"
case .restored: return "Restored"
case .deferred: return "Deferred"
}
}
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(purchase.productId)
.font(.headline)
.fontWeight(.semibold)
Text("Transaction: \(String(purchase.transactionId.prefix(8)))...")
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
VStack(alignment: .trailing, spacing: 4) {
Text(statusText)
.font(.caption)
.fontWeight(.medium)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(statusColor.opacity(0.2))
.foregroundColor(statusColor)
.cornerRadius(4)
if purchase.acknowledgementState == .acknowledged {
Label("Consumed", systemImage: "checkmark.circle.fill")
.font(.caption2)
.foregroundColor(.secondary)
}
}
}
HStack(spacing: 16) {
Label("\(purchase.purchaseTime, style: .date)", systemImage: "calendar")
.font(.caption)
.foregroundColor(.secondary)
if purchase.quantity > 1 {
Label("Qty: \(purchase.quantity)", systemImage: "number")
.font(.caption)
.foregroundColor(.secondary)
}
}
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
}
}
// MARK: - Original Purchase Card (Deprecated)
struct PurchaseCard: View {
let purchase: IapPurchase
let onConsume: () -> Void
private var isSubscription: Bool {
purchase.productId.contains("premium")
}
private var statusColor: Color {
switch purchase.purchaseState {
case .purchased:
return AppColors.success
case .pending:
return AppColors.warning
case .failed:
return AppColors.error
case .restored:
return AppColors.primary
case .deferred:
return AppColors.secondary
}
}
private var statusText: String {
switch purchase.purchaseState {
case .purchased:
return "Purchased"
case .pending:
return "Pending"
case .failed:
return "Failed"
case .restored:
return "Restored"
case .deferred:
return "Deferred"
}
}
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(purchase.productId)
.font(.headline)
.font(.system(.body, design: .monospaced))
Text("Transaction: \\(purchase.transactionId)")
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
Text(statusText)
.font(.caption)
.fontWeight(.medium)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(statusColor.opacity(0.2))
.foregroundColor(statusColor)
.cornerRadius(4)
}
VStack(alignment: .leading, spacing: 4) {
HStack {
Text("Purchased:")
.font(.caption)
.foregroundColor(.secondary)
Text(purchase.purchaseTime, style: .date)
.font(.caption)
}
if let expiryTime = purchase.expiryTime {
HStack {
Text("Expires:")
.font(.caption)
.foregroundColor(.secondary)
Text(expiryTime, style: .relative)
.font(.caption)
.fontWeight(.medium)
}
}
HStack {
Text("Quantity:")
.font(.caption)
.foregroundColor(.secondary)
Text("\\(purchase.quantity)")
.font(.caption)
}
if isSubscription && purchase.isAutoRenewing {
HStack {
Image(systemName: "arrow.triangle.2.circlepath")
.font(.caption)
Text("Auto-renewable")
.font(.caption)
}
.foregroundColor(AppColors.primary)
}
}
if !isSubscription && purchase.acknowledgementState == .notAcknowledged {
Button(action: onConsume) {
HStack {
Image(systemName: "checkmark.circle")
Text("Consume")
Spacer()
}
.padding()
.background(AppColors.primary)
.foregroundColor(.white)
.cornerRadius(8)
}
} else if purchase.acknowledgementState == .acknowledged {
HStack {
Image(systemName: "checkmark.seal.fill")
.foregroundColor(AppColors.success)
Text("Acknowledged")
.font(.caption)
.foregroundColor(AppColors.success)
}
.padding(.vertical, 8)
}
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal)
}
}
// MARK: - Section Header View
struct SectionHeaderView: View {
let title: String
let subtitle: String
let icon: String
var body: some View {
HStack(spacing: 12) {
Image(systemName: icon)
.font(.title2)
.foregroundColor(AppColors.primary)
.frame(width: 28, height: 28)
VStack(alignment: .leading, spacing: 2) {
Text(title)
.font(.title2)
.fontWeight(.bold)
Text(subtitle)
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
.padding(.horizontal)
}
}
// MARK: - Product List Card
struct ProductListCard: View {
let product: IapProductData
let onPurchase: () -> Void
var body: some View {
HStack(spacing: 16) {
// Product Icon
Image(systemName: productIcon)
.font(.system(size: 28))
.foregroundColor(AppColors.primary)
.frame(width: 44, height: 44)
.background(AppColors.primary.opacity(0.1))
.cornerRadius(12)
// Product Info
VStack(alignment: .leading, spacing: 4) {
Text(productTitle)
.font(.headline)
.fontWeight(.semibold)
Text(product.description)
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(2)
}
Spacer()
// Price and Purchase Button
VStack(spacing: 8) {
Text(product.displayPrice)
.font(.system(size: 16, weight: .bold))
.foregroundColor(AppColors.primary)
Button(action: onPurchase) {
Text("Buy")
.font(.system(size: 14, weight: .medium))
.foregroundColor(.white)
.padding(.horizontal, 16)
.padding(.vertical, 6)
.background(AppColors.primary)
.cornerRadius(8)
}
}
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
}
private var productIcon: String {
if product.id.contains("10bulbs") {
return "lightbulb"
} else if product.id.contains("30bulbs") {
return "lightbulb.fill"
} else if product.id.contains("premium") {
return "crown"
} else {
return "bag"
}
}
private var productTitle: String {
if product.id.contains("10bulbs") {
return "10 Bulbs Pack"
} else if product.id.contains("30bulbs") {
return "30 Bulbs Pack"
} else if product.id.contains("premium") {
return "Premium Subscription"
} else {
return product.title
}
}
}
// MARK: - Product Grid Card (Deprecated)
struct ProductGridCard: View {
let product: IapProductData
let onPurchase: () -> Void
var body: some View {
VStack(spacing: 12) {
// Product Icon
Image(systemName: productIcon)
.font(.system(size: 32))
.foregroundColor(AppColors.primary)
.frame(height: 40)
VStack(spacing: 4) {
Text(productTitle)
.font(.headline)
.fontWeight(.semibold)
.multilineTextAlignment(.center)
.lineLimit(2)
Text(product.description)
.font(.caption)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.lineLimit(2)
}
Spacer()
VStack(spacing: 8) {
Text(product.displayPrice)
.font(.title3)
.fontWeight(.bold)
.foregroundColor(AppColors.primary)
Button(action: onPurchase) {
Text("Purchase")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.background(AppColors.primary)
.cornerRadius(8)
}
}
}
.padding()
.frame(height: 180)
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal, 4)
}
private var productIcon: String {
if product.id.contains("10bulbs") {
return "lightbulb"
} else if product.id.contains("30bulbs") {
return "lightbulb.fill"
} else if product.id.contains("premium") {
return "crown"
} else {
return "bag"
}
}
private var productTitle: String {
if product.id.contains("10bulbs") {
return "10 Bulbs"
} else if product.id.contains("30bulbs") {
return "30 Bulbs"
} else if product.id.contains("premium") {
return "Premium"
} else {
return product.title
}
}
}