2
0

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. #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:NO];
  83. [self.cells1 addObject:cell];
  84. cell = [self getAttrCell:WFCString(@"Mobile") rightText:self.userInfo.mobile mutable:YES];
  85. cell.tag = Modify_Mobile;
  86. [self.cells2 addObject:cell];
  87. cell = [self getAttrCell:WFCString(@"Gender") rightText: [self getGenderString:self.userInfo.gender] mutable:YES];
  88. cell.tag = Modify_Gender;
  89. [self.cells2 addObject:cell];
  90. cell = [self getAttrCell:WFCString(@"Email") rightText:self.userInfo.email mutable:YES];
  91. cell.tag = Modify_Email;
  92. [self.cells2 addObject:cell];
  93. cell = [self getAttrCell:WFCString(@"Address") rightText:self.userInfo.address mutable:YES];
  94. cell.tag = Modify_Address;
  95. [self.cells2 addObject:cell];
  96. cell = [self getAttrCell:WFCString(@"Company") rightText:self.userInfo.company mutable:YES];
  97. cell.tag = Modify_Company;
  98. [self.cells2 addObject:cell];
  99. cell = [self getAttrCell:WFCString(@"SocialAccount") rightText:self.userInfo.social mutable:YES];
  100. cell.tag = Modify_Social;
  101. [self.cells2 addObject:cell];
  102. [self.tableView reloadData];
  103. }
  104. - (void)viewWillAppear:(BOOL)animated {
  105. [super viewWillAppear:animated];
  106. [self loadData:YES];
  107. }
  108. - (UITableViewCell *)getAttrCell:(NSString *)leftText rightText:(NSString *)rightText mutable:(BOOL)mutable {
  109. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  110. // for (UIView *subView in cell.subviews) {
  111. // [subView removeFromSuperview];
  112. // }
  113. if (!mutable) {
  114. cell.accessoryType = UITableViewCellAccessoryNone;
  115. } else {
  116. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  117. }
  118. cell.tag = -1;
  119. [self addLabel:leftText onCell:cell isHeaderCell:NO isLeft:YES];
  120. // if (rightText.length == 0) {
  121. // rightText = @"未填写";
  122. // }
  123. [self addLabel:rightText onCell:cell isHeaderCell:NO isLeft:NO];
  124. return cell;
  125. }
  126. - (void)addLabel:(NSString *)titleStr onCell:(UITableViewCell *)cell isHeaderCell:(BOOL)isHeaderCell isLeft:(BOOL)left {
  127. UILabel *title ;
  128. if (isHeaderCell) {
  129. title = [[UILabel alloc] initWithFrame:CGRectMake(8, 4, 68, 68)];
  130. } else {
  131. if (left) {
  132. title = [[UILabel alloc] initWithFrame:CGRectMake(8, 2, 72, 36)];
  133. } else {
  134. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  135. title = [[UILabel alloc] initWithFrame:CGRectMake(88, 2, width - 108 - 28, 36)];
  136. }
  137. }
  138. [title setFont:[UIFont systemFontOfSize:16]];
  139. [title setText:titleStr];
  140. if (left) {
  141. [title setTextAlignment:NSTextAlignmentLeft];
  142. title.tag = 1;
  143. } else {
  144. [title setTextAlignment:NSTextAlignmentRight];
  145. title.tag = 2;
  146. }
  147. [cell addSubview:title];
  148. }
  149. - (void)onViewPortrait:(id)sender {
  150. WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
  151. pvc.userId = self.userInfo.userId;
  152. [self.navigationController pushViewController:pvc animated:YES];
  153. }
  154. - (void)didReceiveMemoryWarning {
  155. [super didReceiveMemoryWarning];
  156. }
  157. #pragma mark - UITableViewDataSource<NSObject>
  158. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  159. return 2;
  160. }
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  162. if (section == 0) {
  163. return self.cells1.count;
  164. }
  165. return self.cells2.count;
  166. }
  167. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. if (indexPath.section == 0) {
  169. return self.cells1[indexPath.row];
  170. }
  171. return self.cells2[indexPath.row];
  172. }
  173. - (void)showMyQrCode {
  174. if (gQrCodeDelegate) {
  175. [gQrCodeDelegate showQrCodeViewController:self.navigationController type:QRType_User target:[WFCCNetworkService sharedInstance].userId];
  176. }
  177. }
  178. #pragma mark - UITableViewDelegate
  179. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  180. UIViewController *vc;
  181. if (indexPath.row == 0 && indexPath.section == 0) {
  182. WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
  183. pvc.userId = [WFCCNetworkService sharedInstance].userId;
  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 = WFCString(@"Updating");
  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 = WFCString(@"UpdateFailure");
  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:WFCString(@"Male") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  261. [self updateGender:1];
  262. }]];
  263. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Female") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  264. [self updateGender:2];
  265. }]];
  266. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Other") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  267. [self updateGender:3];
  268. }]];
  269. [alert addAction:[UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:nil]];
  270. [self presentViewController:alert animated:YES completion:nil];
  271. }
  272. - (void)dealloc {
  273. [[NSNotificationCenter defaultCenter] removeObserver:self];
  274. }
  275. @end