Skip to content

Commit

Permalink
fix response loading
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Jun 4, 2024
1 parent 4cc305f commit c350297
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private extension ManageSubscriptionsView {
func checkAndLoadSubscriptionInformation() async {
if !viewModel.isLoaded {
await viewModel.loadSubscriptionInformation()
await viewModel.loadCustomerCenterConfig()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomerCenter/CustomerCenterData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand Down
28 changes: 21 additions & 7 deletions Sources/Networking/Responses/CustomerCenterConfigResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,6 +53,10 @@ struct CustomerCenterConfigResponse {

let firstSeen: String

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

}

}
Expand All @@ -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

}

}

Expand Down Expand Up @@ -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
Expand All @@ -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 c350297

Please sign in to comment.