CommunityAllCommentView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // CommunityAllCommentView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/7/4.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 视频列表页--评价View
  8. import UIKit
  9. import FWPopupView
  10. import RxSwift
  11. import RxCocoa
  12. class CommunityAllCommentView: FWPopupView {
  13. let disposeBag = DisposeBag()
  14. typealias DismissTransBlock = () -> Void
  15. var disTransBlock : DismissTransBlock?
  16. var postId: Int? {
  17. didSet {
  18. setupData()
  19. }
  20. }
  21. // 视频贴のModel
  22. var videoItemModel: CommunityVideoItemModel?
  23. // 评论
  24. var communityPostCommentsModel : CommunityPostCommentsModel?
  25. var communityPostCommentModels = Array<CommunityPostCommentModel>()
  26. var communityPostCommentModel : CommunityPostCommentModel?
  27. override init(frame: CGRect) {
  28. super.init(frame: frame)
  29. self.backgroundColor = kf7f8faColor
  30. addSubview(topView)
  31. topView.addSubview(titleLabel)
  32. topView.addSubview(dismissBtn)
  33. addSubview(noCommentsLabel)
  34. noCommentsLabel.isHidden = true
  35. addSubview(tableView)
  36. addSubview(commentInputView)
  37. setupLayouts()
  38. }
  39. required init?(coder aDecoder: NSCoder) {
  40. fatalError("init(coder:) has not been implemented")
  41. }
  42. func setupLayouts() {
  43. topView.snp.makeConstraints { (make) in
  44. make.top.left.right.equalToSuperview()
  45. make.height.equalTo(48)
  46. }
  47. titleLabel.snp.makeConstraints { (make) in
  48. make.left.equalTo(14)
  49. make.top.equalTo(13)
  50. make.height.equalTo(23)
  51. }
  52. dismissBtn.snp.makeConstraints { (make) in
  53. make.top.equalTo(4)
  54. make.right.equalTo(-5)
  55. make.size.equalTo(40)
  56. }
  57. noCommentsLabel.snp.makeConstraints { (make) in
  58. make.top.equalTo(228)
  59. make.centerX.equalToSuperview()
  60. make.height.equalTo(23)
  61. }
  62. tableView.snp.makeConstraints { (make) in
  63. make.top.equalTo(topView.snp_bottom)
  64. make.left.right.equalToSuperview()
  65. make.bottom.equalTo(-48-kSafeTabBarHeight)
  66. }
  67. commentInputView.snp.makeConstraints { (make) in
  68. make.top.equalTo(tableView.snp_bottom)
  69. make.height.equalTo(48 + kSafeTabBarHeight)
  70. make.left.right.equalToSuperview()
  71. }
  72. }
  73. func setupData() {
  74. tableView.addHeader(withBeginRefresh: true, animation: false) {
  75. [weak self] (page) in
  76. self?.communityPostCommentApi(page: page)
  77. }
  78. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  79. [weak self] (page) in
  80. self?.communityPostCommentApi(page: page)
  81. })
  82. commentInputView.commentInputViewClosure = {
  83. [weak self] in
  84. self?.showKeyBoardCommentView(placeholder:"添加评论...")
  85. }
  86. }
  87. private lazy var topView: UIView = {
  88. let topView = UIView()
  89. topView.backgroundColor = kffffffColor
  90. return topView
  91. }()
  92. private lazy var titleLabel: UILabel = {
  93. let titleLabel = UILabel()
  94. titleLabel.textColor = k333333Color
  95. titleLabel.font = kRegularFont18
  96. return titleLabel
  97. }()
  98. private lazy var dismissBtn: UIButton = {
  99. let dismissBtn = UIButton(type: UIButton.ButtonType.custom)
  100. dismissBtn.setImage(kImage(name: "popup_btn_close_black"), for: .normal)
  101. dismissBtn.rx.tap.subscribe(onNext: { [weak self] (data) in
  102. if let disTransBlock = self?.disTransBlock {
  103. disTransBlock()
  104. }
  105. }).disposed(by: disposeBag)
  106. return dismissBtn
  107. }()
  108. private lazy var noCommentsLabel: UILabel = {
  109. let noCommentsLabel = UILabel()
  110. noCommentsLabel.text = "你不评一句,更新没动力"
  111. noCommentsLabel.textColor = k999999Color
  112. noCommentsLabel.font = kRegularFont16
  113. return noCommentsLabel
  114. }()
  115. lazy var commentInputView: CommentInputView = {
  116. let commentInputView = CommentInputView()
  117. return commentInputView
  118. }()
  119. lazy var tableView: UITableView = {
  120. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  121. tableView.separatorStyle = .none
  122. tableView.backgroundColor = kffffffColor
  123. tableView.dataSource = self
  124. tableView.delegate = self
  125. tableView.estimatedRowHeight = 0.000001
  126. tableView.estimatedSectionFooterHeight = 0.000001
  127. tableView.estimatedSectionHeaderHeight = 0.000001
  128. return tableView
  129. }()
  130. /// 显示键盘
  131. func showKeyBoardCommentView(placeholder:String) {
  132. KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
  133. [weak self] text in
  134. self?.communityPostCommentApi(text: text, complete: {
  135. [weak self] in
  136. self?.communityPostCommentModel = nil
  137. })
  138. }) {
  139. [weak self] in
  140. self?.communityPostCommentModel = nil
  141. }
  142. }
  143. /// 自定义评论View
  144. class func communityAllCommentView(view:UIView? = nil,postId:Int, videoItemMdl: CommunityVideoItemModel) -> CommunityAllCommentView {
  145. let commentView = CommunityAllCommentView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: kSafeTabBarHeight + 500))
  146. commentView.attachedView = view
  147. commentView.configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
  148. commentView.postId = postId
  149. commentView.videoItemModel = videoItemMdl
  150. let vProperty = FWPopupViewProperty()
  151. vProperty.popupCustomAlignment = .bottomCenter
  152. vProperty.popupAnimationType = .frame
  153. vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
  154. vProperty.touchWildToHide = "1"
  155. vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  156. vProperty.animationDuration = 0.3
  157. commentView.vProperty = vProperty
  158. commentView.show()
  159. commentView.disTransBlock = {
  160. commentView.hide()
  161. }
  162. return commentView
  163. }
  164. }
  165. extension CommunityAllCommentView : UITableViewDelegate, UITableViewDataSource {
  166. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  167. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  168. }
  169. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  170. if communityPostCommentModels.isEmpty {
  171. return UITableViewCell()
  172. } else {
  173. let cell = CommunityVideoMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  174. cell.communityVideoItemModel = videoItemModel
  175. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  176. cell.frame = tableView.bounds
  177. cell.layoutIfNeeded()
  178. cell.reloadData()
  179. return cell
  180. }
  181. }
  182. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  183. if !communityPostCommentModels.isEmpty {
  184. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  185. if communityPostCommentModel?.isDelete != 1 {
  186. CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
  187. [weak self] in
  188. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  189. self?.showKeyBoardCommentView(placeholder:"回复:@\(self?.communityPostCommentModel?.username ?? "")")
  190. }, deleteClosure: {
  191. [weak self] in
  192. self?.communityPostCommentModel?.isDelete = 1
  193. self?.communityPostCommentModel?.content = "该评论已被删除"
  194. tableView.reloadData()
  195. })
  196. }else {
  197. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  198. communityPostCommentModel = nil
  199. }
  200. }
  201. }
  202. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  203. if communityPostCommentModels.isEmpty {
  204. return 0
  205. }else {
  206. return communityPostCommentModels[indexPath.row].height ?? 0
  207. }
  208. }
  209. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  210. return 10
  211. }
  212. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  213. return nil
  214. }
  215. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  216. return 0.000001
  217. }
  218. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  219. return nil
  220. }
  221. }
  222. extension CommunityAllCommentView {
  223. /// 评论列表
  224. ///
  225. /// - Parameters:
  226. /// - postId: 内容id
  227. /// - page: 分页
  228. func communityPostCommentApi(page:Int) {
  229. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: postId ?? 0, page: page,completion: {
  230. [weak self] (communityPostCommentsModel) -> (Void) in
  231. let communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  232. self?.communityPostCommentsModel = communityPostCommentsModel
  233. let totalComments: Int = communityPostCommentsModel?.commentCount ?? 0
  234. self?.titleLabel.text = "全部评论 \(totalComments)"
  235. if totalComments == 0 {
  236. self?.tableView.isHidden = true
  237. self?.noCommentsLabel.isHidden = false
  238. } else {
  239. self?.tableView.isHidden = false
  240. self?.noCommentsLabel.isHidden = true
  241. }
  242. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  243. self?.communityPostCommentModels.removeAll()
  244. self?.tableView.resetNoMoreData()
  245. }
  246. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  247. self?.tableView.reloadData()
  248. MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView, pagination: communityPostCommentsModel?.pagination)
  249. }) {
  250. [weak self] loadingStatus in
  251. MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView,loadingStatus:loadingStatus)
  252. }
  253. }
  254. /// 评论
  255. func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
  256. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  257. communityCustomCommnetModel.postId = videoItemModel?.id ?? 0
  258. communityCustomCommnetModel.content = text
  259. if communityPostCommentModel != nil {
  260. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  261. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  262. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  263. }
  264. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  265. [weak self] (communityPostCommentIdModel) -> (Void) in
  266. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  267. if self?.communityPostCommentModel == nil { //评论
  268. let communityPostCommentModel = CommunityPostCommentModel()
  269. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  270. communityPostCommentModel.content = text
  271. communityPostCommentModel.createdAt = "刚刚"
  272. communityPostCommentModel.id = communityPostCommentIdModel?.id
  273. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  274. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  275. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  276. // 若此条评论前列表没有评论,显示table
  277. let preTotalComments: Int = self?.communityPostCommentsModel?.pagination?.total ?? 0
  278. if preTotalComments == 0 {
  279. self?.tableView.isHidden = false
  280. self?.noCommentsLabel.isHidden = true
  281. }
  282. let count = 1 + (self?.communityPostCommentsModel?.commentCount ?? 0)
  283. self?.communityPostCommentsModel?.commentCount = count
  284. self?.titleLabel.text = "全部评论 \(count)"
  285. VirusViewModel.shared.comment(communityVideoItemModel: (self?.videoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  286. self?.tableView.reloadData()
  287. }else { //回评论
  288. let communityPostReplyModel = CommunityPostReplyModel()
  289. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  290. communityPostReplyModel.content = text
  291. communityPostReplyModel.createdAt = "刚刚"
  292. communityPostReplyModel.id = communityPostCommentIdModel?.id
  293. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  294. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  295. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  296. self?.communityPostCommentModel?.replyCount = count
  297. if self?.communityPostCommentModel?.reply == nil {
  298. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  299. }
  300. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  301. self?.communityPostCommentModel?.replyCount = 1 + (self?.communityPostCommentModel?.replyCount ?? 0)
  302. VirusViewModel.shared.comment(communityVideoItemModel: (self?.videoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  303. self?.tableView.reloadData()
  304. }
  305. complete()
  306. }
  307. }
  308. }