From bae0060552e652559937287f160a550622a5cab8 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 14 Jun 2016 19:59:45 -0700 Subject: [PATCH] add a timer to retrieve the network list every second. --- ZeroTier One/ShowNetworksViewController.swift | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/ZeroTier One/ShowNetworksViewController.swift b/ZeroTier One/ShowNetworksViewController.swift index ed2479d16..d9a4291a0 100644 --- a/ZeroTier One/ShowNetworksViewController.swift +++ b/ZeroTier One/ShowNetworksViewController.swift @@ -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 {