CommunitySubCommentController.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // CommunitySubCommentController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/12.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import IQKeyboardManagerSwift
  10. class CommunitySubCommentController: BaseViewController {
  11. var communityPostDetailModel : CommunityPostDetailModel?
  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 = "0条评论"
  25. view.backgroundColor = kf7f8faColor
  26. view.addSubview(tableView)
  27. view.addSubview(commentInputView)
  28. }
  29. override func setupLayouts() {
  30. commentInputView.snp.makeConstraints { (make) in
  31. make.left.right.bottom.equalToSuperview()
  32. make.height.equalTo(48 + kSafeTabBarHeight)
  33. }
  34. tableView.snp.makeConstraints { (make) in
  35. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  36. make.left.right.equalTo(0)
  37. make.bottom.equalTo(commentInputView.snp.top)
  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. }
  67. }
  68. /// 显示键盘
  69. func showKeyBoardCommentView(placeholder:String) {
  70. keyBoardCommentView = KeyBoardCommentView.keyBoardCommentView(placeholder: placeholder,sendClosure: {
  71. [weak self] text in
  72. self?.communityPostCommentApi(text: text, complete: {
  73. self?.keyBoardCommentView?.dismisskeyBoardCommentView()
  74. })
  75. })
  76. }
  77. /// 隐藏键盘
  78. func hiddenKeyBoardCommentView() {
  79. keyBoardCommentView?.hiddenViewClosure = {
  80. [weak self] in
  81. self?.communityPostReplyModel = nil
  82. }
  83. }
  84. }
  85. extension CommunitySubCommentController {
  86. /// 获取子评论
  87. ///
  88. /// - Parameter page: 分页
  89. func communityPostReplyApi(page:Int) {
  90. SwiftMoyaNetWorkServiceCommunity.shared().communityPostReplyApi(postId: communityPostCommentModel?.id ?? 0, page: page) { [weak self] (communityPostReplysModel) -> (Void) in
  91. let communityPostReplysModel = communityPostReplysModel as? CommunityPostReplysModel
  92. self?.navigationBar.title = "\(self?.communityPostReplysModel?.pagination?.total ?? 0)条评论"
  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?.tableView.reloadData()
  99. if self?.communityPostReplyModels.count ?? 0 >= communityPostReplysModel?.pagination?.total ?? 0 {
  100. self?.tableView.endFooterNoMoreData()
  101. }
  102. }else {
  103. self?.tableView.endFooterNoMoreData()
  104. }
  105. }
  106. }
  107. func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
  108. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  109. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  110. communityCustomCommnetModel.content = text
  111. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  112. communityCustomCommnetModel.replyUid = communityPostReplyModel?.uid
  113. communityCustomCommnetModel.replyUsername = communityPostReplyModel?.username
  114. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  115. [weak self] (communityPostCommentIdModel) -> (Void) in
  116. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  117. let communityPostReplyModel = CommunityPostReplyModel()
  118. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  119. communityPostReplyModel.content = text
  120. communityPostReplyModel.createdAt = "刚刚"
  121. communityPostReplyModel.id = communityPostCommentIdModel?.id
  122. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  123. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  124. if self?.communityPostReplyModel != nil {
  125. communityPostReplyModel.replyUsername = self?.communityPostReplyModel?.username
  126. }
  127. self?.communityPostReplysModel?.pagination?.total = 1 + (self?.communityPostReplysModel?.pagination?.total ?? 0)
  128. self?.tableView.reloadSections([1], with: UITableView.RowAnimation.none)
  129. self?.navigationBar.title = "\(self?.communityPostReplysModel?.pagination?.total ?? 0)条评论"
  130. if self?.communityPostReplyModel != nil {
  131. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0, communityPostCommentModel: (self?.communityPostCommentModel)!, communityPostReplyModel: (self?.communityPostReplyModel)!)
  132. }else {
  133. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0, communityPostCommentModel: (self?.communityPostCommentModel)!)
  134. }
  135. complete()
  136. }
  137. }
  138. }
  139. // MARK: - tableView dataSource && delegate
  140. extension CommunitySubCommentController: UITableViewDataSource, UITableViewDelegate {
  141. func numberOfSections(in tableView: UITableView) -> Int {
  142. return 2
  143. }
  144. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  145. switch section {
  146. case 0:
  147. if communityPostCommentModel != nil {
  148. return 1
  149. }else {
  150. return 0
  151. }
  152. default:
  153. return communityPostReplyModels.isEmpty ? 0 : communityPostReplyModels.count
  154. }
  155. }
  156. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  157. switch indexPath.section {
  158. case 0:
  159. let cell = CommunityMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  160. cell.communityPostCommentModel = communityPostCommentModel
  161. return cell
  162. default:
  163. let cell = CommunityReplyCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  164. cell.communityPostReplyModel = communityPostReplyModels[indexPath.row]
  165. return cell
  166. }
  167. }
  168. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  169. tableView.deselectRow(at: indexPath, animated: true)
  170. switch indexPath.section {
  171. case 0:
  172. break
  173. default:
  174. communityPostReplyModel = communityPostReplyModels[indexPath.row]
  175. AlertSheetView.sheetCommentReplyView(userName: communityPostReplyModel?.username ?? "", content: communityPostReplyModel?.content ?? "") {
  176. [weak self] in
  177. self?.communityPostReplyModel = self?.communityPostReplyModels[indexPath.row]
  178. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostReplyModel?.username ?? "")")
  179. self?.hiddenKeyBoardCommentView()
  180. }
  181. break
  182. }
  183. }
  184. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  185. switch indexPath.section {
  186. case 0:
  187. return communityPostCommentModel?.height ?? 0
  188. default:
  189. return communityPostReplyModels[indexPath.row].height ?? 0
  190. }
  191. }
  192. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  193. return 10
  194. }
  195. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  196. return UIView()
  197. }
  198. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  199. return 0.000001
  200. }
  201. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  202. return UIView()
  203. }
  204. }