NTESRedPacketTipAttachment.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // NTESRedPacketTipAttachment.m
  3. // NIM
  4. //
  5. // Created by chris on 2017/7/17.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "NTESRedPacketTipAttachment.h"
  9. #import "NTESSessionUtil.h"
  10. #import "M80AttributedLabel.h"
  11. #import "NIMKitInfoFetchOption.h"
  12. @interface NTESRedPacketTipAttachment()
  13. @property (nonatomic,weak) NIMMessage *message;
  14. @end
  15. @implementation NTESRedPacketTipAttachment
  16. - (NSString *)encodeAttachment {
  17. NSDictionary *dictContent = @{
  18. CMRedPacketSendId : self.sendPacketId,
  19. CMRedPacketOpenId : self.openPacketId,
  20. CMRedPacketId : self.packetId,
  21. CMRedPacketDone : self.isGetDone,
  22. };
  23. NSDictionary *dict = @{CMType: @(CustomMessageTypeRedPacketTip), CMData: dictContent};
  24. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
  25. options:0
  26. error:nil];
  27. return [[NSString alloc] initWithData:jsonData
  28. encoding:NSUTF8StringEncoding];
  29. }
  30. - (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width{
  31. self.message = message;
  32. M80AttributedLabel *label = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
  33. label.font = [UIFont systemFontOfSize:Notification_Font_Size];
  34. CGFloat messageWidth = width;
  35. [label appendImage:[UIImage imageNamed:@"icon_redpacket_tip"]];
  36. [label appendText:self.formatedMessage];
  37. label.autoDetectLinks = NO;
  38. label.numberOfLines = 0;
  39. CGFloat padding = [NIMKit sharedKit].config.maxNotificationTipPadding;
  40. CGSize size = [label sizeThatFits:CGSizeMake(width - 2 * padding, CGFLOAT_MAX)];
  41. CGFloat cellPadding = 11.f;
  42. CGSize contentSize = CGSizeMake(messageWidth, size.height + 2 * cellPadding);;
  43. return contentSize;
  44. }
  45. - (NSString *)formatedMessage{
  46. NSString * showContent;
  47. NSString * currentUserId = [[NIMSDK sharedSDK].loginManager currentAccount];
  48. // 领取别人的红包
  49. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  50. option.message = self.message;
  51. if ([currentUserId isEqualToString:self.sendPacketId] && [currentUserId isEqualToString:self.openPacketId])
  52. {
  53. if ([self.isGetDone boolValue])
  54. {
  55. showContent = @"你领取了自己的红包,你的红包已被领完";
  56. }
  57. else
  58. {
  59. showContent = @"你领取了自己的红包";
  60. }
  61. }
  62. else if ([currentUserId isEqualToString:self.openPacketId])
  63. {
  64. NIMKitInfo * sendUserInfo = [[NIMKit sharedKit] infoByUser:self.sendPacketId option:option];
  65. NSString * name = sendUserInfo.showName;
  66. showContent = [NSString stringWithFormat:@"你领取了%@的红包", name];
  67. }
  68. // 他人领取你的红包
  69. else if ([currentUserId isEqualToString:self.sendPacketId])
  70. {
  71. NIMKitInfo * openUserInfo = [[NIMKit sharedKit] infoByUser:self.openPacketId option:option];
  72. NSString * name = openUserInfo.showName;
  73. if ([self.isGetDone boolValue])
  74. {
  75. showContent = [NSString stringWithFormat:@"%@领取了你的红包,你的红包已被领完", name];
  76. }
  77. else
  78. {
  79. showContent = [NSString stringWithFormat:@"%@领取了你的红包", name];
  80. }
  81. }
  82. return [NSString stringWithFormat:@" %@",showContent];
  83. }
  84. - (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
  85. {
  86. return UIEdgeInsetsZero;
  87. }
  88. - (NSString *)cellContent:(NIMMessage *)message
  89. {
  90. return @"NTESSessionRedPacketTipContentView";
  91. }
  92. - (BOOL)canBeForwarded
  93. {
  94. return NO;
  95. }
  96. - (BOOL)canBeRevoked
  97. {
  98. return NO;
  99. }
  100. @end