Tunnels list: Update when a tunnel is added

This commit is contained in:
Roopesh Chander 2018-10-23 17:41:37 +05:30
parent 0324cebe2b
commit e8350b689f
2 changed files with 19 additions and 2 deletions

View File

@ -30,8 +30,11 @@ class TunnelsListTableViewController: UITableViewController {
TunnelsManager.create { [weak self] tunnelsManager in TunnelsManager.create { [weak self] tunnelsManager in
guard let tunnelsManager = tunnelsManager else { return } guard let tunnelsManager = tunnelsManager else { return }
self?.tunnelsManager = tunnelsManager if let s = self {
self?.tableView.reloadData() 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 { class TunnelsListTableViewCell: UITableViewCell {
static let id: String = "TunnelsListTableViewCell" static let id: String = "TunnelsListTableViewCell"
var tunnelName: String { var tunnelName: String {

View File

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