|
// |
|
// LGWatchKitListener.swift |
|
// RemoteCamera |
|
// |
|
// Created by Logan Wright on 11/19/14. |
|
// Copyright (c) 2014 lowriDevs. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
// MARK: Listener Ref |
|
|
|
class LGListenerRef : NSObject { |
|
var lastValue: NSObject? |
|
let callback: (value: NSObject?, key: String) -> Void |
|
init(lastValue: NSObject?, callback: (value: NSObject?, key: String) -> Void) { |
|
self.lastValue = lastValue |
|
self.callback = callback |
|
} |
|
} |
|
|
|
// MARK: Listener |
|
|
|
class LGWatchKitListener: NSObject { |
|
|
|
// MARK: Identifier |
|
|
|
class func setGroupContainerIdentifier(groupContainerIdentifier: String) { |
|
Constants.GroupContainerIdentifier = groupContainerIdentifier |
|
} |
|
|
|
private struct Constants { |
|
static var GroupContainerIdentifier: String = "" |
|
} |
|
|
|
// MARK: Defaults Loader |
|
|
|
private lazy var defaults: NSUserDefaults = { |
|
if countElements(Constants.GroupContainerIdentifier) == 0 { |
|
fatalError("LGWatchKitListener: GroupContainerIdentifier Not Set Before Initializing Listener! You should call LGWatchKitListener.setGroupContainerIdentifier in your AppDelegate in appDidLoad") |
|
} |
|
return NSUserDefaults(suiteName: Constants.GroupContainerIdentifier)! |
|
}() |
|
|
|
// MARK: State Management |
|
|
|
private enum State { |
|
case Listening, Pending |
|
} |
|
|
|
// MARK: Private Properties |
|
|
|
private var state: State = .Pending |
|
private var listenerRefDictionary: [String : LGListenerRef] = [:] |
|
private var timer: NSTimer? |
|
|
|
// MARK: Singleton |
|
|
|
private class func sharedListener() -> LGWatchKitListener { |
|
struct Singleton { |
|
static let instance = LGWatchKitListener() |
|
} |
|
return Singleton.instance |
|
} |
|
|
|
// MARK: Listening Interaction |
|
|
|
class func startListeningForKey(key: String, withCallback callback: (value: NSObject?, key: String) -> Void) { |
|
self.sharedListener().listenerRefDictionary[key] = LGListenerRef(lastValue: nil, callback: callback) |
|
self.startListening() |
|
} |
|
|
|
class func stopListeningForKey(key: String) { |
|
self.sharedListener().listenerRefDictionary.removeValueForKey(key) |
|
} |
|
|
|
class func startListening() { |
|
if self.sharedListener().state == .Pending { |
|
self.sharedListener().state = .Listening |
|
self.sharedListener().timer?.invalidate() |
|
self.sharedListener().timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self.sharedListener(), selector: "timerFired:", userInfo: nil, repeats: true) |
|
} |
|
} |
|
|
|
class func stopListening() { |
|
self.sharedListener().timer?.invalidate() |
|
self.sharedListener().state = .Pending |
|
} |
|
|
|
class func removeAllListeningKeys() { |
|
self.stopListening() |
|
self.sharedListener().listenerRefDictionary = [:] |
|
} |
|
|
|
// MARK: Timer Handling |
|
|
|
func timerFired(timer: NSTimer) { |
|
for (key, listenerRef) in self.listenerRefDictionary { |
|
let currentValue: NSObject? = self.defaults.objectForKey(key) as? NSObject |
|
if currentValue != listenerRef.lastValue { |
|
self.listenerRefDictionary[key]?.lastValue = currentValue |
|
listenerRef.callback(value: currentValue, key: key) |
|
} |
|
} |
|
} |
|
|
|
} |