BBSSubjectListViewController.swift 7.2 KB

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