PublishAddTopicController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // PublishAddTopicController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/17.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import SwiftyJSON
  11. import Photos
  12. class PublishAddTopicController: BaseViewController {
  13. override func didReceiveMemoryWarning() {
  14. super.didReceiveMemoryWarning()
  15. }
  16. // 话题组のModelArr
  17. var communityTopicModels : Array<CommunityTopicModel>?
  18. // 话题组下子话题のModelArr
  19. var topicListModels = Array<CommunityTopicDataModel>()
  20. // 选中的话题
  21. var selTopicModelArr: Array<CommunityTopicDataModel>?
  22. typealias SelTopicsClosure = (_ topicMdlArr: Array<CommunityTopicDataModel>) -> Void
  23. var selTopicsClosure : SelTopicsClosure?
  24. var categoryId : Int = 0
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. setupViews()
  28. setupData()
  29. }
  30. override func viewWillAppear(_ animated: Bool) {
  31. navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  32. }
  33. override func viewWillDisappear(_ animated: Bool) {
  34. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  35. }
  36. override func setupViews() {
  37. self.view.backgroundColor = kffffffColor
  38. navigationBar.title = "添加话题"
  39. updateFinishButtonStatus()
  40. navigationBar.onClickRightButton = {
  41. [weak self] in
  42. if let selTopicsClosure = self?.selTopicsClosure {
  43. selTopicsClosure(self!.selTopicModelArr!)
  44. }
  45. self?.navigationController?.popViewController(animated: true)
  46. }
  47. view.addSubview(tableView)
  48. tableView.snp.makeConstraints { (make) in
  49. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  50. make.left.right.bottom.equalToSuperview()
  51. }
  52. view.insertSubview(navigationBar, aboveSubview: tableView)
  53. }
  54. override func setupData() {
  55. communityTopicCategoryApi(page:1)
  56. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true) {
  57. [weak self] (page) in
  58. // 子话题
  59. self?.communityTopicsApi(categoryId: (self?.categoryId)!, page: page)
  60. }
  61. }
  62. lazy var tableView: UITableView = {
  63. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  64. tableView.separatorStyle = .none
  65. tableView.backgroundColor = kffffffColor
  66. tableView.dataSource = self
  67. tableView.delegate = self
  68. return tableView
  69. }()
  70. }
  71. // MARK: - tableView dataSource && delegate
  72. extension PublishAddTopicController : UITableViewDelegate, UITableViewDataSource {
  73. func numberOfSections(in tableView: UITableView) -> Int {
  74. return 2
  75. }
  76. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  77. switch section {
  78. case 0:
  79. return 1
  80. case 1:
  81. return topicListModels.count
  82. default:
  83. return 1
  84. }
  85. }
  86. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  87. switch indexPath.section {
  88. case 0:
  89. let picCell = PublishTopicTypeCell.cellWith(tableView: tableView, indexPath: indexPath)
  90. picCell.topicModels = communityTopicModels
  91. picCell.chooseTopicBlock = {
  92. [weak self] (topicId, indexRow) in
  93. self?.categoryId = topicId
  94. self?.communityTopicsApi(categoryId: topicId, page: 1)
  95. }
  96. return picCell
  97. case 1:
  98. let titleCell = PublishTopicItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  99. titleCell.subTopicModel = topicListModels[indexPath.row]
  100. titleCell.isChoosed = topicListModels[indexPath.row].isSelected
  101. return titleCell
  102. default:
  103. return UITableViewCell()
  104. }
  105. }
  106. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  107. let selTopicMdl: CommunityTopicDataModel = topicListModels[indexPath.row]
  108. if selTopicModelArr!.count >= 5 && selTopicMdl.isSelected == false {
  109. SwiftProgressHUD.shared().showText("最多只能选择5个话题")
  110. return
  111. }
  112. if selTopicMdl.isSelected {
  113. // 点击的已被选中,移除
  114. selTopicMdl.isSelected = false
  115. for (index, topicMdl) in selTopicModelArr!.enumerated() {
  116. if selTopicMdl.id == topicMdl.id {
  117. selTopicModelArr!.remove(at: index)
  118. break
  119. }
  120. }
  121. } else {
  122. // 未被选中过,添加选中
  123. selTopicMdl.isSelected = true
  124. selTopicModelArr!.append(selTopicMdl)
  125. }
  126. tableView.reloadData()
  127. updateFinishButtonStatus()
  128. }
  129. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  130. switch indexPath.section {
  131. case 0:
  132. return 90
  133. case 1:
  134. return 50
  135. default:
  136. return 50
  137. }
  138. }
  139. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  140. if section == 0 {
  141. if selTopicModelArr?.count == 0 {
  142. return 10
  143. } else {
  144. return 44
  145. }
  146. } else {
  147. return 0.000001
  148. }
  149. }
  150. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  151. if section == 0 {
  152. let headerView = PublishTopicHeaderView(frame: CGRect.zero)
  153. headerView.selTopicModels = selTopicModelArr
  154. headerView.deleteTransClosure = {
  155. [weak self] (index) in
  156. // 移除选中 -> 校正列表选中状态 -> 刷新table -> 刷新完成btn
  157. self?.selTopicModelArr!.remove(at: index)
  158. self?.fixTopicSelectedStatus()
  159. self?.tableView.reloadData()
  160. self?.updateFinishButtonStatus()
  161. }
  162. headerView.layoutIfNeeded()
  163. headerView.reloadData()
  164. return headerView
  165. } else {
  166. return nil
  167. }
  168. }
  169. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  170. return 0.000001
  171. }
  172. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  173. return nil
  174. }
  175. }
  176. // MARK: -
  177. extension PublishAddTopicController {
  178. /// 话题组
  179. ///
  180. /// - Parameters:
  181. /// - isSuggest: 是否推荐
  182. /// - page: 分页
  183. func communityTopicCategoryApi(page:Int) {
  184. SwiftMoyaNetWorkServiceCommunity.shared().communityTopicCategoryApi(isSuggest: 0) { [weak self] (communityTopicCategoryModel) -> (Void) in
  185. let topicCategoryModel = communityTopicCategoryModel as? CommunityTopicCategoryModel
  186. self?.communityTopicModels = topicCategoryModel?.data ?? Array<CommunityTopicModel>()
  187. if !(self?.communityTopicModels?.isEmpty ?? true) {
  188. let communityTopicModel = self?.communityTopicModels?[0]
  189. // 子话题
  190. self?.communityTopicsApi(isSuggest: 0, categoryId: communityTopicModel?.id ?? 0, page: 1)
  191. }else {
  192. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: .noData)
  193. }
  194. }
  195. }
  196. /// 话题列表
  197. ///
  198. /// - Parameter page: 分页
  199. func communityTopicsApi(isSuggest:Int = 0, categoryId:Int, page:Int) {
  200. SwiftMoyaNetWorkServiceCommunity.shared().communityTopicsApi(categoryId: categoryId, isSuggest: isSuggest, page: page, completion: {
  201. [weak self] (communityTopicsModel) -> (Void) in
  202. let communityTopicsModel = communityTopicsModel as? CommunityTopicsModel
  203. if communityTopicsModel?.pagination?.currentPage == 1{
  204. self?.topicListModels.removeAll()
  205. self?.tableView.resetNoMoreData()
  206. }
  207. self?.topicListModels = (self?.topicListModels)! + (communityTopicsModel?.data!)!
  208. // 更新选中状态
  209. self?.fixTopicSelectedStatus()
  210. self?.tableView.reloadData()
  211. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityTopicsModel?.pagination)
  212. }) {
  213. [weak self] (loadingStatus) in
  214. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  215. }
  216. }
  217. // 校正选中状态
  218. func fixTopicSelectedStatus() {
  219. for topicMdl in topicListModels {
  220. topicMdl.isSelected = false
  221. for selTopicMdl in selTopicModelArr! {
  222. if topicMdl.id == selTopicMdl.id {
  223. topicMdl.isSelected = true
  224. break
  225. }
  226. }
  227. }
  228. }
  229. func updateFinishButtonStatus() {
  230. let selCount = selTopicModelArr!.count
  231. if selCount == 0 {
  232. navigationBar.wr_setRightButton(title: "完成(\(selCount)/5)", titleColor: k999999Color)
  233. navigationBar.rightButton.isUserInteractionEnabled = false
  234. } else {
  235. navigationBar.wr_setRightButton(title: "完成(\(selCount)/5)", titleColor: kThemeColor)
  236. navigationBar.rightButton.isUserInteractionEnabled = true
  237. }
  238. }
  239. }