Skip to content

Commit

Permalink
Fix crashes and inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Gouline committed Jul 2, 2018
1 parent e0ff92f commit fe49adb
Show file tree
Hide file tree
Showing 13 changed files with 848 additions and 813 deletions.
10 changes: 5 additions & 5 deletions kebabpos/KebabPos/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="633.66666666666663"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" editable="NO" text="Log" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="fat-dE-x2l">
<rect key="frame" x="0.0" y="0.0" width="414" height="633.66666666666663"/>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="fat-dE-x2l">
<rect key="frame" x="0.0" y="0.0" width="414" height="634"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<fontDescription key="fontDescription" name="Courier" family="Courier" pointSize="12"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
</subviews>
Expand Down Expand Up @@ -595,10 +595,10 @@
<connections>
<outlet property="btnConnection" destination="guJ-P5-LFD" id="FE9-Wc-iq9"/>
<outlet property="lblExtraOption" destination="ZfR-qu-WXC" id="MB8-ug-kGN"/>
<outlet property="lblFlowStatus" destination="3jY-SR-5p4" id="uHY-CS-KYR"/>
<outlet property="lblPosAddress" destination="UhR-bd-bfC" id="6ac-D9-Whq"/>
<outlet property="lblPosId" destination="WQc-34-pqQ" id="c1t-XD-s61"/>
<outlet property="lblStatus" destination="gn2-HF-yej" id="fdZ-pT-g7T"/>
<outlet property="lbl_flowStatus" destination="3jY-SR-5p4" id="KB9-kE-Psg"/>
<outlet property="segmentExtraAmount" destination="GLt-dz-UEd" id="CVj-33-eSY"/>
<outlet property="swchReceiptFromEFTPos" destination="9Qd-BI-WpA" id="MYj-wu-0y1"/>
<outlet property="swchSignatureFromEFTPos" destination="r2S-gH-L8p" id="Yx7-dd-Kd3"/>
Expand Down Expand Up @@ -639,7 +639,7 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Enter Pos ID" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="hsd-rZ-tuO">
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="KEBABPOS1" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="hsd-rZ-tuO">
<rect key="frame" x="187" y="7" width="207" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="N0k-ms-Iku"/>
Expand Down
2 changes: 1 addition & 1 deletion kebabpos/KebabPos/Logics/Settings/SettingsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SettingsProvider {

func restoreDefaultValues() {
if (UserDefaults.standard.value(forKey: SettingKeys.posId.rawValue) == nil) {
posId = "ACMEPOS3TEST"
posId = "KEBABPOS1"
eftposAddress = "emulator-prod.herokuapp.com"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ class ConnectionViewController: UITableViewController, NotificationListener {

override func viewDidLoad() {
super.viewDidLoad()

registerForEvents(appEvents: [.connectionStatusChanged, .pairingFlowChanged, .transactionFlowStateChanged])
txtPosId.text = KebabApp.current.settings.posId
txtPosAddress.text = KebabApp.current.settings.eftposAddress
}

@IBAction func pairButtonClicked(_ sender: Any) {
if (KebabApp.current.client.state.status != .unpaired )
{
showAlert(title: "Can not start pairing", message: "SPI Client status: \(KebabApp.current.client.state.status.name)")
if (KebabApp.current.client.state.status != .unpaired ) {
showAlert(title: "Cannot start pairing", message: "SPI Client status: \(KebabApp.current.client.state.status.name)")
return
}

KebabApp.current.settings.posId = txtPosId.text
KebabApp.current.client.posId = txtPosId.text

Expand All @@ -50,60 +51,52 @@ class ConnectionViewController: UITableViewController, NotificationListener {

@objc
func onNotificationArrived(notification: NSNotification) {
switch notification.name.rawValue {
case AppEvent.connectionStatusChanged.rawValue:
if let state = notification.object as? SPIState {
printStatusAndAction(state)
}
case AppEvent.pairingFlowChanged.rawValue:
if let state = notification.object as? SPIState {
printStatusAndAction(state)
}
case AppEvent.transactionFlowStateChanged.rawValue:
if let state = notification.object as? SPIState {
printStatusAndAction(state)
DispatchQueue.main.async {
switch notification.name.rawValue {
case AppEvent.connectionStatusChanged.rawValue,
AppEvent.pairingFlowChanged.rawValue,
AppEvent.transactionFlowStateChanged.rawValue:

if let state = notification.object as? SPIState {
self.printStatusAndAction(state)
}
default:
return
}
default:
return
}
}

func printStatusAndAction(_ state: SPIState?) {
SPILogMsg("printStatusAndAction \(String(describing: state))")

guard let state = state else { return }

switch state.status {
case .unpaired:
switch state.flow {
case .idle:

break
case .pairing:
KebabApp.current.client.ackFlowEndedAndBack { (alreadyInIdle, state) in
print("setting to idle :\(alreadyInIdle) state:\(String(describing: state))")
print("Setting to idle=\(alreadyInIdle) state=\(String(describing: state))")
if let state = state {
self.showPairing(state)
}
}

default:
showError("# .. Unexpected Flow .. \(KebabApp.current.client.state.flow.rawValue)")
showError("Unexpected flow: \(KebabApp.current.client.state.flow.rawValue)")
}

case .pairedConnecting, .pairedConnected:
SPILogMsg("status .connected, flow=\(state.flow.rawValue)")

SPILogMsg("Status .connected, flow=\(state.flow.rawValue)")
switch state.flow {
case .idle:
break

case .transaction:
//No transaction will be in this screen
case .idle, .transaction:
break
case .pairing: // Paired, Pairing - we have just finished the pairing flow. OK to ack.
showPairing(KebabApp.current.client.state)
}

}
}

Expand All @@ -113,23 +106,24 @@ class ConnectionViewController: UITableViewController, NotificationListener {
guard let pairingFlowState = state.pairingFlowState else {
return showError("Missing pairingFlowState \(state)")
}
let alertVC = UIAlertController(title: "EFTPOS PAIRING PROCESS", message: pairingFlowState.message, preferredStyle: .alert)

let alertVC = UIAlertController(title: "EFTPOS Pairing Process", message: pairingFlowState.message, preferredStyle: .alert)

if pairingFlowState.isAwaitingCheckFromPos {
SPILogMsg("# [pair_confirm] - confirm the code matches")

alertVC.addAction(UIAlertAction(title: "NO", style: .cancel, handler: { (_) in
alertVC.addAction(UIAlertAction(title: "No", style: .cancel, handler: { (_) in
self.pairingCancel()
}))

alertVC.addAction(UIAlertAction(title: "YES", style: .default, handler: { _ in
alertVC.addAction(UIAlertAction(title: "Yes", style: .default, handler: { _ in
KebabApp.current.client.pairingConfirmCode()
}))

} else if !pairingFlowState.isFinished {
SPILogMsg("# [pair_cancel] - cancel pairing process")

alertVC.addAction(UIAlertAction(title: "CANCEL", style: .cancel, handler: { _ in
alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
self.pairingCancel()
}))

Expand Down Expand Up @@ -158,7 +152,7 @@ class ConnectionViewController: UITableViewController, NotificationListener {

func showError(_ msg: String, completion: (() -> Swift.Void)? = nil) {
SPILogMsg("ERROR: \(msg)")
showAlert(title: "ERROR!", message: msg)
showAlert(title: "Error", message: msg)
}

func appendReceipt(_ msg: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import SPIClient_iOS

extension MainViewController {

func stateChanged(state: SPIState? = nil) {
guard let state = state ?? KebabApp.current.client.state else { return }

updateUIFlowInfo(state: state)
printFlowInfo(state: state)
selectActions(state: state)
}

func updateUIFlowInfo(state: SPIState) {
lblStatus.font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
lblStatus.textColor = UIColor.darkGray

btnConnection.title = "Pair"
switch state.status {
case .pairedConnected:
Expand All @@ -29,55 +37,44 @@ extension MainViewController {
lblStatus.text = "Not Connected"
break
}

lblPosId.text = KebabApp.current.settings.posId
lblPosAddress.text = KebabApp.current.settings.eftposAddress
lbl_flowStatus.text = state.flow.name
lblFlowStatus.text = state.flow.name
self.title = lblStatus.text
}

func stateChanged(state: SPIState? = nil) {
guard let state = state ?? KebabApp.current.client.state else { return }
updateUIFlowInfo(state: state)
printFlowInfo(state: state)
selectActions(state: state)
}

func selectActions(state: SPIState) {
if client.state.flow == .idle {
//clear UI
if (self.navigationController?.topViewController == self) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
if (client.state.flow == .pairing) {
if (client.state.pairingFlowState.isAwaitingCheckFromPos) {
switch state.flow {
case .idle:
clear()
case .pairing:
if (state.pairingFlowState.isAwaitingCheckFromPos) {
client.pairingConfirmCode()
}
if (!client.state.pairingFlowState.isFinished) {
} else if (!state.pairingFlowState.isFinished) {
pairCancel()
}
if (client.state.pairingFlowState.isFinished) {
} else if (state.pairingFlowState.isFinished) {
ok()
}
}
if client.state.flow == .transaction, let txState = client.state.txFlowState {
if (txState.isAwaitingSignatureCheck) {
txSignature()
}
if (txState.isAwaitingPhoneForAuth) {
case .transaction:
if (state.txFlowState.isAwaitingSignatureCheck) {
txSignature()
} else if (state.txFlowState.isAwaitingPhoneForAuth) {
txAuthCode()
}
}
if (client.state.flow == .transaction) {
if (!client.state.txFlowState.isFinished && !client.state.txFlowState.isAttemptingToCancel) {
txCancel()
}
if (client.state.txFlowState.isFinished) {
} else if (!state.txFlowState.isFinished && !state.txFlowState.isAttemptingToCancel) {
txCancel()
} else if (state.txFlowState.isFinished) {
ok()
}
}
}

func clear() {
if (self.navigationController?.topViewController == self) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}

func ok() {
client.ackFlowEndedAndBack { (_, state) in
DispatchQueue.main.async {
Expand All @@ -86,27 +83,27 @@ extension MainViewController {
}
}

func txCancel() {
let alertVC = UIAlertController(title: "Message", message: client.state.txFlowState.displayMessage, preferredStyle: .alert)
func pairCancel() {
let alertVC = UIAlertController(title: "Pairing", message: client.state.txFlowState.displayMessage, preferredStyle: .alert)
let cancelBtn = UIAlertAction(title: "Cancel", style: .default) { (_) in
self.client.cancelTransaction()
self.client.pairingCancel()
}
alertVC.addAction(cancelBtn)
showAlert(alertController: alertVC)
}

func pairCancel() {
let alertVC = UIAlertController(title: "Message", message: client.state.txFlowState.displayMessage, preferredStyle: .alert)
func txCancel() {
let alertVC = UIAlertController(title: "Transaction", message: client.state.txFlowState.displayMessage, preferredStyle: .alert)
let cancelBtn = UIAlertAction(title: "Cancel", style: .default) { (_) in
self.client.pairingCancel()
self.client.cancelTransaction()
}
alertVC.addAction(cancelBtn)
showAlert(alertController: alertVC)
}

func txAuthCode() {
var txtAuthCode: UITextField?
let alertVC = UIAlertController(title: "Message", message: "Submit Phone for Auth Code", preferredStyle: .alert)
let alertVC = UIAlertController(title: "Auth Code", message: "Submit Phone for Auth Code", preferredStyle: .alert)
_ = alertVC.addTextField { (txt) in
txtAuthCode = txt
txt.text = "Enter code"
Expand All @@ -122,14 +119,13 @@ extension MainViewController {
}

func txSignature() {
let alertVC = UIAlertController(title: "Message", message: "Select Action", preferredStyle: .alert)
let alertVC = UIAlertController(title: "Signature", message: "Select Action", preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "Accept Signature", style: .default, handler: { _ in
KebabApp.current.client.acceptSignature(true)
}))

KebabApp.current.client.acceptSignature(true)
}))
alertVC.addAction(UIAlertAction(title: "Decline Signature", style: .default, handler: { _ in
KebabApp.current.client.acceptSignature(false)
}))
KebabApp.current.client.acceptSignature(false)
}))
showAlert(alertController: alertVC)
}

Expand Down
Loading

0 comments on commit fe49adb

Please sign in to comment.