CommunityVideoSubCommentController.swift 9.8 KB

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