IMConversationItemCell.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. func setInstantContent(item: InstantMessage) {
  24. self.avatarImg.image = UIImage(named: "icon_email")
  25. self.nameLabel.text = "通知消息"
  26. self.messageLabel.isHidden = false
  27. self.messageLabel.text = item.title
  28. if let time = item.createTime {
  29. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  30. self.timeLabel.text = date.friendlyTime()
  31. }
  32. self.emojiImg.isHidden = true
  33. self.unreadNumberLabel.isHidden = true
  34. }
  35. func bindConversation(conversation: IMConversationInfo) {
  36. //avatar name
  37. if conversation.type == o2_im_conversation_type_single {
  38. var person = ""
  39. conversation.personList?.forEach({ (p) in
  40. if p != O2AuthSDK.shared.myInfo()?.distinguishedName {
  41. person = p
  42. }
  43. })
  44. if person != "" {
  45. //头像
  46. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":person as AnyObject], generateTime: false)
  47. if let u = URL(string: urlstr!) {
  48. self.avatarImg.hnk_setImageFromURL(u)
  49. }else {
  50. DDLogError("错误, 没有生成头像url")
  51. self.avatarImg.image = UIImage(named: "icon_men")
  52. }
  53. //姓名
  54. self.nameLabel.text = person.split("@").first ?? ""
  55. }else {
  56. self.avatarImg.image = UIImage(named: "icon_men")
  57. self.nameLabel.text = ""
  58. }
  59. }else {//todo 群组头像 ?
  60. self.nameLabel.text = conversation.title
  61. self.avatarImg.image = UIImage(named: "group_default")
  62. }
  63. //time
  64. if let time = conversation.lastMessage?.createTime {
  65. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  66. self.timeLabel.text = date.friendlyTime()
  67. }
  68. // message
  69. if let msgBody = conversation.lastMessage?.body, let body = parseJson(msg: msgBody) {
  70. if body.type == o2_im_msg_type_text || body.type == o2_im_msg_type_image
  71. || body.type == o2_im_msg_type_audio || body.type == o2_im_msg_type_location {
  72. self.messageLabel.text = body.body
  73. self.messageLabel.isHidden = false
  74. self.emojiImg.isHidden = true
  75. } else if body.type == o2_im_msg_type_emoji {
  76. self.messageLabel.isHidden = true
  77. self.emojiImg.isHidden = false
  78. let bundle = Bundle().o2EmojiBundle(anyClass: IMConversationItemCell.self)
  79. let path = o2ImEmojiPath(emojiBody: body.body!)
  80. self.emojiImg.image = UIImage(named: path, in: bundle, compatibleWith: nil)
  81. } else {
  82. self.messageLabel.isHidden = true
  83. self.emojiImg.isHidden = true
  84. }
  85. }else {
  86. self.messageLabel.isHidden = true
  87. self.emojiImg.isHidden = true
  88. }
  89. //unread number
  90. let number = conversation.unreadNumber ?? 0
  91. if number > 0 && number < 100 {
  92. self.unreadNumberLabel.text = "\(number)"
  93. self.unreadNumberLabel.isHidden = false
  94. }else if number >= 100 {
  95. self.unreadNumberLabel.text = "99.."
  96. self.unreadNumberLabel.isHidden = false
  97. }else {
  98. self.unreadNumberLabel.isHidden = true
  99. }
  100. }
  101. private func parseJson(msg: String) -> IMMessageBodyInfo? {
  102. return IMMessageBodyInfo.deserialize(from: msg)
  103. }
  104. }