WFCUProfileTableViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // WFCUProfileTableViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/22.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUProfileTableViewController.h"
  9. #import "SDWebImage.h"
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUMessageListViewController.h"
  12. #import "MBProgressHUD.h"
  13. #import "WFCUMyPortraitViewController.h"
  14. #import "WFCUVerifyRequestViewController.h"
  15. #import "WFCUGeneralModifyViewController.h"
  16. #import "WFCUVideoViewController.h"
  17. #if WFCU_SUPPORT_VOIP
  18. #import <WFAVEngineKit/WFAVEngineKit.h>
  19. #endif
  20. @interface WFCUProfileTableViewController () <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate>
  21. @property (strong, nonatomic)UIImageView *portraitView;
  22. @property (strong, nonatomic)UILabel *aliasLabel;
  23. @property (strong, nonatomic)UILabel *displayNameLabel;
  24. @property (strong, nonatomic)UITableViewCell *headerCell;
  25. @property (strong, nonatomic)UILabel *mobileLabel;
  26. @property (strong, nonatomic)UILabel *emailLabel;
  27. @property (strong, nonatomic)UILabel *addressLabel;
  28. @property (strong, nonatomic)UILabel *companyLabel;
  29. @property (strong, nonatomic)UILabel *socialLabel;
  30. @property (strong, nonatomic)UITableViewCell *sendMessageCell;
  31. @property (strong, nonatomic)UITableViewCell *voipCallCell;
  32. @property (strong, nonatomic)UITableViewCell *addFriendCell;
  33. @property (nonatomic, strong)UITableView *tableView;
  34. @property (nonatomic, strong)NSMutableArray<UITableViewCell *> *cells;
  35. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  36. @end
  37. @implementation WFCUProfileTableViewController
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:self.userId];
  41. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:YES];
  42. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  43. [self.view addSubview:self.tableView];
  44. self.tableView.delegate = self;
  45. self.tableView.dataSource = self;
  46. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  47. self.tableView.tableFooterView = [[UIView alloc] init];
  48. [self loadData];
  49. }
  50. - (void)onUserInfoUpdated:(NSNotification *)notification {
  51. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  52. if ([self.userId isEqualToString:userInfo.userId]) {
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. self.userInfo = userInfo;
  55. [self loadData];
  56. NSLog(@"reload user info %@", self.userInfo.userId);
  57. });
  58. }
  59. }
  60. - (void)onRightBtn:(id)sender {
  61. NSString *title;
  62. if ([[WFCCIMService sharedWFCIMService] isMyFriend:self.userId]) {
  63. title = @"删除好友";
  64. } else {
  65. title = @"添加好友";
  66. }
  67. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:title otherButtonTitles:@"设置备注", nil];
  68. [actionSheet showInView:self.view];
  69. }
  70. - (void)loadData {
  71. self.cells = [[NSMutableArray alloc] init];
  72. self.headerCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  73. for (UIView *subView in self.headerCell.subviews) {
  74. [subView removeFromSuperview];
  75. }
  76. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  77. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 48, 48)];
  78. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onViewPortrait:)];
  79. [self.portraitView addGestureRecognizer:tap];
  80. self.portraitView.userInteractionEnabled = YES;
  81. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.portrait] placeholderImage: [UIImage imageNamed:@"PersonalChat"]];
  82. NSString *alias = [[WFCCIMService sharedWFCIMService] getFriendAlias:self.userId];
  83. if (alias.length) {
  84. self.aliasLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 8, width - 64 - 8, 21)];
  85. self.aliasLabel.text = alias;
  86. self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 32, width - 64 - 8, 21)];
  87. self.displayNameLabel.text = self.userInfo.displayName;
  88. } else {
  89. self.aliasLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  90. self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 20, width - 64 - 8, 21)];
  91. self.displayNameLabel.text = self.userInfo.displayName;
  92. }
  93. [self.headerCell addSubview:self.portraitView];
  94. [self.headerCell addSubview:self.displayNameLabel];
  95. [self.headerCell addSubview:self.aliasLabel];
  96. if ([[WFCCIMService sharedWFCIMService] isMyFriend:self.userId]) {
  97. if (self.userInfo.mobile.length > 0) {
  98. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  99. cell.textLabel.text = self.userInfo.mobile;
  100. [self.cells addObject:cell];
  101. }
  102. if (self.userInfo.email.length > 0) {
  103. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  104. cell.textLabel.text = self.userInfo.email;
  105. [self.cells addObject:cell];
  106. }
  107. if (self.userInfo.address.length) {
  108. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  109. cell.textLabel.text = self.userInfo.address;
  110. [self.cells addObject:cell];
  111. }
  112. if (self.userInfo.company.length) {
  113. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  114. cell.textLabel.text = self.userInfo.company;
  115. [self.cells addObject:cell];
  116. }
  117. if (self.userInfo.social.length) {
  118. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  119. cell.textLabel.text = self.userInfo.social;
  120. [self.cells addObject:cell];
  121. }
  122. }
  123. if ([[WFCCIMService sharedWFCIMService] isMyFriend:self.userId]) {
  124. self.sendMessageCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  125. for (UIView *subView in self.sendMessageCell.subviews) {
  126. [subView removeFromSuperview];
  127. }
  128. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 8, width - 40, 40)];
  129. [btn setTitle:@"发送消息" forState:UIControlStateNormal];
  130. [btn setBackgroundColor:[UIColor greenColor]];
  131. [btn addTarget:self action:@selector(onSendMessageBtn:) forControlEvents:UIControlEventTouchDown];
  132. btn.layer.cornerRadius = 5.f;
  133. btn.layer.masksToBounds = YES;
  134. [self.sendMessageCell addSubview:btn];
  135. #if WFCU_SUPPORT_VOIP
  136. self.voipCallCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  137. for (UIView *subView in self.voipCallCell.subviews) {
  138. [subView removeFromSuperview];
  139. }
  140. btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 8, width - 40, 40)];
  141. [btn setTitle:@"视频聊天" forState:UIControlStateNormal];
  142. [btn setBackgroundColor:[UIColor blueColor]];
  143. [btn addTarget:self action:@selector(onVoipCallBtn:) forControlEvents:UIControlEventTouchDown];
  144. btn.layer.cornerRadius = 5.f;
  145. btn.layer.masksToBounds = YES;
  146. [self.voipCallCell addSubview:btn];
  147. #endif
  148. } else if([[WFCCNetworkService sharedInstance].userId isEqualToString:self.userId]) {
  149. } else {
  150. self.addFriendCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  151. for (UIView *subView in self.addFriendCell.subviews) {
  152. [subView removeFromSuperview];
  153. }
  154. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 8, width - 40, 40)];
  155. [btn setTitle:@"添加好友" forState:UIControlStateNormal];
  156. [btn setBackgroundColor:[UIColor greenColor]];
  157. [btn addTarget:self action:@selector(onAddFriendBtn:) forControlEvents:UIControlEventTouchDown];
  158. btn.layer.cornerRadius = 5.f;
  159. btn.layer.masksToBounds = YES;
  160. [self.addFriendCell addSubview:btn];
  161. }
  162. [self.tableView reloadData];
  163. }
  164. - (void)onViewPortrait:(id)sender {
  165. WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
  166. pvc.userId = self.userId;
  167. [self.navigationController pushViewController:pvc animated:YES];
  168. }
  169. - (void)onSendMessageBtn:(id)sender {
  170. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  171. mvc.conversation = [WFCCConversation conversationWithType:Single_Type target:self.userId line:0];
  172. for (UIViewController *vc in self.navigationController.viewControllers) {
  173. if ([vc isKindOfClass:[WFCUMessageListViewController class]]) {
  174. WFCUMessageListViewController *old = (WFCUMessageListViewController*)vc;
  175. if (old.conversation.type == Single_Type && [old.conversation.target isEqualToString:self.userId]) {
  176. [self.navigationController popToViewController:vc animated:YES];
  177. return;
  178. }
  179. }
  180. }
  181. UINavigationController *nav = self.navigationController;
  182. [self.navigationController popToRootViewControllerAnimated:NO];
  183. [nav pushViewController:mvc animated:YES];
  184. }
  185. - (void)onVoipCallBtn:(id)sender {
  186. #if WFCU_SUPPORT_VOIP
  187. __weak typeof(self)ws = self;
  188. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  189. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  190. }];
  191. UIAlertAction *actionVoice = [UIAlertAction actionWithTitle:@"语音通话" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  192. WFCCConversation *conversation = [WFCCConversation conversationWithType:Single_Type target:ws.userInfo.userId line:0];
  193. WFCUVideoViewController *videoVC = [[WFCUVideoViewController alloc] initWithTarget:ws.userInfo.userId conversation:conversation audioOnly:YES];
  194. [[WFAVEngineKit sharedEngineKit] presentViewController:videoVC];
  195. }];
  196. UIAlertAction *actionVideo = [UIAlertAction actionWithTitle:@"视频通话" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  197. WFCCConversation *conversation = [WFCCConversation conversationWithType:Single_Type target:ws.userInfo.userId line:0];
  198. WFCUVideoViewController *videoVC = [[WFCUVideoViewController alloc] initWithTarget:ws.userInfo.userId conversation:conversation audioOnly:NO];
  199. [[WFAVEngineKit sharedEngineKit] presentViewController:videoVC];
  200. }];
  201. //把action添加到actionSheet里
  202. [actionSheet addAction:actionVoice];
  203. [actionSheet addAction:actionVideo];
  204. [actionSheet addAction:actionCancel];
  205. //相当于之前的[actionSheet show];
  206. [self presentViewController:actionSheet animated:YES completion:nil];
  207. #endif
  208. }
  209. - (void)onAddFriendBtn:(id)sender {
  210. WFCUVerifyRequestViewController *vc = [[WFCUVerifyRequestViewController alloc] init];
  211. vc.userId = self.userId;
  212. [self.navigationController pushViewController:vc animated:YES];
  213. }
  214. - (void)didReceiveMemoryWarning {
  215. [super didReceiveMemoryWarning];
  216. // Dispose of any resources that can be recreated.
  217. }
  218. #pragma mark - UITableViewDataSource<NSObject>
  219. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  220. if (section == 0) {
  221. return 1;
  222. } else if(section == 1) {
  223. return self.cells.count;
  224. } else {
  225. if (self.sendMessageCell) {
  226. return 2;
  227. } else {
  228. return 1;
  229. }
  230. }
  231. }
  232. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  233. if (indexPath.section == 0) {
  234. return self.headerCell;
  235. } else if (indexPath.section == 1) {
  236. return self.cells[indexPath.row];
  237. } else {
  238. if (self.sendMessageCell) {
  239. if (indexPath.row == 0) {
  240. return self.sendMessageCell;
  241. } else {
  242. return self.voipCallCell;
  243. }
  244. } else {
  245. return self.addFriendCell;
  246. }
  247. }
  248. }
  249. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  250. if (self.sendMessageCell || self.voipCallCell || self.addFriendCell) {
  251. return 3;
  252. } else {
  253. return 2;
  254. }
  255. }
  256. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  257. return @" ";
  258. }
  259. #pragma mark - UITableViewDelegate
  260. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  261. if (indexPath.section == 0) {
  262. return 64;
  263. } else if(indexPath.section == 1) {
  264. return 48;
  265. } else {
  266. return 56;
  267. }
  268. }
  269. #pragma mark - UIActionSheetDelegate <NSObject>
  270. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  271. if(buttonIndex == 0) {
  272. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  273. hud.label.text = @"处理中...";
  274. [hud showAnimated:YES];
  275. if ([[WFCCIMService sharedWFCIMService] isMyFriend:self.userId]) {
  276. [[WFCCIMService sharedWFCIMService] deleteFriend:self.userId success:^{
  277. dispatch_async(dispatch_get_main_queue(), ^{
  278. [hud hideAnimated:YES];
  279. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  280. hud.mode = MBProgressHUDModeText;
  281. hud.label.text = @"处理成功";
  282. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  283. [hud hideAnimated:YES afterDelay:1.f];
  284. });
  285. } error:^(int error_code) {
  286. dispatch_async(dispatch_get_main_queue(), ^{
  287. [hud hideAnimated:YES];
  288. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  289. hud.mode = MBProgressHUDModeText;
  290. hud.label.text = @"处理失败";
  291. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  292. [hud hideAnimated:YES afterDelay:1.f];
  293. });
  294. }];
  295. } else {
  296. WFCUVerifyRequestViewController *vc = [[WFCUVerifyRequestViewController alloc] init];
  297. vc.userId = self.userId;
  298. [self.navigationController pushViewController:vc animated:YES];
  299. }
  300. } else if(buttonIndex == 1) {
  301. WFCUGeneralModifyViewController *gmvc = [[WFCUGeneralModifyViewController alloc] init];
  302. NSString *previousAlias = [[WFCCIMService sharedWFCIMService] getFriendAlias:self.userId];
  303. gmvc.defaultValue = previousAlias;
  304. gmvc.titleText = @"设置备注";
  305. gmvc.canEmpty = YES;
  306. __weak typeof(self)ws = self;
  307. gmvc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  308. if (![newValue isEqualToString:previousAlias]) {
  309. [[WFCCIMService sharedWFCIMService] setFriend:self.userId alias:newValue success:^{
  310. result(YES);
  311. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  312. [ws loadData];
  313. });
  314. } error:^(int error_code) {
  315. result(NO);
  316. }];
  317. }
  318. };
  319. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gmvc];
  320. [self.navigationController presentViewController:nav animated:YES completion:nil];
  321. }
  322. }
  323. - (void)dealloc {
  324. [[NSNotificationCenter defaultCenter] removeObserver:self];
  325. }
  326. @end