QMChatRoomBaseCell.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // QMChatRoomBaseCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/3/10.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomBaseCell.h"
  9. #define kIconImageViewWidth 45
  10. @implementation QMChatRoomBaseCell
  11. {
  12. CustomMessage *_message;
  13. }
  14. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  15. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  16. if (self) {
  17. self.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
  18. [self setUI];
  19. }
  20. return self;
  21. }
  22. - (void)prepareForReuse {
  23. [super prepareForReuse];
  24. self.iconImage.image = nil;
  25. self.chatBackgroudImage.image = nil;
  26. self.timeLabel.text = nil;
  27. }
  28. - (void)setUI {
  29. self.iconImage = [[UIImageView alloc] init];
  30. self.iconImage.backgroundColor = [UIColor whiteColor];
  31. self.iconImage.contentMode = UIViewContentModeScaleAspectFill;
  32. self.iconImage.layer.cornerRadius = 20.5;
  33. self.iconImage.layer.masksToBounds = true;
  34. self.iconImage.clipsToBounds = YES;
  35. [self.contentView addSubview:self.iconImage];
  36. self.timeLabel = [[UILabel alloc] init];
  37. self.timeLabel.hidden = YES;
  38. self.timeLabel.textAlignment = NSTextAlignmentCenter;
  39. self.timeLabel.backgroundColor = [UIColor clearColor];
  40. self.timeLabel.textColor = [UIColor grayColor];
  41. self.timeLabel.font = [UIFont systemFontOfSize:12];
  42. [self.contentView addSubview:self.timeLabel];
  43. self.chatBackgroudImage = [[UIImageView alloc] init];
  44. self.chatBackgroudImage.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
  45. self.chatBackgroudImage.userInteractionEnabled = YES;
  46. [self.contentView addSubview:self.chatBackgroudImage];
  47. self.sendStatus = [[UIImageView alloc] init];
  48. self.sendStatus.userInteractionEnabled = YES;
  49. [self.contentView addSubview:self.sendStatus];
  50. UITapGestureRecognizer *tapResendMessage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reSendAction:)];
  51. [self.sendStatus addGestureRecognizer:tapResendMessage];
  52. }
  53. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  54. _message = message;
  55. if (self.timeLabel.hidden == YES) {
  56. self.timeLabel.frame = CGRectZero;
  57. }else {
  58. self.timeLabel.frame = CGRectMake(0, 10, RQ_SCREEN_WIDTH, 20);
  59. }
  60. self.timeLabel.text = [QMDateManager showChatTime:message.createdTime];
  61. // 重写
  62. if ([message.fromType isEqualToString:@"0"]) {
  63. //发送
  64. if ([NSURL URLWithString:avater]) {
  65. [self.iconImage sd_setImageWithURL:[NSURL URLWithString:avater] placeholderImage:[UIImage imageNamed:@"qm_default_user"]];
  66. }else {
  67. self.iconImage.image = [UIImage imageNamed:@"qm_default_user"];
  68. }
  69. self.iconImage.frame = CGRectMake(RQ_SCREEN_WIDTH-55, CGRectGetMaxY(self.timeLabel.frame)+10, 41, 41);
  70. UIImage *image = [UIImage imageNamed:@"SenderTextNodeBkg"];
  71. self.chatBackgroudImage.image = image;
  72. self.chatBackgroudImage.image = [self.chatBackgroudImage.image stretchableImageWithLeftCapWidth:20 topCapHeight:20];
  73. }else {
  74. //接收
  75. if ([NSURL URLWithString:message.agentIcon]) {
  76. [self.iconImage sd_setImageWithURL:[NSURL URLWithString:message.agentIcon] placeholderImage:[UIImage imageNamed:@"qm_default_agent"]];
  77. }else {
  78. if ([message.isRobot isEqualToString:@"1"]) {
  79. self.iconImage.image = [UIImage imageNamed:@"qm_default_robot"];
  80. }else {
  81. self.iconImage.image = [UIImage imageNamed:@"qm_default_agent"];
  82. }
  83. }
  84. self.iconImage.frame = CGRectMake(10, CGRectGetMaxY(self.timeLabel.frame)+10, 41, 41);
  85. UIImage *image = [UIImage imageNamed:@"ReceiverTextNodeBkg"];
  86. self.chatBackgroudImage.image = image;
  87. self.chatBackgroudImage.image = [self.chatBackgroudImage.image stretchableImageWithLeftCapWidth:20 topCapHeight:20];
  88. self.sendStatus.hidden = YES;
  89. }
  90. if ([message.fromType isEqualToString:@"0"]) {
  91. if ([message.status isEqualToString:@"0"]) {
  92. self.sendStatus.hidden = YES;
  93. }else if ([message.status isEqualToString:@"1"]) {
  94. self.sendStatus.hidden = NO;
  95. self.sendStatus.image = [UIImage imageNamed:@"icon_send_failed"];
  96. [self removeSendingAnimation];
  97. }else {
  98. self.sendStatus.hidden = NO;
  99. self.sendStatus.image = [UIImage imageNamed:@"icon_sending"];
  100. [self showSendingAnimation];
  101. }
  102. }
  103. }
  104. - (void)showSendingAnimation {
  105. CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  106. animation.fromValue = @0.0;
  107. animation.toValue = @(2*M_PI);
  108. animation.duration = 1.0;
  109. animation.repeatCount = MAXFLOAT;
  110. animation.removedOnCompletion = NO;
  111. __weak QMChatRoomBaseCell *strongSelf = self;
  112. dispatch_async(dispatch_get_main_queue(), ^{
  113. [strongSelf.sendStatus.layer addAnimation:animation forKey:@"transform.rotation.z"];
  114. });
  115. }
  116. - (void)removeSendingAnimation {
  117. __weak QMChatRoomBaseCell *strongSelf = self;
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. [strongSelf.sendStatus.layer removeAnimationForKey:@"transform.rotation.z"];
  120. });
  121. }
  122. - (void)setProgress: (float)progress {
  123. }
  124. - (void)longPressTapGesture:(id)sender {
  125. }
  126. - (void)reSendAction: (UITapGestureRecognizer *)gesture {
  127. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"button.sendAgain", nil) preferredStyle:UIAlertControllerStyleAlert];
  128. UIAlertAction * doneAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  129. if ([_message.status isEqualToString:@"1"]) {
  130. [QMConnect resendMessage:_message successBlock:^{
  131. NSLog(@"消息重发成功");
  132. } failBlock:^(NSString *str) {
  133. NSLog(@"消息重发失败");
  134. }];
  135. }
  136. }];
  137. UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  138. }];
  139. [alert addAction:doneAction];
  140. [alert addAction:cancelAction];
  141. [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil];
  142. }
  143. - (void)awakeFromNib {
  144. [super awakeFromNib];
  145. }
  146. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  147. [super setSelected:selected animated:animated];
  148. }
  149. @end