Skip to content

Commit

Permalink
SwiftUI improvements (#133)
Browse files Browse the repository at this point in the history
Improvements to SwiftUI
  • Loading branch information
markmur committed Mar 7, 2024
1 parent e283f2d commit a8a0fa6
Show file tree
Hide file tree
Showing 20 changed files with 646 additions and 139 deletions.
66 changes: 51 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pod "ShopifyCheckoutSheetKit", "~> 2"

For more information on CocoaPods, please see their [getting started guide](https://guides.cocoapods.org/using/getting-started.html).

### Basic Usage
### Programmatic Usage

Once the SDK has been added as a dependency, you can import the library:

Expand Down Expand Up @@ -85,25 +85,46 @@ class MyViewController: UIViewController {
}
```

Alternatively, with SwiftUI:
### SwiftUI Usage

```swift
import SwiftUI
import ShopifyCheckoutSheetKit

struct ContentView: View {
@State private var isPresented = false
let url: URL
let delegate: CheckoutDelegate?

var body: some View {
Button("Checkout") {
self.isPresented = true
}
.sheet(isPresented: $isPresented) {
CheckoutViewController.Representable(url: url, delegate: delegate)
}
@State var isPresented = false
@State var checkoutURL: URL?

var body: some View {
Button("Checkout") {
isPresented = true
}
.sheet(isPresented: $isPresented) {
if let url = checkoutURL {
CheckoutSheet(url: url)
/// Configuration
.title("Checkout")
.colorScheme(.automatic)
.tintColor(.blue)
.backgroundColor(.white)

/// Lifecycle events
.onCancel {
isPresented = false
}
.onComplete { event in
handleCompletedEvent(event)
}
.onFail { error in
handleError(error)
}
.onPixelEvent { event in
handlePixelEvent(event)
}
.edgesIgnoringSafeArea(.all)
}
}
}
}
```

Expand Down Expand Up @@ -158,8 +179,6 @@ ShopifyCheckoutSheetKit.configuration.backgroundColor = UIColor(red: 0.09, green
ShopifyCheckoutSheetKit.configuration.backgroundColor = .systemBackground
```

### Localization

#### `title`

By default, the Checkout Sheet Kit will look for a `shopify_checkout_sheet_title` key in a `Localizable.xcstrings` file to set the sheet title, otherwise it will fallback to "Checkout" across all locales.
Expand Down Expand Up @@ -197,6 +216,23 @@ Here is an example of a `Localizable.xcstrings` containing translations for 2 lo
}
```

#### SwiftUI Configuration

Similarly, configuration modifiers are available to set the configuration of your checkout when using SwiftUI:

```swift
CheckoutSheet(checkout: checkoutURL)
.title("Checkout")
.colorScheme(.automatic)
.tintColor(.blue)
.backgroundColor(.black)
```

> [!NOTE]
> Note that if the values of your SwiftUI configuration are **variable** and you are using `preload()`,
> you will need to call `preload()` each time your variables change to ensure that the checkout cache
> has been invalidated, for checkout to be loaded with the new configuration.
### Preloading

Initializing a checkout session requires communicating with Shopify servers and, depending on the network weather and the quality of the buyer's connection, can result in undesirable wait time for the buyer. To help optimize and deliver the best experience, the SDK provides a preloading hint that allows app developers to signal and initialize the checkout session in the background and ahead of time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
repositoryURL = "https://github.com/Shopify/mobile-buy-sdk-ios";
requirement = {
kind = exactVersion;
version = 11.1.0;
version = 11.3.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Shopify/mobile-buy-sdk-ios",
"state" : {
"revision" : "e6e85dcf8f9eb95baaa8336ad3d7967ea8c36ade",
"version" : "11.1.0"
"revision" : "3a6ecdce6b9e8f356078fbdc2b738ac32cd18153",
"version" : "11.3.0"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
}
}
},
}
},
"version" : "1.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Shopify/mobile-buy-sdk-ios",
"state" : {
"revision" : "3a6ecdce6b9e8f356078fbdc2b738ac32cd18153",
"version" : "11.3.0"
"revision" : "e6e85dcf8f9eb95baaa8336ad3d7967ea8c36ade",
"version" : "11.1.0"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{
"pins" : [
{
"identity" : "mobile-buy-sdk-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Shopify/mobile-buy-sdk-ios",
"state" : {
"revision" : "e6e85dcf8f9eb95baaa8336ad3d7967ea8c36ade",
"version" : "11.3.0"
}
},
{
"identity" : "swiftlintplugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"state" : {
"revision" : "b1090ecd269dddd96bda0df24ca3f1aa78f33578",
"version" : "0.52.4"
"revision" : "ea6d3ca895b49910f790e98e4b4ca658e0fe490e",
"version" : "0.54.0"
}
}
],
Expand Down
10 changes: 9 additions & 1 deletion Samples/SwiftUIExample/SwiftUIExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
6A2DBC202B90ECBA00761222 /* SwiftUIExampleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2DBC1F2B90ECBA00761222 /* SwiftUIExampleUITests.swift */; };
6A2DBC222B90ECBA00761222 /* SwiftUIExampleUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2DBC212B90ECBA00761222 /* SwiftUIExampleUITestsLaunchTests.swift */; };
6A71777C2B90DEB000ED3B99 /* ShopifyCheckoutSheetKit in Frameworks */ = {isa = PBXBuildFile; productRef = 6A71777B2B90DEB000ED3B99 /* ShopifyCheckoutSheetKit */; };
6A71777E2B90DECF00ED3B99 /* Buy in Frameworks */ = {isa = PBXBuildFile; productRef = 6A71777D2B90DECF00ED3B99 /* Buy */; };
6AC6C3902B9089B60007EA2E /* ProductViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AC6C38F2B9089B60007EA2E /* ProductViewModel.swift */; };
6ACED4192B909C5C00AC6947 /* ShopifyCheckoutSheetKit in Frameworks */ = {isa = PBXBuildFile; productRef = 6ACED4182B909C5C00AC6947 /* ShopifyCheckoutSheetKit */; };
6ACED41B2B90A7FE00AC6947 /* CartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ACED41A2B90A7FE00AC6947 /* CartView.swift */; };
86C569C32B15DA2B00F26028 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C569C22B15DA2B00F26028 /* App.swift */; };
86C569C52B15DA2B00F26028 /* CatalogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C569C42B15DA2B00F26028 /* CatalogView.swift */; };
86C569C72B15DA2D00F26028 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 86C569C62B15DA2D00F26028 /* Assets.xcassets */; };
Expand All @@ -31,8 +33,10 @@

/* Begin PBXFileReference section */
6A2DBC1D2B90ECBA00761222 /* SwiftUIExampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftUIExampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
6A2DBC1F2B90ECBA00761222 /* SwiftUIExampleUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIExampleUITests.swift; sourceTree = "<group>"; };
6A2DBC212B90ECBA00761222 /* SwiftUIExampleUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIExampleUITestsLaunchTests.swift; sourceTree = "<group>"; };
6AC6C38F2B9089B60007EA2E /* ProductViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductViewModel.swift; sourceTree = "<group>"; };
6ACED41A2B90A7FE00AC6947 /* CartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartView.swift; sourceTree = "<group>"; };
8652C4602B1659E600A770F9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
8666476C2B1642730039400B /* Storefront.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Storefront.xcconfig; sourceTree = "<group>"; };
86C569BF2B15DA2B00F26028 /* SwiftUIExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -67,6 +71,7 @@
6A2DBC1E2B90ECBA00761222 /* SwiftUIExampleUITests */ = {
isa = PBXGroup;
children = (
6A2DBC1F2B90ECBA00761222 /* SwiftUIExampleUITests.swift */,
6A2DBC212B90ECBA00761222 /* SwiftUIExampleUITestsLaunchTests.swift */,
);
path = SwiftUIExampleUITests;
Expand Down Expand Up @@ -117,6 +122,7 @@
86C569C42B15DA2B00F26028 /* CatalogView.swift */,
86C569C62B15DA2D00F26028 /* Assets.xcassets */,
6AC6C38F2B9089B60007EA2E /* ProductViewModel.swift */,
6ACED41A2B90A7FE00AC6947 /* CartView.swift */,
);
path = SwiftUIExample;
sourceTree = "<group>";
Expand Down Expand Up @@ -252,6 +258,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6A2DBC202B90ECBA00761222 /* SwiftUIExampleUITests.swift in Sources */,
6A2DBC222B90ECBA00761222 /* SwiftUIExampleUITestsLaunchTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -260,6 +267,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6ACED41B2B90A7FE00AC6947 /* CartView.swift in Sources */,
86C569C52B15DA2B00F26028 /* CatalogView.swift in Sources */,
86C569ED2B15DB0A00F26028 /* StorefrontClient.swift in Sources */,
86C569C32B15DA2B00F26028 /* App.swift in Sources */,
Expand Down Expand Up @@ -551,7 +559,7 @@
repositoryURL = "https://github.com/Shopify/mobile-buy-sdk-ios";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 11.1.0;
minimumVersion = 11.3.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"pins" : [
{
"identity" : "swiftlintplugin",
"identity" : "mobile-buy-sdk-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"location" : "https://github.com/Shopify/mobile-buy-sdk-ios",
"state" : {
"revision" : "ea6d3ca895b49910f790e98e4b4ca658e0fe490e",
"version" : "0.54.0"
"revision" : "3a6ecdce6b9e8f356078fbdc2b738ac32cd18153",
"version" : "11.3.0"
}
}
],
Expand Down
24 changes: 24 additions & 0 deletions Samples/SwiftUIExample/SwiftUIExample/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ struct RootTabView: View {
.accessibilityIdentifier("catalogTabIcon")
Text("Catalog")
}

NavigationView {
CartView(checkoutURL: $checkoutURL, isShowingCheckout: $isShowingCheckout, cart: $cartManager.cart)
.navigationTitle("Cart")
.navigationBarTitleDisplayMode(.inline)
.padding(20)
.toolbar {
if cartManager.cart?.lines != nil {
ToolbarItem(placement: .navigationBarTrailing) {
Text("Clear")
.font(.body)
.foregroundStyle(Color.accentColor)
.onTapGesture {
cartManager.resetCart()
}
}
}
}
}
.tabItem {
SwiftUI.Image(systemName: "cart")
.accessibilityIdentifier("cartTabIcon")
Text("Cart")
}
}
}
}
Expand Down
Loading

0 comments on commit a8a0fa6

Please sign in to comment.