WFCUMyProfileTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "WFCUMyProfileTableViewController.h"
  9. #import "SDWebImage.h"
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUMessageListViewController.h"
  12. #import "MBProgressHUD.h"
  13. #import "WFCUMyPortraitViewController.h"
  14. #import "WFCUModifyMyProfileViewController.h"
  15. @interface WFCUMyProfileTableViewController () <UITableViewDelegate, UITableViewDataSource>
  16. @property (strong, nonatomic)UIImageView *portraitView;
  17. @property (nonatomic, strong)UITableView *tableView;
  18. @property (nonatomic, strong)NSMutableArray<UITableViewCell *> *cells1;
  19. @property (nonatomic, strong)NSMutableArray<UITableViewCell *> *cells2;
  20. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  21. @end
  22. @implementation WFCUMyProfileTableViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  26. [self.view addSubview:self.tableView];
  27. self.tableView.delegate = self;
  28. self.tableView.dataSource = self;
  29. self.tableView.tableFooterView = [[UIView alloc] init];
  30. self.tableView.tableHeaderView = nil;
  31. self.tableView.backgroundColor = [UIColor colorWithRed:239/255.f green:239/255.f blue:239/255.f alpha:1.0f];
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  33. self.title = @"我的信息";
  34. }
  35. - (void)onUserInfoUpdated:(NSNotification *)notification {
  36. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  37. if ([[WFCCNetworkService sharedInstance].userId isEqualToString:userInfo.userId]) {
  38. [self loadData:NO];
  39. }
  40. }
  41. - (NSString *)getGenderString:(int)gender {
  42. if (gender == 0) {
  43. return @"";
  44. } else if(gender == 1) {
  45. return @"男";
  46. } else if(gender == 2) {
  47. return @"女";
  48. }
  49. return @"其他";
  50. }
  51. - (void)loadData:(BOOL)refresh {
  52. self.cells1 = [[NSMutableArray alloc] init];
  53. self.cells2 = [[NSMutableArray alloc] init];
  54. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:refresh];
  55. UITableViewCell *headerCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  56. for (UIView *subView in headerCell.subviews) {
  57. [subView removeFromSuperview];
  58. }
  59. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  60. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(width - 104, 6, 64, 64)];
  61. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onViewPortrait:)];
  62. [self.portraitView addGestureRecognizer:tap];
  63. self.portraitView.userInteractionEnabled = YES;
  64. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.portrait] placeholderImage: [UIImage imageNamed:@"PersonalChat"]];
  65. [headerCell addSubview:self.portraitView];
  66. headerCell.tag = -1;
  67. headerCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  68. [self addLabel:@"头像" onCell:headerCell isHeaderCell:YES isLeft:YES];
  69. [self.cells1 addObject:headerCell];
  70. UITableViewCell *cell = [self getAttrCell:@"昵称" rightText:self.userInfo.displayName mutable:YES];
  71. cell.tag = Modify_DisplayName;
  72. [self.cells1 addObject:cell];
  73. // cell = [self getAttrCell:@"账号" rightText:self.userInfo.name mutable:NO];
  74. // [self.cells1 addObject:cell];
  75. cell = [self getAttrCell:@"电话" rightText:self.userInfo.mobile mutable:YES];
  76. cell.tag = Modify_Mobile;
  77. [self.cells2 addObject:cell];
  78. cell = [self getAttrCell:@"性别" rightText: [self getGenderString:self.userInfo.gender] mutable:YES];
  79. cell.tag = Modify_Gender;
  80. [self.cells2 addObject:cell];
  81. cell = [self getAttrCell:@"邮箱" rightText:self.userInfo.email mutable:YES];
  82. cell.tag = Modify_Email;
  83. [self.cells2 addObject:cell];
  84. cell = [self getAttrCell:@"地址" rightText:self.userInfo.address mutable:YES];
  85. cell.tag = Modify_Address;
  86. [self.cells2 addObject:cell];
  87. cell = [self getAttrCell:@"公司" rightText:self.userInfo.company mutable:YES];
  88. cell.tag = Modify_Company;
  89. [self.cells2 addObject:cell];
  90. cell = [self getAttrCell:@"社交账号" rightText:self.userInfo.social mutable:YES];
  91. cell.tag = Modify_Social;
  92. [self.cells2 addObject:cell];
  93. [self.tableView reloadData];
  94. }
  95. - (void)viewWillAppear:(BOOL)animated {
  96. [super viewWillAppear:animated];
  97. [self loadData:YES];
  98. }
  99. - (UITableViewCell *)getAttrCell:(NSString *)leftText rightText:(NSString *)rightText mutable:(BOOL)mutable {
  100. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  101. // for (UIView *subView in cell.subviews) {
  102. // [subView removeFromSuperview];
  103. // }
  104. if (!mutable) {
  105. cell.accessoryType = UITableViewCellAccessoryNone;
  106. } else {
  107. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  108. }
  109. cell.tag = -1;
  110. [self addLabel:leftText onCell:cell isHeaderCell:NO isLeft:YES];
  111. // if (rightText.length == 0) {
  112. // rightText = @"未填写";
  113. // }
  114. [self addLabel:rightText onCell:cell isHeaderCell:NO isLeft:NO];
  115. return cell;
  116. }
  117. - (void)addLabel:(NSString *)titleStr onCell:(UITableViewCell *)cell isHeaderCell:(BOOL)isHeaderCell isLeft:(BOOL)left {
  118. UILabel *title ;
  119. if (isHeaderCell) {
  120. title = [[UILabel alloc] initWithFrame:CGRectMake(8, 4, 68, 68)];
  121. } else {
  122. if (left) {
  123. title = [[UILabel alloc] initWithFrame:CGRectMake(8, 2, 72, 36)];
  124. } else {
  125. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  126. title = [[UILabel alloc] initWithFrame:CGRectMake(88, 2, width - 108 - 28, 36)];
  127. }
  128. }
  129. [title setFont:[UIFont systemFontOfSize:16]];
  130. [title setText:titleStr];
  131. if (left) {
  132. [title setTextAlignment:NSTextAlignmentLeft];
  133. title.tag = 1;
  134. } else {
  135. [title setTextAlignment:NSTextAlignmentRight];
  136. title.tag = 2;
  137. }
  138. [cell addSubview:title];
  139. }
  140. - (void)onViewPortrait:(id)sender {
  141. WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
  142. pvc.userId = self.userInfo.userId;
  143. [self.navigationController pushViewController:pvc animated:YES];
  144. }
  145. - (void)didReceiveMemoryWarning {
  146. [super didReceiveMemoryWarning];
  147. }
  148. #pragma mark - UITableViewDataSource<NSObject>
  149. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  150. return 2;
  151. }
  152. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  153. if (section == 0) {
  154. return self.cells1.count;
  155. }
  156. return self.cells2.count;
  157. }
  158. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. if (indexPath.section == 0) {
  160. return self.cells1[indexPath.row];
  161. }
  162. return self.cells2[indexPath.row];
  163. }
  164. #pragma mark - UITableViewDelegate
  165. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  166. UIViewController *vc;
  167. if (indexPath.row == 0 && indexPath.section == 0) {
  168. WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
  169. pvc.userId = [WFCCNetworkService sharedInstance].userId;
  170. vc = pvc;
  171. [self.navigationController pushViewController:pvc animated:YES];
  172. } else {
  173. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  174. if (cell.tag < 0) {
  175. return;
  176. }
  177. if (cell.tag == Modify_Gender) {
  178. [self sexAlterView];
  179. return;
  180. }
  181. WFCUModifyMyProfileViewController *mpvc = [[WFCUModifyMyProfileViewController alloc] init];
  182. mpvc.modifyType = cell.tag;
  183. __weak typeof(self)ws = self;
  184. mpvc.onModified = ^(NSInteger modifyType, NSString *value) {
  185. NSArray *cells =ws.cells2;
  186. if (indexPath.section == 0) {
  187. cells = ws.cells1;
  188. }
  189. for (UITableViewCell *cell in cells) {
  190. if (cell.tag == modifyType) {
  191. for (UIView *view in cell.subviews) {
  192. if (view.tag == 2) {
  193. UILabel *label = (UILabel *)view;
  194. label.text = value;
  195. }
  196. }
  197. }
  198. }
  199. };
  200. [self.navigationController pushViewController:mpvc animated:YES];
  201. }
  202. }
  203. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  204. if (indexPath.row == 0 && indexPath.section == 0) {
  205. return 76;
  206. } else {
  207. return 40 ;
  208. }
  209. }
  210. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  211. return @" ";
  212. }
  213. - (void)updateGender:(int)gender {
  214. __weak typeof(self) ws = self;
  215. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  216. hud.label.text = @"修改中...";
  217. [hud showAnimated:YES];
  218. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Gender):[NSString stringWithFormat:@"%d", gender]} success:^{
  219. [hud hideAnimated:NO];
  220. for (UITableViewCell *cell in ws.cells2) {
  221. if (cell.tag == Modify_Gender) {
  222. for (UIView *v in cell.subviews) {
  223. if ([v isKindOfClass:[UILabel class]] && v.tag == 2) {
  224. UILabel *label = (UILabel *)v;
  225. label.text = [ws getGenderString:gender];
  226. break;
  227. }
  228. }
  229. break;
  230. }
  231. }
  232. } error:^(int error_code) {
  233. [hud hideAnimated:NO];
  234. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  235. hud.mode = MBProgressHUDModeText;
  236. hud.label.text = @"修改失败";
  237. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  238. [hud hideAnimated:YES afterDelay:1.f];
  239. }];
  240. }
  241. - (void)sexAlterView{
  242. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  243. [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  244. [self updateGender:1];
  245. }]];
  246. [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  247. [self updateGender:2];
  248. }]];
  249. [alert addAction:[UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  250. [self updateGender:3];
  251. }]];
  252. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
  253. [self presentViewController:alert animated:YES completion:nil];
  254. }
  255. - (void)dealloc {
  256. [[NSNotificationCenter defaultCenter] removeObserver:self];
  257. }
  258. @end