ConversationListViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // ConversationListViewController.m
  3. // ShareExtension
  4. //
  5. // Created by Tom Lee on 2020/10/6.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "ConversationListViewController.h"
  9. #import "SharedConversation.h"
  10. #import "ShareUtility.h"
  11. #import "SharePredefine.h"
  12. #import "ShareAppService.h"
  13. #import "MBProgressHUD.h"
  14. #import "ConversationCell.h"
  15. @interface ConversationListViewController () <UITableViewDelegate, UITableViewDataSource>
  16. @property(nonatomic, strong)NSData *cookiesData;
  17. @property(nonatomic, strong)NSArray<SharedConversation *> *sharedConversations;
  18. @property(nonatomic, strong)UITableView *tableView;
  19. @end
  20. @implementation ConversationListViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.view.backgroundColor = [UIColor whiteColor];
  24. [self prepardDataFromContainer];
  25. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  26. if (@available(iOS 15, *)) {
  27. self.tableView.sectionHeaderTopPadding = 0;
  28. }
  29. self.tableView.delegate = self;
  30. self.tableView.dataSource = self;
  31. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  32. [self.tableView reloadData];
  33. [self.view addSubview:self.tableView];
  34. }
  35. - (void)prepardDataFromContainer {
  36. NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:WFC_SHARE_APP_GROUP_ID];//此处id要与开发者中心创建时一致
  37. NSError *error = nil;
  38. self.sharedConversations = [NSKeyedUnarchiver unarchivedArrayOfObjectsOfClass:[SharedConversation class] fromData:[sharedDefaults objectForKey:WFC_SHARE_BACKUPED_CONVERSATION_LIST] error:&error];
  39. }
  40. - (void)sendTo:(SharedConversation *)conversation {
  41. __weak typeof(self)ws = self;
  42. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  43. [MBProgressHUD HUDForView:self.view].mode = MBProgressHUDModeDeterminate;
  44. [MBProgressHUD HUDForView:self.view].label.text = @"正在发送中...";
  45. if (self.textMessageContent.length) {
  46. [[ShareAppService sharedAppService] sendTextMessage:conversation text:self.textMessageContent success:^(NSDictionary * _Nonnull dict) {
  47. [ws showSuccess];
  48. } error:^(NSString * _Nonnull message) {
  49. [ws showFailure];
  50. }];
  51. } else if(self.url.length) {
  52. [[ShareAppService sharedAppService] sendLinkMessage:conversation link:self.url title:self.urlTitle thumbnailLink:self.urlThumbnail success:^(NSDictionary * _Nonnull dict) {
  53. [ws showSuccess];
  54. } error:^(NSString * _Nonnull message) {
  55. NSLog(@"send msg failure %@", message);
  56. [ws showFailure];
  57. }];
  58. } else if(self.imageUrls.count){
  59. [[ShareAppService sharedAppService] uploadFiles:self.imageUrls[0] mediaType:1 fullImage:self.fullImage progress:^(int sentcount, int dataSize) {
  60. [ws showProgress:sentcount total:dataSize];
  61. } success:^(NSString *url){
  62. NSLog(@"sent done, url is %@", url);
  63. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ws.imageUrls[0]]]];
  64. UIImage *thumbnail = [ShareUtility generateThumbnail:image withWidth:120 withHeight:120];
  65. NSLog(@"sent image msg");
  66. [[ShareAppService sharedAppService] sendImageMessage:conversation
  67. mediaUrl:url
  68. thubnail:thumbnail
  69. success:^(NSDictionary * _Nonnull dict) {
  70. [ws showSuccess];
  71. }
  72. error:^(NSString * _Nonnull message) {
  73. [ws showFailure];
  74. }];
  75. } error:^(NSString * _Nonnull errorMsg) {
  76. [ws showFailure];
  77. }];
  78. } else if(self.fileUrl.length) {
  79. __block int size = 0;
  80. [[ShareAppService sharedAppService] uploadFiles:self.fileUrl mediaType:4 fullImage:YES progress:^(int sentcount, int total) {
  81. size = total;
  82. [ws showProgress:sentcount total:total];
  83. } success:^(NSString * _Nonnull url) {
  84. NSString *fileName = ws.fileUrl.lastPathComponent;
  85. NSLog(@"sent done, url is %@", url);
  86. [[ShareAppService sharedAppService] sendFileMessage:conversation mediaUrl:url fileName:fileName size:size success:^(NSDictionary * _Nonnull dict) {
  87. [ws showSuccess];
  88. } error:^(NSString * _Nonnull message) {
  89. [ws showFailure];
  90. }];
  91. } error:^(NSString * _Nonnull errorMsg) {
  92. [ws showFailure];
  93. }];
  94. } else if(ws.image) {
  95. UIImage *image = [ShareUtility generateThumbnail:ws.image withWidth:1024 withHeight:1024];
  96. NSData *imgData = UIImageJPEGRepresentation(image, 0.85);
  97. [[ShareAppService sharedAppService] uploadData:imgData mediaType:1 progress:^(int sentcount, int total) {
  98. [ws showProgress:sentcount total:total];
  99. } success:^(NSString * _Nonnull url) {
  100. UIImage *thumbnail = [ShareUtility generateThumbnail:ws.image withWidth:120 withHeight:120];
  101. [[ShareAppService sharedAppService] sendImageMessage:conversation
  102. mediaUrl:url
  103. thubnail:thumbnail
  104. success:^(NSDictionary * _Nonnull dict) {
  105. [ws showSuccess];
  106. } error:^(NSString * _Nonnull message) {
  107. [ws showFailure];
  108. }];
  109. } error:^(NSString * _Nonnull errorMsg) {
  110. [ws showFailure];
  111. }];
  112. }
  113. }
  114. - (void)showProgress:(int)sent total:(int)total {
  115. NSLog(@"progress %d %d", sent, total);
  116. __weak typeof(self)ws = self;
  117. dispatch_async(dispatch_get_main_queue(), ^{
  118. [MBProgressHUD HUDForView:ws.view].progress = (float)sent/total;
  119. });
  120. }
  121. - (void)showSuccess {
  122. [MBProgressHUD hideHUDForView:self.view animated:YES];
  123. __weak typeof(self)ws = self;
  124. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已发送" message:@"您可以在野火IM中查看" preferredStyle:UIAlertControllerStyleAlert];
  125. UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  126. [ws.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  127. }];
  128. [alertController addAction:action];
  129. [self presentViewController:alertController animated:YES completion:nil];
  130. }
  131. - (void)showFailure {
  132. [MBProgressHUD hideHUDForView:self.view animated:YES];
  133. __weak typeof(self)ws = self;
  134. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"网络错误" message:@"糟糕!网络出问题了!" preferredStyle:UIAlertControllerStyleAlert];
  135. UIAlertAction *action = [UIAlertAction actionWithTitle:@"算了吧" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  136. [ws.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  137. }];
  138. [alertController addAction:action];
  139. [self presentViewController:alertController animated:YES completion:nil];
  140. }
  141. #pragma mark - UITableViewDataSource
  142. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  143. return self.sharedConversations.count;
  144. }
  145. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  146. ConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  147. if (!cell) {
  148. cell = [[ConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  149. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0 );
  150. }
  151. cell.conversation = self.sharedConversations[indexPath.row];
  152. return cell;
  153. }
  154. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  155. return 56;
  156. }
  157. #pragma mark - UITableViewDelegate
  158. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  159. SharedConversation *sc = self.sharedConversations[indexPath.row];
  160. __weak typeof(self)ws = self;
  161. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确认发送给" message:sc.title preferredStyle:UIAlertControllerStyleAlert];
  162. UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  163. [ws sendTo:sc];
  164. }];
  165. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  166. }];
  167. [alertController addAction:cancel];
  168. [alertController addAction:action];
  169. [self presentViewController:alertController animated:YES completion:nil];
  170. }
  171. @end