BBSSubjectListViewController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // BBSSubjectListViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/11/4.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import CocoaLumberjack
  14. class BBSSubjectListViewController: UIViewController {
  15. @IBOutlet weak var subjectTableView: ZLBaseTableView!
  16. var pageModel:SubjectPageModel = SubjectPageModel()
  17. var sectionData:BBSectionListData?
  18. var sectionUrl:String?
  19. var subjects:[BBSSubjectData] = []
  20. var window:UIWindow?
  21. var button:UIButton?
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. title = sectionData?.sectionName
  25. subjectTableView.dataSource = self
  26. subjectTableView.delegate = self
  27. subjectTableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  28. self.pageModel = SubjectPageModel()
  29. self.loadFirstData()
  30. })
  31. subjectTableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {
  32. //先生成下一页的页号
  33. if self.pageModel.isLast() == false {
  34. self.pageModel.nextPage()
  35. self.loadNextPageData()
  36. }else{
  37. self.showSuccess(title: "最后一页了,没有帖子了")
  38. }
  39. })
  40. self.loadFirstData()
  41. if !O2AuthSDK.shared.isBBSMute() {
  42. self.createButton()
  43. } else {
  44. DDLogError("当前用户被禁言!")
  45. }
  46. }
  47. override func viewWillAppear(_ animated: Bool) {
  48. self.window?.isHidden = false
  49. }
  50. override func viewWillDisappear(_ animated: Bool) {
  51. self.window?.isHidden = true
  52. }
  53. override var preferredStatusBarStyle : UIStatusBarStyle {
  54. return .lightContent
  55. }
  56. func createButton(){
  57. let width = SCREEN_WIDTH
  58. let height = SCREEN_HEIGHT
  59. self.button = UIButton(frame: CGRect(x: 0,y: 0,width: 40,height: 40))
  60. self.button?.setImage(UIImage(named: "icon_bbs_publish"), for: UIControl.State())
  61. self.button?.addTarget(self, action: #selector(createAction), for: .touchUpInside)
  62. self.window = UIWindow(frame: CGRect(x: width - 60, y: height - 60, width: 40, height: 40))
  63. self.window?.windowLevel = UIWindow.Level.alert + 1
  64. self.window?.backgroundColor = base_color
  65. self.window?.layer.cornerRadius = 20
  66. self.window?.layer.masksToBounds = true
  67. self.window?.addSubview(self.button!)
  68. self.window?.makeKeyAndVisible()
  69. }
  70. @objc func createAction(sender:Any?){
  71. self.performSegue(withIdentifier: "showCreateSubjectSegue", sender: nil)
  72. }
  73. func loadFirstData(){
  74. self.subjects.removeAll()
  75. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.subjectFromSectionByPageQuery, parameter: ["##pageNumber##":self.pageModel.pageNumber.toString as AnyObject,"##pageSize##":self.pageModel.pageSize.toString as AnyObject])
  76. AF.request(url!, method: .put, parameters: ["sectionId":(sectionData?.id)!,"withTopSubject":true], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  77. debugPrint("sectionId = \(String(describing: self.sectionData?.id))!")
  78. debugPrint(response)
  79. switch response.result {
  80. case .success(let val):
  81. let type = JSON(val)["type"]
  82. let count = JSON(val)["count"]
  83. //第一次设置总数
  84. self.pageModel.setPageTotal(count.int!)
  85. if type == "success" && count > 0 {
  86. let subjectArray:[BBSSubjectData] = Mapper<BBSSubjectData>().mapArray(JSONString: JSON(val)["data"].description)!
  87. self.subjects.append(contentsOf: subjectArray)
  88. }else{
  89. DDLogError(JSON(val).description)
  90. }
  91. case .failure(let err):
  92. DDLogError(err.localizedDescription)
  93. }
  94. self.subjectTableView.reloadData()
  95. if self.subjectTableView.mj_header.isRefreshing(){
  96. self.subjectTableView.mj_header.endRefreshing()
  97. }
  98. }
  99. }
  100. func loadNextPageData(){
  101. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.subjectFromSectionByPageQuery, parameter: ["##pageNumber##":self.pageModel.pageNumber.toString as AnyObject,"##pageSize##":self.pageModel.pageSize.toString as AnyObject])
  102. AF.request(url!, method: .put, parameters: ["sectionId":(sectionData?.id)!,"withTopSubject":true], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  103. switch response.result {
  104. case .success(let val):
  105. let type = JSON(val)["type"]
  106. let count = JSON(val)["count"]
  107. if type == "success" && count > 0 {
  108. let subjectArray:[BBSSubjectData] = Mapper<BBSSubjectData>().mapArray(JSONString: JSON(val)["data"].description)!
  109. self.subjects.append(contentsOf: subjectArray)
  110. }else{
  111. DDLogError(JSON(val).description)
  112. }
  113. case .failure(let err):
  114. DDLogError(err.localizedDescription)
  115. }
  116. self.subjectTableView.reloadData()
  117. if self.subjectTableView.mj_footer.isRefreshing(){
  118. self.subjectTableView.mj_footer.endRefreshing()
  119. }
  120. }
  121. }
  122. @IBAction func UnBackPublishSubjectSuccess(_ segue:UIStoryboardSegue){
  123. self.loadFirstData()
  124. }
  125. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  126. if segue.identifier == "showSubjectDetailSegue" {
  127. let destVC = segue.destination as! BBSSubjectDetailViewController
  128. destVC.subject = sender as? BBSSubjectData
  129. }else if segue.identifier == "showCreateSubjectSegue" {
  130. let destVC = segue.destination as! BBSSubjectCreateTableViewController
  131. destVC.sectionData = self.sectionData
  132. }
  133. }
  134. override func didReceiveMemoryWarning() {
  135. super.didReceiveMemoryWarning()
  136. // Dispose of any resources that can be recreated.
  137. }
  138. }
  139. extension BBSSubjectListViewController:UITableViewDataSource {
  140. func numberOfSections(in tableView: UITableView) -> Int {
  141. return 1
  142. }
  143. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  144. return subjects.count
  145. }
  146. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  147. let cell:SubjectTableViewCell = tableView.dequeueReusableCell(withIdentifier: "SubjectTableViewCell", for: indexPath) as! SubjectTableViewCell
  148. let subject = self.subjects[indexPath.row]
  149. cell.bbsSubjectData = subject
  150. return cell
  151. }
  152. }
  153. extension BBSSubjectListViewController:UITableViewDelegate {
  154. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  155. tableView.deselectRow(at: indexPath, animated: false)
  156. let subject = subjects[indexPath.row]
  157. self.performSegue(withIdentifier: "showSubjectDetailSegue", sender: subject)
  158. }
  159. }