Skip to content

Commit 31cd09f

Browse files
kiftiotiagocandido
authored andcommitted
rough plumbing together
1 parent 2aa50f6 commit 31cd09f

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

Samples/MobileBuyIntegration/MobileBuyIntegration/AppConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public struct AppConfiguration {
2727
/// Prefill buyer information
2828
public var useVaultedState: Bool = false
2929

30+
/// Pass in customerAccessToken after user logs in
31+
public var useAuthenticatedState: Bool = false
32+
3033
/// Logger to retain Web Pixel events
3134
internal let webPixelsLogger = FileLogger("analytics.txt")
3235
}

Samples/MobileBuyIntegration/MobileBuyIntegration/CartManager.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,20 @@ class CartManager {
132132
}
133133
}
134134

135+
func authenticatedCart() {
136+
137+
}
138+
135139
private func performCartCreate(items: [GraphQL.ID] = [], handler: @escaping CartResultHandler) {
136-
let input = (appConfiguration.useVaultedState) ? vaultedStateCart(items) : defaultCart(items)
140+
let sfApiAccessToken = CustomerAccountClient.shared.sfApiAccessToken
141+
142+
let input = if appConfiguration.useAuthenticatedState && sfApiAccessToken != nil {
143+
authenticatedCart(sfApiAccessToken!, items)
144+
} else if appConfiguration.useVaultedState {
145+
vaultedStateCart(items)
146+
} else {
147+
defaultCart(items)
148+
}
137149

138150
let countryCode: Storefront.CountryCode = appConfiguration.useVaultedState
139151
? ((Storefront.CountryCode(rawValue: Bundle.main.infoDictionary?["Country"] as? String ?? "")) ?? .ca)
@@ -142,6 +154,10 @@ class CartManager {
142154
let mutation = Storefront.buildMutation(inContext: Storefront.InContextDirective(country: countryCode)) { $0
143155
.cartCreate(input: input) { $0
144156
.cart { $0.cartManagerFragment() }
157+
.userErrors {
158+
$0.field()
159+
$0.message()
160+
}
145161
}
146162
}
147163

@@ -205,6 +221,15 @@ class CartManager {
205221
))
206222
)
207223
}
224+
225+
private func authenticatedCart(_ customerAccessToken: String, _ items: [GraphQL.ID] = []) -> Storefront.CartInput {
226+
return Storefront.CartInput.create(
227+
lines: Input(orNull: items.map({ Storefront.CartLineInput.create(merchandiseId: $0) })),
228+
buyerIdentity: Input(orNull: Storefront.CartBuyerIdentityInput.create(
229+
customerAccessToken: Input(orNull: customerAccessToken)
230+
))
231+
)
232+
}
208233
}
209234

210235
extension Storefront.CartQuery {

Samples/MobileBuyIntegration/MobileBuyIntegration/CustomerAccountClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class CustomerAccountClient {
231231
}
232232

233233
let storefrontToken = storefrontCustomerAccessToken.data.storefrontCustomerAccessTokenCreate.customerAccessToken
234+
self.sfApiAccessToken = storefrontToken
234235
callback(storefrontToken, nil)
235236
}
236237
)

Samples/MobileBuyIntegration/MobileBuyIntegration/Localizable.xcstrings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
},
77
"App version" : {
88

9+
},
10+
"Authenticate cart when logged in" : {
11+
912
},
1013
"Clear" : {
1114

Samples/MobileBuyIntegration/MobileBuyIntegration/SceneDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
5454

5555
tabBarController.viewControllers = [
5656
UINavigationController(
57-
rootViewController: catalogController
57+
rootViewController: loginController
5858
),
5959
UINavigationController(
60-
rootViewController: cartController
60+
rootViewController: catalogController
6161
),
6262
UINavigationController(
63-
rootViewController: loginController
63+
rootViewController: cartController
6464
)
6565
]
6666

Samples/MobileBuyIntegration/MobileBuyIntegration/SettingsViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import ShopifyCheckoutSheetKit
2929
struct SettingsView: View {
3030
@State private var preloadingEnabled = ShopifyCheckoutSheetKit.configuration.preloading.enabled
3131
@State private var useVaultedState = appConfiguration.useVaultedState
32+
@State private var useAuthenticatedState = appConfiguration.useAuthenticatedState
3233
@State private var logs: [String?] = LogReader.shared.readLogs() ?? []
3334
@State private var selectedColorScheme = ShopifyCheckoutSheetKit.configuration.colorScheme
3435
@State private var colorScheme: ColorScheme = .light
@@ -45,6 +46,10 @@ struct SettingsView: View {
4546
.onChange(of: useVaultedState) { newValue in
4647
appConfiguration.useVaultedState = newValue
4748
}
49+
Toggle("Authenticate cart when logged in", isOn: $useAuthenticatedState)
50+
.onChange(of: useAuthenticatedState) { newValue in
51+
appConfiguration.useAuthenticatedState = newValue
52+
}
4853
}
4954

5055
Section(header: Text("Theme")) {

0 commit comments

Comments
 (0)