Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
Improve syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Aug 15, 2016
1 parent c84546f commit ec88c02
Show file tree
Hide file tree
Showing 13 changed files with 620 additions and 620 deletions.
130 changes: 65 additions & 65 deletions Gulps WatchKit Extension/ComplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,75 @@ import ClockKit

class ComplicationController: NSObject, CLKComplicationDataSource {

func requestedUpdateDidBegin() {
let server = CLKComplicationServer.sharedInstance()
guard let activeComplications = server.activeComplications else { return }
activeComplications.forEach { server.reloadTimelineForComplication($0) }
}
func requestedUpdateDidBegin() {
let server = CLKComplicationServer.sharedInstance()
guard let activeComplications = server.activeComplications else { return }
activeComplications.forEach { server.reloadTimelineForComplication($0) }
}

func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
if complication.family == .UtilitarianSmall {
let smallFlat = CLKComplicationTemplateUtilitarianSmallFlat()
smallFlat.textProvider = CLKSimpleTextProvider(text: "42%")
smallFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
smallFlat.tintColor = .mainColor()
handler(smallFlat)
} else if complication.family == .UtilitarianLarge {
let largeFlat = CLKComplicationTemplateUtilitarianLargeFlat()
largeFlat.textProvider = CLKSimpleTextProvider(text: "Goal: 42%", shortText:"42%")
largeFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
largeFlat.tintColor = .mainColor()
handler(largeFlat)
} else if complication.family == .CircularSmall {
let circularSmall = CLKComplicationTemplateCircularSmallRingImage()
circularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
circularSmall.ringStyle = .Closed
circularSmall.tintColor = .mainColor()
handler(circularSmall)
} else if complication.family == .ModularSmall {
let modularSmall = CLKComplicationTemplateModularSmallRingImage()
modularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
modularSmall.ringStyle = .Closed
modularSmall.tintColor = .mainColor()
handler(modularSmall)
}
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
if complication.family == .UtilitarianSmall {
let smallFlat = CLKComplicationTemplateUtilitarianSmallFlat()
smallFlat.textProvider = CLKSimpleTextProvider(text: "42%")
smallFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
smallFlat.tintColor = .mainColor()
handler(smallFlat)
} else if complication.family == .UtilitarianLarge {
let largeFlat = CLKComplicationTemplateUtilitarianLargeFlat()
largeFlat.textProvider = CLKSimpleTextProvider(text: "Goal: 42%", shortText:"42%")
largeFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
largeFlat.tintColor = .mainColor()
handler(largeFlat)
} else if complication.family == .CircularSmall {
let circularSmall = CLKComplicationTemplateCircularSmallRingImage()
circularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
circularSmall.ringStyle = .Closed
circularSmall.tintColor = .mainColor()
handler(circularSmall)
} else if complication.family == .ModularSmall {
let modularSmall = CLKComplicationTemplateModularSmallRingImage()
modularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
modularSmall.ringStyle = .Closed
modularSmall.tintColor = .mainColor()
handler(modularSmall)
}
}

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {
let percentage = WatchEntryHelper.sharedHelper.percentage() ?? 0
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {
let percentage = WatchEntryHelper.sharedHelper.percentage() ?? 0

if complication.family == .UtilitarianSmall {
let smallFlat = CLKComplicationTemplateUtilitarianSmallFlat()
smallFlat.textProvider = CLKSimpleTextProvider(text: "\(percentage)%")
smallFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
smallFlat.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: smallFlat))
} else if complication.family == .UtilitarianLarge {
let largeFlat = CLKComplicationTemplateUtilitarianLargeFlat()
largeFlat.textProvider = CLKSimpleTextProvider(text: "Goal: \(percentage)%", shortText: "\(percentage)%")
largeFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
largeFlat.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: largeFlat))
} else if complication.family == .CircularSmall {
let circularSmall = CLKComplicationTemplateCircularSmallRingImage()
circularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
circularSmall.ringStyle = .Closed
circularSmall.fillFraction = Float(percentage) / 100.0
circularSmall.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: circularSmall))
} else if complication.family == .ModularSmall {
let modularSmall = CLKComplicationTemplateModularSmallRingImage()
modularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
modularSmall.ringStyle = .Closed
modularSmall.fillFraction = Float(percentage) / 100.0
modularSmall.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: modularSmall))
}
if complication.family == .UtilitarianSmall {
let smallFlat = CLKComplicationTemplateUtilitarianSmallFlat()
smallFlat.textProvider = CLKSimpleTextProvider(text: "\(percentage)%")
smallFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
smallFlat.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: smallFlat))
} else if complication.family == .UtilitarianLarge {
let largeFlat = CLKComplicationTemplateUtilitarianLargeFlat()
largeFlat.textProvider = CLKSimpleTextProvider(text: "Goal: \(percentage)%", shortText: "\(percentage)%")
largeFlat.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
largeFlat.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: largeFlat))
} else if complication.family == .CircularSmall {
let circularSmall = CLKComplicationTemplateCircularSmallRingImage()
circularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
circularSmall.ringStyle = .Closed
circularSmall.fillFraction = Float(percentage) / 100.0
circularSmall.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: circularSmall))
} else if complication.family == .ModularSmall {
let modularSmall = CLKComplicationTemplateModularSmallRingImage()
modularSmall.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "complication")!)
modularSmall.ringStyle = .Closed
modularSmall.fillFraction = Float(percentage) / 100.0
modularSmall.tintColor = .mainColor()
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: modularSmall))
}

// MARK: - Time Travel
}

func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
handler(.None)
}
// MARK: - Time Travel

func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
handler(.None)
}
}
102 changes: 51 additions & 51 deletions Gulps WatchKit Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,69 @@ import ClockKit

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

lazy var notificationCenter: NSNotificationCenter = {
return NSNotificationCenter.defaultCenter()
}()
lazy var notificationCenter: NSNotificationCenter = {
return NSNotificationCenter.defaultCenter()
}()

func applicationDidFinishLaunching() {
setupWatchConnectivity()
setupNotificationCenter()
}
func applicationDidFinishLaunching() {
setupWatchConnectivity()
setupNotificationCenter()
}

// MARK: - Notification Center
// MARK: - Notification Center

private func setupNotificationCenter() {
notificationCenter.addObserverForName(NotificationWatchGulpAdded, object: nil, queue: nil) {
(notification: NSNotification) in
self.sendApplicationContext()
self.reloadComplications()
}
private func setupNotificationCenter() {
notificationCenter.addObserverForName(NotificationWatchGulpAdded, object: nil, queue: nil) {
(notification: NSNotification) in
self.sendApplicationContext()
self.reloadComplications()
}
}

// MARK: - Watch Connectivity

private func setupWatchConnectivity() {
guard WCSession.isSupported() else {
return
}
// MARK: - Watch Connectivity

let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
private func setupWatchConnectivity() {
guard WCSession.isSupported() else {
return
}

private func sendApplicationContext() {
guard WCSession.isSupported() else {
return
}
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}

private func sendApplicationContext() {
guard WCSession.isSupported() else {
return
}

do {
let context = WatchEntryHelper.sharedHelper.applicationContext()
try WCSession.defaultSession().updateApplicationContext(context)
} catch {
print("Unable to send cache data to WCSession: \(error)")
}
do {
let context = WatchEntryHelper.sharedHelper.applicationContext()
try WCSession.defaultSession().updateApplicationContext(context)
} catch {
print("Unable to send cache data to WCSession: \(error)")
}
}

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
if let goal = applicationContext[Constants.Gulp.Goal.key()] as? Double,
let current = applicationContext[Constants.WatchContext.Current.key()] as? Double,
let small = applicationContext[Constants.Gulp.Small.key()] as? Double,
let big = applicationContext[Constants.Gulp.Big.key()] as? Double {
WatchEntryHelper.sharedHelper.saveSettings(goal: goal, current: current, small: small, big: big)
NSNotificationCenter.defaultCenter().postNotificationName(NotificationContextReceived, object: nil)
self.reloadComplications()
}
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
if let goal = applicationContext[Constants.Gulp.Goal.key()] as? Double,
let current = applicationContext[Constants.WatchContext.Current.key()] as? Double,
let small = applicationContext[Constants.Gulp.Small.key()] as? Double,
let big = applicationContext[Constants.Gulp.Big.key()] as? Double {
WatchEntryHelper.sharedHelper.saveSettings(goal: goal, current: current, small: small, big: big)
NSNotificationCenter.defaultCenter().postNotificationName(NotificationContextReceived, object: nil)
self.reloadComplications()
}
}

func reloadComplications() {
dispatch_async(dispatch_get_main_queue()) {
if let complications: [CLKComplication] = CLKComplicationServer.sharedInstance().activeComplications {
complications.forEach({
(complication: CLKComplication) in
CLKComplicationServer.sharedInstance().reloadTimelineForComplication(complication)
})
}
}
func reloadComplications() {
dispatch_async(dispatch_get_main_queue()) {
if let complications: [CLKComplication] = CLKComplicationServer.sharedInstance().activeComplications {
complications.forEach({
(complication: CLKComplication) in
CLKComplicationServer.sharedInstance().reloadTimelineForComplication(complication)
})
}
}
}
}
26 changes: 13 additions & 13 deletions Gulps WatchKit Extension/GlanceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import Foundation

class GlanceController: WKInterfaceController {

@IBOutlet weak var percentageLabel: WKInterfaceLabel!

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
@IBOutlet weak var percentageLabel: WKInterfaceLabel!

override func willActivate() {
super.willActivate()
if let percentage = WatchEntryHelper.sharedHelper.percentage() {
percentageLabel.setText("\(percentage)%")
}
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}

override func didDeactivate() {
super.didDeactivate()
override func willActivate() {
super.willActivate()
if let percentage = WatchEntryHelper.sharedHelper.percentage() {
percentageLabel.setText("\(percentage)%")
}
}

override func didDeactivate() {
super.didDeactivate()
}

}
Loading

0 comments on commit ec88c02

Please sign in to comment.