// // 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 import Kingfisher @objc(PublishMediaType) enum PublishMediaType: Int { case image = 0 case video = 1 } class PublishEditController: BaseViewController { deinit { NXLLog("deinit") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() KingfisherManager.shared.cache.clearDiskCache() KingfisherManager.shared.cache.clearMemoryCache() } @objc var mediaType: PublishMediaType = .image @objc var videoPath: String = "" @objc var coverImagePath: String = "" @objc var videoSize: CGSize = CGSize() @objc var videoImage: UIImage? { didSet { imageArr = [videoImage] as? Array } } var imageArr: Array? private var imageAssetUrl: Array = [] // 选中的话题 var selTopicModelArr: Array = [] // 已选话题id var selTopicIdArr: Array = [] // 已选话题name var selTopicName: String = "" // 标题 var pubTitle: String = "" // 内容 var pubContent: String = "" // 位置 var locationStr: String = "" // 纬度 var latitude: String = "" // 经度 var longitude: String = "" // MARK: 控制器生命周期 override func viewDidLoad() { super.viewDidLoad() setupViews() setupData() checkPublishButtonStatus() } override func viewWillAppear(_ animated: Bool) { // 禁用返回手势 if navigationController?.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) ?? false { navigationController?.interactivePopGestureRecognizer?.isEnabled = false } } override func viewWillDisappear(_ animated: Bool) { if navigationController?.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) ?? false { navigationController?.interactivePopGestureRecognizer?.isEnabled = true } } 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 self?.navigationController?.dismiss(animated: true, completion: 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(tableView) tableView.snp.makeConstraints { (make) in make.top.equalTo(kNavBarTotalHeight) 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 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 = 13 rightButton.masksToBounds = true rightButton.rx.tap.subscribe(onNext: { [weak self] (data) in self?.checkAndPublishAction() }).disposed(by: disposeBag) return rightButton }() } // 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.mediaType = mediaType if mediaType == .video { picCell.maxImageCount = 1 } picCell.reloadData() picCell.choosePicBlock = { [weak self] in let vc = KSMediaPickerController(maxVideoItemCount: 0, maxPictureItemCount: (UInt(9 - (self?.imageArr?.count ?? 0)))) vc.mediaType = KSMediaPickerController.mediaType.picture vc.jumpType = .publishEdit vc.pubEditImgClosure = { [weak self] (imageArray) in self?.imageArr = (self?.imageArr ?? Array()) + imageArray self?.tableView.reloadData() } vc.modalPresentationStyle = .fullScreen self?.present(vc, animated: true, completion: nil) } picCell.delPicTransBlock = { [weak self] (picIdx) in self?.imageArr?.remove(at: picIdx!) self?.tableView.reloadData() } return picCell case 1: let titleCell = PublishEditContentViewTitleCell.cellWith(tableView: tableView, indexPath: indexPath) titleCell.titleTextClosure = { [weak self] (text) in self?.pubTitle = text } return titleCell case 2: let desCell = PublishEditContentViewDescribeCell.cellWith(tableView: tableView, indexPath: indexPath) desCell.commentTextViewClosure = { [weak self] (text) in self?.pubContent = text self?.checkPublishButtonStatus() } return desCell case 3: let topicCell = PublishEditAddTopicCell.cellWith(tableView: tableView, indexPath: indexPath) topicCell.isTopicsNull = selTopicIdArr.count == 0 ? true : false topicCell.selTopicName = selTopicName return topicCell case 4: let addrCell = PublishEditAddAddressCell.cellWith(tableView: tableView, indexPath: indexPath) addrCell.location = locationStr return addrCell default: return UITableViewCell() } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { switch indexPath.row { case 3: let vc = PublishAddTopicController() vc.selTopicModelArr = self.selTopicModelArr vc.selTopicsClosure = { [weak self] (topicMdlArr) in self?.selTopicModelArr = topicMdlArr self?.selTopicName = "" self?.selTopicIdArr.removeAll() for (index,topicModel) in (topicMdlArr.enumerated()) { let strTopicId: String = String(format: "%d", topicModel.id ?? 0) self?.selTopicIdArr.append(strTopicId) if index == 0 { self?.selTopicName.append(topicModel.name!) } else { self?.selTopicName.append(", \(topicModel.name!)") } } self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none) self?.checkPublishButtonStatus() } self.navigationController?.pushViewController(vc, animated: true) case 4: let vc = PublishAddAddressController() vc.selectLocationClosure = { [weak self] (address,latitude,longitude) in self?.locationStr = address self?.latitude = latitude ?? "" self?.longitude = longitude ?? "" self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none) } 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.000001 } 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 checkAndPublishAction() { if pubTitle.count > 20 { SwiftProgressHUD.shared().showText("标题最多输入20个字符") return } if pubContent.count == 0 { SwiftProgressHUD.shared().showText("还没输入描述内容") return } if selTopicIdArr.count == 0 { SwiftProgressHUD.shared().showText("还没选择话题哟") return } ///发布参数 let aliyunVodUpToYoPublishModel = AliyunVodUpToYoPublishModel() aliyunVodUpToYoPublishModel.topicIds = self.selTopicIdArr aliyunVodUpToYoPublishModel.title = self.pubTitle aliyunVodUpToYoPublishModel.content = self.pubContent aliyunVodUpToYoPublishModel.locationString = self.locationStr aliyunVodUpToYoPublishModel.latitude = self.latitude aliyunVodUpToYoPublishModel.longitude = self.longitude if self.mediaType == .video { aliyunVodUpToYoPublishModel.publishType = .video aliyunVodUpToYoPublishModel.videoCoverImage = videoImage aliyunVodUpToYoPublishModel.videoCoverImageUrlPath = coverImagePath aliyunVodUpToYoPublishModel.videoPath = self.videoPath } if self.mediaType == .image { aliyunVodUpToYoPublishModel.publishType = .image aliyunVodUpToYoPublishModel.images = self.imageArr } self.dismissToRootViewController(animated: false) { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: aliyunVodUpToYoPublishModel) baseTabbarViewController?.selectedIndex = 0 } } // 改变发布按钮颜色 func checkPublishButtonStatus() { if pubContent.count == 0 || selTopicIdArr.count == 0 { rightButton.backgroundColor = kd8d8d8Color } else { rightButton.backgroundColor = kThemeColor } } }