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 pathAllProductsView.swift
More file actions
362 lines (315 loc) · 11.4 KB
/
AllProductsView.swift
File metadata and controls
362 lines (315 loc) · 11.4 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
import SwiftUI
import OpenIAP
@available(iOS 15.0, *)
struct AllProductsView: View {
@StateObject private var store = OpenIapStore()
@State private var isLoading = false
@State private var errorMessage: String?
@Environment(\.dismiss) private var dismiss
// Product IDs from other screens
private let allProductIds: [String] = [
"dev.hyo.martie.10bulbs",
"dev.hyo.martie.30bulbs",
"dev.hyo.martie.certified",
"dev.hyo.martie.premium",
"dev.hyo.martie.premium_year"
]
// Enum to unify product types for display
private enum UnifiedProduct {
case regular(ProductIOS)
case subscription(ProductSubscriptionIOS)
var id: String {
switch self {
case .regular(let product):
return product.id
case .subscription(let sub):
return sub.id
}
}
var sortOrder: Int {
switch self {
case .regular(let product):
switch product.typeIOS {
case .nonConsumable:
return 1 // First
case .consumable:
return 2 // Second
default:
return 3 // Third
}
case .subscription:
return 4 // Last
}
}
}
// All products sorted by type: non-consumable, consumable, then subscriptions
private var sortedAllProducts: [UnifiedProduct] {
let regularProducts = store.iosProducts.map { UnifiedProduct.regular($0) }
let subscriptionProducts = store.iosSubscriptionProducts.map { UnifiedProduct.subscription($0) }
let allProducts = regularProducts + subscriptionProducts
return allProducts.sorted { first, second in
first.sortOrder < second.sortOrder
}
}
var body: some View {
NavigationView {
ZStack {
Color(UIColor.systemGroupedBackground)
.ignoresSafeArea()
ScrollView {
VStack(spacing: 16) {
if !store.isConnected {
connectionWarningCard
}
if isLoading {
loadingCard
}
if !isLoading && sortedAllProducts.isEmpty && store.isConnected {
emptyStateCard
}
// Display all products sorted by type: non-consumable, consumable, then subscriptions
ForEach(sortedAllProducts, id: \.id) { unifiedProduct in
switch unifiedProduct {
case .regular(let product):
productCard(for: product)
case .subscription(let subscription):
subscriptionCard(for: subscription)
}
}
if let error = errorMessage {
errorCard(message: error)
}
}
.padding()
}
}
.navigationTitle("All Products")
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
Task {
try? await store.endConnection()
}
dismiss()
}) {
Image(systemName: "arrow.left")
.foregroundColor(.primary)
}
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
.onAppear {
Task {
await initializeStore()
}
}
}
private var connectionWarningCard: some View {
HStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.orange)
.font(.title3)
VStack(alignment: .leading, spacing: 4) {
Text("Not Connected")
.font(.headline)
Text("Billing service is not connected")
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
Button("Retry") {
Task {
await initializeStore()
}
}
.buttonStyle(.bordered)
.controlSize(.small)
}
.padding()
.background(Color.orange.opacity(0.1))
.cornerRadius(12)
}
private var loadingCard: some View {
HStack {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Text("Loading products...")
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.leading, 8)
}
.frame(maxWidth: .infinity)
.padding()
.background(Color(UIColor.secondarySystemGroupedBackground))
.cornerRadius(12)
}
private var emptyStateCard: some View {
VStack(spacing: 12) {
Image(systemName: "bag")
.font(.largeTitle)
.foregroundColor(.secondary)
Text("No products available")
.font(.headline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 32)
.background(Color(UIColor.secondarySystemGroupedBackground))
.cornerRadius(12)
}
private func productCard(for product: ProductIOS) -> some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: 4) {
Text(product.displayName ?? product.displayNameIOS)
.font(.headline)
if !product.description.isEmpty {
Text(product.description)
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(2)
}
}
Spacer()
// Product type badges
HStack(spacing: 4) {
// Main type badge
Text(product.type == .subs ? "subs" : "in-app")
.font(.caption)
.fontWeight(.medium)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(product.type == .subs ? Color.blue.opacity(0.1) : Color.green.opacity(0.1))
.foregroundColor(product.type == .subs ? .blue : .green)
.cornerRadius(6)
// Detailed type badge for non-subscription products
if product.type != .subs {
Text(getDetailedProductType(product.typeIOS))
.font(.caption2)
.fontWeight(.medium)
.padding(.horizontal, 6)
.padding(.vertical, 3)
.background(getTypeColor(product.typeIOS).opacity(0.1))
.foregroundColor(getTypeColor(product.typeIOS))
.cornerRadius(4)
}
}
}
HStack {
Text(product.displayPrice ?? "--")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.blue)
Spacer()
Text("SKU: \(product.id)")
.font(.caption2)
.foregroundColor(.secondary)
}
}
.padding()
.background(Color(UIColor.secondarySystemGroupedBackground))
.cornerRadius(12)
.shadow(color: .black.opacity(0.05), radius: 2, x: 0, y: 1)
}
private func errorCard(message: String) -> some View {
HStack {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(.red)
Text(message)
.font(.subheadline)
.foregroundColor(.red)
Spacer()
}
.padding()
.background(Color.red.opacity(0.1))
.cornerRadius(12)
}
private func initializeStore() async {
isLoading = true
errorMessage = nil
do {
try await store.initConnection()
if store.isConnected {
// Fetch all products using "all" type
try await store.fetchProducts(
skus: allProductIds,
type: .all
)
} else {
errorMessage = "Failed to connect to App Store"
}
} catch {
errorMessage = error.localizedDescription
}
isLoading = false
}
private func getDetailedProductType(_ type: ProductTypeIOS) -> String {
switch type {
case .consumable:
return "consumable"
case .nonConsumable:
return "non-consumable"
case .autoRenewableSubscription:
return "auto-renewable"
case .nonRenewingSubscription:
return "non-renewing"
}
}
private func getTypeColor(_ type: ProductTypeIOS) -> Color {
switch type {
case .consumable:
return .orange
case .nonConsumable:
return .purple
case .autoRenewableSubscription:
return .blue
case .nonRenewingSubscription:
return .indigo
}
}
private func subscriptionCard(for subscription: ProductSubscriptionIOS) -> some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: 4) {
Text(subscription.displayName ?? subscription.displayNameIOS)
.font(.headline)
if !subscription.description.isEmpty {
Text(subscription.description)
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(2)
}
}
Spacer()
// Subscription badge
Text("subs")
.font(.caption)
.fontWeight(.medium)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Color.blue.opacity(0.1))
.foregroundColor(.blue)
.cornerRadius(6)
}
HStack {
Text(subscription.displayPrice)
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.blue)
Spacer()
Text("SKU: \(subscription.id)")
.font(.caption2)
.foregroundColor(.secondary)
}
}
.padding()
.background(Color(UIColor.secondarySystemGroupedBackground))
.cornerRadius(12)
.shadow(color: .black.opacity(0.05), radius: 2, x: 0, y: 1)
}
}
#Preview {
AllProductsView()
}