TUIBubbleMessageCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // TBubbleMessageCell.m
  3. // TXIMSDK_TUIKit_iOS
  4. //
  5. // Created by annidyfeng on 2019/5/22.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIBubbleMessageCell.h"
  9. #import <TIMCommon/TIMCommonModel.h>
  10. #import <TIMCommon/TIMDefine.h>
  11. #import <TUICore/TUICore.h>
  12. @implementation TUIBubbleMessageCell
  13. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  14. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  15. if (self) {
  16. _bubbleView = [[UIImageView alloc] initWithFrame:self.container.bounds];
  17. _bubbleView.userInteractionEnabled = YES;
  18. [self.container addSubview:_bubbleView];
  19. _bubbleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  20. self.securityStrikeView = [[TUISecurityStrikeView alloc] init];
  21. [self.bubbleView addSubview:self.securityStrikeView];
  22. }
  23. return self;
  24. }
  25. - (void)fillWithData:(TUIBubbleMessageCellData *)data {
  26. [super fillWithData:data];
  27. self.bubbleData = data;
  28. self.bubbleView.image = self.getBubble;
  29. self.bubbleView.highlightedImage = self.getHighlightBubble;
  30. self.securityStrikeView.hidden = YES;
  31. BOOL hasRiskContent = self.messageData.innerMessage.hasRiskContent;
  32. if (hasRiskContent) {
  33. self.bubbleView.image = [self getErrorBubble];
  34. self.securityStrikeView.hidden = NO;
  35. }
  36. [self prepareReactTagUI:self.container];
  37. // tell constraints they need updating
  38. [self setNeedsUpdateConstraints];
  39. // update constraints now so we can animate the change
  40. [self updateConstraintsIfNeeded];
  41. [self layoutIfNeeded];
  42. }
  43. + (BOOL)requiresConstraintBasedLayout {
  44. return YES;
  45. }
  46. // this is Apple's recommended place for adding/updating constraints
  47. - (void)updateConstraints {
  48. [super updateConstraints];
  49. [self.bubbleView mas_remakeConstraints:^(MASConstraintMaker *make) {
  50. make.leading.mas_equalTo(0);
  51. make.size.mas_equalTo(self.container);
  52. make.top.mas_equalTo(self.container);
  53. }];
  54. CGPoint center = self.retryView.center;
  55. center.y = self.bubbleView.center.y;
  56. self.retryView.center = center;
  57. }
  58. - (void)layoutSubviews {
  59. [super layoutSubviews];
  60. }
  61. - (void)highlightWhenMatchKeyword:(NSString *)keyword {
  62. /**
  63. * The parent class implements the default highlighting effect - flickering
  64. */
  65. if (keyword) {
  66. if (self.highlightAnimating) {
  67. return;
  68. }
  69. [self animate:3];
  70. }
  71. }
  72. - (void)animate:(int)times {
  73. times--;
  74. if (times < 0) {
  75. self.bubbleView.image = self.getBubble;
  76. self.highlightAnimating = NO;
  77. return;
  78. }
  79. self.highlightAnimating = YES;
  80. self.bubbleView.image = self.getAnimateHighlightBubble_alpha50;
  81. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  82. self.bubbleView.image = self.getAnimateHighlightBubble_alpha20;
  83. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  84. if (!self.bubbleData.highlightKeyword) {
  85. [self animate:0];
  86. return;
  87. }
  88. [self animate:times];
  89. });
  90. });
  91. }
  92. - (CGFloat)getBubbleTop {
  93. return [self.class getBubbleTop:self.bubbleData];
  94. }
  95. - (UIImage *)getBubble {
  96. if (!TIMConfig.defaultConfig.enableMessageBubble) {
  97. return nil;
  98. }
  99. if (self.bubbleData.direction == MsgDirectionIncoming) {
  100. return self.class.incommingBubble;
  101. } else {
  102. return self.class.outgoingBubble;
  103. }
  104. }
  105. - (UIImage *)getHighlightBubble {
  106. if (!TIMConfig.defaultConfig.enableMessageBubble) {
  107. return nil;
  108. }
  109. if (self.bubbleData.direction == MsgDirectionIncoming) {
  110. return self.class.incommingHighlightedBubble;
  111. } else {
  112. return self.class.outgoingHighlightedBubble;
  113. }
  114. }
  115. - (UIImage *)getErrorBubble {
  116. if (self.bubbleData.direction == MsgDirectionIncoming) {
  117. return self.class.incommingErrorBubble;
  118. } else {
  119. return self.class.outgoingErrorBubble;
  120. }
  121. }
  122. - (UIImage *)getAnimateHighlightBubble_alpha50 {
  123. if (!TIMConfig.defaultConfig.enableMessageBubble) {
  124. return nil;
  125. }
  126. if (self.bubbleData.direction == MsgDirectionIncoming) {
  127. return self.class.incommingAnimatedHighlightedAlpha50;
  128. } else {
  129. return self.class.outgoingAnimatedHighlightedAlpha50;
  130. }
  131. }
  132. - (UIImage *)getAnimateHighlightBubble_alpha20 {
  133. if (!TIMConfig.defaultConfig.enableMessageBubble) {
  134. return nil;
  135. }
  136. if (self.bubbleData.direction == MsgDirectionIncoming) {
  137. return self.class.incommingAnimatedHighlightedAlpha20;
  138. } else {
  139. return self.class.outgoingAnimatedHighlightedAlpha20;
  140. }
  141. }
  142. - (void)prepareReactTagUI:(UIView *)containerView {
  143. NSDictionary *param = @{TUICore_TUIChatExtension_ChatMessageReactPreview_Delegate: self};
  144. [TUICore raiseExtension:TUICore_TUIChatExtension_ChatMessageReactPreview_ClassicExtensionID parentView:containerView param:param];
  145. }
  146. + (CGFloat)getBubbleTop:(TUIBubbleMessageCellData *)data {
  147. if (data.direction == MsgDirectionIncoming) {
  148. return self.class.incommingBubbleTop;
  149. } else {
  150. return self.class.outgoingBubbleTop;
  151. }
  152. }
  153. @end
  154. @implementation TUIBubbleMessageCell (TUILayoutConfiguration)
  155. + (void)initialize {
  156. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onThemeChanged:) name:TUIDidApplyingThemeChangedNotfication object:nil];
  157. }
  158. #pragma mark - outgoing Bubble
  159. static UIImage *gOutgoingBubble;
  160. + (UIImage *)outgoingBubble {
  161. if (!gOutgoingBubble) {
  162. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"SenderTextNodeBkg")];
  163. [self setOutgoingBubble:TUIChatDynamicImage(@"chat_bubble_send_img", defaultImage)];
  164. }
  165. return gOutgoingBubble;
  166. }
  167. + (void)setOutgoingBubble:(UIImage *)outgoingBubble {
  168. gOutgoingBubble = [self stretchImage:outgoingBubble];
  169. }
  170. static UIImage *gOutgoingHighlightedBubble;
  171. + (UIImage *)outgoingHighlightedBubble {
  172. if (!gOutgoingHighlightedBubble) {
  173. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"SenderTextNodeBkgHL")];
  174. [self setOutgoingHighlightedBubble:TUIChatDynamicImage(@"chat_bubble_send_img", defaultImage)];
  175. }
  176. return gOutgoingHighlightedBubble;
  177. }
  178. + (void)setOutgoingHighlightedBubble:(UIImage *)outgoingHighlightedBubble {
  179. gOutgoingHighlightedBubble = [self stretchImage:outgoingHighlightedBubble];
  180. }
  181. static UIImage *gOutgoingAnimatedHighlightedAlpha50;
  182. + (UIImage *)outgoingAnimatedHighlightedAlpha50 {
  183. if (!gOutgoingAnimatedHighlightedAlpha50) {
  184. UIImage *alpha50 = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"SenderTextNodeBkg_alpha50")];
  185. [self setOutgoingAnimatedHighlightedAlpha50:TUIChatDynamicImage(@"chat_bubble_send_alpha50_img", alpha50)];
  186. }
  187. return gOutgoingAnimatedHighlightedAlpha50;
  188. }
  189. + (void)setOutgoingAnimatedHighlightedAlpha50:(UIImage *)outgoingAnimatedHighlightedAlpha50 {
  190. gOutgoingAnimatedHighlightedAlpha50 = [self stretchImage:outgoingAnimatedHighlightedAlpha50];
  191. }
  192. static UIImage *gOutgoingAnimatedHighlightedAlpha20;
  193. + (UIImage *)outgoingAnimatedHighlightedAlpha20 {
  194. if (!gOutgoingAnimatedHighlightedAlpha20) {
  195. UIImage *alpha20 = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"SenderTextNodeBkg_alpha20")];
  196. [self setOutgoingAnimatedHighlightedAlpha20:TUIChatDynamicImage(@"chat_bubble_send_alpha20_img", alpha20)];
  197. }
  198. return gOutgoingAnimatedHighlightedAlpha20;
  199. }
  200. + (void)setOutgoingAnimatedHighlightedAlpha20:(UIImage *)outgoingAnimatedHighlightedAlpha20 {
  201. gOutgoingAnimatedHighlightedAlpha20 = [self stretchImage:outgoingAnimatedHighlightedAlpha20];
  202. }
  203. static UIImage *gOutgoingErrorBubble;
  204. + (UIImage *)outgoingErrorBubble {
  205. if (!gOutgoingErrorBubble) {
  206. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"SenderTextNodeBkg")];
  207. UIImage *formatImage = TUIChatDynamicImage(@"chat_bubble_send_img", defaultImage);
  208. formatImage = [TUISecurityStrikeView changeImageColorWith:[UIColor tui_colorWithHex:@"#FA5151" alpha:0.16] image:formatImage alpha:1];
  209. gOutgoingErrorBubble = [self stretchImage:formatImage];
  210. }
  211. return gOutgoingErrorBubble;
  212. }
  213. #pragma mark - incomming Bubble
  214. static UIImage *gIncommingBubble;
  215. + (UIImage *)incommingBubble {
  216. if (!gIncommingBubble) {
  217. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"ReceiverTextNodeBkg")];
  218. [self setIncommingBubble:TUIChatDynamicImage(@"chat_bubble_receive_img", defaultImage)];
  219. }
  220. return gIncommingBubble;
  221. }
  222. + (void)setIncommingBubble:(UIImage *)incommingBubble {
  223. gIncommingBubble = [self stretchImage:incommingBubble];
  224. }
  225. static UIImage *gIncommingHighlightedBubble;
  226. + (UIImage *)incommingHighlightedBubble {
  227. if (!gIncommingHighlightedBubble) {
  228. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"ReceiverTextNodeBkgHL")];
  229. [self setIncommingHighlightedBubble:TUIChatDynamicImage(@"chat_bubble_receive_img", defaultImage)];
  230. }
  231. return gIncommingHighlightedBubble;
  232. }
  233. + (void)setIncommingHighlightedBubble:(UIImage *)incommingHighlightedBubble {
  234. gIncommingHighlightedBubble = [self stretchImage:incommingHighlightedBubble];
  235. }
  236. static UIImage *gIncommingAnimatedHighlightedAlpha50;
  237. + (UIImage *)incommingAnimatedHighlightedAlpha50 {
  238. if (!gIncommingAnimatedHighlightedAlpha50) {
  239. UIImage *alpha50 = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"ReceiverTextNodeBkg_alpha50")];
  240. [self setIncommingAnimatedHighlightedAlpha50:TUIChatDynamicImage(@"chat_bubble_receive_alpha50_img", alpha50)];
  241. }
  242. return gIncommingAnimatedHighlightedAlpha50;
  243. }
  244. + (void)setIncommingAnimatedHighlightedAlpha50:(UIImage *)incommingAnimatedHighlightedAlpha50 {
  245. gIncommingAnimatedHighlightedAlpha50 = [self stretchImage:incommingAnimatedHighlightedAlpha50];
  246. }
  247. static UIImage *gIncommingAnimatedHighlightedAlpha20;
  248. + (UIImage *)incommingAnimatedHighlightedAlpha20 {
  249. if (!gIncommingAnimatedHighlightedAlpha20) {
  250. UIImage *alpha20 = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"ReceiverTextNodeBkg_alpha20")];
  251. [self setIncommingAnimatedHighlightedAlpha20:TUIChatDynamicImage(@"chat_bubble_receive_alpha20_img", alpha20)];
  252. }
  253. return gIncommingAnimatedHighlightedAlpha20;
  254. }
  255. + (void)setIncommingAnimatedHighlightedAlpha20:(UIImage *)incommingAnimatedHighlightedAlpha20 {
  256. gIncommingAnimatedHighlightedAlpha20 = [self stretchImage:incommingAnimatedHighlightedAlpha20];
  257. }
  258. static UIImage *gIncommingErrorBubble;
  259. + (UIImage *)incommingErrorBubble {
  260. if (!gIncommingErrorBubble) {
  261. UIImage *defaultImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"ReceiverTextNodeBkg")];
  262. UIImage *formatImage = TUIChatDynamicImage(@"chat_bubble_receive_img", defaultImage);
  263. formatImage = [TUISecurityStrikeView changeImageColorWith:[UIColor tui_colorWithHex:@"#FA5151" alpha:0.16] image:formatImage alpha:1];
  264. gIncommingErrorBubble = [self stretchImage:formatImage];
  265. }
  266. return gIncommingErrorBubble;
  267. }
  268. + (UIImage *)stretchImage:(UIImage *)oldImage {
  269. UIImage *image = [oldImage rtl_imageFlippedForRightToLeftLayoutDirection];
  270. UIEdgeInsets insets = rtlEdgeInsetsWithInsets(UIEdgeInsetsFromString(@"{12,12,12,12}"));
  271. return [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
  272. }
  273. static CGFloat gOutgoingBubbleTop = 0;
  274. + (CGFloat)outgoingBubbleTop {
  275. return gOutgoingBubbleTop;
  276. }
  277. + (void)setOutgoingBubbleTop:(CGFloat)outgoingBubble {
  278. gOutgoingBubbleTop = outgoingBubble;
  279. }
  280. static CGFloat gIncommingBubbleTop = 0;
  281. + (CGFloat)incommingBubbleTop {
  282. return gIncommingBubbleTop;
  283. }
  284. + (void)setIncommingBubbleTop:(CGFloat)incommingBubbleTop {
  285. gIncommingBubbleTop = incommingBubbleTop;
  286. }
  287. + (void)onThemeChanged:(NSNotification *)notice {
  288. gOutgoingBubble = nil;
  289. gOutgoingHighlightedBubble = nil;
  290. gOutgoingAnimatedHighlightedAlpha50 = nil;
  291. gOutgoingAnimatedHighlightedAlpha20 = nil;
  292. gIncommingBubble = nil;
  293. gIncommingHighlightedBubble = nil;
  294. gIncommingAnimatedHighlightedAlpha50 = nil;
  295. gIncommingAnimatedHighlightedAlpha20 = nil;
  296. }
  297. @end