Commit 4f189e2a by Dima Bart

Merge pull request #141 from Shopify/feature/customer-sample-app

Customer API Sample Application
parents b250d1ef 44a839c4
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15A282b" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="rS3-R9-Ivy">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="rS3-R9-Ivy">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--Master-->
......@@ -11,7 +11,6 @@
<navigationController title="Master" id="rS3-R9-Ivy" sceneMemberID="viewController">
<navigationBar key="navigationBar" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" id="yXu-0R-QUA">
<autoresizingMask key="autoresizingMask"/>
<animations/>
</navigationBar>
<connections>
<segue destination="pGg-6v-bdr" kind="relationship" relationship="rootViewController" id="RxB-wf-QIq"/>
......@@ -28,8 +27,7 @@
<tableView key="view" opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="mLL-gJ-YKr">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
<sections/>
<connections>
<outlet property="dataSource" destination="pGg-6v-bdr" id="P41-gY-KXY"/>
......
//
// AccountViewController.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
class AccountViewController: UIViewController {
@IBOutlet weak var loginContainerView: UIView!
@IBOutlet weak var signupContainerView: UIView!
private var loginViewController: LoginViewController!
private var signupViewController: SignupViewController!
// ----------------------------------
// MARK: - Segue -
//
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.identifier {
case .Some("loginSegue"):
self.loginViewController = segue.destinationViewController as! LoginViewController
self.loginViewController.delegate = self
case .Some("signupSegue"):
self.signupViewController = segue.destinationViewController as! SignupViewController
self.signupViewController.delegate = self
default:
break
}
if self.loginViewController != nil && self.signupViewController != nil {
self.updateSelectedIndex(0)
}
}
// ----------------------------------
// MARK: - Updates -
//
private func updateSelectedIndex(index: Int) {
self.loginContainerView.hidden = (index != 0)
self.signupContainerView.hidden = (index == 0)
}
// ----------------------------------
// MARK: - UI Actions -
//
@IBAction func segmentAction(sender: UISegmentedControl) {
self.updateSelectedIndex(sender.selectedSegmentIndex)
}
}
// ----------------------------------
// MARK: - AuthenticationDelegate -
//
extension AccountViewController: AuthenticationDelegate {
func authenticationDidSucceedForCustomer(customer: BUYCustomer, withToken token: String) {
if let orders = self.storyboard?.instantiateViewControllerWithIdentifier("ordersViewController") {
self.navigationController?.pushViewController(orders, animated: true)
}
}
func authenticationDidFailWithError(error: NSError?) {
let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
//
// ActionCell.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
class ActionCell: UITableViewCell {
@IBOutlet private weak var actionLabel: UILabel!
@IBOutlet private weak var loader: UIActivityIndicatorView!
var loading: Bool {
get {
return self.actionLabel.hidden
}
set {
self.actionLabel.hidden = newValue
self.loader.hidden = !newValue
}
}
}
//
// AppDelegate.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
/* ---------------------------------
** Configure store credentials to
** use with your specific store.
*/
let shopDomain: String = ""
let apiKey: String = ""
let appID: String = ""
private(set) var client: BUYClient!
// ----------------------------------
// MARK: - Application Launch -
//
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.client = BUYClient(shopDomain: self.shopDomain, apiKey: self.apiKey, appId: self.appID)
return true
}
}
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
//
// AuthenticationDelegate.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 Foundation
import Buy
protocol AuthenticationDelegate: class {
func authenticationDidSucceedForCustomer(customer: BUYCustomer, withToken token: String)
func authenticationDidFailWithError(error: NSError?)
}
//
// BUYClient+Extensions.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 Foundation
import Buy
extension BUYClient {
static var sharedClient: BUYClient {
if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate {
return delegate.client
}
fatalError("Could not retrieve shared BUYClient")
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
//
// LineItemCell.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
class LineItemCell: UITableViewCell {
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var subtitleLabel: UILabel!
@IBOutlet private weak var priceLabel: UILabel!
// ----------------------------------
// MARK: - Setters -
//
func setLineItem(item: BUYLineItem) {
self.titleLabel.text = item.title
self.subtitleLabel.text = item.variantTitle
self.priceLabel.text = "$\(item.linePrice)"
}
}
//
// LoginViewController.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
class LoginViewController: UITableViewController {
weak var delegate: AuthenticationDelegate?
@IBOutlet private weak var emailField: UITextField!
@IBOutlet private weak var passwordField: UITextField!
@IBOutlet private weak var actionCell: ActionCell!
private var email: String { return self.emailField.text ?? "" }
private var password: String { return self.passwordField.text ?? "" }
// ----------------------------------
// MARK: - View Loading -
//
override func viewDidLoad() {
super.viewDidLoad()
self.actionCell.loading = false
}
// ----------------------------------
// MARK: - Actions -
//
private func loginUser() {
guard !self.actionCell.loading else { return }
let credentials = BUYAccountCredentials(items: [
BUYAccountCredentialItem(email: self.email),
BUYAccountCredentialItem(password: self.password),
])
self.actionCell.loading = true
BUYClient.sharedClient.loginCustomerWithCredentials(credentials) { (customer, token, error) in
self.actionCell.loading = false
if let customer = customer,
let token = token {
self.clear()
self.delegate?.authenticationDidSucceedForCustomer(customer, withToken: token)
} else {
self.delegate?.authenticationDidFailWithError(error)
}
}
}
private func clear() {
self.emailField.text = ""
self.passwordField.text = ""
}
// ----------------------------------
// MARK: - UITableViewDelegate -
//
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 1 {
if !self.email.isEmpty &&
!self.password.isEmpty {
self.loginUser()
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
//
// OrdersViewController.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
class OrdersViewController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
private var orders = [BUYOrder]()
private let dateFormatter: NSDateFormatter = {
let f = NSDateFormatter()
f.dateStyle = .MediumStyle
f.timeStyle = .NoStyle
return f
}()
// ----------------------------------
// MARK: - View Loading -
//
override func viewDidLoad() {
super.viewDidLoad()
self.loadOrders()
}
private func loadOrders() {
BUYClient.sharedClient.getOrdersForCustomerWithCallback { (orders, error) in
if let orders = orders {
self.orders = orders
self.tableView.reloadData()
} else {
print("Could not fetch orders: \(error)")
}
}
}
// ----------------------------------
// MARK: - UI Actions -
//
@IBAction func logoutAction(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
}
// ----------------------------------
// MARK: - UITableViewDataSource -
//
extension OrdersViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.orders.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.orders[section].lineItems.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let order = self.orders[section]
return "\(self.dateFormatter.stringFromDate(order.processedAt)) - \(order.name)"
}
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
let order = self.orders[section]
return "Order total: $\(order.totalPrice) \(order.currency)"
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! LineItemCell
let lineItem = self.orders[indexPath.section].lineItems[indexPath.row]
cell.setLineItem(lineItem)
return cell
}
}
// ----------------------------------
// MARK: - BUYOrder -
//
extension BUYOrder {
var lineItems: [BUYLineItem] {
var items = self.fulfilledLineItems
items.appendContentsOf(self.unfulfilledLineItems)
return items
}
}
//
// SignupViewController.swift
// Sample App Customers
//
// Created by Shopify.
// Copyright (c) 2016 Shopify Inc. All rights reserved.
//
// 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 UIKit
import Buy
class SignupViewController: UITableViewController {
weak var delegate: AuthenticationDelegate?
@IBOutlet private weak var firstNameField: UITextField!
@IBOutlet private weak var lastNameField: UITextField!
@IBOutlet private weak var emailField: UITextField!
@IBOutlet private weak var passwordField: UITextField!
@IBOutlet private weak var passwordConfirmField: UITextField!
@IBOutlet private weak var actionCell: ActionCell!
private var firstName: String { return self.firstNameField.text ?? "" }
private var lastName: String { return self.lastNameField.text ?? "" }
private var email: String { return self.emailField.text ?? "" }
private var password: String { return self.passwordField.text ?? "" }
private var passwordConfirm: String { return self.passwordConfirmField.text ?? "" }
// ----------------------------------
// MARK: - View Loading -
//
override func viewDidLoad() {
super.viewDidLoad()
self.actionCell.loading = false
}
// ----------------------------------
// MARK: - Actions -
//
private func createUser() {
guard !self.actionCell.loading else { return }
let credentials = BUYAccountCredentials(items: [
BUYAccountCredentialItem(firstName: self.firstName),
BUYAccountCredentialItem(lastName: self.lastName),
BUYAccountCredentialItem(email: self.email),
BUYAccountCredentialItem(password: self.password),
BUYAccountCredentialItem(passwordConfirmation: self.passwordConfirm),
])
self.actionCell.loading = true
BUYClient.sharedClient.createCustomerWithCredentials(credentials) { (customer, token, error) in
self.actionCell.loading = false
if let customer = customer,
let token = token {
self.clear()
self.delegate?.authenticationDidSucceedForCustomer(customer, withToken: token)
} else {
self.delegate?.authenticationDidFailWithError(error)
}
}
}
private func clear() {
self.firstNameField.text = ""
self.lastNameField.text = ""
self.emailField.text = ""
self.passwordField.text = ""
self.passwordConfirmField.text = ""
}
// ----------------------------------
// MARK: - UITableViewDelegate -
//
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 2 {
if !self.email.isEmpty &&
!self.password.isEmpty &&
!self.firstName.isEmpty &&
!self.lastName.isEmpty &&
!self.passwordConfirm.isEmpty {
self.createUser()
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment