WFCUCompositeBaseCell.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // WFCUCompositeBaseCell.m
  3. // WFChatUIKit
  4. //
  5. // Created by Tom Lee on 2020/10/4.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUCompositeBaseCell.h"
  9. #import "WFCUCompositeTextCell.h"
  10. #import "WFCUCompositeUnknownCell.h"
  11. #import "WFCUCompositeImageCell.h"
  12. #import <WFChatClient/WFCChatClient.h>
  13. #import <SDWebImage/SDWebImage.h>
  14. #import "WFCUImage.h"
  15. @interface WFCUCompositeBaseCell ()
  16. @property(nonatomic, strong)UIImageView *portraitImageView;
  17. @property(nonatomic, strong)UILabel *nameLabel;
  18. @property(nonatomic, strong)UILabel *timeLabel;
  19. @property(nonatomic, strong)UIView *line;
  20. @end
  21. @implementation WFCUCompositeBaseCell
  22. - (void)awakeFromNib {
  23. [super awakeFromNib];
  24. // Initialization code
  25. }
  26. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  27. [super setSelected:selected animated:animated];
  28. // Configure the view for the selected state
  29. }
  30. + (instancetype)cellOfMessage:(WFCCMessage *)message {
  31. WFCUCompositeBaseCell *cell;
  32. if ([message.content isKindOfClass:[WFCCTextMessageContent class]]) {
  33. cell = [[WFCUCompositeTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([message.content class])];
  34. } else if([message.content isKindOfClass:[WFCCImageMessageContent class]]) {
  35. cell = [[WFCUCompositeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([message.content class])];
  36. } else {
  37. cell = [[WFCUCompositeUnknownCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([message.content class])];
  38. }
  39. for (UIView *view in cell.contentView.subviews) {
  40. [view removeFromSuperview];
  41. }
  42. return cell;
  43. }
  44. + (CGFloat)heightForMessage:(WFCCMessage *)message {
  45. return COMPOSITE_CELL_TOP_PADDING + COMPOSITE_CELL_NAME_LABEL_HEIGHT + COMPOSITE_CELL_NAME_CONTENT_PADDING + [self heightForMessageContent:message] + COMPOSITE_CELL_BUTTOM_PADDING + COMPOSITE_CELL_LINE_HEIGHT;
  46. }
  47. + (CGFloat)heightForMessageContent:(WFCCMessage *)message {
  48. return 0;
  49. }
  50. + (CGRect)contentFrame {
  51. CGFloat x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  52. CGFloat y = COMPOSITE_CELL_TOP_PADDING+COMPOSITE_CELL_NAME_LABEL_HEIGHT+COMPOSITE_CELL_NAME_CONTENT_PADDING;
  53. CGFloat w = [UIScreen mainScreen].bounds.size.width - x - COMPOSITE_CELL_RIGHT_PADDING;
  54. return CGRectMake(x, y, w, 0);
  55. }
  56. - (void)setMessage:(WFCCMessage *)message {
  57. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:message.fromUser refresh:NO];
  58. if (self.hiddenPortrait) {
  59. _portraitImageView.hidden = YES;
  60. } else {
  61. self.portraitImageView.hidden = NO;
  62. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.portrait] placeholderImage:[WFCUImage imageNamed:@"PersonlChat"]];
  63. }
  64. self.nameLabel.text = userInfo.displayName;
  65. NSDate *from = [[NSDate alloc] initWithTimeIntervalSince1970:message.serverTime/1000];
  66. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  67. [dateFormatter setDateFormat:@"MM-dd HH:mm"];
  68. [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
  69. self.timeLabel.text = [dateFormatter stringFromDate:from];
  70. CGFloat x;
  71. CGFloat cellHeight = [self.class heightForMessage:message];
  72. if (self.lastMessage) {
  73. x = COMPOSITE_CELL_RIGHT_PADDING;
  74. } else {
  75. x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  76. }
  77. self.line.frame = CGRectMake(x, cellHeight-1, [UIScreen mainScreen].bounds.size.width-x-COMPOSITE_CELL_RIGHT_PADDING, 1);
  78. }
  79. - (UIImageView *)portraitImageView {
  80. if (!_portraitImageView) {
  81. _portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(COMPOSITE_CELL_PORTRAIT_PADDING, COMPOSITE_CELL_TOP_PADDING, COMPOSITE_CELL_PORTRAIT_WIDTH, COMPOSITE_CELL_PORTRAIT_WIDTH)];
  82. [self.contentView addSubview:_portraitImageView];
  83. }
  84. return _portraitImageView;
  85. }
  86. - (UILabel *)nameLabel {
  87. if (!_nameLabel) {
  88. CGFloat x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  89. CGFloat w = [UIScreen mainScreen].bounds.size.width - x -
  90. - COMPOSITE_CELL_RIGHT_PADDING - COMPOSITE_CELL_TIME_LABEL_WIDTH - COMPOSITE_CELL_RIGHT_PADDING;
  91. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, COMPOSITE_CELL_TOP_PADDING, w, COMPOSITE_CELL_NAME_LABEL_HEIGHT)];
  92. [_nameLabel setFont:[UIFont systemFontOfSize:COMPOSITE_CELL_NAME_LABEL_FONT]];
  93. _nameLabel.textColor = [UIColor grayColor];
  94. [self.contentView addSubview:_nameLabel];
  95. }
  96. return _nameLabel;
  97. }
  98. - (UILabel *)timeLabel {
  99. if (!_timeLabel) {
  100. CGFloat x = [UIScreen mainScreen].bounds.size.width - COMPOSITE_CELL_TIME_LABEL_WIDTH - COMPOSITE_CELL_RIGHT_PADDING;
  101. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, COMPOSITE_CELL_TOP_PADDING, COMPOSITE_CELL_TIME_LABEL_WIDTH, COMPOSITE_CELL_TIME_LABEL_HEIGHT)];
  102. [_timeLabel setFont:[UIFont systemFontOfSize:COMPOSITE_CELL_TIME_LABEL_FONT]];
  103. _timeLabel.textAlignment = NSTextAlignmentRight;
  104. _timeLabel.textColor = [UIColor grayColor];
  105. [self.contentView addSubview:_timeLabel];
  106. }
  107. return _timeLabel;
  108. }
  109. - (UIView *)line {
  110. if (!_line) {
  111. _line = [[UIView alloc] init];
  112. _line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  113. [self.contentView addSubview:_line];
  114. }
  115. return _line;
  116. }
  117. @end