CommunitySubCommentController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import Kingfisher
  11. class CommunitySubCommentController: BaseViewController {
  12. override func didReceiveMemoryWarning() {
  13. super.didReceiveMemoryWarning()
  14. KingfisherManager.shared.cache.clearDiskCache()
  15. KingfisherManager.shared.cache.clearMemoryCache()
  16. }
  17. var communityPostDetailModel : CommunityPostDetailModel?
  18. var communityPostCommentModel : CommunityPostCommentModel?
  19. var communityPostReplysModel : CommunityPostReplysModel?
  20. var communityPostReplyModels = Array<CommunityPostReplyModel>()
  21. var communityPostReplyModel : CommunityPostReplyModel?
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. setupViews()
  25. setupLayouts()
  26. setupData()
  27. }
  28. override func setupViews() {
  29. navigationBar.title = "0条评论"
  30. view.backgroundColor = kf7f8faColor
  31. view.addSubview(tableView)
  32. view.addSubview(commentInputView)
  33. }
  34. override func setupLayouts() {
  35. commentInputView.snp.makeConstraints { (make) in
  36. make.left.right.bottom.equalToSuperview()
  37. make.height.equalTo(48 + kSafeTabBarHeight)
  38. }
  39. tableView.snp.makeConstraints { (make) in
  40. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  41. make.left.right.equalTo(0)
  42. make.bottom.equalTo(commentInputView.snp.top)
  43. }
  44. }
  45. lazy var tableView: UITableView = {
  46. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  47. tableView.separatorStyle = .none
  48. tableView.backgroundColor = kf7f8faColor
  49. tableView.dataSource = self
  50. tableView.delegate = self
  51. return tableView
  52. }()
  53. lazy var commentInputView: CommentInputView = {
  54. let commentInputView = CommentInputView()
  55. return commentInputView
  56. }()
  57. override func setupData() {
  58. tableView.addHeader(withBeginRefresh: true, animation: true) { [weak self] (page) in
  59. self?.communityPostReplyApi(page: page)
  60. }
  61. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  62. [weak self] (page) in
  63. self?.communityPostReplyApi(page: page)
  64. })
  65. commentInputView.commentInputViewClosure = {
  66. [weak self] in
  67. self?.communityPostReplyModel = nil
  68. self?.showKeyBoardCommentView(placeholder:"添加评论...")
  69. }
  70. }
  71. /// 显示键盘
  72. func showKeyBoardCommentView(placeholder:String) {
  73. KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
  74. [weak self] text in
  75. self?.communityPostCommentApi(text: text, complete: {
  76. [weak self] in
  77. self?.communityPostReplyModel = nil
  78. })
  79. }) {
  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,completion: {
  91. [weak self] (communityPostReplysModel) -> (Void) in
  92. let communityPostReplysModel = communityPostReplysModel as? CommunityPostReplysModel
  93. if communityPostReplysModel?.pagination?.currentPage == 1{
  94. self?.communityPostReplyModels.removeAll()
  95. self?.tableView.resetNoMoreData()
  96. }
  97. self?.communityPostReplyModels = (self?.communityPostReplyModels)! + (communityPostReplysModel?.data!)!
  98. self?.tableView.reloadData()
  99. self?.navigationBar.title = "\(self?.communityPostReplyModels.count ?? 0)条评论"
  100. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostReplysModel?.pagination)
  101. }) {
  102. [weak self] loadingStatus in
  103. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  104. }
  105. }
  106. func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
  107. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  108. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  109. communityCustomCommnetModel.content = text
  110. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  111. if self.communityPostReplyModel != nil {
  112. communityCustomCommnetModel.replyUid = communityPostReplyModel?.uid
  113. communityCustomCommnetModel.replyUsername = communityPostReplyModel?.username
  114. }
  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?.communityPostReplysModel?.pagination?.total = 1 + (self?.communityPostReplysModel?.pagination?.total ?? 0)
  129. self?.communityPostReplyModels.insert(communityPostReplyModel, at: 0)
  130. self?.tableView.reloadData()
  131. self?.navigationBar.title = "\(self?.communityPostReplyModels.count ?? 0)条评论"
  132. if self?.communityPostReplyModel != nil {
  133. VirusViewModel.shared.commentReplyAIT(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text,communityPostCommentModel: (self?.communityPostCommentModel)!,communityPostReplyModel: self?.communityPostReplyModel)
  134. }else {
  135. VirusViewModel.shared.commentReply(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: (self?.communityPostCommentModel)!)
  136. }
  137. complete()
  138. }
  139. }
  140. }
  141. // MARK: - tableView dataSource && delegate
  142. extension CommunitySubCommentController: 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. if communityPostReplyModel?.isDelete != 1 {
  178. CommentReplyView.commentReplyView(id: communityPostReplyModel?.id, uid: communityPostReplyModel?.uid, userName: communityPostReplyModel?.username ?? "", content: communityPostReplyModel?.content ?? "", replyClosure: {
  179. [weak self] in
  180. self?.communityPostReplyModel = self?.communityPostReplyModels[indexPath.row]
  181. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostReplyModel?.username ?? "")")
  182. }, deleteClosure: {
  183. [weak self,weak tableView] in
  184. self?.communityPostReplyModel?.isDelete = 1
  185. self?.communityPostReplyModel?.content = "该回复已被删除"
  186. tableView?.reloadData()
  187. })
  188. }else {
  189. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  190. }
  191. break
  192. }
  193. }
  194. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  195. switch indexPath.section {
  196. case 0:
  197. return communityPostCommentModel?.height ?? 0
  198. default:
  199. return communityPostReplyModels[indexPath.row].height ?? 0
  200. }
  201. }
  202. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  203. return 10
  204. }
  205. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  206. return UIView()
  207. }
  208. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  209. return 0.000001
  210. }
  211. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  212. return UIView()
  213. }
  214. }