Skip to content

Commit

Permalink
added in Watch Session and Crown support code
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiralArm Consulting Ltd committed Jan 29, 2019
1 parent 83036cd commit 87c8d52
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions ios/WatchTips Extension/InterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController, WKCrownDelegate {
class InterfaceController: WKInterfaceController, WCSessionDelegate, WKCrownDelegate {


var tipAmount: Int = 10
Expand All @@ -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()

}

Expand All @@ -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()
}
Expand All @@ -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(){

Expand All @@ -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)
}
}


Expand Down

0 comments on commit 87c8d52

Please sign in to comment.