WFCUMyProfileTableViewController.m 12 KB

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