123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- //
- // CommunityAllCommentView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/7/4.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 视频列表页--评价View
- import UIKit
- import FWPopupView
- import RxSwift
- import RxCocoa
- class CommunityAllCommentView: FWPopupView {
-
- let disposeBag = DisposeBag()
-
- typealias DismissTransBlock = () -> Void
- var disTransBlock : DismissTransBlock?
-
- var postId: Int? {
- didSet {
- setupData()
- }
- }
-
- // 视频贴のModel
- var videoItemModel: CommunityVideoItemModel?
-
- // 评论
- var communityPostCommentsModel : CommunityPostCommentsModel?
- var communityPostCommentModels = Array<CommunityPostCommentModel>()
- var communityPostCommentModel : CommunityPostCommentModel?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- self.backgroundColor = kf7f8faColor
-
- addSubview(topView)
- topView.addSubview(titleLabel)
- topView.addSubview(dismissBtn)
-
- addSubview(noCommentsLabel)
- noCommentsLabel.isHidden = true
-
- addSubview(tableView)
- addSubview(commentInputView)
-
- setupLayouts()
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- func setupLayouts() {
- topView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo(48)
- }
- titleLabel.snp.makeConstraints { (make) in
- make.left.equalTo(14)
- make.top.equalTo(13)
- make.height.equalTo(23)
- }
- dismissBtn.snp.makeConstraints { (make) in
- make.top.equalTo(4)
- make.right.equalTo(-5)
- make.size.equalTo(40)
- }
-
- noCommentsLabel.snp.makeConstraints { (make) in
- make.top.equalTo(228)
- make.centerX.equalToSuperview()
- make.height.equalTo(23)
- }
-
- tableView.snp.makeConstraints { (make) in
- make.top.equalTo(topView.snp_bottom)
- make.left.right.equalToSuperview()
- make.bottom.equalTo(-48-kSafeTabBarHeight)
- }
- commentInputView.snp.makeConstraints { (make) in
- make.top.equalTo(tableView.snp_bottom)
- make.height.equalTo(48 + kSafeTabBarHeight)
- make.left.right.equalToSuperview()
- }
- }
-
- func setupData() {
- tableView.addHeader(withBeginRefresh: true, animation: false) {
- [weak self] (page) in
- self?.communityPostCommentApi(page: page)
- }
- tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
- [weak self] (page) in
- self?.communityPostCommentApi(page: page)
- })
- commentInputView.commentInputViewClosure = {
- [weak self] in
- self?.showKeyBoardCommentView(placeholder:"添加评论...")
- }
- }
-
- private lazy var topView: UIView = {
- let topView = UIView()
- topView.backgroundColor = kffffffColor
- return topView
- }()
-
- private lazy var titleLabel: UILabel = {
- let titleLabel = UILabel()
- titleLabel.textColor = k333333Color
- titleLabel.font = kRegularFont18
- return titleLabel
- }()
-
- private lazy var dismissBtn: UIButton = {
- let dismissBtn = UIButton(type: UIButton.ButtonType.custom)
- dismissBtn.setImage(kImage(name: "popup_btn_close_black"), for: .normal)
- dismissBtn.rx.tap.subscribe(onNext: { [weak self] (data) in
- if let disTransBlock = self?.disTransBlock {
- disTransBlock()
- }
- }).disposed(by: disposeBag)
- return dismissBtn
- }()
-
- private lazy var noCommentsLabel: UILabel = {
- let noCommentsLabel = UILabel()
- noCommentsLabel.text = "你不评一句,更新没动力"
- noCommentsLabel.textColor = k999999Color
- noCommentsLabel.font = kRegularFont16
- return noCommentsLabel
- }()
-
- lazy var commentInputView: CommentInputView = {
- let commentInputView = CommentInputView()
- return commentInputView
- }()
-
- 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
- }()
-
- /// 显示键盘
- func showKeyBoardCommentView(placeholder:String) {
-
- KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
- [weak self] text in
- self?.communityPostCommentApi(text: text, complete: {
- [weak self] in
- self?.communityPostCommentModel = nil
- })
- }) {
- [weak self] in
- self?.communityPostCommentModel = nil
- }
- }
-
- /// 自定义评论View
- class func communityAllCommentView(view:UIView? = nil,postId:Int, videoItemMdl: CommunityVideoItemModel) -> CommunityAllCommentView {
- let commentView = CommunityAllCommentView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: kSafeTabBarHeight + 500))
- commentView.attachedView = view
-
- commentView.configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
- commentView.postId = postId
- commentView.videoItemModel = videoItemMdl
- let vProperty = FWPopupViewProperty()
- vProperty.popupCustomAlignment = .bottomCenter
- vProperty.popupAnimationType = .frame
- vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
- vProperty.touchWildToHide = "1"
- vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
- vProperty.animationDuration = 0.3
- commentView.vProperty = vProperty
- commentView.show()
- commentView.disTransBlock = {
- commentView.hide()
- }
- return commentView
- }
-
- }
- extension CommunityAllCommentView : UITableViewDelegate, UITableViewDataSource {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if communityPostCommentModels.isEmpty {
- return UITableViewCell()
- } else {
- let cell = CommunityVideoMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.communityVideoItemModel = videoItemModel
- cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
- cell.frame = tableView.bounds
- cell.layoutIfNeeded()
- cell.reloadData()
- return cell
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- if !communityPostCommentModels.isEmpty {
- communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
- if communityPostCommentModel?.isDelete != 1 {
- CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
- [weak self] in
- self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
- self?.showKeyBoardCommentView(placeholder:"回复:@\(self?.communityPostCommentModel?.username ?? "")")
- }, deleteClosure: {
- [weak self] in
- self?.communityPostCommentModel?.isDelete = 1
- self?.communityPostCommentModel?.content = "该评论已被删除"
- tableView.reloadData()
- })
- }else {
- SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
- communityPostCommentModel = nil
- }
- }
-
-
-
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if communityPostCommentModels.isEmpty {
- return 0
- }else {
- return communityPostCommentModels[indexPath.row].height ?? 0
- }
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 10
- }
-
- 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
- }
-
- }
- extension CommunityAllCommentView {
-
- /// 评论列表
- ///
- /// - Parameters:
- /// - postId: 内容id
- /// - page: 分页
- func communityPostCommentApi(page:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: postId ?? 0, page: page,completion: {
- [weak self] (communityPostCommentsModel) -> (Void) in
- let communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
- self?.communityPostCommentsModel = communityPostCommentsModel
- let totalComments: Int = communityPostCommentsModel?.commentCount ?? 0
- self?.titleLabel.text = "全部评论 \(totalComments)"
- if totalComments == 0 {
- self?.tableView.isHidden = true
- self?.noCommentsLabel.isHidden = false
- } else {
- self?.tableView.isHidden = false
- self?.noCommentsLabel.isHidden = true
- }
-
- if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
- self?.communityPostCommentModels.removeAll()
- self?.tableView.resetNoMoreData()
-
- }
- self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
- self?.tableView.reloadData()
-
- MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView, pagination: communityPostCommentsModel?.pagination)
-
- }) {
- [weak self] loadingStatus in
- MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView,loadingStatus:loadingStatus)
- }
- }
-
- /// 评论
- func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
- let communityCustomCommnetModel = CommunityCustomCommnetModel()
- communityCustomCommnetModel.postId = videoItemModel?.id ?? 0
- communityCustomCommnetModel.content = text
- if communityPostCommentModel != nil {
- communityCustomCommnetModel.parentId = communityPostCommentModel?.id
- communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
- communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
- }
-
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
- [weak self] (communityPostCommentIdModel) -> (Void) in
-
- let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
-
- if self?.communityPostCommentModel == nil { //评论
- let communityPostCommentModel = CommunityPostCommentModel()
- communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
- communityPostCommentModel.content = text
- communityPostCommentModel.createdAt = "刚刚"
- communityPostCommentModel.id = communityPostCommentIdModel?.id
- communityPostCommentModel.username = UserModel.shared().getModel()?.username
- communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
- self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
-
- // 若此条评论前列表没有评论,显示table
- let preTotalComments: Int = self?.communityPostCommentsModel?.pagination?.total ?? 0
- if preTotalComments == 0 {
- self?.tableView.isHidden = false
- self?.noCommentsLabel.isHidden = true
- }
-
- let count = 1 + (self?.communityPostCommentsModel?.commentCount ?? 0)
- self?.communityPostCommentsModel?.commentCount = count
- self?.titleLabel.text = "全部评论 \(count)"
- VirusViewModel.shared.comment(communityVideoItemModel: (self?.videoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
-
- self?.tableView.reloadData()
-
- }else { //回评论
- let communityPostReplyModel = CommunityPostReplyModel()
- communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
- communityPostReplyModel.content = text
- communityPostReplyModel.createdAt = "刚刚"
- communityPostReplyModel.id = communityPostCommentIdModel?.id
- communityPostReplyModel.username = UserModel.shared().getModel()?.username
- communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
- let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
- self?.communityPostCommentModel?.replyCount = count
- if self?.communityPostCommentModel?.reply == nil {
- self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
- }
- self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
- self?.communityPostCommentModel?.replyCount = 1 + (self?.communityPostCommentModel?.replyCount ?? 0)
-
- VirusViewModel.shared.comment(communityVideoItemModel: (self?.videoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
-
- self?.tableView.reloadData()
- }
- complete()
-
- }
- }
-
- }
|