Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix response loading
Browse files Browse the repository at this point in the history
vegaro committed Jun 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b0ce01d commit 9423df6
Showing 4 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -48,11 +48,6 @@ class ManageSubscriptionsViewModel: ObservableObject {
state = .notLoaded
}

init(configuration: CustomerCenterConfigData) {
state = .notLoaded
self.configuration = configuration
}

init(configuration: CustomerCenterConfigData, subscriptionInformation: SubscriptionInformation) {
self.configuration = configuration
self.subscriptionInformation = subscriptionInformation
@@ -85,6 +80,14 @@ class ManageSubscriptionsViewModel: ObservableObject {
}
}

func loadCustomerCenterConfig() async {
do {
self.configuration = try await Purchases.shared.loadCustomerCenter()
} catch {
self.state = .error(error)
}
}

func handleAction(for path: CustomerCenterConfigData.HelpPath) {
switch path.type {
case .missingPurchase:
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ private extension ManageSubscriptionsView {
func checkAndLoadSubscriptionInformation() async {
if !viewModel.isLoaded {
await viewModel.loadSubscriptionInformation()
await viewModel.loadCustomerCenterConfig()
}
}

2 changes: 1 addition & 1 deletion Sources/CustomerCenter/CustomerCenterData.swift
Original file line number Diff line number Diff line change
@@ -186,7 +186,7 @@ extension CustomerCenterConfigData.HelpPath {
extension CustomerCenterConfigData.LocalizedString {

init(from response: CustomerCenterConfigResponse.LocalizedString) {
self.en_US = response.en_US
self.en_US = response.en_us
}

}
28 changes: 21 additions & 7 deletions Sources/Networking/Responses/CustomerCenterConfigResponse.swift
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ struct CustomerCenterConfigResponse {

enum PathType: String {

case missingPurchase = "MISSING_PURCHASE"
case missingPurchase = "MISSING_ENTITLEMENT"
case refundRequest = "REFUND_REQUEST"
case changePlans = "CHANGE_PLANS"
case cancel = "CANCEL"
@@ -53,6 +53,10 @@ struct CustomerCenterConfigResponse {

let firstSeen: String

enum CodingKeys: String, CodingKey {
case firstSeen = "first_seen"
}

}

}
@@ -77,15 +81,25 @@ struct CustomerCenterConfigResponse {
struct LocalizedString {

// swiftlint:disable:next identifier_name
let en_US: String
let en_us: String

enum CodingKeys: String, CodingKey {
case en_us = "en_US"
}

}

struct Appearance {

let mode: String
let light: String
let dark: String
let light: AppearanceMode
let dark: AppearanceMode

struct AppearanceMode {

let accentColor: String

}

}

@@ -184,12 +198,11 @@ struct CustomerCenterConfigResponse {
}
}
}
"""

do {
let data = try JSONSerialization.data(withJSONObject: jsonData, options: [])
return try JSONDecoder.default.decode(jsonData: data, logErrors: true)
guard let data = jsonData.data(using: .utf8, allowLossyConversion: false) else { return nil }
return try JSONDecoder().decode(CustomerCenterConfigResponse.self, from: data)
} catch {
print(error)
return nil
@@ -209,5 +222,6 @@ extension CustomerCenterConfigResponse.HelpPath.FeedbackSurvey: Codable, Equatab
extension CustomerCenterConfigResponse.HelpPath.FeedbackSurvey.Option: Codable, Equatable {}
extension CustomerCenterConfigResponse.LocalizedString: Codable, Equatable {}
extension CustomerCenterConfigResponse.Appearance: Codable, Equatable {}
extension CustomerCenterConfigResponse.Appearance.AppearanceMode: Codable, Equatable {}

extension CustomerCenterConfigResponse: HTTPResponseBody {}

0 comments on commit 9423df6

Please sign in to comment.