WFCUMyProfileTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = WFCString(@"MyInformation");
  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 WFCString(@"Male");
  47. } else if(gender == 2) {
  48. return WFCString(@"Female");
  49. }
  50. return WFCString(@"Other");
  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:WFCString(@"Portrait") onCell:headerCell isHeaderCell:YES isLeft:YES];
  70. [self.cells1 addObject:headerCell];
  71. UITableViewCell *cell = [self getAttrCell:WFCString(@"Nickname") rightText:self.userInfo.displayName mutable:YES];
  72. cell.tag = Modify_DisplayName;
  73. [self.cells1 addObject:cell];
  74. cell = [self getAttrCell:WFCString(@"QRCode") 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:WFCString(@"Mobile") rightText:self.userInfo.mobile mutable:YES];
  84. cell.tag = Modify_Mobile;
  85. [self.cells2 addObject:cell];
  86. cell = [self getAttrCell:WFCString(@"Gender") rightText: [self getGenderString:self.userInfo.gender] mutable:YES];
  87. cell.tag = Modify_Gender;
  88. [self.cells2 addObject:cell];
  89. cell = [self getAttrCell:WFCString(@"Email") rightText:self.userInfo.email mutable:YES];
  90. cell.tag = Modify_Email;
  91. [self.cells2 addObject:cell];
  92. cell = [self getAttrCell:WFCString(@"Address") rightText:self.userInfo.address mutable:YES];
  93. cell.tag = Modify_Address;
  94. [self.cells2 addObject:cell];
  95. cell = [self getAttrCell:WFCString(@"Company") rightText:self.userInfo.company mutable:YES];
  96. cell.tag = Modify_Company;
  97. [self.cells2 addObject:cell];
  98. cell = [self getAttrCell:WFCString(@"SocialAccount") 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:[WFCCNetworkService sharedInstance].userId];
  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. [self.navigationController pushViewController:pvc animated:YES];
  184. } else {
  185. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  186. if (cell.tag == 1000) {
  187. [self showMyQrCode];
  188. return;
  189. }
  190. if (cell.tag < 0) {
  191. return;
  192. }
  193. if (cell.tag == Modify_Gender) {
  194. [self sexAlterView];
  195. return;
  196. }
  197. WFCUModifyMyProfileViewController *mpvc = [[WFCUModifyMyProfileViewController alloc] init];
  198. mpvc.modifyType = cell.tag;
  199. __weak typeof(self)ws = self;
  200. mpvc.onModified = ^(NSInteger modifyType, NSString *value) {
  201. NSArray *cells =ws.cells2;
  202. if (indexPath.section == 0) {
  203. cells = ws.cells1;
  204. }
  205. for (UITableViewCell *cell in cells) {
  206. if (cell.tag == modifyType) {
  207. for (UIView *view in cell.subviews) {
  208. if (view.tag == 2) {
  209. UILabel *label = (UILabel *)view;
  210. label.text = value;
  211. }
  212. }
  213. }
  214. }
  215. };
  216. [self.navigationController pushViewController:mpvc animated:YES];
  217. }
  218. }
  219. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  220. if (indexPath.row == 0 && indexPath.section == 0) {
  221. return 76;
  222. } else {
  223. return 40 ;
  224. }
  225. }
  226. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  227. return @" ";
  228. }
  229. - (void)updateGender:(int)gender {
  230. __weak typeof(self) ws = self;
  231. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  232. hud.label.text = WFCString(@"Updating");
  233. [hud showAnimated:YES];
  234. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Gender):[NSString stringWithFormat:@"%d", gender]} success:^{
  235. [hud hideAnimated:NO];
  236. for (UITableViewCell *cell in ws.cells2) {
  237. if (cell.tag == Modify_Gender) {
  238. for (UIView *v in cell.subviews) {
  239. if ([v isKindOfClass:[UILabel class]] && v.tag == 2) {
  240. UILabel *label = (UILabel *)v;
  241. label.text = [ws getGenderString:gender];
  242. break;
  243. }
  244. }
  245. break;
  246. }
  247. }
  248. } error:^(int error_code) {
  249. [hud hideAnimated:NO];
  250. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  251. hud.mode = MBProgressHUDModeText;
  252. hud.label.text = WFCString(@"UpdateFailure");
  253. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  254. [hud hideAnimated:YES afterDelay:1.f];
  255. }];
  256. }
  257. - (void)sexAlterView{
  258. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  259. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Male") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  260. [self updateGender:1];
  261. }]];
  262. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Female") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  263. [self updateGender:2];
  264. }]];
  265. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Other") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  266. [self updateGender:3];
  267. }]];
  268. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:nil]];
  269. [self presentViewController:alert animated:YES completion:nil];
  270. }
  271. - (void)dealloc {
  272. [[NSNotificationCenter defaultCenter] removeObserver:self];
  273. }
  274. @end