123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- //
- // PublishEditController.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/17.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 编辑发布Vc
- import UIKit
- import RxSwift
- import SwiftyJSON
- import Photos
- enum PublishMediaType {
- case image
- case video
- }
- class PublishEditController: BaseViewController {
-
- var mediaType: PublishMediaType = .image
-
- var imgCount: Int = 0
- var majorImageUrl: String?
- var imageUrlArray: Array<String> = []
- var imageArr: Array<UIImage>? {
- didSet {
- imgCount = self.imageArr?.count ?? 0
- }
- }
-
- // 话题id
- var topic_ids: String = ""
- // 标题
- var pubTitle: String = ""
- // 内容
- var pubContent: String = ""
- // 位置
- var location: String = ""
-
- // MARK: 控制器生命周期
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupData()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- // imageUrlArray每次进入页面需置空
- imageUrlArray = []
- rightButton.isEnabled = false
- rightButton.backgroundColor = kd8d8d8Color
- uploadAllImages(totalTimes: 0)
- }
-
- override func setupViews() {
- self.view.backgroundColor = kffffffColor
-
- navigationBar.title = ""
- navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_black")!)
- navigationBar.onClickLeftButton = {
- [weak self] in
- AlertSheetView.alert(title: "是否退出内容发布?退出后你所编辑的内容无法恢复!", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
-
- }, confirmBlock: {
- (popupView, index, string) in
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: nil)
- })
- }
-
- view.addSubview(rightButton)
- rightButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight+8)
- make.right.equalTo(-14)
- make.width.equalTo(64)
- make.height.equalTo(26)
- }
-
- view.addSubview(progressBackView)
- progressBackView.addSubview(progressView)
- progressBackView.addSubview(subLabel)
-
- progressBackView.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kNavBarTotalHeight)
- make.left.right.equalToSuperview()
- make.height.equalTo(38)
- }
- progressView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo(3)
- }
- subLabel.snp.makeConstraints { (make) in
- make.left.equalTo(14)
- make.centerY.equalToSuperview()
- make.height.equalTo(20)
- }
-
- view.addSubview(tableView)
- tableView.snp.makeConstraints { (make) in
- make.top.equalTo(progressBackView.snp_bottom)
- make.left.right.bottom.equalToSuperview()
- }
- }
-
- override func setupData() {
-
- }
-
- 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
- tableView.estimatedRowHeight = 0.000001
- tableView.estimatedSectionFooterHeight = 0.000001
- tableView.estimatedSectionHeaderHeight = 0.000001
- return tableView
- }()
-
- private lazy var rightButton: UIButton = {
- let rightButton = UIButton(type: UIButton.ButtonType.custom)
- rightButton.setTitle("发布", for: UIControl.State.normal)
- rightButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
- rightButton.titleLabel?.font = kMediumFont13
- rightButton.cornerRadius = 16
- rightButton.masksToBounds = true
- rightButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- self?.communityPublishApi()
- }).disposed(by: disposeBag)
- return rightButton
- }()
-
- private lazy var progressBackView: UIView = {
- let progressBackView = UIView()
- progressBackView.backgroundColor = kffffffColor
- return progressBackView
- }()
-
- private lazy var progressView: UIProgressView = {
- let progressView = UIProgressView(progressViewStyle: .default)
- // progressView.frame = CGRect(x: 0, y: 0, width: 200, height: 10)
- // progressView.layer.position = CGPoint(x: self.view.frame.width/2, y: 90)
- progressView.setProgress(0, animated: false)
- progressView.progressTintColor = k62CC74Color //进度颜色
- progressView.trackTintColor = kd8d8d8Color //剩余进度颜色
- //通过改变进度条高度(宽度不变,高度变为默认的2倍)
- // progressView.transform = CGAffineTransform(scaleX: 1.0, y: 2.0)
- return progressView
- }()
-
- private lazy var subLabel: UILabel = {
- let subLabel = UILabel()
- subLabel.text = "图片正在上传中(0/\(imageArr?.count ?? 0))..."
- subLabel.textColor = k333333Color
- subLabel.font = kRegularFont14
- subLabel.textAlignment = .left
- return subLabel
- }()
-
- }
- // MARK: - tableView dataSource && delegate
- extension PublishEditController : UITableViewDelegate, UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 5
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.row {
- case 0:
- let picCell = PublishEditAddPicCell.cellWith(tableView: tableView, indexPath: indexPath)
- picCell.pubImageArr = imageArr
- picCell.reloadData()
- picCell.choosePicBlock = {
- [weak self] in
- print("\n----选取图片")
- }
- picCell.delPicTransBlock = {
- [weak self] (picIdx) in
- self?.imageArr?.remove(at: picIdx!)
- self?.imageUrlArray.remove(at: picIdx!)
- self?.tableView.reloadData()
- }
- return picCell
- case 1:
- let titleCell = PublishEditTitleCell.cellWith(tableView: tableView, indexPath: indexPath)
- titleCell.topicTextClosure = {
- [weak self] (text) in
- self?.pubTitle = text
- }
- return titleCell
- case 2:
- let desCell = PublishEditDescribeCell.cellWith(tableView: tableView, indexPath: indexPath)
- desCell.commentTextViewClosure = {
- [weak self] (text) in
- self?.pubContent = text
- }
- return desCell
- case 3:
- let topicCell = PublishEditAddTopicCell.cellWith(tableView: tableView, indexPath: indexPath)
- return topicCell
- case 4:
- let addrCell = PublishEditAddAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
- return addrCell
- default:
- return UITableViewCell()
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- switch indexPath.row {
- case 3:
- let vc = PublishAddTopicController()
- self.navigationController?.pushViewController(vc, animated: true)
- case 4:
- let vc = PublishAddAddressController()
- self.navigationController?.pushViewController(vc, animated: true)
- default:
- break
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.row {
- case 0:
- return 130
- case 1,3,4:
- return 50
- case 2:
- return 190
- default:
- return UITableView.automaticDimension
- }
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 0
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- 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 PublishEditController {
-
- func uploadAllImages(totalTimes: Int) {
-
- var curTimes: Int = totalTimes
-
- print("-----StartUpload,totalTimes == \(totalTimes)")
- SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgApi(imageArray: [imageArr![totalTimes]]) {
- [weak self] (imgUrl) -> (Void) in
- let urlStr: String = imgUrl as! String
- self?.imageUrlArray.append(urlStr)
- if curTimes == 0 {
- // 设置主图
- self?.majorImageUrl = urlStr
- }
-
- print("-----FinishUpload,Times == \(curTimes)")
- curTimes += 1
- // 更新进度
- self?.progressView.setProgress(Float(curTimes/(self?.imageArr!.count)!), animated: true)
- if curTimes == self?.imageArr!.count {
- self?.subLabel.text = "图片上传成功!"
- } else {
- self?.subLabel.text = "图片正在上传中(\(curTimes)/\(self?.imageArr?.count ?? 0))..."
- }
-
- if curTimes < self?.imageArr?.count ?? 0 {
- self?.uploadAllImages(totalTimes: curTimes)
- } else {
- self?.rightButton.isEnabled = true
- self?.rightButton.backgroundColor = k62CC74Color
- }
- }
- }
-
- // 发布Api
- func communityPublishApi() {
-
- if pubTitle.count > 20 {
- SwiftProgressHUD.shared().showText("标题最多输入20个字符")
- return
- }
-
- if pubContent.count == 0 {
- SwiftProgressHUD.shared().showText("还没输入描述内容")
- return
- }
-
- var typeStr: String = ""
- if mediaType == .image {
- typeStr = "image"
- } else {
- typeStr = "video"
- }
-
- let simuTopicIds: Array<String> = ["1","2"]
- let simuTopicJsonStr = JSON(simuTopicIds).description
- // SwiftProgressHUD.shared().showText("还没选择话题哟")
-
- let imgsJsonStr = JSON(imageUrlArray).description
-
- print("----mediaType == \(typeStr)\n----pubTitle = \(pubTitle)\n----simuTopicJsonStr == \(simuTopicJsonStr)\n----imgsJsonStr == \(imgsJsonStr)")
-
- SwiftMoyaNetWorkServiceCommunity.shared().communityPublishApi(type: typeStr, img: majorImageUrl ?? "", topic_ids: simuTopicJsonStr, video: "", title: pubTitle, content: pubContent, location: "", imgs: imgsJsonStr) {
- [weak self] (communityPublishModel) -> (Void) in
- let communityPublishModel = communityPublishModel as? CommunityPublishModel
- print("----发布成功")
- }
- }
-
- }
|