EaseConversationCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseConversationCell.h"
  13. //#if ENABLE_LITE == 1
  14. #import <HyphenateLite/EMConversation.h>
  15. //#else
  16. //#import <Hyphenate/EMConversation.h>
  17. //#endif
  18. #import "UIImageView+EMWebCache.h"
  19. CGFloat const EaseConversationCellPadding = 10;
  20. @interface EaseConversationCell()
  21. @property (nonatomic) NSLayoutConstraint *titleWithAvatarLeftConstraint;
  22. @property (nonatomic) NSLayoutConstraint *titleWithoutAvatarLeftConstraint;
  23. @property (nonatomic) NSLayoutConstraint *detailWithAvatarLeftConstraint;
  24. @property (nonatomic) NSLayoutConstraint *detailWithoutAvatarLeftConstraint;
  25. @end
  26. @implementation EaseConversationCell
  27. + (void)initialize
  28. {
  29. // UIAppearance Proxy Defaults
  30. /** @brief 默认配置 */
  31. EaseConversationCell *cell = [self appearance];
  32. cell.titleLabelColor = [UIColor blackColor];
  33. cell.titleLabelFont = [UIFont systemFontOfSize:17];
  34. cell.detailLabelColor = [UIColor lightGrayColor];
  35. cell.detailLabelFont = [UIFont systemFontOfSize:15];
  36. cell.timeLabelColor = [UIColor blackColor];
  37. cell.timeLabelFont = [UIFont systemFontOfSize:13];
  38. }
  39. - (instancetype)initWithStyle:(UITableViewCellStyle)style
  40. reuseIdentifier:(NSString *)reuseIdentifier
  41. {
  42. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  43. if (self) {
  44. _showAvatar = YES;
  45. [self _setupSubview];
  46. }
  47. return self;
  48. }
  49. #pragma mark - private layout subviews
  50. /*!
  51. @method
  52. @brief 加载视图
  53. @discussion
  54. @return
  55. */
  56. - (void)_setupSubview
  57. {
  58. self.accessibilityIdentifier = @"table_cell";
  59. _avatarView = [[EaseImageView alloc] init];
  60. _avatarView.translatesAutoresizingMaskIntoConstraints = NO;
  61. [self.contentView addSubview:_avatarView];
  62. _timeLabel = [[UILabel alloc] init];
  63. _timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
  64. _timeLabel.font = _timeLabelFont;
  65. _timeLabel.textColor = _timeLabelColor;
  66. _timeLabel.textAlignment = NSTextAlignmentRight;
  67. _timeLabel.backgroundColor = [UIColor clearColor];
  68. [self.contentView addSubview:_timeLabel];
  69. _titleLabel = [[UILabel alloc] init];
  70. _titleLabel.accessibilityIdentifier = @"title";
  71. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  72. _titleLabel.numberOfLines = 1;
  73. _titleLabel.backgroundColor = [UIColor clearColor];
  74. _titleLabel.font = _titleLabelFont;
  75. _titleLabel.textColor = _titleLabelColor;
  76. [self.contentView addSubview:_titleLabel];
  77. _detailLabel = [[UILabel alloc] init];
  78. _detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
  79. _detailLabel.backgroundColor = [UIColor clearColor];
  80. _detailLabel.font = _detailLabelFont;
  81. _detailLabel.textColor = _detailLabelColor;
  82. [self.contentView addSubview:_detailLabel];
  83. [self _setupAvatarViewConstraints];
  84. [self _setupTimeLabelConstraints];
  85. [self _setupTitleLabelConstraints];
  86. [self _setupDetailLabelConstraints];
  87. }
  88. #pragma mark - Setup Constraints
  89. /*!
  90. @method
  91. @brief 设置avatarView的约束
  92. @discussion
  93. @return
  94. */
  95. - (void)_setupAvatarViewConstraints
  96. {
  97. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseConversationCellPadding]];
  98. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseConversationCellPadding]];
  99. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseConversationCellPadding]];
  100. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  101. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
  102. }
  103. /*!
  104. @method
  105. @brief 设置timeLabel的约束
  106. @discussion
  107. @return
  108. */
  109. - (void)_setupTimeLabelConstraints
  110. {
  111. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.timeLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseConversationCellPadding]];
  112. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.timeLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-EaseConversationCellPadding]];
  113. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.timeLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
  114. }
  115. /*!
  116. @method
  117. @brief 设置titleLabel的约束
  118. @discussion
  119. @return
  120. */
  121. - (void)_setupTitleLabelConstraints
  122. {
  123. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseConversationCellPadding]];
  124. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:-EaseConversationCellPadding]];
  125. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.timeLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-EaseConversationCellPadding]];
  126. self.titleWithAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeRight multiplier:1.0 constant:EaseConversationCellPadding];
  127. self.titleWithoutAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseConversationCellPadding];
  128. [self addConstraint:self.titleWithAvatarLeftConstraint];
  129. }
  130. /*!
  131. @method
  132. @brief 设置detailLabel的约束
  133. @discussion
  134. @return
  135. */
  136. - (void)_setupDetailLabelConstraints
  137. {
  138. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
  139. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseConversationCellPadding]];
  140. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-EaseConversationCellPadding]];
  141. self.detailWithAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeRight multiplier:1.0 constant:EaseConversationCellPadding];
  142. self.detailWithoutAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseConversationCellPadding];
  143. [self addConstraint:self.detailWithAvatarLeftConstraint];
  144. }
  145. #pragma mark - setter
  146. - (void)setShowAvatar:(BOOL)showAvatar
  147. {
  148. if (_showAvatar != showAvatar) {
  149. _showAvatar = showAvatar;
  150. self.avatarView.hidden = !showAvatar;
  151. if (_showAvatar) {
  152. [self removeConstraint:self.titleWithoutAvatarLeftConstraint];
  153. [self removeConstraint:self.detailWithoutAvatarLeftConstraint];
  154. [self addConstraint:self.titleWithAvatarLeftConstraint];
  155. [self addConstraint:self.detailWithAvatarLeftConstraint];
  156. }
  157. else{
  158. [self removeConstraint:self.titleWithAvatarLeftConstraint];
  159. [self removeConstraint:self.detailWithAvatarLeftConstraint];
  160. [self addConstraint:self.titleWithoutAvatarLeftConstraint];
  161. [self addConstraint:self.detailWithoutAvatarLeftConstraint];
  162. }
  163. }
  164. }
  165. - (void)setModel:(id<IConversationModel>)model
  166. {
  167. _model = model;
  168. if ([_model.title length] > 0) {
  169. self.titleLabel.text = _model.title;
  170. }
  171. else{
  172. self.titleLabel.text = _model.conversation.conversationId;
  173. }
  174. if (self.showAvatar) {
  175. if ([_model.avatarURLPath length] > 0){
  176. [self.avatarView.imageView sd_setImageWithURL:[NSURL URLWithString:_model.avatarURLPath] placeholderImage:_model.avatarImage];
  177. } else {
  178. if (_model.avatarImage) {
  179. self.avatarView.image = _model.avatarImage;
  180. }
  181. }
  182. }
  183. if (_model.conversation.unreadMessagesCount == 0) {
  184. _avatarView.showBadge = NO;
  185. }
  186. else{
  187. _avatarView.showBadge = YES;
  188. _avatarView.badge = _model.conversation.unreadMessagesCount;
  189. }
  190. }
  191. - (void)setTitleLabelFont:(UIFont *)titleLabelFont
  192. {
  193. _titleLabelFont = titleLabelFont;
  194. _titleLabel.font = _titleLabelFont;
  195. }
  196. - (void)setTitleLabelColor:(UIColor *)titleLabelColor
  197. {
  198. _titleLabelColor = titleLabelColor;
  199. _titleLabel.textColor = _titleLabelColor;
  200. }
  201. - (void)setDetailLabelFont:(UIFont *)detailLabelFont
  202. {
  203. _detailLabelFont = detailLabelFont;
  204. _detailLabel.font = _detailLabelFont;
  205. }
  206. - (void)setDetailLabelColor:(UIColor *)detailLabelColor
  207. {
  208. _detailLabelColor = detailLabelColor;
  209. _detailLabel.textColor = _detailLabelColor;
  210. }
  211. - (void)setTimeLabelFont:(UIFont *)timeLabelFont
  212. {
  213. _timeLabelFont = timeLabelFont;
  214. _timeLabel.font = _timeLabelFont;
  215. }
  216. - (void)setTimeLabelColor:(UIColor *)timeLabelColor
  217. {
  218. _timeLabelColor = timeLabelColor;
  219. _timeLabel.textColor = _timeLabelColor;
  220. }
  221. #pragma mark - class method
  222. /*!
  223. @method
  224. @brief 获取cell的重用标识
  225. @discussion
  226. @param model 消息model
  227. @return 返回cell的重用标识
  228. */
  229. + (NSString *)cellIdentifierWithModel:(id)model
  230. {
  231. return @"EaseConversationCell";
  232. }
  233. /*!
  234. @method
  235. @brief 获取cell的高度
  236. @discussion
  237. @param model 消息model
  238. @return 返回cell的高度
  239. */
  240. + (CGFloat)cellHeightWithModel:(id)model
  241. {
  242. return EaseConversationCellMinHeight;
  243. }
  244. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  245. {
  246. [super setSelected:selected animated:animated];
  247. if (_avatarView.badge) {
  248. _avatarView.badgeBackgroudColor = [UIColor redColor];
  249. }
  250. }
  251. -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
  252. [super setHighlighted:highlighted animated:animated];
  253. if (_avatarView.badge) {
  254. _avatarView.badgeBackgroudColor = [UIColor redColor];
  255. }
  256. }
  257. @end