Commit f1be323a by Dima Bart

Implement orders view. Add delegate to handle pushing orders.

parent fcf59bda
......@@ -9,7 +9,8 @@
/* Begin PBXBuildFile section */
9A102CEE1CDAA6080026CC43 /* ActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A102CED1CDAA6080026CC43 /* ActionCell.swift */; };
9A102CF01CDB7C0F0026CC43 /* OrdersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A102CEF1CDB7C0F0026CC43 /* OrdersViewController.swift */; };
9A102CFE1CDB86920026CC43 /* OrderCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A102CFC1CDB86920026CC43 /* OrderCell.swift */; };
9A102CFE1CDB86920026CC43 /* LineItemCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A102CFC1CDB86920026CC43 /* LineItemCell.swift */; };
9A102D111CDBCD320026CC43 /* AuthenticationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A102D101CDBCD320026CC43 /* AuthenticationDelegate.swift */; };
9A9C03071CD8F9AC00AE79BD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9C03061CD8F9AC00AE79BD /* AppDelegate.swift */; };
9A9C03091CD8F9AC00AE79BD /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9C03081CD8F9AC00AE79BD /* LoginViewController.swift */; };
9A9C030C1CD8F9AC00AE79BD /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9A9C030A1CD8F9AC00AE79BD /* Main.storyboard */; };
......@@ -69,7 +70,8 @@
/* Begin PBXFileReference section */
9A102CED1CDAA6080026CC43 /* ActionCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionCell.swift; sourceTree = "<group>"; };
9A102CEF1CDB7C0F0026CC43 /* OrdersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrdersViewController.swift; sourceTree = "<group>"; };
9A102CFC1CDB86920026CC43 /* OrderCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderCell.swift; sourceTree = "<group>"; };
9A102CFC1CDB86920026CC43 /* LineItemCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineItemCell.swift; sourceTree = "<group>"; };
9A102D101CDBCD320026CC43 /* AuthenticationDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationDelegate.swift; sourceTree = "<group>"; };
9A9C03031CD8F9AC00AE79BD /* Sample App Customers.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Sample App Customers.app"; sourceTree = BUILT_PRODUCTS_DIR; };
9A9C03061CD8F9AC00AE79BD /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9A9C03081CD8F9AC00AE79BD /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
......@@ -98,12 +100,20 @@
9A102CE81CDAA5F00026CC43 /* Cells */ = {
isa = PBXGroup;
children = (
9A102CFC1CDB86920026CC43 /* OrderCell.swift */,
9A102CFC1CDB86920026CC43 /* LineItemCell.swift */,
9A102CED1CDAA6080026CC43 /* ActionCell.swift */,
);
name = Cells;
sourceTree = "<group>";
};
9A102D0F1CDBCD210026CC43 /* Protcols */ = {
isa = PBXGroup;
children = (
9A102D101CDBCD320026CC43 /* AuthenticationDelegate.swift */,
);
name = Protcols;
sourceTree = "<group>";
};
9A9C02FA1CD8F9AC00AE79BD = {
isa = PBXGroup;
children = (
......@@ -126,6 +136,7 @@
isa = PBXGroup;
children = (
9A9C03061CD8F9AC00AE79BD /* AppDelegate.swift */,
9A102D0F1CDBCD210026CC43 /* Protcols */,
9A9C03391CD920F600AE79BD /* Extensions */,
9A102CE81CDAA5F00026CC43 /* Cells */,
9A9C031A1CD8FA0C00AE79BD /* View Controllers */,
......@@ -290,8 +301,9 @@
9A9C033B1CD921D700AE79BD /* BUYClient+Extensions.swift in Sources */,
9A102CF01CDB7C0F0026CC43 /* OrdersViewController.swift in Sources */,
9A102CEE1CDAA6080026CC43 /* ActionCell.swift in Sources */,
9A102D111CDBCD320026CC43 /* AuthenticationDelegate.swift in Sources */,
9A9C031C1CD8FC6A00AE79BD /* AccountViewController.swift in Sources */,
9A102CFE1CDB86920026CC43 /* OrderCell.swift in Sources */,
9A102CFE1CDB86920026CC43 /* LineItemCell.swift in Sources */,
9A9C031E1CD8FD2300AE79BD /* SignupViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
......@@ -25,6 +25,7 @@
//
import UIKit
import Buy
class AccountViewController: UIViewController {
......@@ -40,10 +41,12 @@ class AccountViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.identifier {
case let login where login == "loginSegue":
self.loginViewController = segue.destinationViewController as! LoginViewController
self.loginViewController = segue.destinationViewController as! LoginViewController
self.loginViewController.delegate = self
case let signup where signup == "signupSegue":
self.signupViewController = segue.destinationViewController as! SignupViewController
self.signupViewController.delegate = self
default:
break
......@@ -69,3 +72,19 @@ class AccountViewController: UIViewController {
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?) {
print("Failed to authenticate customer: \(error)")
}
}
\ 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?)
}
\ No newline at end of file
......@@ -219,7 +219,7 @@
<!--Orders-->
<scene sceneID="Kny-Kh-xPd">
<objects>
<viewController id="gsn-tk-U20" customClass="OrdersViewController" customModule="Sample_App_Customers" customModuleProvider="target" sceneMemberID="viewController">
<viewController storyboardIdentifier="ordersViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="gsn-tk-U20" customClass="OrdersViewController" customModule="Sample_App_Customers" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="A7z-xl-YN2"/>
<viewControllerLayoutGuide type="bottom" id="61m-HD-4Dc"/>
......@@ -228,9 +228,53 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="0Fn-mj-l5t">
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="70" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="0Fn-mj-l5t">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="cell" rowHeight="70" id="6md-wi-AGU" customClass="LineItemCell" customModule="Sample_App_Customers" customModuleProvider="target">
<rect key="frame" x="0.0" y="114" width="600" height="70"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="6md-wi-AGU" id="vsD-MK-pMi">
<rect key="frame" x="0.0" y="0.0" width="600" height="69"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Cool Shoes" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yKe-Oz-Zb0">
<rect key="frame" x="15" y="15" width="88" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Model B Shoe with red laces" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iif-N0-Vdu">
<rect key="frame" x="15" y="36" width="183" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="755" verticalCompressionResistancePriority="755" text="$843.84" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="tc7-8s-fgw">
<rect key="frame" x="518" y="24" width="67" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.39108031988143921" green="0.71921610832214355" blue="0.21925181150436401" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="iif-N0-Vdu" firstAttribute="top" secondItem="yKe-Oz-Zb0" secondAttribute="bottom" id="2TM-j7-I0x"/>
<constraint firstItem="tc7-8s-fgw" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="yKe-Oz-Zb0" secondAttribute="trailing" constant="15" id="HtJ-bo-lzd"/>
<constraint firstAttribute="trailing" secondItem="tc7-8s-fgw" secondAttribute="trailing" constant="15" id="QkA-Ok-IlL"/>
<constraint firstItem="tc7-8s-fgw" firstAttribute="centerY" secondItem="vsD-MK-pMi" secondAttribute="centerY" id="Rzi-Md-fU4"/>
<constraint firstItem="yKe-Oz-Zb0" firstAttribute="centerY" secondItem="vsD-MK-pMi" secondAttribute="centerY" constant="-9" id="Xd9-l2-t22"/>
<constraint firstItem="tc7-8s-fgw" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="iif-N0-Vdu" secondAttribute="trailing" constant="15" id="s76-v4-jWb"/>
<constraint firstItem="iif-N0-Vdu" firstAttribute="leading" secondItem="yKe-Oz-Zb0" secondAttribute="leading" id="wXD-0W-DDz"/>
<constraint firstItem="yKe-Oz-Zb0" firstAttribute="leading" secondItem="vsD-MK-pMi" secondAttribute="leadingMargin" constant="7" id="ysW-l5-ayq"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="priceLabel" destination="tc7-8s-fgw" id="Icp-Z6-TGU"/>
<outlet property="subtitleLabel" destination="iif-N0-Vdu" id="5ap-bO-jbP"/>
<outlet property="titleLabel" destination="yKe-Oz-Zb0" id="CXd-S3-dnM"/>
</connections>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="gsn-tk-U20" id="I04-cV-mh5"/>
<outlet property="delegate" destination="gsn-tk-U20" id="nwo-WG-rYn"/>
......
//
// 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)"
}
}
......@@ -29,12 +29,14 @@ 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!
var email: String { return self.emailField.text ?? "" }
var password: String { return self.passwordField.text ?? "" }
private var email: String { return self.emailField.text ?? "" }
private var password: String { return self.passwordField.text ?? "" }
// ----------------------------------
// MARK: - View Loading -
......@@ -58,10 +60,13 @@ class LoginViewController: UITableViewController {
BUYClient.sharedClient.loginCustomerWithCredentials(credentials) { (customer, token, error) in
self.actionCell.loading = false
if let _ = customer {
if let customer = customer,
let token = token {
self.clear()
self.delegate?.authenticationDidSucceedForCustomer(customer, withToken: token)
} else {
self.delegate?.authenticationDidFailWithError(error)
}
print("Customer: \(customer), Token: \(token), Error: \(error)")
}
}
......
//
// OrderCell.swift
// Sample App Customers
//
// Created by Dima Bart on 2016-05-05.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
import UIKit
class OrderCell: UITableViewCell {
}
......@@ -33,6 +33,13 @@ class OrdersViewController: UIViewController {
private var orders = [BUYOrder]()
private let dateFormatter: NSDateFormatter = {
let f = NSDateFormatter()
f.dateStyle = .MediumStyle
f.timeStyle = .NoStyle
return f
}()
// ----------------------------------
// MARK: - View Loading -
//
......@@ -53,3 +60,48 @@ class OrdersViewController: UIViewController {
}
}
}
// ----------------------------------
// 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
}
}
\ No newline at end of file
......@@ -29,6 +29,8 @@ 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!
......@@ -36,11 +38,11 @@ class SignupViewController: UITableViewController {
@IBOutlet private weak var passwordConfirmField: UITextField!
@IBOutlet private weak var actionCell: ActionCell!
var firstName: String { return self.firstNameField.text ?? "" }
var lastName: String { return self.lastNameField.text ?? "" }
var email: String { return self.emailField.text ?? "" }
var password: String { return self.passwordField.text ?? "" }
var passwordConfirm: String { return self.passwordConfirmField.text ?? "" }
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 -
......@@ -67,10 +69,13 @@ class SignupViewController: UITableViewController {
BUYClient.sharedClient.createCustomerWithCredentials(credentials) { (customer, token, error) in
self.actionCell.loading = false
if let _ = customer {
if let customer = customer,
let token = token {
self.clear()
self.delegate?.authenticationDidSucceedForCustomer(customer, withToken: token)
} else {
self.delegate?.authenticationDidFailWithError(error)
}
print("Customer: \(customer), Token: \(token), Error: \(error)")
}
}
......
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