123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- //
- // PublishAddTopicController.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/17.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import RxSwift
- import SwiftyJSON
- import Photos
- class PublishAddTopicController: BaseViewController {
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
-
- // 话题组のModelArr
- var communityTopicModels : Array<CommunityTopicModel>?
-
- // 话题组下子话题のModelArr
- var topicListModels = Array<CommunityTopicDataModel>()
-
- // 选中的话题
- var selTopicModelArr: Array<CommunityTopicDataModel>?
-
- typealias SelTopicsClosure = (_ topicMdlArr: Array<CommunityTopicDataModel>) -> Void
- var selTopicsClosure : SelTopicsClosure?
-
- var categoryId : Int = 0
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupData()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- navigationController?.interactivePopGestureRecognizer?.isEnabled = false
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- navigationController?.interactivePopGestureRecognizer?.isEnabled = true
- }
-
- override func setupViews() {
- self.view.backgroundColor = kffffffColor
-
- navigationBar.title = "添加话题"
- updateFinishButtonStatus()
- navigationBar.onClickRightButton = {
- [weak self] in
- if let selTopicsClosure = self?.selTopicsClosure {
- selTopicsClosure(self!.selTopicModelArr!)
- }
- self?.navigationController?.popViewController(animated: true)
- }
-
- view.addSubview(tableView)
- tableView.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kNavBarTotalHeight)
- make.left.right.bottom.equalToSuperview()
- }
- view.insertSubview(navigationBar, aboveSubview: tableView)
- }
-
- override func setupData() {
- communityTopicCategoryApi(page:1)
- tableView.addAutoNormalFooter(withAutomaticallyRefresh: true) {
- [weak self] (page) in
- // 子话题
- self?.communityTopicsApi(categoryId: (self?.categoryId)!, page: page)
- }
-
- }
-
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
- tableView.separatorStyle = .none
- tableView.backgroundColor = kffffffColor
- tableView.dataSource = self
- tableView.delegate = self
- return tableView
- }()
-
- }
- // MARK: - tableView dataSource && delegate
- extension PublishAddTopicController : UITableViewDelegate, UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 2
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- switch section {
- case 0:
- return 1
- case 1:
- return topicListModels.count
- default:
- return 1
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.section {
- case 0:
- let picCell = PublishTopicTypeCell.cellWith(tableView: tableView, indexPath: indexPath)
- picCell.topicModels = communityTopicModels
- picCell.chooseTopicBlock = {
- [weak self] (topicId, indexRow) in
- self?.categoryId = topicId
- self?.communityTopicsApi(categoryId: topicId, page: 1)
- }
- return picCell
- case 1:
- let titleCell = PublishTopicItemCell.cellWith(tableView: tableView, indexPath: indexPath)
- titleCell.subTopicModel = topicListModels[indexPath.row]
- titleCell.isChoosed = topicListModels[indexPath.row].isSelected
- return titleCell
- default:
- return UITableViewCell()
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let selTopicMdl: CommunityTopicDataModel = topicListModels[indexPath.row]
-
- if selTopicModelArr!.count >= 5 && selTopicMdl.isSelected == false {
- SwiftProgressHUD.shared().showText("最多只能选择5个话题")
- return
- }
-
- if selTopicMdl.isSelected {
- // 点击的已被选中,移除
- selTopicMdl.isSelected = false
- for (index, topicMdl) in selTopicModelArr!.enumerated() {
- if selTopicMdl.id == topicMdl.id {
- selTopicModelArr!.remove(at: index)
- break
- }
- }
- } else {
- // 未被选中过,添加选中
- selTopicMdl.isSelected = true
- selTopicModelArr!.append(selTopicMdl)
- }
- tableView.reloadData()
-
- updateFinishButtonStatus()
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.section {
- case 0:
- return 90
- case 1:
- return 50
- default:
- return 50
- }
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- if section == 0 {
- if selTopicModelArr?.count == 0 {
- return 10
- } else {
- return 44
- }
- } else {
- return 0.000001
- }
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- if section == 0 {
- let headerView = PublishTopicHeaderView(frame: CGRect.zero)
- headerView.selTopicModels = selTopicModelArr
- headerView.deleteTransClosure = {
- [weak self] (index) in
- // 移除选中 -> 校正列表选中状态 -> 刷新table -> 刷新完成btn
- self?.selTopicModelArr!.remove(at: index)
- self?.fixTopicSelectedStatus()
- self?.tableView.reloadData()
- self?.updateFinishButtonStatus()
- }
- headerView.layoutIfNeeded()
- headerView.reloadData()
- return headerView
- } else {
- return nil
- }
-
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.000001
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
-
- }
- // MARK: -
- extension PublishAddTopicController {
-
- /// 话题组
- ///
- /// - Parameters:
- /// - isSuggest: 是否推荐
- /// - page: 分页
- func communityTopicCategoryApi(page:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityTopicCategoryApi(isSuggest: 0) { [weak self] (communityTopicCategoryModel) -> (Void) in
- let topicCategoryModel = communityTopicCategoryModel as? CommunityTopicCategoryModel
- self?.communityTopicModels = topicCategoryModel?.data ?? Array<CommunityTopicModel>()
- if !(self?.communityTopicModels?.isEmpty ?? true) {
- let communityTopicModel = self?.communityTopicModels?[0]
- // 子话题
- self?.communityTopicsApi(isSuggest: 0, categoryId: communityTopicModel?.id ?? 0, page: 1)
- }else {
- MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: .noData)
- }
- }
- }
-
- /// 话题列表
- ///
- /// - Parameter page: 分页
- func communityTopicsApi(isSuggest:Int = 0, categoryId:Int, page:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityTopicsApi(categoryId: categoryId, isSuggest: isSuggest, page: page, completion: {
- [weak self] (communityTopicsModel) -> (Void) in
- let communityTopicsModel = communityTopicsModel as? CommunityTopicsModel
- if communityTopicsModel?.pagination?.currentPage == 1{
- self?.topicListModels.removeAll()
- self?.tableView.resetNoMoreData()
- }
- self?.topicListModels = (self?.topicListModels)! + (communityTopicsModel?.data!)!
- // 更新选中状态
- self?.fixTopicSelectedStatus()
- self?.tableView.reloadData()
- MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityTopicsModel?.pagination)
- }) {
- [weak self] (loadingStatus) in
- MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
-
- }
- }
-
- // 校正选中状态
- func fixTopicSelectedStatus() {
- for topicMdl in topicListModels {
- topicMdl.isSelected = false
- for selTopicMdl in selTopicModelArr! {
- if topicMdl.id == selTopicMdl.id {
- topicMdl.isSelected = true
- break
- }
- }
- }
- }
-
- func updateFinishButtonStatus() {
- let selCount = selTopicModelArr!.count
- if selCount == 0 {
- navigationBar.wr_setRightButton(title: "完成(\(selCount)/5)", titleColor: k999999Color)
- navigationBar.rightButton.isUserInteractionEnabled = false
- } else {
- navigationBar.wr_setRightButton(title: "完成(\(selCount)/5)", titleColor: kThemeColor)
- navigationBar.rightButton.isUserInteractionEnabled = true
- }
- }
-
- }
|