From 0593a77d7ab54a3366d88d788021eeb7cf314684 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 17 Oct 2018 13:47:57 +0530 Subject: [PATCH] Tunnels list: Show data from the tunnels manager --- .../iOS/TunnelsListTableViewController.swift | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index 7900dd2..6fb7799 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -10,6 +10,8 @@ import UIKit class TunnelsListTableViewController: UITableViewController { + var tunnelsManager: TunnelsManager? = nil + init() { super.init(style: .plain) } @@ -23,6 +25,14 @@ class TunnelsListTableViewController: UITableViewController { self.title = "WireGuard" let addButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonTapped(sender:))) self.navigationItem.rightBarButtonItem = addButtonItem + + self.tableView.register(TunnelsListTableViewCell.self, forCellReuseIdentifier: TunnelsListTableViewCell.id) + + TunnelsManager.create { [weak self] tunnelsManager in + guard let tunnelsManager = tunnelsManager else { return } + self?.tunnelsManager = tunnelsManager + self?.tableView.reloadData() + } } @objc func addButtonTapped(sender: UIBarButtonItem!) { @@ -34,10 +44,40 @@ class TunnelsListTableViewController: UITableViewController { extension TunnelsListTableViewController { override func numberOfSections(in tableView: UITableView) -> Int { - return 0 + return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 0 + return (tunnelsManager?.numberOfTunnels() ?? 0) + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: TunnelsListTableViewCell.id, for: indexPath) as! TunnelsListTableViewCell + if let tunnelsManager = tunnelsManager { + let tunnel = tunnelsManager.tunnel(at: indexPath.row) + cell.tunnelName = tunnel.name + } + return cell + } +} + +class TunnelsListTableViewCell: UITableViewCell { + static let id: String = "TunnelsListTableViewCell" + var tunnelName: String { + get { return textLabel?.text ?? "" } + set(value) { textLabel?.text = value } + } + + override init(style: UITableViewCellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + self.accessoryType = .disclosureIndicator + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func prepareForReuse() { + super.prepareForReuse() } }