Skip to content

Instantly share code, notes, and snippets.

@foffer
Created September 16, 2014 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foffer/942e63cf2b19ebf800c8 to your computer and use it in GitHub Desktop.
Save foffer/942e63cf2b19ebf800c8 to your computer and use it in GitHub Desktop.
class MainMenuViewController: UICollectionViewController, SharedLightsNetworkContextInterface {
let reuseIdentifier = "menuItemCell"
var localNetworkContext = LFXNetworkContext()
var lights = NSArray()
var delegate: SharedLightsReceiver?
override func viewDidLoad() {
collectionView!.registerClass(MainMenuCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView!.backgroundColor = UIColor.clearColor()
title = "Searching for Lights"
var sharedLightsManager = SharedLightsManager()
sharedLightsManager.delegate = self
sharedLightsManager.loadNetworkContext()
}
func loadNetworkContext(networkContext: LFXNetworkContext) {
localNetworkContext = networkContext
println("\(localNetworkContext)")
/*
This returns <LFXNetworkContext: 0x7fa692731b40> {name = LAN, isConnected = 0}, so the object is correctly created, however,
in the SharedLightsManger, it doesn't connect to the network, or call any of the callback methods provided by the LFX Framework.
Can this LFX network context and its method not be extracted into a seperate class like this? Or does it have to be created on each viewcontroller that is responsible for
controlling lights? It would be really convenient to have it in a 'master' class and delegate the calls out from that*/
}
class SharedLightsManager: NSObject, LFXNetworkContextObserver, LFXLightCollectionObserver, LFXLightObserver, SharedLightsReceiver {
var localNetworkContext = LFXNetworkContext()
var lights = NSArray()
var delegate: SharedLightsNetworkContextInterface?
func loadNetworkContext()
{
println("Setting up local network context")
localNetworkContext = LFXClient.sharedClient().localNetworkContext
localNetworkContext.addNetworkContextObserver(self)
localNetworkContext.allLightsCollection.addLightCollectionObserver(self)
delegate?.loadNetworkContext(localNetworkContext)
updateLights()
}
func updateLights() -> [AnyObject]
{
if localNetworkContext.isConnected == true
{
lights = localNetworkContext.allLightsCollection.lights
println("Returning \(lights) as lights")
return lights
}
else { println("Nothing found, returning []"); return [] }
}
func updateTitle()
{
var statusString: NSString = localNetworkContext.isConnected ? "Connected" : "Searching"
}
func networkContextDidConnect(networkContext: LFXNetworkContext)
{
println("Network Context Did Connect")
updateTitle()
}
func networkContextDidDisconnect(networkContext: LFXNetworkContext)
{
println("Network Context Did Disconnect")
}
func light(light: LFXLight!, didChangeLabel label: String!)
{
println("\(light) Did change label: \(label)")
var itemIndex: Int = lights.indexOfObject(light)
}
func lightCollection(lightCollection: LFXLightCollection!, didAddLight light: LFXLight!)
{
light.addLightObserver(self)
println("Light Collection: \(lightCollection) Did Add Light: \(light)")
updateLights()
}
func lightCollection(lightCollection: LFXLightCollection!, didRemoveLight light: LFXLight!)
{
light.removeLightObserver(self)
println("Light Collection: \(lightCollection) Did Remove Light: \(light)")
updateLights()
}
func light(light: LFXLight!, didChangeColor color: LFXHSBKColor!)
{
println("Light: \(light), changed color to: \(color)")
var itemIndex: Int = lights.indexOfObject(light)
updateLights()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment