IMConversationItemCell.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // IMConversationItemCell.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/4.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class IMConversationItemCell: UITableViewCell {
  11. @IBOutlet weak var avatarImg: UIImageView!
  12. @IBOutlet weak var nameLabel: UILabel!
  13. @IBOutlet weak var timeLabel: UILabel!
  14. @IBOutlet weak var messageLabel: UILabel!
  15. @IBOutlet weak var unreadNumberLabel: UILabel!
  16. @IBOutlet weak var emojiImg: UIImageView!
  17. override func awakeFromNib() {
  18. super.awakeFromNib()
  19. }
  20. override func setSelected(_ selected: Bool, animated: Bool) {
  21. super.setSelected(selected, animated: animated)
  22. }
  23. // 边距
  24. override var frame: CGRect {
  25. didSet {
  26. var newFrame = frame
  27. newFrame.origin.x += 10
  28. newFrame.size.width -= 20
  29. newFrame.origin.y += 10
  30. newFrame.size.height -= 20
  31. super.frame = newFrame
  32. }
  33. }
  34. func setInstantContent(item: InstantMessage) {
  35. self.avatarImg.image = UIImage(named: "icon_email")
  36. self.nameLabel.text = "通知消息"
  37. self.messageLabel.isHidden = false
  38. self.messageLabel.text = item.title
  39. if let time = item.createTime {
  40. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  41. self.timeLabel.text = date.friendlyTime()
  42. }
  43. self.emojiImg.isHidden = true
  44. self.unreadNumberLabel.isHidden = true
  45. }
  46. func bindConversation(conversation: IMConversationInfo) {
  47. //avatar name
  48. if conversation.type == o2_im_conversation_type_single {
  49. var person = ""
  50. conversation.personList?.forEach({ (p) in
  51. if p != O2AuthSDK.shared.myInfo()?.distinguishedName {
  52. person = p
  53. }
  54. })
  55. if person != "" {
  56. //头像
  57. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":person as AnyObject], generateTime: false)
  58. if let u = URL(string: urlstr!) {
  59. self.avatarImg.hnk_setImageFromURL(u)
  60. }else {
  61. DDLogError("错误, 没有生成头像url")
  62. self.avatarImg.image = UIImage(named: "icon_men")
  63. }
  64. //姓名
  65. self.nameLabel.text = person.split("@").first ?? ""
  66. }else {
  67. self.avatarImg.image = UIImage(named: "icon_men")
  68. self.nameLabel.text = ""
  69. }
  70. }else {//todo 群组头像 ?
  71. self.nameLabel.text = conversation.title
  72. self.avatarImg.image = UIImage(named: "group_default")
  73. }
  74. //time
  75. if let time = conversation.lastMessage?.createTime {
  76. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  77. self.timeLabel.text = date.friendlyTime()
  78. }
  79. // message
  80. if let msgBody = conversation.lastMessage?.body, let body = parseJson(msg: msgBody) {
  81. if body.type == o2_im_msg_type_text || body.type == o2_im_msg_type_image || body.type == o2_im_msg_type_file
  82. || body.type == o2_im_msg_type_audio || body.type == o2_im_msg_type_location {
  83. self.messageLabel.text = body.body
  84. self.messageLabel.isHidden = false
  85. self.emojiImg.isHidden = true
  86. } else if body.type == o2_im_msg_type_emoji {
  87. self.messageLabel.isHidden = true
  88. self.emojiImg.isHidden = false
  89. let bundle = Bundle().o2EmojiBundle(anyClass: IMConversationItemCell.self)
  90. let path = o2ImEmojiPath(emojiBody: body.body!)
  91. self.emojiImg.image = UIImage(named: path, in: bundle, compatibleWith: nil)
  92. } else if body.type == o2_im_msg_type_process {
  93. self.messageLabel.text = o2_im_msg_body_process
  94. self.messageLabel.isHidden = false
  95. self.emojiImg.isHidden = true
  96. } else if body.type == o2_im_msg_type_cms {
  97. self.messageLabel.text = o2_im_msg_body_cms
  98. self.messageLabel.isHidden = false
  99. self.emojiImg.isHidden = true
  100. } else {
  101. self.messageLabel.isHidden = true
  102. self.emojiImg.isHidden = true
  103. }
  104. }else {
  105. self.messageLabel.isHidden = true
  106. self.emojiImg.isHidden = true
  107. }
  108. //unread number
  109. let number = conversation.unreadNumber ?? 0
  110. if number > 0 && number < 100 {
  111. self.unreadNumberLabel.text = "\(number)"
  112. self.unreadNumberLabel.isHidden = false
  113. }else if number >= 100 {
  114. self.unreadNumberLabel.text = "99.."
  115. self.unreadNumberLabel.isHidden = false
  116. }else {
  117. self.unreadNumberLabel.isHidden = true
  118. }
  119. }
  120. private func parseJson(msg: String) -> IMMessageBodyInfo? {
  121. return IMMessageBodyInfo.deserialize(from: msg)
  122. }
  123. }