WFCUShareMessageView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // ShareMessageView.m
  3. // TYAlertControllerDemo
  4. //
  5. // Created by tanyang on 15/10/26.
  6. // Copyright © 2015年 tanyang. All rights reserved.
  7. //
  8. #import "WFCUShareMessageView.h"
  9. #import "UIView+TYAlertView.h"
  10. #import "UITextView+Placeholder.h"
  11. #import <SDWebImage/SDWebImage.h>
  12. #import "WFCUImage.h"
  13. @interface WFCUShareMessageView ()
  14. @property (weak, nonatomic) IBOutlet UIImageView *portraitImageView;
  15. @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
  16. @property (weak, nonatomic) IBOutlet UILabel *digestLabel;
  17. @property (weak, nonatomic) IBOutlet UITextView *messageTextView;
  18. @property (weak, nonatomic) IBOutlet UIView *digestBackgrouView;
  19. @end
  20. @implementation WFCUShareMessageView
  21. - (void)updateUI {
  22. self.digestBackgrouView.clipsToBounds = YES;
  23. self.digestBackgrouView.layer.masksToBounds = YES;
  24. self.digestBackgrouView.layer.cornerRadius = 8.f;
  25. self.messageTextView.placeholder = WFCString(@"LeaveMessage");
  26. self.messageTextView.layer.masksToBounds = YES;
  27. self.messageTextView.layer.cornerRadius = 8.f;
  28. self.messageTextView.contentInset = UIEdgeInsetsMake(2, 8, 2, 2);
  29. self.messageTextView.layer.borderWidth = 0.5f;
  30. self.messageTextView.layer.borderColor = [[UIColor greenColor] CGColor];
  31. }
  32. - (WFCCMessageContent *)filterContent:(WFCCMessage *)message {
  33. WFCCMessageContent *content = message.content;
  34. if([content isKindOfClass:[WFCCCallStartMessageContent class]]) {
  35. content = [WFCCTextMessageContent contentWith:[content digest:message]];
  36. } else if([content isKindOfClass:[WFCCSoundMessageContent class]]) {
  37. WFCCSoundMessageContent *sound = (WFCCSoundMessageContent *)content;
  38. content = [WFCCTextMessageContent contentWith:[NSString stringWithFormat:@"%@ %ld\"", [content digest:message], sound.duration]];
  39. }
  40. return content;
  41. }
  42. - (IBAction)sendAction:(id)sender {
  43. [self hideView];
  44. WFCCTextMessageContent *textMsg;
  45. if (self.messageTextView.text.length) {
  46. textMsg = [[WFCCTextMessageContent alloc] init];
  47. textMsg.text = self.messageTextView.text;
  48. }
  49. __strong WFCCConversation *conversation = self.conversation;
  50. __strong void (^forwardDone)(BOOL success) = self.forwardDone;
  51. if (self.message) {
  52. [[WFCCIMService sharedWFCIMService] send:conversation content:[self filterContent:self.message] success:^(long long messageUid, long long timestamp) {
  53. if (textMsg) {
  54. [[WFCCIMService sharedWFCIMService] send:conversation content:textMsg success:^(long long messageUid, long long timestamp) {
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. if (forwardDone) {
  57. forwardDone(YES);
  58. }
  59. });
  60. } error:^(int error_code) {
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. if (forwardDone) {
  63. forwardDone(NO);
  64. }
  65. });
  66. }];
  67. } else {
  68. dispatch_async(dispatch_get_main_queue(), ^{
  69. if (forwardDone) {
  70. forwardDone(YES);
  71. }
  72. });
  73. }
  74. } error:^(int error_code) {
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. if (forwardDone) {
  77. forwardDone(NO);
  78. }
  79. });
  80. }];
  81. } else {
  82. for (WFCCMessage *msg in self.messages) {
  83. WFCCMessageContent *content = msg.content;
  84. if([content isKindOfClass:[WFCCCallStartMessageContent class]]) {
  85. content = [WFCCTextMessageContent contentWith:[content digest:msg]];
  86. }
  87. [[WFCCIMService sharedWFCIMService] send:conversation content:[self filterContent:msg] success:^(long long messageUid, long long timestamp) {
  88. } error:^(int error_code) {
  89. }];
  90. [NSThread sleepForTimeInterval:0.1];
  91. }
  92. if (textMsg) {
  93. [[WFCCIMService sharedWFCIMService] send:conversation content:textMsg success:^(long long messageUid, long long timestamp) {
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. if (forwardDone) {
  96. forwardDone(YES);
  97. }
  98. });
  99. } error:^(int error_code) {
  100. dispatch_async(dispatch_get_main_queue(), ^{
  101. if (forwardDone) {
  102. forwardDone(NO);
  103. }
  104. });
  105. }];
  106. } else {
  107. if (forwardDone) {
  108. forwardDone(YES);
  109. }
  110. }
  111. }
  112. }
  113. - (IBAction)cancelAction:(id)sender {
  114. [self hideView];
  115. }
  116. - (void)setConversation:(WFCCConversation *)conversation {
  117. [self updateUI];
  118. _conversation = conversation;
  119. NSString *name;
  120. NSString *portrait;
  121. if (conversation.type == Single_Type) {
  122. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:conversation.target refresh:NO];
  123. if (userInfo) {
  124. name = userInfo.displayName;
  125. portrait = userInfo.portrait;
  126. } else {
  127. name = [NSString stringWithFormat:@"%@<%@>", WFCString(@"User"), conversation.target];
  128. }
  129. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:[portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  130. } else if (conversation.type == Group_Type) {
  131. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:conversation.target refresh:NO];
  132. if (groupInfo) {
  133. name = groupInfo.displayName;
  134. if (groupInfo.portrait.length) {
  135. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:[groupInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"group_default_portrait"]];
  136. } else {
  137. NSString *path = [WFCCUtilities getGroupGridPortrait:groupInfo.target width:80 generateIfNotExist:YES defaultUserPortrait:^UIImage *(NSString *userId) {
  138. return [WFCUImage imageNamed:@"PersonalChat"];
  139. }];
  140. if (path) {
  141. [self.portraitImageView sd_setImageWithURL:[NSURL fileURLWithPath:path] placeholderImage:[WFCUImage imageNamed:@"group_default_portrait"]];
  142. }
  143. }
  144. } else {
  145. name = WFCString(@"GroupChat");
  146. [self.portraitImageView setImage:[WFCUImage imageNamed:@"group_default_portrait"]];
  147. }
  148. } else if (conversation.type == Channel_Type) {
  149. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:conversation.target refresh:NO];
  150. if (channelInfo) {
  151. name = channelInfo.name;
  152. portrait = channelInfo.portrait;
  153. } else {
  154. name = WFCString(@"Channel");
  155. }
  156. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:[portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"channel_default_portrait"]];
  157. } else if (conversation.type == SecretChat_Type) {
  158. NSString *userId = [[WFCCIMService sharedWFCIMService] getSecretChatInfo:conversation.target].userId;
  159. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId refresh:NO];
  160. if (userInfo) {
  161. name = userInfo.displayName;
  162. portrait = userInfo.portrait;
  163. } else {
  164. name = [NSString stringWithFormat:@"%@<%@>", WFCString(@"User"), userId];
  165. }
  166. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:[portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  167. }
  168. self.nameLabel.text = name;
  169. }
  170. - (void)setMessage:(WFCCMessage *)message {
  171. _message = message;
  172. if (message) {
  173. self.digestLabel.text = [message.content digest:message];
  174. }
  175. }
  176. - (void)setMessages:(NSArray<WFCCMessage *> *)messages {
  177. _messages = messages;
  178. if (messages.count) {
  179. self.digestLabel.text = [NSString stringWithFormat:@"[逐条转发]共%d条消息", messages.count];
  180. }
  181. }
  182. @end