Skip to content

Commit

Permalink
create settings option to test vaulted state with dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
cianbuckley committed Oct 10, 2023
1 parent bed3e67 commit f8c7ff5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
4EF54F242A6F456B00F5E407 /* CartManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF54F232A6F456B00F5E407 /* CartManager.swift */; };
4EF54F272A6F4C4F00F5E407 /* CartViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF54F262A6F4C4F00F5E407 /* CartViewController.swift */; };
4EF54F312A6F63C000F5E407 /* CartViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4EF54F2F2A6F63C000F5E407 /* CartViewController.xib */; };
86250DE42AD5521C002E45C2 /* AppConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86250DE32AD5521C002E45C2 /* AppConfiguration.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -42,6 +43,7 @@
4EF54F232A6F456B00F5E407 /* CartManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartManager.swift; sourceTree = "<group>"; };
4EF54F262A6F4C4F00F5E407 /* CartViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartViewController.swift; sourceTree = "<group>"; };
4EF54F2F2A6F63C000F5E407 /* CartViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CartViewController.xib; sourceTree = "<group>"; };
86250DE32AD5521C002E45C2 /* AppConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfiguration.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -100,6 +102,7 @@
isa = PBXGroup;
children = (
4EBBA76A2A5F0CE200193E19 /* AppDelegate.swift */,
86250DE32AD5521C002E45C2 /* AppConfiguration.swift */,
4EBBA76C2A5F0CE200193E19 /* SceneDelegate.swift */,
4EF54F232A6F456B00F5E407 /* CartManager.swift */,
4EBBA7A92A5F124F00193E19 /* StorefrontClient.swift */,
Expand Down Expand Up @@ -244,6 +247,7 @@
4EBBA76F2A5F0CE200193E19 /* ProductViewController.swift in Sources */,
4EF54F272A6F4C4F00F5E407 /* CartViewController.swift in Sources */,
4EBBA76B2A5F0CE200193E19 /* AppDelegate.swift in Sources */,
86250DE42AD5521C002E45C2 /* AppConfiguration.swift in Sources */,
4EBBA7AA2A5F124F00193E19 /* StorefrontClient.swift in Sources */,
4EBBA76D2A5F0CE200193E19 /* SceneDelegate.swift in Sources */,
4EA7F9B62A9D2B9D003276A1 /* SettingsViewController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
MIT License

Copyright 2023 - Present, Shopify Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import ShopifyCheckout

public struct AppConfiguration {
public var testVaultedState: Bool = false
}

public var appConfiguration = AppConfiguration() {
didSet {
CartManager.shared.resetCart()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ class CartManager {
}

private func performCartCreate(items: [GraphQL.ID] = [], handler: @escaping CartResultHandler) {
let input = Storefront.CartInput.create(
lines: Input(orNull: items.map({ Storefront.CartLineInput.create(merchandiseId: $0) }))
)

let input = (appConfiguration.testVaultedState) ? vaultedStateCart(items) : defaultCart(items)
let mutation = Storefront.buildMutation { $0
.cartCreate(input: input) { $0
.cart { $0.cartManagerFragment() }
Expand All @@ -110,6 +107,37 @@ class CartManager {
}
}
}

private func defaultCart(_ items: [GraphQL.ID]) -> Storefront.CartInput {
return Storefront.CartInput.create(
lines: Input(orNull: items.map({ Storefront.CartLineInput.create(merchandiseId: $0) }))
)
}

private func vaultedStateCart(_ items: [GraphQL.ID] = []) -> Storefront.CartInput {
let deliveryAddress = Storefront.MailingAddressInput.create(
address1: Input(orNull: "1st Street Southeast"),
address2: Input(orNull: ""),
city: Input(orNull: "Cagalry"),
company: Input(orNull: ""),
country: Input(orNull: "CA"),
firstName: Input(orNull: "Test"),
lastName: Input(orNull: "McTest"),
phone: Input(orNull: ""),
province: Input(orNull: "AB"),
zip: Input(orNull: "T1X 0L3"))

let deliveryAddressPreferences = [Storefront.DeliveryAddressInput.create(deliveryAddress: Input(orNull: deliveryAddress))]

return Storefront.CartInput.create(
attributes: Input(orNull: [Storefront.AttributeInput(key: "isBuyWithPrimeIntent", value: "true")]),
lines: Input(orNull: items.map({ Storefront.CartLineInput.create(merchandiseId: $0) })),
buyerIdentity: Input(orNull: Storefront.CartBuyerIdentityInput.create(
email: Input(orNull: "[email protected]"),
deliveryAddressPreferences: Input(orNull: deliveryAddressPreferences)
))
)
}
}

extension Storefront.CartQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import ShopifyCheckout
class SettingsViewController: UITableViewController {

// MARK: Properties
enum Section: Int {
case preloading = 0
case vaultedState = 1
case colorScheme = 2
case version = 3
}

private lazy var preloadingSwitch: UISwitch = {
let view = UISwitch()
Expand All @@ -35,6 +41,13 @@ class SettingsViewController: UITableViewController {
return view
}()

private lazy var vaultedStateSwitch: UISwitch = {
let view = UISwitch()
view.isOn = appConfiguration.testVaultedState
view.addTarget(self, action: #selector(vaultedStateSwitchDidChange), for: .valueChanged)
return view
}()

// MARK: Initializers

init() {
Expand All @@ -60,12 +73,12 @@ class SettingsViewController: UITableViewController {
// MARK: UITableViewDataSource

override func numberOfSections(in tableView: UITableView) -> Int {
return 3
return 4
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 1:
case Section.colorScheme.rawValue:
return "Color Scheme"
default:
return nil
Expand All @@ -74,7 +87,7 @@ class SettingsViewController: UITableViewController {

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch section {
case 1:
case Section.colorScheme.rawValue:
return "NOTE: If preloading is enabled, color scheme changes may not be applied unless the cart is preloaded again."
default:
return nil
Expand All @@ -83,11 +96,13 @@ class SettingsViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
case Section.preloading.rawValue:
return 1
case Section.vaultedState.rawValue:
return 1
case 1:
case Section.colorScheme.rawValue:
return ShopifyCheckout.Configuration.ColorScheme.allCases.count
case 2:
case Section.version.rawValue:
return 1
default:
return 0
Expand All @@ -100,14 +115,17 @@ class SettingsViewController: UITableViewController {
var content = cell.defaultContentConfiguration()

switch indexPath.section {
case 0:
case Section.preloading.rawValue:
content.text = "Use Preloading"
cell.accessoryView = preloadingSwitch
case 1:
case Section.vaultedState.rawValue:
content.text = "Test Vaulted State"
cell.accessoryView = vaultedStateSwitch
case Section.colorScheme.rawValue:
let scheme = colorScheme(at: indexPath)
content.text = scheme.prettyTitle
content.secondaryText = ShopifyCheckout.configuration.colorScheme == scheme ? "Active" : ""
case 2:
case Section.version.rawValue:
content = UIListContentConfiguration.valueCell()
content.text = "Version"
content.secondaryText = currentVersion()
Expand All @@ -122,10 +140,13 @@ class SettingsViewController: UITableViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
case Section.preloading.rawValue:
preloadingSwitch.isOn.toggle()
preloadingSwitchDidChange()
case 1:
case Section.vaultedState.rawValue:
vaultedStateSwitch.isOn.toggle()
vaultedStateSwitchDidChange()
case Section.colorScheme.rawValue:
let newColorScheme = colorScheme(at: indexPath)
ShopifyCheckout.configuration.colorScheme = newColorScheme
let navigationBarAppearance = newColorScheme.navigationBarAppearance
Expand All @@ -146,6 +167,10 @@ class SettingsViewController: UITableViewController {
ShopifyCheckout.configuration.preloading.enabled = preloadingSwitch.isOn
}

@objc private func vaultedStateSwitchDidChange() {
appConfiguration.testVaultedState = vaultedStateSwitch.isOn
}

private func currentColorScheme() -> Configuration.ColorScheme {
return ShopifyCheckout.configuration.colorScheme
}
Expand Down

0 comments on commit f8c7ff5

Please sign in to comment.