JCConversationCell.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // JCConversationCell.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/22.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. import CocoaLumberjack
  11. class JCConversationCell: JCTableViewCell {
  12. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  13. super.init(style: style, reuseIdentifier: reuseIdentifier)
  14. _init()
  15. }
  16. required init?(coder aDecoder: NSCoder) {
  17. super.init(coder: aDecoder)
  18. _init()
  19. }
  20. override func awakeFromNib() {
  21. super.awakeFromNib()
  22. _init()
  23. }
  24. private lazy var avatorView: UIImageView = {
  25. let avatorView = UIImageView()
  26. avatorView.contentMode = .scaleToFill
  27. return avatorView
  28. }()
  29. private lazy var statueView: UIImageView = UIImageView()
  30. private lazy var titleLabel: UILabel = {
  31. let titleLabel = UILabel()
  32. titleLabel.font = UIFont.systemFont(ofSize: 16)
  33. return titleLabel
  34. }()
  35. private lazy var msgLabel: UILabel = {
  36. let msgLabel = UILabel()
  37. msgLabel.textColor = UIColor(netHex: 0x808080)
  38. msgLabel.font = UIFont.systemFont(ofSize: 14)
  39. return msgLabel
  40. }()
  41. private lazy var dateLabel: UILabel = {
  42. let dateLabel = UILabel()
  43. dateLabel.textAlignment = .right
  44. dateLabel.font = UIFont.systemFont(ofSize: 12)
  45. dateLabel.textColor = UIColor(netHex: 0xB3B3B3)
  46. return dateLabel
  47. }()
  48. private lazy var redPoin: UILabel = {
  49. let redPoin = UILabel(frame: CGRect(x: 65 - 17, y: 4.5, width: 20, height: 20))
  50. redPoin.textAlignment = .center
  51. redPoin.font = UIFont.systemFont(ofSize: 11)
  52. redPoin.textColor = .white
  53. redPoin.layer.backgroundColor = UIColor(netHex: 0xEB424C).cgColor
  54. redPoin.textAlignment = .center
  55. return redPoin
  56. }()
  57. //MARK: - public func
  58. open func bindConversation(_ conversation: JMSGConversation) {
  59. statueView.isHidden = true
  60. let isGroup = conversation.ex.isGroup
  61. if conversation.unreadCount != nil && (conversation.unreadCount?.intValue)! > 0 {
  62. redPoin.isHidden = false
  63. var text = ""
  64. if (conversation.unreadCount?.intValue)! > 99 {
  65. text = "99+"
  66. redPoin.layer.cornerRadius = 9.0
  67. redPoin.layer.masksToBounds = true
  68. redPoin.frame = CGRect(x: 65 - 28, y: 4.5, width: 33, height: 18)
  69. } else {
  70. redPoin.layer.cornerRadius = 10.0
  71. redPoin.layer.masksToBounds = true
  72. redPoin.frame = CGRect(x: 65 - 15, y: 4.5, width: 20, height: 20)
  73. text = "\(conversation.unreadCount!)"
  74. }
  75. redPoin.text = text
  76. var isNoDisturb = false
  77. if isGroup {
  78. if let group = conversation.target as? JMSGGroup {
  79. isNoDisturb = group.isNoDisturb
  80. }
  81. } else {
  82. if let user = conversation.target as? JMSGUser {
  83. isNoDisturb = user.isNoDisturb
  84. }
  85. }
  86. if isNoDisturb {
  87. redPoin.layer.cornerRadius = 4.0
  88. redPoin.layer.masksToBounds = true
  89. redPoin.text = ""
  90. redPoin.frame = CGRect(x: 65 - 5, y: 4.5, width: 8, height: 8)
  91. }
  92. } else {
  93. redPoin.isHidden = true
  94. }
  95. if let latestMessage = conversation.latestMessage {
  96. let time = latestMessage.timestamp.intValue / 1000
  97. let date = Date(timeIntervalSince1970: TimeInterval(time))
  98. dateLabel.text = date.conversationDate()
  99. } else {
  100. dateLabel.text = ""
  101. }
  102. msgLabel.text = conversation.latestMessageContentText()
  103. if isGroup {
  104. if let latestMessage = conversation.latestMessage {
  105. let fromUser = latestMessage.fromUser
  106. if !fromUser.isEqual(to: JMSGUser.myInfo()) &&
  107. latestMessage.contentType != .eventNotification &&
  108. latestMessage.contentType != .prompt {
  109. msgLabel.text = "\(fromUser.displayName()):\(msgLabel.text!)"
  110. }
  111. if conversation.unreadCount != nil &&
  112. conversation.unreadCount!.intValue > 0 &&
  113. latestMessage.contentType != .prompt {
  114. if latestMessage.isAtAll() {
  115. msgLabel.attributedText = getAttributString(attributString: "[@所有人]", string: msgLabel.text!)
  116. } else if latestMessage.isAtMe() {
  117. msgLabel.attributedText = getAttributString(attributString: "[有人@我]", string: msgLabel.text!)
  118. }
  119. }
  120. }
  121. }
  122. if let draft = JCDraft.getDraft(conversation) {
  123. if !draft.isEmpty {
  124. msgLabel.attributedText = getAttributString(attributString: "[草稿]", string: draft)
  125. }
  126. }
  127. if !isGroup {
  128. let user = conversation.target as? JMSGUser
  129. titleLabel.text = user?.displayName() ?? ""
  130. // 处理头像
  131. DDLogDebug("更新头像,发送者头像:\(user?.username ?? "")")
  132. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":user?.username as AnyObject])
  133. let url = URL(string: urlstr!)
  134. let bound = self.avatorView.bounds
  135. if bound.width <= 0 || bound.height <= 0 {
  136. self.avatorView.bounds = CGRect(x: 15, y: 7.5, width: 50, height: 50)
  137. }
  138. self.avatorView.hnk_setImageFromURL(url!)
  139. // user?.thumbAvatarData { (data, username, error) in
  140. // guard let imageData = data else {
  141. // self.avatorView.image = self.userDefaultIcon
  142. // return
  143. // }
  144. // let image = UIImage(data: imageData)
  145. // self.avatorView.image = image
  146. // }
  147. } else {
  148. if let group = conversation.target as? JMSGGroup {
  149. titleLabel.text = group.displayName()
  150. if group.isShieldMessage {
  151. statueView.isHidden = false
  152. }
  153. group.thumbAvatarData({ (data, _, error) in
  154. if let data = data {
  155. self.avatorView.image = UIImage(data: data)
  156. } else {
  157. self.avatorView.image = self.groupDefaultIcon
  158. }
  159. })
  160. }
  161. }
  162. if conversation.ex.isSticky {
  163. backgroundColor = UIColor(netHex: 0xF5F6F8)
  164. } else {
  165. backgroundColor = .white
  166. }
  167. }
  168. func getAttributString(attributString: String, string: String) -> NSMutableAttributedString {
  169. let attr = NSMutableAttributedString(string: "")
  170. var attrSearchString: NSAttributedString!
  171. attrSearchString = NSAttributedString(string: attributString, attributes: [ NSAttributedString.Key.foregroundColor : UIColor(netHex: 0xEB424C), NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14.0)])
  172. attr.append(attrSearchString)
  173. attr.append(NSAttributedString(string: string))
  174. return attr
  175. }
  176. private lazy var groupDefaultIcon = UIImage.loadImage("com_icon_group_50")
  177. private lazy var userDefaultIcon = UIImage.loadImage("com_icon_user_50")
  178. //MARK: - private func
  179. private func _init() {
  180. avatorView.image = userDefaultIcon
  181. statueView.image = UIImage.loadImage("com_icon_shield")
  182. contentView.addSubview(avatorView)
  183. contentView.addSubview(statueView)
  184. contentView.addSubview(titleLabel)
  185. contentView.addSubview(msgLabel)
  186. contentView.addSubview(dateLabel)
  187. contentView.addSubview(redPoin)
  188. addConstraint(_JCLayoutConstraintMake(avatorView, .left, .equal, contentView, .left, 15))
  189. addConstraint(_JCLayoutConstraintMake(avatorView, .top, .equal, contentView, .top, 7.5))
  190. addConstraint(_JCLayoutConstraintMake(avatorView, .width, .equal, nil, .notAnAttribute, 50))
  191. addConstraint(_JCLayoutConstraintMake(avatorView, .height, .equal, nil, .notAnAttribute, 50))
  192. addConstraint(_JCLayoutConstraintMake(titleLabel, .left, .equal, avatorView, .right, 10.5))
  193. addConstraint(_JCLayoutConstraintMake(titleLabel, .top, .equal, contentView, .top, 10.5))
  194. addConstraint(_JCLayoutConstraintMake(titleLabel, .right, .equal, dateLabel, .left, -3))
  195. addConstraint(_JCLayoutConstraintMake(titleLabel, .height, .equal, nil, .notAnAttribute, 22.5))
  196. addConstraint(_JCLayoutConstraintMake(msgLabel, .left, .equal, titleLabel, .left))
  197. addConstraint(_JCLayoutConstraintMake(msgLabel, .top, .equal, titleLabel, .bottom, 1.5))
  198. addConstraint(_JCLayoutConstraintMake(msgLabel, .right, .equal, statueView, .left, -5))
  199. addConstraint(_JCLayoutConstraintMake(msgLabel, .height, .equal, nil, .notAnAttribute, 20))
  200. addConstraint(_JCLayoutConstraintMake(dateLabel, .top, .equal, contentView, .top, 16))
  201. addConstraint(_JCLayoutConstraintMake(dateLabel, .right, .equal, contentView, .right, -15))
  202. addConstraint(_JCLayoutConstraintMake(dateLabel, .height, .equal, nil, .notAnAttribute, 16.5))
  203. addConstraint(_JCLayoutConstraintMake(dateLabel, .width, .equal, nil, .notAnAttribute, 100))
  204. addConstraint(_JCLayoutConstraintMake(statueView, .top, .equal, dateLabel, .bottom, 7))
  205. addConstraint(_JCLayoutConstraintMake(statueView, .right, .equal, contentView, .right, -16))
  206. addConstraint(_JCLayoutConstraintMake(statueView, .height, .equal, nil, .notAnAttribute, 12))
  207. addConstraint(_JCLayoutConstraintMake(statueView, .width, .equal, nil, .notAnAttribute, 12))
  208. }
  209. }