CircleLeavingMessageCommentTopStepTableViewCell.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // CircleLeavingMessageCommentTopStepTableViewCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/10/16.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. class CircleLeavingMessageCommentTopStepTableViewCell: UITableViewCell {
  11. let disposeBag = DisposeBag()
  12. class func cellWith(tableView:UITableView,indexPath:IndexPath) -> CircleLeavingMessageCommentTopStepTableViewCell {
  13. let ID = "CircleLeavingMessageCommentTopStepTableViewCell"
  14. tableView.register(CircleLeavingMessageCommentTopStepTableViewCell.self, forCellReuseIdentifier: ID)
  15. let cell : CircleLeavingMessageCommentTopStepTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! CircleLeavingMessageCommentTopStepTableViewCell
  16. cell.indexPath = indexPath
  17. return cell
  18. }
  19. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  20. super.init(style: style, reuseIdentifier: reuseIdentifier)
  21. setupViews()
  22. setupLayouts()
  23. setupData()
  24. }
  25. required init?(coder aDecoder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. var indexPath: IndexPath? {
  29. didSet {
  30. }
  31. }
  32. //MRAK: - 设置View
  33. private func setupViews() {
  34. self.selectionStyle = .none
  35. addSubview(commentLabel)
  36. addSubview(topButton)
  37. addSubview(stepButton)
  38. }
  39. private func setupLayouts() {
  40. stepButton.snp.makeConstraints { (make) in
  41. make.centerY.equalToSuperview()
  42. make.right.equalTo(-14)
  43. make.height.equalTo(20)
  44. }
  45. stepButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 4)
  46. topButton.snp.makeConstraints { (make) in
  47. make.centerY.equalToSuperview()
  48. make.right.equalTo(stepButton.snp.left).offset(-20)
  49. make.height.equalTo(20)
  50. }
  51. topButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 4)
  52. commentLabel.snp.makeConstraints { (make) in
  53. make.centerY.equalToSuperview()
  54. make.left.equalTo(14)
  55. make.right.equalTo(topButton.snp.left).offset(12)
  56. make.height.equalTo(48)
  57. }
  58. }
  59. func setupData() {
  60. topButton.rx.tap.subscribe(onNext: {[weak self] in
  61. self?.communityCircleMessageAction(action:1)
  62. }).disposed(by: disposeBag)
  63. stepButton.rx.tap.subscribe(onNext: {[weak self] in
  64. self?.communityCircleMessageAction(action:-1)
  65. }).disposed(by: disposeBag)
  66. }
  67. private lazy var commentLabel: UILabel = {
  68. let commentLabel = UILabel()
  69. commentLabel.numberOfLines = 1
  70. commentLabel.textColor = k999999Color
  71. commentLabel.font = kRegularFont14
  72. commentLabel.textAlignment = .left
  73. commentLabel.text = "添加评论..."
  74. commentLabel.addTapGesture(1, target: self, action: #selector(commentAction))
  75. return commentLabel
  76. }()
  77. lazy var topButton: UIButton = {
  78. let topButton = UIButton()
  79. topButton.setImage(kImage(name: "question_btn_like"), for: UIControl.State.normal)
  80. topButton.setImage(kImage(name: "question_btn_like_pre"), for: UIControl.State.selected)
  81. topButton.setTitleColor(k666666Color, for: UIControl.State.normal)
  82. topButton.setTitle("0", for: UIControl.State.normal)
  83. return topButton
  84. }()
  85. lazy var stepButton: UIButton = {
  86. let stepButton = UIButton()
  87. stepButton.setImage(kImage(name: "question_btn_dislike"), for: UIControl.State.normal)
  88. stepButton.setImage(kImage(name: "question_btn_dislike_pre"), for: UIControl.State.selected)
  89. stepButton.setTitleColor(k666666Color, for: UIControl.State.normal)
  90. stepButton.setTitle("0", for: UIControl.State.normal)
  91. return stepButton
  92. }()
  93. var communityCircleMessageModel: CommunityCircleMessageModel? {
  94. didSet {
  95. action()
  96. }
  97. }
  98. func action() {
  99. if communityCircleMessageModel?.action ?? 0 == 0 {
  100. topButton.isUserInteractionEnabled = true
  101. stepButton.isUserInteractionEnabled = true
  102. topButton.isSelected = false
  103. stepButton.isSelected = false
  104. }else if communityCircleMessageModel?.action ?? 0 == 1 {
  105. topButton.isUserInteractionEnabled = false
  106. stepButton.isUserInteractionEnabled = false
  107. topButton.isSelected = true
  108. stepButton.isSelected = false
  109. }else if communityCircleMessageModel?.action ?? 0 == -1 {
  110. topButton.isUserInteractionEnabled = false
  111. stepButton.isUserInteractionEnabled = false
  112. topButton.isSelected = false
  113. stepButton.isSelected = true
  114. }
  115. topButton.setTitle("\(communityCircleMessageModel?.good ?? 0)", for: UIControl.State.normal)
  116. topButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 4)
  117. stepButton.setTitle("\(communityCircleMessageModel?.bad ?? 0)", for: UIControl.State.normal)
  118. stepButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 4)
  119. }
  120. /// 评论回调
  121. typealias CommentCloSure = () -> Void
  122. var commentCloSure : CommentCloSure?
  123. // 评论
  124. @objc func commentAction() {
  125. showKeyBoardCommentView()
  126. }
  127. /// 显示键盘
  128. func showKeyBoardCommentView() {
  129. KeyBoardInputView.show(inputViewResultClosure: { [weak self] (content) in
  130. self?.communityCircleCommentPostApi(content: content)
  131. }) {
  132. }
  133. }
  134. /// 评论
  135. ///
  136. /// - Parameter content: 评论内容
  137. func communityCircleCommentPostApi(content:String?) {
  138. SwiftMoyaNetWorkServiceCommunity.shared().communityCircleCommentPostApi(circleId: communityCircleMessageModel?.circleId ?? 0, msgId: communityCircleMessageModel?.id ?? 0, content: content ?? "") { [weak self] (communityCircleCommentModel) -> (Void) in
  139. let communityCircleCommentModel = communityCircleCommentModel as? CommunityCircleCommentModel
  140. let communityCircleMessageCommentModel = CommunityCircleMessageCommentModel()
  141. communityCircleMessageCommentModel.id = communityCircleCommentModel?.id
  142. communityCircleMessageCommentModel.uid = UserModel.shared().getModel()?.uid
  143. communityCircleMessageCommentModel.username = UserModel.shared().getModel()?.username
  144. communityCircleMessageCommentModel.content = content
  145. communityCircleMessageCommentModel.isDelete = 0
  146. communityCircleMessageCommentModel.replyCount = 0
  147. self?.communityCircleMessageModel?.commentCount = (self?.communityCircleMessageModel?.commentCount ?? 0) + 1
  148. self?.communityCircleMessageModel?.comment?.insert(communityCircleMessageCommentModel, at: 0)
  149. if let commentCloSure = self?.commentCloSure {
  150. commentCloSure()
  151. }
  152. }
  153. }
  154. /// 提问顶踩
  155. ///
  156. /// - Parameter action: 0/1/-1
  157. func communityCircleMessageAction(action:Int) {
  158. SwiftMoyaNetWorkServiceCommunity.shared().communityCircleMessageActionApi(msgId: communityCircleMessageModel?.id ?? 0, action: action) { [weak self] (_) -> (Void) in
  159. self?.communityCircleMessageModel?.action = action
  160. if action == -1 {
  161. self?.communityCircleMessageModel?.bad = (self?.communityCircleMessageModel?.bad ?? 0) + 1
  162. }else if action == 1 {
  163. self?.communityCircleMessageModel?.good = (self?.communityCircleMessageModel?.good ?? 0) + 1
  164. }
  165. self?.action()
  166. }
  167. }
  168. }