PublishAddTopicController.swift 9.4 KB

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