JCVerificationCell.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // JCVerificationCell.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/4/6.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCVerificationCell: JCTableViewCell {
  11. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  12. super.init(style: style, reuseIdentifier: reuseIdentifier)
  13. _init()
  14. }
  15. required init?(coder aDecoder: NSCoder) {
  16. super.init(coder: aDecoder)
  17. _init()
  18. }
  19. override func awakeFromNib() {
  20. super.awakeFromNib()
  21. _init()
  22. }
  23. func bindData(_ info: JCVerificationInfo) {
  24. self.info = info
  25. if info.nickname.isEmpty {
  26. nickname.text = info.username
  27. } else {
  28. nickname.text = info.nickname
  29. }
  30. reason.text = info.resaon
  31. switch info.state {
  32. case JCVerificationType.accept.rawValue:
  33. tipInfo.text = "已添加"
  34. acceptButton.isHidden = true
  35. case JCVerificationType.wait.rawValue:
  36. tipInfo.text = "等待验证"
  37. acceptButton.isHidden = true
  38. case JCVerificationType.receive.rawValue:
  39. acceptButton.isHidden = false
  40. case JCVerificationType.reject.rawValue:
  41. tipInfo.text = "已拒绝"
  42. acceptButton.isHidden = true
  43. default:
  44. break
  45. }
  46. iconView.image = UIImage.loadImage("com_icon_user_50")
  47. JMSGUser.userInfoArray(withUsernameArray: [info.username]) { (result, error) in
  48. if error == nil {
  49. let users = result as! [JMSGUser]
  50. let user = users.first
  51. user?.thumbAvatarData({ (data, id, error) in
  52. if let data = data {
  53. let image = UIImage(data: data)
  54. self.iconView.image = image
  55. }
  56. })
  57. }
  58. }
  59. }
  60. private var info: JCVerificationInfo!
  61. private lazy var iconView: UIImageView = UIImageView()
  62. private lazy var nickname: UILabel = UILabel()
  63. private lazy var reason: UILabel = UILabel()
  64. private lazy var tipInfo: UILabel = UILabel()
  65. private lazy var acceptButton: UIButton = {
  66. let acceptButton = UIButton()
  67. acceptButton.setTitle("同意", for: .normal)
  68. let bgImage = UIImage.createImage(color: UIColor(netHex: 0x2dd0cf), size: CGSize(width: 50, height: 25))
  69. acceptButton.setBackgroundImage(bgImage, for: .normal)
  70. acceptButton.addTarget(self, action: #selector(_clickAcceptButton), for: .touchUpInside)
  71. return acceptButton
  72. }()
  73. private func _init() {
  74. nickname.font = UIFont.systemFont(ofSize: 16)
  75. nickname.textColor = UIColor(netHex: 0x2c2c2c)
  76. nickname.textAlignment = .left
  77. reason.font = UIFont.systemFont(ofSize: 14)
  78. reason.textColor = UIColor(netHex: 0x999999)
  79. reason.textAlignment = .left
  80. tipInfo.font = UIFont.systemFont(ofSize: 14)
  81. tipInfo.textColor = UIColor(netHex: 0xb5b6b6)
  82. tipInfo.textAlignment = .right
  83. acceptButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  84. addSubview(iconView)
  85. addSubview(nickname)
  86. addSubview(reason)
  87. addSubview(tipInfo)
  88. addSubview(acceptButton)
  89. addConstraint(_JCLayoutConstraintMake(iconView, .top, .equal, contentView, .top, 7.5))
  90. addConstraint(_JCLayoutConstraintMake(iconView, .left, .equal, self, .left, 15))
  91. addConstraint(_JCLayoutConstraintMake(iconView, .width, .equal, nil, .notAnAttribute, 50))
  92. addConstraint(_JCLayoutConstraintMake(iconView, .height, .equal, nil, .notAnAttribute, 50))
  93. addConstraint(_JCLayoutConstraintMake(nickname, .top, .equal, contentView, .top, 10.5))
  94. addConstraint(_JCLayoutConstraintMake(nickname, .left, .equal, iconView, .right, 11))
  95. addConstraint(_JCLayoutConstraintMake(nickname, .right, .equal, self, .centerX))
  96. addConstraint(_JCLayoutConstraintMake(nickname, .height, .equal, nil, .notAnAttribute, 22.5))
  97. addConstraint(_JCLayoutConstraintMake(reason, .top, .equal, nickname, .bottom, 1.5))
  98. addConstraint(_JCLayoutConstraintMake(reason, .left, .equal, nickname, .left))
  99. addConstraint(_JCLayoutConstraintMake(reason, .right, .equal, tipInfo, .left, -5))
  100. addConstraint(_JCLayoutConstraintMake(reason, .height, .equal, nil, .notAnAttribute, 20))
  101. addConstraint(_JCLayoutConstraintMake(tipInfo, .centerY, .equal, self, .centerY))
  102. addConstraint(_JCLayoutConstraintMake(tipInfo, .width, .equal, nil, .notAnAttribute, 80))
  103. addConstraint(_JCLayoutConstraintMake(tipInfo, .right, .equal, self, .right, -15))
  104. addConstraint(_JCLayoutConstraintMake(tipInfo, .height, .equal, nil, .notAnAttribute, 20))
  105. addConstraint(_JCLayoutConstraintMake(acceptButton, .top, .equal, contentView, .top, 20))
  106. addConstraint(_JCLayoutConstraintMake(acceptButton, .right, .equal, self, .right, -15))
  107. addConstraint(_JCLayoutConstraintMake(acceptButton, .width, .equal, nil, .notAnAttribute, 50))
  108. addConstraint(_JCLayoutConstraintMake(acceptButton, .height, .equal, nil, .notAnAttribute, 25))
  109. }
  110. func _clickAcceptButton() {
  111. JMSGFriendManager.acceptInvitation(withUsername: info.username, appKey: info.appkey) { (result, error) in
  112. if error == nil {
  113. self.info.state = JCVerificationType.accept.rawValue
  114. JCVerificationInfoDB.shareInstance.updateData(self.info)
  115. self.bindData(self.info)
  116. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateFriendList), object: nil)
  117. }
  118. }
  119. }
  120. }