|
| 1 | +// |
| 2 | +// ApiListViewController.swift |
| 3 | +// NetworkInspector |
| 4 | +// |
| 5 | +// Created by Revanth A on 01/01/26. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +final class ApiListViewController: UIViewController { |
| 11 | + |
| 12 | + private let tableView = UITableView() |
| 13 | + private var items: [APIItem] = [] |
| 14 | + |
| 15 | + override func viewDidLoad() { |
| 16 | + super.viewDidLoad() |
| 17 | + |
| 18 | + title = "APIs" |
| 19 | + view.backgroundColor = .systemBackground |
| 20 | + |
| 21 | + setupTableView() |
| 22 | + loadItems() |
| 23 | + observeLogs() |
| 24 | + } |
| 25 | + |
| 26 | + deinit { |
| 27 | + NotificationCenter.default.removeObserver(self) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +private extension ApiListViewController { |
| 32 | + |
| 33 | + func setupTableView() { |
| 34 | + tableView.translatesAutoresizingMaskIntoConstraints = false |
| 35 | + view.addSubview(tableView) |
| 36 | + |
| 37 | + NSLayoutConstraint.activate([ |
| 38 | + tableView.topAnchor.constraint(equalTo: view.topAnchor), |
| 39 | + tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), |
| 40 | + tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 41 | + tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor) |
| 42 | + ]) |
| 43 | + |
| 44 | + tableView.dataSource = self |
| 45 | + tableView.delegate = self |
| 46 | + } |
| 47 | + |
| 48 | + func observeLogs() { |
| 49 | + NotificationCenter.default.addObserver( |
| 50 | + self, |
| 51 | + selector: #selector(onLogsUpdated), |
| 52 | + name: .networkLogStoreDidUpdate, |
| 53 | + object: nil |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + @objc |
| 58 | + func onLogsUpdated() { |
| 59 | + loadItems() |
| 60 | + } |
| 61 | + |
| 62 | + func loadItems() { |
| 63 | + let logs = NetworkLogStore.shared.getAllLogs() |
| 64 | + items = buildItems(from: logs) |
| 65 | + tableView.reloadData() |
| 66 | + } |
| 67 | + |
| 68 | + func buildItems(from logs: [NetworkLog]) -> [APIItem] { |
| 69 | + var counts: [String: APIItem] = [:] |
| 70 | + |
| 71 | + for log in logs { |
| 72 | + let host = log.request.url?.host ?? "-" |
| 73 | + let path = endpointPath(from: log.request.url) |
| 74 | + let key = "\(host)|\(path)" |
| 75 | + |
| 76 | + if let existing = counts[key] { |
| 77 | + counts[key] = APIItem(host: host, path: path, count: existing.count + 1) |
| 78 | + } else { |
| 79 | + counts[key] = APIItem(host: host, path: path, count: 1) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return counts.values.sorted { lhs, rhs in |
| 84 | + if lhs.host == rhs.host { |
| 85 | + return lhs.path < rhs.path |
| 86 | + } |
| 87 | + return lhs.host < rhs.host |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + func endpointPath(from url: URL?) -> String { |
| 92 | + let path = url?.path ?? "/" |
| 93 | + if path == "/" || path.isEmpty { |
| 94 | + return "/" |
| 95 | + } |
| 96 | + return path.hasPrefix("/") ? String(path.dropFirst()) : path |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extension ApiListViewController: UITableViewDataSource { |
| 101 | + |
| 102 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 103 | + items.count |
| 104 | + } |
| 105 | + |
| 106 | + func tableView( |
| 107 | + _ tableView: UITableView, |
| 108 | + cellForRowAt indexPath: IndexPath |
| 109 | + ) -> UITableViewCell { |
| 110 | + let cell = tableView.dequeueReusableCell(withIdentifier: "ApiCell") |
| 111 | + ?? UITableViewCell(style: .subtitle, reuseIdentifier: "ApiCell") |
| 112 | + |
| 113 | + let item = items[indexPath.row] |
| 114 | + cell.textLabel?.text = item.path |
| 115 | + cell.textLabel?.font = .systemFont(ofSize: 15, weight: .medium) |
| 116 | + cell.detailTextLabel?.text = "\(item.host) • \(item.count) calls" |
| 117 | + cell.detailTextLabel?.textColor = .secondaryLabel |
| 118 | + cell.selectionStyle = .none |
| 119 | + |
| 120 | + return cell |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +extension ApiListViewController: UITableViewDelegate { |
| 125 | + |
| 126 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 127 | + let item = items[indexPath.row] |
| 128 | + let title = item.path == "/" ? item.host : item.path |
| 129 | + |
| 130 | + let filter: (NetworkLog) -> Bool = { log in |
| 131 | + let host = log.request.url?.host ?? "-" |
| 132 | + let path = self.endpointPath(from: log.request.url) |
| 133 | + return host == item.host && path == item.path |
| 134 | + } |
| 135 | + |
| 136 | + let detailVC = LogListViewController(filter: filter, title: title) |
| 137 | + navigationController?.pushViewController(detailVC, animated: true) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +private struct APIItem { |
| 142 | + let host: String |
| 143 | + let path: String |
| 144 | + let count: Int |
| 145 | +} |
0 commit comments