Skip to content

Commit 3378b6c

Browse files
authored
Add postInAppPurchaseLocalization (#10)
1 parent 3318f1d commit 3378b6c

4 files changed

Lines changed: 88 additions & 9 deletions

File tree

Sources/OversizeAppStoreServices/Models/InAppPurchaseLocalization.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import AppStoreAPI
77
import AppStoreConnect
88
import Foundation
9+
import SwiftUI
910

1011
public struct InAppPurchaseLocalization: Identifiable, Equatable, Hashable, Sendable {
1112
public let id: String
@@ -33,5 +34,31 @@ public struct InAppPurchaseLocalization: Identifiable, Equatable, Hashable, Send
3334
case waitingForReview = "WAITING_FOR_REVIEW"
3435
case approved = "APPROVED"
3536
case rejected = "REJECTED"
37+
38+
public var displayName: String {
39+
switch self {
40+
case .prepareForSubmission:
41+
"Prepare for Submission"
42+
case .waitingForReview:
43+
"Waiting for Review"
44+
case .approved:
45+
"Approved"
46+
case .rejected:
47+
"Rejected"
48+
}
49+
}
50+
51+
public var statusColor: Color {
52+
switch self {
53+
case .prepareForSubmission:
54+
.orange
55+
case .waitingForReview:
56+
.yellow
57+
case .approved:
58+
.green
59+
case .rejected:
60+
.red
61+
}
62+
}
3663
}
3764
}

Sources/OversizeAppStoreServices/Models/InAppPurchaseV2.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public struct InAppPurchaseV2: Identifiable, Sendable {
4747
)
4848

4949
self.included = .init(
50-
inAppPurchaseLocalization: included?.compactMap {
50+
inAppPurchaseLocalizations: included?.compactMap {
5151
if case let .inAppPurchaseLocalization(value) = $0 { return .init(schema: value) }
5252
return nil
5353
},
54-
inAppPurchasePricePoint: included?.compactMap {
54+
inAppPurchasePricePoints: included?.compactMap {
5555
if case let .inAppPurchasePricePoint(value) = $0 { return .init(schema: value) }
5656
return nil
5757
},
@@ -83,7 +83,7 @@ public struct InAppPurchaseV2: Identifiable, Sendable {
8383
if case let .inAppPurchaseAvailability(value) = $0 { return value.attributes?.isAvailableInNewTerritories }
8484
return nil
8585
}.first,
86-
inAppPurchaseImage: included?.compactMap {
86+
inAppPurchaseImages: included?.compactMap {
8787
if case let .inAppPurchaseImage(value) = $0 { return .init(schema: value) }
8888
return nil
8989
}
@@ -102,13 +102,13 @@ public struct InAppPurchaseV2: Identifiable, Sendable {
102102
}
103103

104104
public struct Included: Sendable {
105-
public let inAppPurchaseLocalization: [InAppPurchaseLocalization]?
106-
public let inAppPurchasePricePoint: [InAppPurchasePricePoint]?
105+
public let inAppPurchaseLocalizations: [InAppPurchaseLocalization]?
106+
public let inAppPurchasePricePoints: [InAppPurchasePricePoint]?
107107
public let inAppPurchaseContent: InAppPurchaseContent?
108108
public let inAppPurchaseAppStoreReviewScreenshot: ImageAsset?
109109
public let promotedPurchase: PromotedPurchase?
110110
public let inAppPurchasePriceSchedule: InAppPurchasePriceSchedule?
111111
public let inAppPurchaseAvailability: Bool?
112-
public let inAppPurchaseImage: [InAppPurchaseImage]?
112+
public let inAppPurchaseImages: [InAppPurchaseImage]?
113113
}
114114
}

Sources/OversizeAppStoreServices/Models/Typs/InAppPurchaseState.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ public enum InAppPurchaseState: String, CaseIterable, Codable, Sendable {
2626
switch self {
2727
case .approved:
2828
.green
29-
case .waitingForUpload, .processingContent, .readyToSubmit, .waitingForReview, .inReview, .pendingBinaryApproval:
29+
case .waitingForUpload, .processingContent, .readyToSubmit, .waitingForReview, .inReview, .pendingBinaryApproval, .missingMetadata:
3030
.yellow
3131
case .developerActionNeeded, .developerRemovedFromSale, .removedFromSale, .rejected:
3232
.red
33-
case .missingMetadata:
34-
.gray
3533
}
3634
}
3735

Sources/OversizeAppStoreServices/Services/InAppPurchasesService.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ public actor InAppPurchasesService {
6767
}
6868
}
6969

70+
public func fetchAppInAppPurchaseIncludAllWithoutAvailability(inAppPurchaseId: String, force: Bool = false) async -> Result<InAppPurchaseV2, AppError> {
71+
guard let client else { return .failure(.network(type: .unauthorized)) }
72+
return await cacheService.fetchWithCache(key: "fetchAppInAppPurchaseIncludAllWithoutAvailability\(inAppPurchaseId)", force: force) {
73+
let request = Resources.v2.inAppPurchases.id(inAppPurchaseId).get(include: [
74+
.appStoreReviewScreenshot,
75+
.content,
76+
.iapPriceSchedule,
77+
.images,
78+
.inAppPurchaseLocalizations,
79+
.pricePoints,
80+
.promotedPurchase,
81+
])
82+
return try await client.send(request)
83+
}.flatMap {
84+
guard let inAppPurchaseV2: InAppPurchaseV2 = .init(schema: $0.data, included: $0.included) else {
85+
return .failure(.network(type: .decode))
86+
}
87+
return .success(inAppPurchaseV2)
88+
}
89+
}
90+
7091
public func postInAppPurchase(
7192
appId: String,
7293
name: String,
@@ -104,6 +125,39 @@ public actor InAppPurchasesService {
104125
return handleRequestFailure(error: error, replaces: replacements)
105126
}
106127
}
128+
129+
public func postInAppPurchaseLocalization(
130+
inAppPurchaseV2Id: String,
131+
name: String,
132+
description: String?,
133+
locale: AppStoreLanguage
134+
) async -> Result<InAppPurchaseLocalization, AppError> {
135+
guard let client else { return .failure(.network(type: .unauthorized)) }
136+
137+
let requestData: InAppPurchaseLocalizationCreateRequest.Data = .init(
138+
type: .inAppPurchaseLocalizations,
139+
attributes: .init(
140+
name: name,
141+
locale: locale.rawValue,
142+
description: description
143+
),
144+
relationships: .init(inAppPurchaseV2: .init(data: .init(
145+
type: .inAppPurchases,
146+
id: inAppPurchaseV2Id
147+
)))
148+
)
149+
let request = Resources.v1.inAppPurchaseLocalizations.post(.init(data: requestData))
150+
do {
151+
let data = try await client.send(request).data
152+
guard let app = InAppPurchaseLocalization(schema: data) else {
153+
return .failure(.network(type: .decode))
154+
}
155+
return .success(app)
156+
} catch {
157+
let replacements: [String: String] = [:] /* ["@@LANGUAGE_VALUE@@": locale.displayName] */
158+
return handleRequestFailure(error: error, replaces: replacements)
159+
}
160+
}
107161
}
108162

109163
extension InAppPurchasesService {

0 commit comments

Comments
 (0)