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 pathOfferCodeScreen.swift
More file actions
142 lines (123 loc) · 4.63 KB
/
OfferCodeScreen.swift
File metadata and controls
142 lines (123 loc) · 4.63 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
import SwiftUI
import OpenIAP
@available(iOS 15.0, *)
struct OfferCodeScreen: View {
@StateObject private var iapStore = OpenIapStore()
@State private var showError = false
@State private var errorMessage = ""
var body: some View {
ScrollView {
VStack(spacing: 20) {
VStack(alignment: .leading, spacing: 16) {
HStack {
Image(systemName: "gift.fill")
.font(.largeTitle)
.foregroundColor(AppColors.primary)
VStack(alignment: .leading, spacing: 4) {
Text("Offer Code Redemption")
.font(.headline)
Text("iOS")
.font(.caption)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(AppColors.secondary.opacity(0.2))
.cornerRadius(4)
}
Spacer()
}
Text("Redeem promotional offer codes for in-app purchases and subscriptions.")
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal)
Button(action: {
Task {
await presentOfferCodeRedemption()
}
}) {
HStack {
Image(systemName: "gift")
Text("Redeem Offer Code")
Spacer()
Image(systemName: "arrow.up.forward.app")
}
.padding()
.background(AppColors.primary)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding(.horizontal)
InstructionCard()
TestingNotesCard()
Spacer(minLength: 20)
}
.padding(.vertical)
}
.background(AppColors.background)
.navigationTitle("Offer Code")
.navigationBarTitleDisplayMode(.large)
.alert("Error", isPresented: $showError) {
Button("OK") {}
} message: {
Text(errorMessage)
}
.onAppear {
setupIapProvider()
}
.onDisappear {
teardownConnection()
}
}
// MARK: - OpenIapStore Setup
private func setupIapProvider() {
print("🔷 [OfferCode] Setting up OpenIapStore...")
iapStore.onPurchaseSuccess = { purchase in
if let iosPurchase = purchase.asIOS() {
Task { @MainActor in
print("✅ [OfferCode] Offer code redeemed successfully: \(iosPurchase.productId)")
}
}
}
iapStore.onPurchaseError = { error in
Task { @MainActor in
errorMessage = error.message
showError = true
}
}
Task {
do {
try await iapStore.initConnection()
print("✅ [OfferCode] Connection initialized")
} catch {
await MainActor.run {
errorMessage = "Failed to initialize connection: \(error.localizedDescription)"
showError = true
}
}
}
}
private func teardownConnection() {
print("🔷 [OfferCode] Tearing down connection...")
Task {
try await iapStore.endConnection()
print("✅ [OfferCode] Connection ended")
}
}
// MARK: - Offer Code Redemption
private func presentOfferCodeRedemption() async {
do {
try await iapStore.presentCodeRedemptionSheetIOS()
print("✅ [OfferCode] Offer code redemption sheet presented")
} catch {
await MainActor.run {
errorMessage = "Failed to present offer code redemption: \(error.localizedDescription)"
showError = true
}
}
}
}
// InstructionCard, TestingNotesCard, InstructionRow, and TestingNote moved to Screens/uis/