IVTEditUserInfoViewController.m 13.9 KB
//
//  IVTEditUserInfoViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTEditUserInfoViewController.h"
#import "IVTEditMyNickNameViewController.h"
#import "IVTEditMySexViewController.h"
#import "IVTEditMySignatureViewController.h"
#import "IVTEditMyProfessionalViewController.h"

@interface IVTEditUserInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    NSArray *titlesArray;
    UITableView *myTableView;
    
    UIImageView *headImageView;
    UILabel *nickNameLabel;
    UIImageView *sexImageView;
    UILabel *signatureLabel;
    UILabel *professionalLabel;
    UILabel *certificationLabel;
}
@end

@implementation IVTEditUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    titlesArray = @[@[@"头像",@"昵称",@"性别"],@[@"个人签名",@"职业"]];

    __weak __typeof(self)weakSelf = self;
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64) style:UITableViewStyleGrouped];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:myTableView];
    [myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.top.equalTo(weakSelf.view.mas_top);
        make.right.equalTo(weakSelf.view.mas_right);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
}

- (void)setNavigationBar{
    self.navigationItem.title = @"个人资料";
}
#pragma mark - UITableViewDelegate / UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return titlesArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[titlesArray objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 70;
    }
    else{
        return 46;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle  =UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont IVTH3Font];
        cell.textLabel.textColor = [UIColor IVTMainTextColor];
    }
    cell.textLabel.text = [[titlesArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.accessoryType = UITableViewCellAccessoryNone;

            headImageView = [[UIImageView alloc] init];
            headImageView.frame = CGRectMake(kScreenWidth - 60, 10, 45, 45);
            CGRect rect = headImageView.frame;
            headImageView.layer.cornerRadius = rect.size.width/2;
            headImageView.layer.masksToBounds = YES;
            NSString *avatar = [IVTAccountTool getAvatarUrl];
            [headImageView sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"占位头像"]];
            [cell.contentView addSubview:headImageView];
        }else if (indexPath.row == 1){
            nickNameLabel = [[UILabel alloc]init];
            nickNameLabel.frame = CGRectMake(60, 13, kScreenWidth - 100, 20);
            nickNameLabel.font = [UIFont IVTH3Font];
            nickNameLabel.textColor = [UIColor IVTMainTextColor];
            nickNameLabel.text = [[IVTAccountTool getNickName] base64DecodedToString];
            nickNameLabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:nickNameLabel];
        }else if (indexPath.row == 2){
            sexImageView = [[UIImageView alloc] init];
            sexImageView.frame = CGRectMake(60, 16, 12, 14);
            [cell.contentView addSubview:sexImageView];

            NSString *sex = [IVTAccountTool getSex];
            if ([sex isEqualToString:@"male"]) {
                sexImageView.image = [UIImage imageNamed:@"性别男"];
            }else if([sex isEqualToString:@"female"]){
                sexImageView.image = [UIImage imageNamed:@"性别女"];
            }else {
                sexImageView.image = [UIImage imageNamed:@"性别女"];
            }
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            NSString *signString = [[IVTAccountTool getSignature] base64DecodedToString];
            if (signString == nil || signString.length == 0) {
                signString = @" ";
            }
            signatureLabel = [[UILabel alloc]init];
            signatureLabel.frame = CGRectMake(100, 13, kScreenWidth - 140, 20);
            signatureLabel.font = [UIFont systemFontOfSize:14];
            signatureLabel.textColor = [UIColor IVTMainTextColor];
            signatureLabel.text = signString;
            signatureLabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:signatureLabel];
        }else if (indexPath.row == 1){
            NSString *careerString = [[IVTAccountTool getProfessional] base64DecodedToString];
            if (careerString == nil || careerString.length == 0) {
                careerString = @" ";
            }
            professionalLabel = [[UILabel alloc]init];
            professionalLabel.frame = CGRectMake(100, 13, kScreenWidth - 140, 20);
            professionalLabel.font = [UIFont IVTH3Font];
            professionalLabel.textColor = [UIColor IVTMainTextColor];
            professionalLabel.text = careerString;
            professionalLabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:professionalLabel];
        }
//        else if (indexPath.row == 2){
//            cell.textLabel.text = @"认证";
//            cell.detailTextLabel.text = @"申请蜜家认证";
//            cell.detailTextLabel.font = [UIFont IVTH3Font];
//            cell.detailTextLabel.textColor = [UIColor IVTMainTextColor];
//        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] && ![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到摄像头" preferredStyle:UIAlertControllerStyleAlert];
                    [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        
                    }]];
                    [self presentViewController:alertC animated:YES completion:^{
                        
                    }];
                }
                else{
                    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
                    imagePicker.delegate = self;
                    imagePicker.allowsEditing = YES;
                    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
                    [self presentViewController:imagePicker animated:YES completion:nil];
                }
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                [self presentViewController:imagePicker animated:YES completion:nil];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
        else if (indexPath.row == 1){
            IVTEditMyNickNameViewController *editMyNickNameVC = [[IVTEditMyNickNameViewController alloc]init];
            editMyNickNameVC.editSuccessBlock = ^(NSString *nickNameString){
                nickNameLabel.text = nickNameString;
            };
            [self.navigationController pushViewController:editMyNickNameVC animated:YES];
        }
        else if (indexPath.row == 2){
            IVTEditMySexViewController *editMySexVC = [[IVTEditMySexViewController alloc]init];
            editMySexVC.editSuccessBlock = ^(NSString *sexString){
                if ([sexString isEqualToString:@"male"]) {
                    sexImageView.image = [UIImage imageNamed:@"性别男"];
                }else if([sexString isEqualToString:@"female"]){
                    sexImageView.image = [UIImage imageNamed:@"性别女"];
                }else {
                    sexImageView.image = [UIImage imageNamed:@"性别女"];
                }
            };
            [self.navigationController pushViewController:editMySexVC animated:YES];
        }
    }
    else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            IVTEditMySignatureViewController *editMySignatureVC = [[IVTEditMySignatureViewController alloc]init];
            editMySignatureVC.editSuccessBlock = ^(NSString *signatureString){
                signatureLabel.text = signatureString;
            };
            [self.navigationController pushViewController:editMySignatureVC animated:YES];
        }
        else if (indexPath.row == 1){
            IVTEditMyProfessionalViewController *editMyProfessionalVC = [[IVTEditMyProfessionalViewController alloc]init];
            editMyProfessionalVC.editSuccessBlock = ^(NSString *professionalString){
                professionalLabel.text = professionalString;
            };
            [self.navigationController pushViewController:editMyProfessionalVC animated:YES];
        }
        else if (indexPath.row == 2){
            
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *imagePicker = [self imageWithImage:info[@"UIImagePickerControllerEditedImage"] scaledToSize:CGSizeMake(400, 400)];
    NSData *imageData = UIImageJPEGRepresentation(imagePicker, 0.4);
    UIImage *image = [UIImage imageWithData:imageData];
    [picker dismissViewControllerAnimated:YES completion:nil];
    [HUDUtil showLoading];
    [[IVTNetworkAPIClient sharedClient] uploadUserAvatarWithData:imageData success:^(NSDictionary *successData) {
        [IVTAccountTool setUserAvatarUrl:[successData valueForKey:@"filePath"]];
        headImageView.image = image;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeHead" object:nil];

        [HUDUtil showSuccess:@"上传成功"];

    } error:^(NSDictionary *successData) {
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
    } failure:^(NSError *failureError) {
        [HUDUtil showError:@"网络异常"];
    }];
}


- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)targetSize{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (!CGSizeEqualToSize(imageSize, targetSize))
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        if (widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end