UserInfoViewController.m 9.03 KB
//
//  UserInfoViewController.m
//  CNLiveUserSystemDemo
//
//  Created by 朱林 on 16/9/17.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserInfoHeaderImageTableViewCell.h"
#import "UserInfoTableViewCell.h"
#import "ModifyUserInfoViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface UserInfoViewController ()<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAlertViewDelegate,UIActionSheetDelegate>
{
    UITableView *_tableView;
    NSData *_imageData;
    NSArray *_titlesArray;
    NSArray *_detailsArray;
}

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"个人资料";
    
    _titlesArray = @[@"头像", @"更新手机号", @"昵称修改", @"性别修改", @"邮箱修改",@"地址修改"];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.sectionHeaderHeight = 6;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titlesArray.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        UserInfoHeaderImageTableViewCell *cell = [[UserInfoHeaderImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        
        return cell;
    } else {
        UserInfoTableViewCell *cell = [[UserInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
        [cell setTitle:[_titlesArray objectAtIndex:indexPath.row] detail:(int)(indexPath.row+indexPath.section*5)];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 6)];
    tableView.contentSize = CGSizeMake(KScreenWidth, 53*5+63+12+80+60);
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row) {
        case 0:
        {
            UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择", nil];
            action.tag = 100;
            [action showInView:self.view];
        }
            break;
        case 1:
        {
            ModifyUserInfoViewController *modify = [[ModifyUserInfoViewController alloc] init];
            modify.title = @"更新手机号";
            [self.navigationController pushViewController:modify animated:YES];
        }
            break;
        case 2:
        {
            ModifyUserInfoViewController *modify = [[ModifyUserInfoViewController alloc] init];
            modify.modifyType = UserInfoTypeNickName;
            modify.title = @"昵称修改";
            [self.navigationController pushViewController:modify animated:YES];
        }
            break;
        case 3:
        {
            UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"男" otherButtonTitles:@"女", nil];
            action.tag = 102;
            [action showInView:self.view];
            
        }
            break;
        case 4:
        {
            ModifyUserInfoViewController *modify = [[ModifyUserInfoViewController alloc] init];
            modify.title = @"邮箱修改";
            modify.modifyType = UserInfoTypeEmail;
            [self.navigationController pushViewController:modify animated:YES];
        }
            break;
        case 5:
        {
            ModifyUserInfoViewController *modify = [[ModifyUserInfoViewController alloc] init];
            modify.title = @"联系方式修改";
            modify.modifyType = UserInfoTypeLocation;
            [self.navigationController pushViewController:modify animated:YES];
        }
            break;
    }
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 102) {
        if (buttonIndex == 0) {
            
            [self modifyGender:@"m"];
        } else if (buttonIndex == 1) {
            
            [self modifyGender:@"f"];
        }
    }else if (actionSheet.tag == 100) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        
        switch (buttonIndex)
        {
            case 0:
            {
                if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusDenied || [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusRestricted) {
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您未允许‘互动电视’访问您的相机,请在‘设置-隐私-相机’中更改设置" delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消",@"去设置", nil];
                    alert.tag = 1001;
                    [alert show];
                    
                    return;
                }
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:imagePicker animated:YES completion:NULL];
                } else {
                    [Tools toast:@"您的设备没有相机功能!"];
                }
            }
                break;
            case 1:
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:imagePicker animated:YES completion:NULL];
                break;
        }
    }
}

- (void)modifyGender:(NSString *)string
{
    [SVProgressHUD show];
    
    UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

    [CNLiveUserSystemCenter modifyUserInfoType:UserInfoTypeGender uid:model.uid modifyInfo:string success:^(id result) {
        
        NSLog(@"%@",result);
        if ([[result allKeys] containsObject:@"errorCode"] && [result[@"errorCode"] isEqualToString:@"0"]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            
        } else {
            [Tools toast:result[@"errorMessage"]];
            [SVProgressHUD dismiss];
        }

    } failure:^(NSError *error) {
        [Tools toast:error.localizedDescription];
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
    UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
    if ([image isKindOfClass:[UIImage class]]) {
        [self postPhoto:image];
    } else {
        [Tools toast:@"获取图片失败"];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

-(void)postPhoto:(UIImage *)image
{
    if (image) {
        
        UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

        [CNLiveUserSystemCenter uploadIconWithUid:model.uid icon:image success:^(id result) {
  
            NSLog(@"%@",result);
            
        } failure:^(NSError *error) {
            [Tools toast:error.localizedDescription];
        }];
        
    } else {
        [Tools toast:@"获取图片失败"];
    }
}

- (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