123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- //
- // WFCUProfileTableViewController.m
- // WFChat UIKit
- //
- // Created by WF Chat on 2017/10/22.
- // Copyright © 2017年 WildFireChat. All rights reserved.
- //
- #import "WFCUMyProfileTableViewController.h"
- #import "SDWebImage.h"
- #import <WFChatClient/WFCChatClient.h>
- #import "WFCUMessageListViewController.h"
- #import "MBProgressHUD.h"
- #import "WFCUMyPortraitViewController.h"
- #import "WFCUModifyMyProfileViewController.h"
- @interface WFCUMyProfileTableViewController () <UITableViewDelegate, UITableViewDataSource>
- @property (strong, nonatomic)UIImageView *portraitView;
- @property (nonatomic, strong)UITableView *tableView;
- @property (nonatomic, strong)NSMutableArray<UITableViewCell *> *cells1;
- @property (nonatomic, strong)NSMutableArray<UITableViewCell *> *cells2;
- @property (nonatomic, strong)WFCCUserInfo *userInfo;
- @end
- @implementation WFCUMyProfileTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
- [self.view addSubview:self.tableView];
-
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- self.tableView.tableFooterView = [[UIView alloc] init];
- self.tableView.tableHeaderView = nil;
- self.tableView.backgroundColor = [UIColor colorWithRed:239/255.f green:239/255.f blue:239/255.f alpha:1.0f];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
-
- self.title = @"我的信息";
- }
- - (void)onUserInfoUpdated:(NSNotification *)notification {
- WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
- if ([[WFCCNetworkService sharedInstance].userId isEqualToString:userInfo.userId]) {
- [self loadData:NO];
- }
- }
- - (NSString *)getGenderString:(int)gender {
- if (gender == 0) {
- return @"";
- } else if(gender == 1) {
- return @"男";
- } else if(gender == 2) {
- return @"女";
- }
-
- return @"其他";
- }
- - (void)loadData:(BOOL)refresh {
- self.cells1 = [[NSMutableArray alloc] init];
- self.cells2 = [[NSMutableArray alloc] init];
- self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:refresh];
-
- UITableViewCell *headerCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
- for (UIView *subView in headerCell.subviews) {
- [subView removeFromSuperview];
- }
- CGFloat width = [UIScreen mainScreen].bounds.size.width;
-
- self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(width - 104, 6, 64, 64)];
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onViewPortrait:)];
- [self.portraitView addGestureRecognizer:tap];
- self.portraitView.userInteractionEnabled = YES;
-
-
- [self.portraitView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.portrait] placeholderImage: [UIImage imageNamed:@"PersonalChat"]];
-
- [headerCell addSubview:self.portraitView];
- headerCell.tag = -1;
- headerCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- [self addLabel:@"头像" onCell:headerCell isHeaderCell:YES isLeft:YES];
- [self.cells1 addObject:headerCell];
- UITableViewCell *cell = [self getAttrCell:@"昵称" rightText:self.userInfo.displayName mutable:YES];
- cell.tag = Modify_DisplayName;
- [self.cells1 addObject:cell];
- // cell = [self getAttrCell:@"账号" rightText:self.userInfo.name mutable:NO];
- // [self.cells1 addObject:cell];
- cell = [self getAttrCell:@"电话" rightText:self.userInfo.mobile mutable:YES];
- cell.tag = Modify_Mobile;
- [self.cells2 addObject:cell];
- cell = [self getAttrCell:@"性别" rightText: [self getGenderString:self.userInfo.gender] mutable:YES];
- cell.tag = Modify_Gender;
- [self.cells2 addObject:cell];
- cell = [self getAttrCell:@"邮箱" rightText:self.userInfo.email mutable:YES];
- cell.tag = Modify_Email;
- [self.cells2 addObject:cell];
- cell = [self getAttrCell:@"地址" rightText:self.userInfo.address mutable:YES];
- cell.tag = Modify_Address;
- [self.cells2 addObject:cell];
- cell = [self getAttrCell:@"公司" rightText:self.userInfo.company mutable:YES];
- cell.tag = Modify_Company;
- [self.cells2 addObject:cell];
- cell = [self getAttrCell:@"社交账号" rightText:self.userInfo.social mutable:YES];
- cell.tag = Modify_Social;
- [self.cells2 addObject:cell];
-
- [self.tableView reloadData];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self loadData:YES];
- }
- - (UITableViewCell *)getAttrCell:(NSString *)leftText rightText:(NSString *)rightText mutable:(BOOL)mutable {
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
- // for (UIView *subView in cell.subviews) {
- // [subView removeFromSuperview];
- // }
- if (!mutable) {
- cell.accessoryType = UITableViewCellAccessoryNone;
- } else {
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }
-
- cell.tag = -1;
- [self addLabel:leftText onCell:cell isHeaderCell:NO isLeft:YES];
- // if (rightText.length == 0) {
- // rightText = @"未填写";
- // }
- [self addLabel:rightText onCell:cell isHeaderCell:NO isLeft:NO];
- return cell;
- }
- - (void)addLabel:(NSString *)titleStr onCell:(UITableViewCell *)cell isHeaderCell:(BOOL)isHeaderCell isLeft:(BOOL)left {
- UILabel *title ;
- if (isHeaderCell) {
- title = [[UILabel alloc] initWithFrame:CGRectMake(8, 4, 68, 68)];
- } else {
- if (left) {
- title = [[UILabel alloc] initWithFrame:CGRectMake(8, 2, 72, 36)];
- } else {
- CGFloat width = [UIScreen mainScreen].bounds.size.width;
- title = [[UILabel alloc] initWithFrame:CGRectMake(88, 2, width - 108 - 28, 36)];
- }
- }
-
- [title setFont:[UIFont systemFontOfSize:16]];
- [title setText:titleStr];
- if (left) {
- [title setTextAlignment:NSTextAlignmentLeft];
- title.tag = 1;
- } else {
- [title setTextAlignment:NSTextAlignmentRight];
- title.tag = 2;
- }
-
- [cell addSubview:title];
- }
- - (void)onViewPortrait:(id)sender {
- WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
- pvc.userId = self.userInfo.userId;
- [self.navigationController pushViewController:pvc animated:YES];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- #pragma mark - UITableViewDataSource<NSObject>
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 2;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (section == 0) {
- return self.cells1.count;
- }
- return self.cells2.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section == 0) {
- return self.cells1[indexPath.row];
- }
- return self.cells2[indexPath.row];
- }
- #pragma mark - UITableViewDelegate
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- UIViewController *vc;
- if (indexPath.row == 0 && indexPath.section == 0) {
- WFCUMyPortraitViewController *pvc = [[WFCUMyPortraitViewController alloc] init];
- pvc.userId = [WFCCNetworkService sharedInstance].userId;
- vc = pvc;
- [self.navigationController pushViewController:pvc animated:YES];
- } else {
- UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- if (cell.tag < 0) {
- return;
- }
-
- if (cell.tag == Modify_Gender) {
- [self sexAlterView];
- return;
- }
-
- WFCUModifyMyProfileViewController *mpvc = [[WFCUModifyMyProfileViewController alloc] init];
- mpvc.modifyType = cell.tag;
- __weak typeof(self)ws = self;
- mpvc.onModified = ^(NSInteger modifyType, NSString *value) {
- NSArray *cells =ws.cells2;
- if (indexPath.section == 0) {
- cells = ws.cells1;
- }
- for (UITableViewCell *cell in cells) {
- if (cell.tag == modifyType) {
- for (UIView *view in cell.subviews) {
- if (view.tag == 2) {
- UILabel *label = (UILabel *)view;
- label.text = value;
- }
- }
- }
- }
- };
- [self.navigationController pushViewController:mpvc animated:YES];
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.row == 0 && indexPath.section == 0) {
- return 76;
- } else {
- return 40 ;
- }
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- return @" ";
- }
- - (void)updateGender:(int)gender {
- __weak typeof(self) ws = self;
-
- __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- hud.label.text = @"修改中...";
- [hud showAnimated:YES];
-
- [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Gender):[NSString stringWithFormat:@"%d", gender]} success:^{
- [hud hideAnimated:NO];
- for (UITableViewCell *cell in ws.cells2) {
- if (cell.tag == Modify_Gender) {
- for (UIView *v in cell.subviews) {
- if ([v isKindOfClass:[UILabel class]] && v.tag == 2) {
- UILabel *label = (UILabel *)v;
- label.text = [ws getGenderString:gender];
- break;
- }
- }
- break;
- }
- }
- } error:^(int error_code) {
- [hud hideAnimated:NO];
-
- hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
- hud.mode = MBProgressHUDModeText;
- hud.label.text = @"修改失败";
- hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
- [hud hideAnimated:YES afterDelay:1.f];
- }];
- }
- - (void)sexAlterView{
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self updateGender:1];
- }]];
- [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
- [self updateGender:2];
- }]];
- [alert addAction:[UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
- [self updateGender:3];
- }]];
- [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- @end
|