CommunityVideoSubCommentController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // CommunityVideoSubCommentController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/7/11.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 视频--子评论Vc
  8. import UIKit
  9. import IQKeyboardManagerSwift
  10. class CommunityVideoSubCommentController: BaseViewController {
  11. override func didReceiveMemoryWarning() {
  12. super.didReceiveMemoryWarning()
  13. }
  14. var communityVideoItemModel : CommunityVideoItemModel?
  15. var communityPostCommentModel : CommunityPostCommentModel?
  16. var communityPostReplysModel : CommunityPostReplysModel?
  17. var communityPostReplyModels = Array<CommunityPostReplyModel>()
  18. var communityPostReplyModel : CommunityPostReplyModel?
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. setupViews()
  22. setupLayouts()
  23. setupData()
  24. }
  25. override func setupViews() {
  26. navigationBar.title = "3条评论"
  27. view.backgroundColor = kf7f8faColor
  28. view.addSubview(tableView)
  29. view.addSubview(commentInputView)
  30. }
  31. override func setupLayouts() {
  32. tableView.snp.makeConstraints { (make) in
  33. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  34. make.left.right.equalTo(0)
  35. make.bottom.equalTo(commentInputView.snp.top)
  36. }
  37. commentInputView.snp.makeConstraints { (make) in
  38. make.bottom.left.right.equalToSuperview()
  39. make.height.equalTo(48 + kSafeTabBarHeight)
  40. }
  41. }
  42. lazy var tableView: UITableView = {
  43. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  44. tableView.separatorStyle = .none
  45. tableView.backgroundColor = kf7f8faColor
  46. tableView.dataSource = self
  47. tableView.delegate = self
  48. return tableView
  49. }()
  50. lazy var commentInputView: CommentInputView = {
  51. let commentInputView = CommentInputView()
  52. return commentInputView
  53. }()
  54. override func setupData() {
  55. tableView.addHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in
  56. self?.communityPostReplyApi(page: page)
  57. }
  58. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  59. [weak self] (page) in
  60. self?.communityPostReplyApi(page: page)
  61. })
  62. commentInputView.commentInputViewClosure = {
  63. [weak self] in
  64. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  65. }
  66. }
  67. /// 显示键盘
  68. func showKeyBoardCommentView(placeholder:String) {
  69. KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
  70. [weak self] text in
  71. self?.communityPostCommentApi(text: text, complete: {
  72. [weak self] in
  73. self?.communityPostReplyModel = nil
  74. })
  75. }) {
  76. [weak self] in
  77. self?.communityPostReplyModel = nil
  78. }
  79. }
  80. }
  81. extension CommunityVideoSubCommentController {
  82. /// 获取子评论
  83. ///
  84. /// - Parameter page: 分页
  85. func communityPostReplyApi(page:Int) {
  86. SwiftMoyaNetWorkServiceCommunity.shared().communityPostReplyApi(postId: communityPostCommentModel?.id ?? 0, page: page,completion: {
  87. [weak self] (communityPostReplysModel) -> (Void) in
  88. let communityPostReplysModel = communityPostReplysModel as? CommunityPostReplysModel
  89. if communityPostReplysModel?.pagination?.currentPage == 1{
  90. self?.communityPostReplyModels.removeAll()
  91. self?.tableView.resetNoMoreData()
  92. }
  93. self?.communityPostReplyModels = (self?.communityPostReplyModels)! + (communityPostReplysModel?.data!)!
  94. self?.navigationBar.title = "\(communityPostReplysModel?.pagination?.total ?? 0)条评论"
  95. self?.tableView.reloadData()
  96. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostReplysModel?.pagination)
  97. }) {
  98. [weak self] loadingStatus in
  99. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  100. }
  101. }
  102. func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
  103. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  104. communityCustomCommnetModel.postId = communityVideoItemModel?.id ?? 0
  105. communityCustomCommnetModel.content = text
  106. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  107. communityCustomCommnetModel.replyUid = communityPostReplyModel?.uid
  108. communityCustomCommnetModel.replyUsername = communityPostReplyModel?.username
  109. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  110. [weak self] (communityPostCommentIdModel) -> (Void) in
  111. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  112. let communityPostReplyModel = CommunityPostReplyModel()
  113. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  114. communityPostReplyModel.content = text
  115. communityPostReplyModel.createdAt = "刚刚"
  116. communityPostReplyModel.id = communityPostCommentIdModel?.id
  117. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  118. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  119. if self?.communityPostReplyModel != nil {
  120. communityPostReplyModel.replyUsername = self?.communityPostReplyModel?.username
  121. }
  122. self?.communityPostReplyModels.insert(communityPostReplyModel, at: 0)
  123. self?.communityPostReplysModel?.pagination?.total = 1 + (self?.communityPostReplysModel?.pagination?.total ?? 0)
  124. self?.tableView.reloadSections([1], with: UITableView.RowAnimation.none)
  125. self?.navigationBar.title = "\(self?.communityPostReplysModel?.pagination?.total ?? 0)条评论"
  126. if self?.communityPostReplyModel != nil {
  127. VirusViewModel.shared.comment(communityVideoItemModel: (self?.communityVideoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: (self?.communityPostCommentModel)!, communityPostReplyModel: (self?.communityPostReplyModel)!)
  128. }else {
  129. VirusViewModel.shared.comment(communityVideoItemModel: (self?.communityVideoItemModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: (self?.communityPostCommentModel)!)
  130. }
  131. complete()
  132. }
  133. }
  134. }
  135. // MARK: - tableView dataSource && delegate
  136. extension CommunityVideoSubCommentController: UITableViewDataSource, UITableViewDelegate {
  137. func numberOfSections(in tableView: UITableView) -> Int {
  138. return 2
  139. }
  140. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  141. switch section {
  142. case 0:
  143. if communityPostCommentModel != nil {
  144. return 1
  145. }else {
  146. return 0
  147. }
  148. default:
  149. return communityPostReplyModels.isEmpty ? 0 : communityPostReplyModels.count
  150. }
  151. }
  152. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  153. switch indexPath.section {
  154. case 0:
  155. let cell = CommunityMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  156. cell.communityPostCommentModel = communityPostCommentModel
  157. return cell
  158. default:
  159. let cell = CommunityReplyCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  160. cell.communityPostReplyModel = communityPostReplyModels[indexPath.row]
  161. return cell
  162. }
  163. }
  164. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  165. tableView.deselectRow(at: indexPath, animated: true)
  166. switch indexPath.section {
  167. case 0:
  168. break
  169. default:
  170. communityPostReplyModel = communityPostReplyModels[indexPath.row]
  171. if communityPostReplyModel?.isDelete != 1 {
  172. CommentReplyView.commentReplyView(id:communityPostReplyModel?.id,uid: communityPostReplyModel?.uid,userName: communityPostReplyModel?.username ?? "", content: communityPostReplyModel?.content ?? "", replyClosure: {
  173. [weak self] in
  174. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostReplyModel?.username ?? "")")
  175. }, deleteClosure: {
  176. [weak self] in
  177. self?.communityPostReplyModel?.isDelete = 1
  178. self?.communityPostReplyModel?.content = "该回复已被删除"
  179. tableView.reloadData()
  180. })
  181. }else {
  182. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  183. }
  184. break
  185. }
  186. }
  187. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  188. switch indexPath.section {
  189. case 0:
  190. return communityPostCommentModel?.height ?? 0
  191. default:
  192. return communityPostReplyModels[indexPath.row].height ?? 0
  193. }
  194. }
  195. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  196. return 10
  197. }
  198. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  199. return UIView()
  200. }
  201. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  202. return 0.000001
  203. }
  204. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  205. return UIView()
  206. }
  207. }