PublishEditController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // PublishEditController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/17.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 编辑发布Vc
  8. import UIKit
  9. import RxSwift
  10. import SwiftyJSON
  11. import Photos
  12. import Kingfisher
  13. @objc(PublishMediaType)
  14. enum PublishMediaType: Int {
  15. case image = 0
  16. case video = 1
  17. }
  18. class PublishEditController: BaseViewController {
  19. deinit {
  20. NXLLog("deinit")
  21. }
  22. override func didReceiveMemoryWarning() {
  23. super.didReceiveMemoryWarning()
  24. KingfisherManager.shared.cache.clearDiskCache()
  25. KingfisherManager.shared.cache.clearMemoryCache()
  26. }
  27. @objc var mediaType: PublishMediaType = .image
  28. @objc var videoPath: String = ""
  29. @objc var coverImagePath: String = ""
  30. @objc var videoSize: CGSize = CGSize()
  31. @objc var videoImage: UIImage? {
  32. didSet {
  33. imageArr = [videoImage] as? Array<UIImage>
  34. }
  35. }
  36. var imageArr: Array<UIImage>?
  37. private var imageAssetUrl: Array<String> = []
  38. // 选中的话题
  39. var selTopicModelArr: Array<CommunityTopicDataModel> = []
  40. // 已选话题id
  41. var selTopicIdArr: Array<String> = []
  42. // 已选话题name
  43. var selTopicName: String = ""
  44. // 标题
  45. var pubTitle: String = ""
  46. // 内容
  47. var pubContent: String = ""
  48. // 位置
  49. var locationStr: String = ""
  50. // 纬度
  51. var latitude: String = ""
  52. // 经度
  53. var longitude: String = ""
  54. // MARK: 控制器生命周期
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. setupViews()
  58. setupData()
  59. checkPublishButtonStatus()
  60. }
  61. override func viewWillAppear(_ animated: Bool) {
  62. // 禁用返回手势
  63. if navigationController?.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) ?? false {
  64. navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  65. }
  66. }
  67. override func viewWillDisappear(_ animated: Bool) {
  68. if navigationController?.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) ?? false {
  69. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  70. }
  71. }
  72. override func setupViews() {
  73. self.view.backgroundColor = kffffffColor
  74. navigationBar.title = ""
  75. navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_black")!)
  76. navigationBar.onClickLeftButton = {
  77. [weak self] in
  78. AlertSheetView.alert(title: "是否退出内容发布?退出后你所编辑的内容无法恢复!", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
  79. }, confirmBlock: {
  80. (popupView, index, string) in
  81. self?.navigationController?.dismiss(animated: true, completion: nil)
  82. })
  83. }
  84. view.addSubview(rightButton)
  85. rightButton.snp.makeConstraints { (make) in
  86. make.top.equalToSuperview().offset(kSafeStatusBarHeight+8)
  87. make.right.equalTo(-14)
  88. make.width.equalTo(64)
  89. make.height.equalTo(26)
  90. }
  91. view.addSubview(tableView)
  92. tableView.snp.makeConstraints { (make) in
  93. make.top.equalTo(kNavBarTotalHeight)
  94. make.left.right.bottom.equalToSuperview()
  95. }
  96. }
  97. override func setupData() {
  98. }
  99. lazy var tableView: UITableView = {
  100. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  101. tableView.separatorStyle = .none
  102. tableView.backgroundColor = kffffffColor
  103. tableView.dataSource = self
  104. tableView.delegate = self
  105. return tableView
  106. }()
  107. private lazy var rightButton: UIButton = {
  108. let rightButton = UIButton(type: UIButton.ButtonType.custom)
  109. rightButton.setTitle("发布", for: UIControl.State.normal)
  110. rightButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  111. rightButton.titleLabel?.font = kMediumFont13
  112. rightButton.cornerRadius = 13
  113. rightButton.masksToBounds = true
  114. rightButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  115. self?.checkAndPublishAction()
  116. }).disposed(by: disposeBag)
  117. return rightButton
  118. }()
  119. }
  120. // MARK: - tableView dataSource && delegate
  121. extension PublishEditController : UITableViewDelegate, UITableViewDataSource {
  122. func numberOfSections(in tableView: UITableView) -> Int {
  123. return 1
  124. }
  125. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126. return 5
  127. }
  128. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  129. switch indexPath.row {
  130. case 0:
  131. let picCell = PublishEditAddPicCell.cellWith(tableView: tableView, indexPath: indexPath)
  132. picCell.pubImageArr = imageArr
  133. picCell.mediaType = mediaType
  134. if mediaType == .video {
  135. picCell.maxImageCount = 1
  136. }
  137. picCell.reloadData()
  138. picCell.choosePicBlock = {
  139. [weak self] in
  140. let vc = KSMediaPickerController(maxVideoItemCount: 0, maxPictureItemCount: (UInt(9 -
  141. (self?.imageArr?.count ?? 0))))
  142. vc.mediaType = KSMediaPickerController.mediaType.picture
  143. vc.jumpType = .publishEdit
  144. vc.pubEditImgClosure = {
  145. [weak self] (imageArray) in
  146. self?.imageArr = (self?.imageArr ?? Array<UIImage>()) + imageArray
  147. self?.tableView.reloadData()
  148. }
  149. vc.modalPresentationStyle = .fullScreen
  150. self?.present(vc, animated: true, completion: nil)
  151. }
  152. picCell.delPicTransBlock = {
  153. [weak self] (picIdx) in
  154. self?.imageArr?.remove(at: picIdx!)
  155. self?.tableView.reloadData()
  156. }
  157. return picCell
  158. case 1:
  159. let titleCell = PublishEditContentViewTitleCell.cellWith(tableView: tableView, indexPath: indexPath)
  160. titleCell.titleTextClosure = {
  161. [weak self] (text) in
  162. self?.pubTitle = text
  163. }
  164. return titleCell
  165. case 2:
  166. let desCell = PublishEditContentViewDescribeCell.cellWith(tableView: tableView, indexPath: indexPath)
  167. desCell.commentTextViewClosure = {
  168. [weak self] (text) in
  169. self?.pubContent = text
  170. self?.checkPublishButtonStatus()
  171. }
  172. return desCell
  173. case 3:
  174. let topicCell = PublishEditAddTopicCell.cellWith(tableView: tableView, indexPath: indexPath)
  175. topicCell.isTopicsNull = selTopicIdArr.count == 0 ? true : false
  176. topicCell.selTopicName = selTopicName
  177. return topicCell
  178. case 4:
  179. let addrCell = PublishEditAddAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  180. addrCell.location = locationStr
  181. return addrCell
  182. default:
  183. return UITableViewCell()
  184. }
  185. }
  186. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  187. switch indexPath.row {
  188. case 3:
  189. let vc = PublishAddTopicController()
  190. vc.selTopicModelArr = self.selTopicModelArr
  191. vc.selTopicsClosure = {
  192. [weak self] (topicMdlArr) in
  193. self?.selTopicModelArr = topicMdlArr
  194. self?.selTopicName = ""
  195. self?.selTopicIdArr.removeAll()
  196. for (index,topicModel) in (topicMdlArr.enumerated()) {
  197. let strTopicId: String = String(format: "%d", topicModel.id ?? 0)
  198. self?.selTopicIdArr.append(strTopicId)
  199. if index == 0 {
  200. self?.selTopicName.append(topicModel.name!)
  201. } else {
  202. self?.selTopicName.append(", \(topicModel.name!)")
  203. }
  204. }
  205. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  206. self?.checkPublishButtonStatus()
  207. }
  208. self.navigationController?.pushViewController(vc, animated: true)
  209. case 4:
  210. let vc = PublishAddAddressController()
  211. vc.selectLocationClosure = {
  212. [weak self] (address,latitude,longitude) in
  213. self?.locationStr = address
  214. self?.latitude = latitude ?? ""
  215. self?.longitude = longitude ?? ""
  216. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  217. }
  218. self.navigationController?.pushViewController(vc, animated: true)
  219. default:
  220. break
  221. }
  222. }
  223. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  224. switch indexPath.row {
  225. case 0:
  226. return 130
  227. case 1,3,4:
  228. return 50
  229. case 2:
  230. return 190
  231. default:
  232. return UITableView.automaticDimension
  233. }
  234. }
  235. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  236. return 0.000001
  237. }
  238. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  239. return nil
  240. }
  241. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  242. return 0.000001
  243. }
  244. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  245. return nil
  246. }
  247. }
  248. // MARK: - 逻辑处理
  249. extension PublishEditController {
  250. func checkAndPublishAction() {
  251. if pubTitle.count > 20 {
  252. SwiftProgressHUD.shared().showText("标题最多输入20个字符")
  253. return
  254. }
  255. if pubContent.count == 0 {
  256. SwiftProgressHUD.shared().showText("还没输入描述内容")
  257. return
  258. }
  259. if selTopicIdArr.count == 0 {
  260. SwiftProgressHUD.shared().showText("还没选择话题哟")
  261. return
  262. }
  263. ///发布参数
  264. let aliyunVodUpToYoPublishModel = AliyunVodUpToYoPublishModel()
  265. aliyunVodUpToYoPublishModel.topicIds = self.selTopicIdArr
  266. aliyunVodUpToYoPublishModel.title = self.pubTitle
  267. aliyunVodUpToYoPublishModel.content = self.pubContent
  268. aliyunVodUpToYoPublishModel.locationString = self.locationStr
  269. aliyunVodUpToYoPublishModel.latitude = self.latitude
  270. aliyunVodUpToYoPublishModel.longitude = self.longitude
  271. if self.mediaType == .video {
  272. aliyunVodUpToYoPublishModel.publishType = .video
  273. aliyunVodUpToYoPublishModel.videoCoverImage = videoImage
  274. aliyunVodUpToYoPublishModel.videoCoverImageUrlPath = coverImagePath
  275. aliyunVodUpToYoPublishModel.videoPath = self.videoPath
  276. }
  277. if self.mediaType == .image {
  278. aliyunVodUpToYoPublishModel.publishType = .image
  279. aliyunVodUpToYoPublishModel.images = self.imageArr
  280. }
  281. self.dismissToRootViewController(animated: false) {
  282. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: aliyunVodUpToYoPublishModel)
  283. baseTabbarViewController?.selectedIndex = 0
  284. }
  285. }
  286. // 改变发布按钮颜色
  287. func checkPublishButtonStatus() {
  288. if pubContent.count == 0 || selTopicIdArr.count == 0 {
  289. rightButton.backgroundColor = kd8d8d8Color
  290. } else {
  291. rightButton.backgroundColor = kThemeColor
  292. }
  293. }
  294. }