From 87c8d52453d7489ba544214ac58bb881bd6a8f67 Mon Sep 17 00:00:00 2001 From: SpiralArm Consulting Ltd Date: Tue, 29 Jan 2019 08:15:12 +0000 Subject: [PATCH] added in Watch Session and Crown support code --- .../InterfaceController.swift | 85 ++++++++++++++++--- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/ios/WatchTips Extension/InterfaceController.swift b/ios/WatchTips Extension/InterfaceController.swift index 1281caf..1c315c6 100644 --- a/ios/WatchTips Extension/InterfaceController.swift +++ b/ios/WatchTips Extension/InterfaceController.swift @@ -8,9 +8,10 @@ import WatchKit import Foundation +import WatchConnectivity -class InterfaceController: WKInterfaceController, WKCrownDelegate { +class InterfaceController: WKInterfaceController, WCSessionDelegate, WKCrownDelegate { var tipAmount: Int = 10 @@ -27,14 +28,55 @@ class InterfaceController: WKInterfaceController, WKCrownDelegate { @IBOutlet weak var withTip: WKInterfaceLabel! + public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { + // This will be called when the activation of a session finishes. + } + + + func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) { accumulatedCrownDelta = 0.0 } func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) { accumulatedCrownDelta += rotationalDelta - //let threshoold = 0.5 + let threshold = 0.05 + + // do nothing if delta is less that threshold + guard abs(accumulatedCrownDelta) > threshold else { + return + } + + if focusButton == 1 { + if accumulatedCrownDelta > 0 { + splitBetween += 1 + } else { + splitBetween -= 1 + } + if splitBetween < 1 { + splitBetween = 1 + } + if splitBetween > 50 { + splitBetween = 50 + } + } + if focusButton == 2 { + if accumulatedCrownDelta > 0 { + tipAmount += 1 + } else { + tipAmount -= 1 + } + if tipAmount < 0 { + tipAmount = 0 + } + if tipAmount > 100 { + tipAmount = 100 + } + } + + accumulatedCrownDelta = 0 + updateCalc() } @@ -50,7 +92,11 @@ class InterfaceController: WKInterfaceController, WKCrownDelegate { override func willActivate() { // This method is called when watch view controller is about to be visible to user super.willActivate() - + if WCSession.isSupported() { + let session = WCSession.default + session.delegate = self + session.activate() + } crownSequencer.focus() updateCalc() } @@ -63,19 +109,34 @@ class InterfaceController: WKInterfaceController, WKCrownDelegate { // chnage how many the bill is split between @IBAction func showSplitController() { + resetButtonFocus() + currentSplit?.setBackgroundColor(UIColor(red: 0/255, green: 128/255, blue: 255/255, alpha: 1.0)) + focusButton = 1 presentController(withName: "SplitController", context: self) } @IBAction func showTipController() { + resetButtonFocus() + currentTip?.setBackgroundColor(UIColor(red: 0/255, green: 128/255, blue: 255/255, alpha: 1.0)) + focusButton = 2 presentController(withName: "TipController", context: self) } @IBAction func showBillController() { + resetButtonFocus() presentController(withName: "BillController", context: self) } + // reset the focus to the currently selected button + func resetButtonFocus(){ + focusButton = 0 + currentSplit?.setBackgroundColor(UIColor.darkGray) + currentTip?.setBackgroundColor(UIColor.darkGray) + billButton?.setBackgroundColor(UIColor.darkGray) + } + // Update the actual calculations func updateCalc(){ @@ -92,15 +153,15 @@ class InterfaceController: WKInterfaceController, WKCrownDelegate { costEach.setText(String(format: "%.2f", perPerson)) // now see if we can sent the calc data back to the app - //if WCSession.isSupported() { - // let session = WCSession.default() - // let calcInfo = [ - // "tip":"\(tipAmount)", - // "split": "\(splitBetween)", - // "bill": "\(billTotal)" - // ] as [String : Any]; - // session.transferUserInfo(calcInfo) - //} + if WCSession.isSupported() { + let session = WCSession.default + let calcInfo = [ + "tip":"\(tipAmount)", + "split": "\(splitBetween)", + "bill": "\(billTotal)" + ] as [String : Any]; + session.transferUserInfo(calcInfo) + } }