add a timer to retrieve the network list every second.

This commit is contained in:
Grant Limberg 2016-06-14 19:59:45 -07:00
parent c8750e5812
commit bae0060552

View File

@ -14,6 +14,9 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
var networkList: [Network] = [Network]()
var timer: NSTimer? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
@ -45,10 +48,36 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
override func viewWillAppear() {
super.viewWillAppear()
updateNetworkInfo()
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateNetworkInfo), userInfo: nil, repeats: true)
}
override func viewWillDisappear() {
super.viewWillDisappear()
self.timer?.invalidate()
let filePath = dataFile()
NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
}
func deleteNetworkFromList(nwid: String) {
if let nwid = UInt64(nwid, radix: 16) {
let index = findNetworkWithID(nwid)
if index != NSNotFound {
networkList.removeAtIndex(index)
tableView.reloadData()
}
}
}
func updateNetworkInfo() {
let filePath = self.dataFile()
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
networkList = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
self.networkList = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
ServiceCom.getNetworkList() { (networkList) -> Void in
@ -85,7 +114,7 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
return false
})
self.networkList = networkList
self.tableView.reloadData()
}
@ -93,24 +122,6 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
}
}
override func viewWillDisappear() {
super.viewWillDisappear()
let filePath = dataFile()
NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
}
func deleteNetworkFromList(nwid: String) {
if let nwid = UInt64(nwid, radix: 16) {
let index = findNetworkWithID(nwid)
if index != NSNotFound {
networkList.removeAtIndex(index)
tableView.reloadData()
}
}
}
// NSTableViewDataSource
func numberOfRowsInTableView(tableView: NSTableView) -> Int {