WFCUMyProfileTableViewController.m 12 KB

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