PublishEditController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. enum PublishMediaType {
  13. case image
  14. case video
  15. }
  16. class PublishEditController: BaseViewController {
  17. var mediaType: PublishMediaType = .image
  18. var imgCount: Int = 0
  19. var majorImageUrl: String?
  20. var imageUrlArray: Array<String> = []
  21. var imageArr: Array<UIImage>? {
  22. didSet {
  23. imgCount = self.imageArr?.count ?? 0
  24. }
  25. }
  26. // 话题id
  27. var topic_ids: String = ""
  28. // 标题
  29. var pubTitle: String = ""
  30. // 内容
  31. var pubContent: String = ""
  32. // 位置
  33. var location: String = ""
  34. // MARK: 控制器生命周期
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. setupViews()
  38. setupData()
  39. }
  40. override func viewWillAppear(_ animated: Bool) {
  41. // imageUrlArray每次进入页面需置空
  42. imageUrlArray = []
  43. rightButton.isEnabled = false
  44. rightButton.backgroundColor = kd8d8d8Color
  45. uploadAllImages(totalTimes: 0)
  46. }
  47. override func setupViews() {
  48. self.view.backgroundColor = kffffffColor
  49. navigationBar.title = ""
  50. navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_black")!)
  51. navigationBar.onClickLeftButton = {
  52. [weak self] in
  53. AlertSheetView.alert(title: "是否退出内容发布?退出后你所编辑的内容无法恢复!", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
  54. }, confirmBlock: {
  55. (popupView, index, string) in
  56. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: nil)
  57. })
  58. }
  59. view.addSubview(rightButton)
  60. rightButton.snp.makeConstraints { (make) in
  61. make.top.equalToSuperview().offset(kSafeStatusBarHeight+8)
  62. make.right.equalTo(-14)
  63. make.width.equalTo(64)
  64. make.height.equalTo(26)
  65. }
  66. view.addSubview(progressBackView)
  67. progressBackView.addSubview(progressView)
  68. progressBackView.addSubview(subLabel)
  69. progressBackView.snp.makeConstraints { (make) in
  70. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  71. make.left.right.equalToSuperview()
  72. make.height.equalTo(38)
  73. }
  74. progressView.snp.makeConstraints { (make) in
  75. make.top.left.right.equalToSuperview()
  76. make.height.equalTo(3)
  77. }
  78. subLabel.snp.makeConstraints { (make) in
  79. make.left.equalTo(14)
  80. make.centerY.equalToSuperview()
  81. make.height.equalTo(20)
  82. }
  83. view.addSubview(tableView)
  84. tableView.snp.makeConstraints { (make) in
  85. make.top.equalTo(progressBackView.snp_bottom)
  86. make.left.right.bottom.equalToSuperview()
  87. }
  88. }
  89. override func setupData() {
  90. }
  91. lazy var tableView: UITableView = {
  92. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  93. tableView.separatorStyle = .none
  94. tableView.backgroundColor = kffffffColor
  95. tableView.dataSource = self
  96. tableView.delegate = self
  97. tableView.estimatedRowHeight = 0.000001
  98. tableView.estimatedSectionFooterHeight = 0.000001
  99. tableView.estimatedSectionHeaderHeight = 0.000001
  100. return tableView
  101. }()
  102. private lazy var rightButton: UIButton = {
  103. let rightButton = UIButton(type: UIButton.ButtonType.custom)
  104. rightButton.setTitle("发布", for: UIControl.State.normal)
  105. rightButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  106. rightButton.titleLabel?.font = kMediumFont13
  107. rightButton.cornerRadius = 16
  108. rightButton.masksToBounds = true
  109. rightButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  110. self?.communityPublishApi()
  111. }).disposed(by: disposeBag)
  112. return rightButton
  113. }()
  114. private lazy var progressBackView: UIView = {
  115. let progressBackView = UIView()
  116. progressBackView.backgroundColor = kffffffColor
  117. return progressBackView
  118. }()
  119. private lazy var progressView: UIProgressView = {
  120. let progressView = UIProgressView(progressViewStyle: .default)
  121. // progressView.frame = CGRect(x: 0, y: 0, width: 200, height: 10)
  122. // progressView.layer.position = CGPoint(x: self.view.frame.width/2, y: 90)
  123. progressView.setProgress(0, animated: false)
  124. progressView.progressTintColor = k62CC74Color //进度颜色
  125. progressView.trackTintColor = kd8d8d8Color //剩余进度颜色
  126. //通过改变进度条高度(宽度不变,高度变为默认的2倍)
  127. // progressView.transform = CGAffineTransform(scaleX: 1.0, y: 2.0)
  128. return progressView
  129. }()
  130. private lazy var subLabel: UILabel = {
  131. let subLabel = UILabel()
  132. subLabel.text = "图片正在上传中(0/\(imageArr?.count ?? 0))..."
  133. subLabel.textColor = k333333Color
  134. subLabel.font = kRegularFont14
  135. subLabel.textAlignment = .left
  136. return subLabel
  137. }()
  138. }
  139. // MARK: - tableView dataSource && delegate
  140. extension PublishEditController : UITableViewDelegate, UITableViewDataSource {
  141. func numberOfSections(in tableView: UITableView) -> Int {
  142. return 1
  143. }
  144. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  145. return 5
  146. }
  147. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  148. switch indexPath.row {
  149. case 0:
  150. let picCell = PublishEditAddPicCell.cellWith(tableView: tableView, indexPath: indexPath)
  151. picCell.pubImageArr = imageArr
  152. picCell.reloadData()
  153. picCell.choosePicBlock = {
  154. [weak self] in
  155. print("\n----选取图片")
  156. }
  157. picCell.delPicTransBlock = {
  158. [weak self] (picIdx) in
  159. self?.imageArr?.remove(at: picIdx!)
  160. self?.imageUrlArray.remove(at: picIdx!)
  161. self?.tableView.reloadData()
  162. }
  163. return picCell
  164. case 1:
  165. let titleCell = PublishEditTitleCell.cellWith(tableView: tableView, indexPath: indexPath)
  166. titleCell.topicTextClosure = {
  167. [weak self] (text) in
  168. self?.pubTitle = text
  169. }
  170. return titleCell
  171. case 2:
  172. let desCell = PublishEditDescribeCell.cellWith(tableView: tableView, indexPath: indexPath)
  173. desCell.commentTextViewClosure = {
  174. [weak self] (text) in
  175. self?.pubContent = text
  176. }
  177. return desCell
  178. case 3:
  179. let topicCell = PublishEditAddTopicCell.cellWith(tableView: tableView, indexPath: indexPath)
  180. return topicCell
  181. case 4:
  182. let addrCell = PublishEditAddAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  183. return addrCell
  184. default:
  185. return UITableViewCell()
  186. }
  187. }
  188. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  189. switch indexPath.row {
  190. case 3:
  191. let vc = PublishAddTopicController()
  192. self.navigationController?.pushViewController(vc, animated: true)
  193. case 4:
  194. let vc = PublishAddAddressController()
  195. self.navigationController?.pushViewController(vc, animated: true)
  196. default:
  197. break
  198. }
  199. }
  200. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  201. switch indexPath.row {
  202. case 0:
  203. return 130
  204. case 1,3,4:
  205. return 50
  206. case 2:
  207. return 190
  208. default:
  209. return UITableView.automaticDimension
  210. }
  211. }
  212. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  213. return 0
  214. }
  215. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  216. return nil
  217. }
  218. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  219. return 0.000001
  220. }
  221. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  222. return nil
  223. }
  224. }
  225. // MARK: - 逻辑处理
  226. extension PublishEditController {
  227. func uploadAllImages(totalTimes: Int) {
  228. var curTimes: Int = totalTimes
  229. print("-----StartUpload,totalTimes == \(totalTimes)")
  230. SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgApi(imageArray: [imageArr![totalTimes]]) {
  231. [weak self] (imgUrl) -> (Void) in
  232. let urlStr: String = imgUrl as! String
  233. self?.imageUrlArray.append(urlStr)
  234. if curTimes == 0 {
  235. // 设置主图
  236. self?.majorImageUrl = urlStr
  237. }
  238. print("-----FinishUpload,Times == \(curTimes)")
  239. curTimes += 1
  240. // 更新进度
  241. self?.progressView.setProgress(Float(curTimes/(self?.imageArr!.count)!), animated: true)
  242. if curTimes == self?.imageArr!.count {
  243. self?.subLabel.text = "图片上传成功!"
  244. } else {
  245. self?.subLabel.text = "图片正在上传中(\(curTimes)/\(self?.imageArr?.count ?? 0))..."
  246. }
  247. if curTimes < self?.imageArr?.count ?? 0 {
  248. self?.uploadAllImages(totalTimes: curTimes)
  249. } else {
  250. self?.rightButton.isEnabled = true
  251. self?.rightButton.backgroundColor = k62CC74Color
  252. }
  253. }
  254. }
  255. // 发布Api
  256. func communityPublishApi() {
  257. if pubTitle.count > 20 {
  258. SwiftProgressHUD.shared().showText("标题最多输入20个字符")
  259. return
  260. }
  261. if pubContent.count == 0 {
  262. SwiftProgressHUD.shared().showText("还没输入描述内容")
  263. return
  264. }
  265. var typeStr: String = ""
  266. if mediaType == .image {
  267. typeStr = "image"
  268. } else {
  269. typeStr = "video"
  270. }
  271. let simuTopicIds: Array<String> = ["1","2"]
  272. let simuTopicJsonStr = JSON(simuTopicIds).description
  273. // SwiftProgressHUD.shared().showText("还没选择话题哟")
  274. let imgsJsonStr = JSON(imageUrlArray).description
  275. print("----mediaType == \(typeStr)\n----pubTitle = \(pubTitle)\n----simuTopicJsonStr == \(simuTopicJsonStr)\n----imgsJsonStr == \(imgsJsonStr)")
  276. SwiftMoyaNetWorkServiceCommunity.shared().communityPublishApi(type: typeStr, img: majorImageUrl ?? "", topic_ids: simuTopicJsonStr, video: "", title: pubTitle, content: pubContent, location: "", imgs: imgsJsonStr) {
  277. [weak self] (communityPublishModel) -> (Void) in
  278. let communityPublishModel = communityPublishModel as? CommunityPublishModel
  279. print("----发布成功")
  280. }
  281. }
  282. }