BBSSubjectDetailViewController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // BBSSubjectDetailViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/11/8.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import Alamofire
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. class BBSSubjectDetailViewController: BaseWebViewUIViewController {
  16. @IBOutlet weak var progressView: UIProgressView!
  17. @IBOutlet weak var webViewContainer: UIView!
  18. @IBOutlet weak var attachmentBtn: UIButton!
  19. var loadUrl:String?
  20. var subject:BBSSubjectData? {
  21. didSet {
  22. loadUrl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.bbsItemDetailQuery, parameter: ["##subjectId##":subject?.id as AnyObject])
  23. }
  24. }
  25. private lazy var viewModel: BBSViewModel = {
  26. return BBSViewModel()
  27. }()
  28. private var attachmentList: [O2BBSSubjectAttachmentInfo] = []
  29. override func viewWillAppear(_ animated: Bool) {
  30. //监控进度
  31. webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
  32. }
  33. override func viewWillDisappear(_ animated: Bool) {
  34. webView.removeObserver(self, forKeyPath: "estimatedProgress")
  35. }
  36. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  37. if keyPath == "estimatedPrgress" {
  38. progressView.isHidden = webView.estimatedProgress == 1
  39. progressView.setProgress(Float(webView.estimatedProgress), animated: true)
  40. }
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. self.attachmentBtn.isHidden = true
  45. if let subjectId = self.subject?.id {
  46. self.viewModel.getSubjectAttachmentList(subjectId: subjectId)
  47. .then { attachments in
  48. if attachments.count > 0 {
  49. self.attachmentList = attachments
  50. self.attachmentBtn.isHidden = false
  51. }
  52. }.catch { (error) in
  53. DDLogError(error.localizedDescription)
  54. }
  55. }
  56. self.theWebView()
  57. self.webViewContainer.addSubview(self.webView)
  58. }
  59. @IBAction func clickAttachmentBtn(_ sender: UIButton) {
  60. DDLogDebug("点击附件列表")
  61. if self.attachmentList.count > 0 {
  62. self.performSegue(withIdentifier: "showSubAttachmentActionSegue", sender: nil)
  63. }else {
  64. self.showError(title: "没有附件!")
  65. }
  66. }
  67. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  68. if segue.identifier == "showReplyActionSegue" {
  69. let navVC = segue.destination as! ZLNavigationController
  70. let destVC = navVC.topViewController as! BBSReplySubjectViewController
  71. destVC.subject = self.subject
  72. if let parentId = sender {
  73. destVC.parentId = parentId as? String
  74. }
  75. }else if segue.identifier == "showSubAttachmentActionSegue" {
  76. let navVC = segue.destination as! ZLNavigationController
  77. let destVC = navVC.topViewController as! BBSSubjectAttachmentViewController
  78. destVC.attachmentList = self.attachmentList
  79. }
  80. }
  81. override func theWebView(){
  82. super.theWebView()
  83. webView.navigationDelegate = self
  84. webView.uiDelegate = self
  85. //self.view.insertSubview(webView, belowSubview: progressView)
  86. webView.allowsBackForwardNavigationGestures = true
  87. //监控进度
  88. //self.addObserver(webView, forKeyPath: "estimatedProgress", options: .new, context: nil)
  89. loadDetailSubject()
  90. }
  91. func loadDetailSubject(){
  92. if let itemUrl = loadUrl, let urlR = URL(string: itemUrl) {
  93. let req = URLRequest(url: urlR)
  94. webView.load(req)
  95. }else {
  96. webView.loadHTMLString("<h2>没有获取到正确的URL!</h2>", baseURL: nil)
  97. }
  98. }
  99. @IBAction func unFromReplyBackSubject(_ segue:UIStoryboardSegue){
  100. loadDetailSubject()
  101. }
  102. //写评论
  103. @IBAction func replaySubject(_ sender: UIButton) {
  104. self.performSegue(withIdentifier:"showReplyActionSegue", sender: nil)
  105. }
  106. override func didReceiveMemoryWarning() {
  107. super.didReceiveMemoryWarning()
  108. // Dispose of any resources that can be recreated.
  109. }
  110. }
  111. extension BBSSubjectDetailViewController:WKNavigationDelegate,WKUIDelegate {
  112. func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  113. DDLogDebug("didFailProvisionalNavigation \(String(describing: navigation)) error = \(error)")
  114. }
  115. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  116. DDLogDebug("didStartProvisionalNavigation \(String(describing: navigation))")
  117. }
  118. func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
  119. DDLogDebug("didCommit")
  120. }
  121. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  122. DDLogDebug("didFinish")
  123. //self.setupData()
  124. }
  125. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  126. DDLogDebug("didFail")
  127. DDLogError(error.localizedDescription)
  128. }
  129. }