Tunnels list: Update when a tunnel is added

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-10-23 17:41:37 +05:30
parent 05efb453f3
commit 20b49518da
2 changed files with 19 additions and 2 deletions

View File

@ -30,8 +30,11 @@ class TunnelsListTableViewController: UITableViewController {
TunnelsManager.create { [weak self] tunnelsManager in
guard let tunnelsManager = tunnelsManager else { return }
self?.tunnelsManager = tunnelsManager
self?.tableView.reloadData()
if let s = self {
tunnelsManager.delegate = s
s.tunnelsManager = tunnelsManager
s.tableView.reloadData()
}
}
}
@ -78,6 +81,14 @@ extension TunnelsListTableViewController {
}
}
// MARK: TunnelsManagerDelegate
extension TunnelsListTableViewController: TunnelsManagerDelegate {
func tunnelsAdded(atIndex index: Int, numberOfTunnels: Int) {
self.tableView.insertRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
class TunnelsListTableViewCell: UITableViewCell {
static let id: String = "TunnelsListTableViewCell"
var tunnelName: String {

View File

@ -28,9 +28,14 @@ class TunnelContainer {
}
}
protocol TunnelsManagerDelegate: class {
func tunnelsAdded(atIndex: Int, numberOfTunnels: Int)
}
class TunnelsManager {
var tunnels: [TunnelContainer]
weak var delegate: TunnelsManagerDelegate? = nil
enum TunnelsManagerError: Error {
case tunnelsUninitialized
@ -56,6 +61,7 @@ class TunnelsManager {
}
let tunnel = TunnelContainer(tunnel: tunnelProvider, index: 0)
tunnels.insert(tunnel, at: 0)
delegate?.tunnelsAdded(atIndex: 0, numberOfTunnels: 1)
completionHandler(nil)
}