NIMMessageModel.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // NIMMessageModel.m
  3. // NIMKit
  4. //
  5. // Created by NetEase.
  6. // Copyright (c) 2015年 NetEase. All rights reserved.
  7. //
  8. #import "NIMMessageModel.h"
  9. #import "NIMKit.h"
  10. @interface NIMMessageModel()
  11. @property (nonatomic,strong) NSMutableDictionary *contentSizeInfo;
  12. @end
  13. @implementation NIMMessageModel
  14. @synthesize contentViewInsets = _contentViewInsets;
  15. @synthesize bubbleViewInsets = _bubbleViewInsets;
  16. @synthesize shouldShowAvatar = _shouldShowAvatar;
  17. @synthesize shouldShowNickName = _shouldShowNickName;
  18. @synthesize shouldShowLeft = _shouldShowLeft;
  19. @synthesize avatarMargin = _avatarMargin;
  20. @synthesize nickNameMargin = _nickNameMargin;
  21. @synthesize avatarSize = _avatarSize;
  22. - (instancetype)initWithMessage:(NIMMessage*)message
  23. {
  24. if (self = [self init])
  25. {
  26. _message = message;
  27. _messageTime = message.timestamp;
  28. _contentSizeInfo = [[NSMutableDictionary alloc] init];
  29. }
  30. return self;
  31. }
  32. - (void)cleanCache
  33. {
  34. [_contentSizeInfo removeAllObjects];
  35. _contentViewInsets = UIEdgeInsetsZero;
  36. _bubbleViewInsets = UIEdgeInsetsZero;
  37. }
  38. - (NSString*)description{
  39. return self.message.text;
  40. }
  41. - (BOOL)isEqual:(id)object
  42. {
  43. if (![object isKindOfClass:[NIMMessageModel class]])
  44. {
  45. return NO;
  46. }
  47. else
  48. {
  49. NIMMessageModel *model = object;
  50. return [self.message isEqual:model.message];
  51. }
  52. }
  53. - (CGSize)contentSize:(CGFloat)width
  54. {
  55. CGSize size = [self.contentSizeInfo[@(width)] CGSizeValue];
  56. if (CGSizeEqualToSize(size, CGSizeZero))
  57. {
  58. [self updateLayoutConfig];
  59. id<NIMCellLayoutConfig> layoutConfig = [[NIMKit sharedKit] layoutConfig];
  60. size = [layoutConfig contentSize:self cellWidth:width];
  61. [self.contentSizeInfo setObject:[NSValue valueWithCGSize:size] forKey:@(width)];
  62. }
  63. return size;
  64. }
  65. - (UIEdgeInsets)contentViewInsets{
  66. if (UIEdgeInsetsEqualToEdgeInsets(_contentViewInsets, UIEdgeInsetsZero))
  67. {
  68. id<NIMCellLayoutConfig> layoutConfig = [[NIMKit sharedKit] layoutConfig];
  69. _contentViewInsets = [layoutConfig contentViewInsets:self];
  70. }
  71. return _contentViewInsets;
  72. }
  73. - (UIEdgeInsets)bubbleViewInsets{
  74. if (UIEdgeInsetsEqualToEdgeInsets(_bubbleViewInsets, UIEdgeInsetsZero))
  75. {
  76. id<NIMCellLayoutConfig> layoutConfig = [[NIMKit sharedKit] layoutConfig];
  77. _bubbleViewInsets = [layoutConfig cellInsets:self];
  78. }
  79. return _bubbleViewInsets;
  80. }
  81. - (void)updateLayoutConfig
  82. {
  83. id<NIMCellLayoutConfig> layoutConfig = [[NIMKit sharedKit] layoutConfig];
  84. _shouldShowAvatar = [layoutConfig shouldShowAvatar:self];
  85. _shouldShowNickName = [layoutConfig shouldShowNickName:self];
  86. _shouldShowLeft = [layoutConfig shouldShowLeft:self];
  87. _avatarMargin = [layoutConfig avatarMargin:self];
  88. _nickNameMargin = [layoutConfig nickNameMargin:self];
  89. _avatarSize = [layoutConfig avatarSize:self];
  90. }
  91. - (BOOL)shouldShowReadLabel
  92. {
  93. if (self.message.session.sessionType == NIMSessionTypeP2P)
  94. {
  95. return _shouldShowReadLabel && self.message.isRemoteRead;
  96. }
  97. else
  98. {
  99. return _shouldShowReadLabel;
  100. }
  101. }
  102. @end