WFCUMyProfileTableViewController.m 12 KB

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