DetailViewController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // DetailViewController.swift
  2. //
  3. // Copyright (c) 2014 Alamofire (http://alamofire.org)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import UIKit
  23. import Alamofire
  24. class DetailViewController: UITableViewController {
  25. enum Sections: Int {
  26. case Headers, Body
  27. }
  28. var request: Alamofire.Request? {
  29. willSet {
  30. if self.request != nil {
  31. self.request?.cancel()
  32. self.refreshControl?.endRefreshing()
  33. self.headers.removeAll()
  34. self.body = nil
  35. self.elapsedTime = nil
  36. }
  37. }
  38. didSet {
  39. self.title = self.request?.description
  40. }
  41. }
  42. var headers: [String: String] = [:]
  43. var body: String?
  44. var elapsedTime: NSTimeInterval?
  45. override func awakeFromNib() {
  46. super.awakeFromNib()
  47. self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
  48. }
  49. // MARK: - UIViewController
  50. override func viewDidAppear(animated: Bool) {
  51. super.viewDidAppear(animated)
  52. self.refresh()
  53. }
  54. // MARK: - IBAction
  55. @IBAction func refresh() {
  56. if self.request == nil {
  57. return
  58. }
  59. self.refreshControl?.beginRefreshing()
  60. let start = CACurrentMediaTime()
  61. self.request?.responseString { (request, response, body, error) in
  62. let end = CACurrentMediaTime()
  63. self.elapsedTime = end - start
  64. for (field, value) in response!.allHeaderFields {
  65. self.headers["\(field)"] = "\(value)"
  66. }
  67. self.body = body
  68. self.tableView.reloadData()
  69. self.refreshControl?.endRefreshing()
  70. }
  71. }
  72. // MARK: UITableViewDataSource
  73. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  74. switch Sections.fromRaw(section)! {
  75. case .Headers:
  76. return self.headers.count
  77. case .Body:
  78. return self.body == nil ? 0 : 1
  79. default:
  80. return 0
  81. }
  82. }
  83. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  84. switch Sections.fromRaw(indexPath.section)! {
  85. case .Headers:
  86. let cell = self.tableView.dequeueReusableCellWithIdentifier("Header") as UITableViewCell
  87. let field = self.headers.keys.array.sorted(<)[indexPath.row]
  88. let value = self.headers[field]
  89. cell.textLabel!.text = field
  90. cell.detailTextLabel!.text = value
  91. return cell
  92. case .Body:
  93. let cell = self.tableView.dequeueReusableCellWithIdentifier("Body") as UITableViewCell
  94. cell.textLabel!.text = self.body
  95. return cell
  96. }
  97. }
  98. // MARK: UITableViewDelegate
  99. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  100. return 2
  101. }
  102. override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
  103. switch Sections.fromRaw(section)! {
  104. case .Headers:
  105. return self.headers.isEmpty ? "" : "Headers"
  106. case .Body:
  107. return self.body == nil ? "" : "Body"
  108. }
  109. }
  110. override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  111. switch Sections.fromRaw(indexPath.section)! {
  112. case .Body:
  113. return 300
  114. default:
  115. return tableView.rowHeight
  116. }
  117. }
  118. override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String {
  119. switch Sections.fromRaw(section)! {
  120. case .Body:
  121. return self.elapsedTime == nil ? "" : "Elapsed Time: \(self.elapsedTime!) sec"
  122. default:
  123. return ""
  124. }
  125. }
  126. }