EMMessageStatusView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // EMMessageStatusView.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/25.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMMessageStatusView.h"
  9. @interface EMMessageStatusView()
  10. @property (nonatomic, strong) UILabel *label;
  11. @property (nonatomic, strong) UIButton *failButton;
  12. @property (nonatomic, strong) UIActivityIndicatorView *activityView;
  13. @property (nonatomic) EMMessageStatus status;
  14. @property (nonatomic) BOOL isReadAcked;
  15. @end
  16. @implementation EMMessageStatusView
  17. - (instancetype)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. self.hidden = YES;
  22. self.backgroundColor = [UIColor clearColor];
  23. }
  24. return self;
  25. }
  26. #pragma mark - Subviews
  27. - (UILabel *)label
  28. {
  29. if (_label == nil) {
  30. _label = [[UILabel alloc] init];
  31. _label.textColor = UIColorHex(999999);
  32. _label.font = [UIFont systemFontOfSize:12];
  33. }
  34. return _label;
  35. }
  36. - (UIButton *)failButton
  37. {
  38. if (_failButton == nil) {
  39. _failButton = [[UIButton alloc] init];
  40. [_failButton setImage:[UIImage imageNamed:@"msg_fail"] forState:UIControlStateNormal];
  41. [_failButton addTarget:self action:@selector(failButtonAction) forControlEvents:UIControlEventTouchUpInside];
  42. }
  43. return _failButton;
  44. }
  45. - (UIActivityIndicatorView *)activityView
  46. {
  47. if (_activityView == nil) {
  48. _activityView = [[UIActivityIndicatorView alloc] init];
  49. _activityView.color = kColor_Blue;
  50. }
  51. return _activityView;
  52. }
  53. #pragma mark - Public
  54. - (void)setSenderStatus:(EMMessageStatus)aStatus
  55. isReadAcked:(BOOL)aIsReadAcked
  56. type:(EMChatType)chatType
  57. {
  58. if (_status != aStatus) {
  59. _status = aStatus;
  60. if (aStatus == EMMessageStatusDelivering) {
  61. self.hidden = NO;
  62. [_label removeFromSuperview];
  63. [_failButton removeFromSuperview];
  64. [self addSubview:self.activityView];
  65. [self.activityView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.edges.equalTo(self);
  67. make.width.equalTo(@20);
  68. }];
  69. [self.activityView startAnimating];
  70. } else if (aStatus == EMMessageStatusFailed) {
  71. self.hidden = NO;
  72. [_label removeFromSuperview];
  73. [_activityView stopAnimating];
  74. [_activityView removeFromSuperview];
  75. [self addSubview:self.failButton];
  76. [self.failButton mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.edges.equalTo(self);
  78. make.width.equalTo(@20);
  79. }];
  80. } else if (aStatus == EMMessageStatusSucceed) {
  81. self.hidden = NO;
  82. [_failButton removeFromSuperview];
  83. [_activityView stopAnimating];
  84. [_activityView removeFromSuperview];
  85. [self addSubview:self.label];
  86. [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.edges.equalTo(self);
  88. }];
  89. } else {
  90. self.hidden = YES;
  91. [_label removeFromSuperview];
  92. [_failButton removeFromSuperview];
  93. [_activityView stopAnimating];
  94. [_activityView removeFromSuperview];
  95. }
  96. self.label.text = aIsReadAcked ? @"已读" : @"未读";
  97. } else if (self.isReadAcked != aIsReadAcked && aStatus == EMMessageStatusSucceed) {
  98. // if (chatType == EMChatTypeGroupChat) {
  99. // self.label.text = @"";
  100. // }else{
  101. // self.label.text = aIsReadAcked ? @"已读" : @"未读";
  102. // }
  103. self.label.text = aIsReadAcked ? @"已读" : @"未读";
  104. }
  105. self.isReadAcked = aIsReadAcked;
  106. }
  107. #pragma mark - Action
  108. - (void)failButtonAction
  109. {
  110. if (self.resendCompletion) {
  111. self.resendCompletion();
  112. }
  113. }
  114. @end