GiGaMaskTime/GIGA/Modules/Me/Controller/GiGaUserViewController.m

314 lines
11 KiB
Objective-C

//
// GiGaUserViewController.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaUserViewController.h"
#import "GIGaUserInfoAvatorCell.h"
#import "GIGaUserIDViewCell.h"
#import "GIGaUserWeChatViewCell.h"
#import "GiGaUserSexViewCell.h"
#import "GIGaUserCityViewCell.h"
#import "GiGaUser.h"
#import "GiGaUserManager.h"
#import "LYSDatePickerController.h"
#import "JYAddressPicker.h"
#import "GiGaBaseAPiRequest.h"
#import "GiGaUserJobViewCell.h"
@interface GiGaUserViewController ()<LYSDatePickerSelectDelegate>
@property (nonatomic,strong) GiGaUser *user;
@property (nonatomic,strong) NSMutableArray *jobArr;
@property (nonatomic) BOOL jobSpread;
@end
@implementation GiGaUserViewController
- (NSArray *)jobArr{
if (!_jobArr) {
_jobArr = [NSMutableArray new];
}
return _jobArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.title = @"个人中心";
_jobSpread = NO;
[self getJobInfos];
self.user = [[GiGaUserManager shareUser] getCurrentUser];
[self addNavTitile:@"个人信息"];
UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[saveBtn addTarget:self action:@selector(saveUserInfo) forControlEvents:UIControlEventTouchUpInside];
saveBtn.frame = CGRectMake(0, 0,40, 36);
NSAttributedString *attrititle = [GiGaHelper stringWithText:@"保存" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTMEDIUM(16) leterSpace:0];
[saveBtn setAttributedTitle:attrititle forState:UIControlStateNormal];
UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
self.navigationItem.rightBarButtonItem = saveItem;
[self creatTab];
}
-(void)creatTab{
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.sectionFooterHeight = 0;
self.tableView.estimatedSectionFooterHeight= 10;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.bounces = YES;
self.tableView.frame = CGRectMake(0, 1, KMainW,self.view.bounds.size.height - SAFE_NAV_HEIGHT);
[self.tableView registerClass: [GiGaUserJobViewCell class] forCellReuseIdentifier:@"GiGaUserJobViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return 1;
}else if (section == 1){
return 3;
}else if (section == 3){
return 1;
}
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section ==0){
GIGaUserInfoAvatorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserInfoAvatorCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserInfoAvatorCell" owner:self options:nil] lastObject];
}
[cell loadCellData:indexPath user:self.user];
return cell;
}else if (indexPath.section == 1 && indexPath.row < 2){
GIGaUserIDViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserIDViewCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserIDViewCell" owner:self options:nil] lastObject];
}
[cell loadCellData:indexPath user: self.user];
return cell;
}else if (indexPath.section == 1 && indexPath.row == 2){
GIGaUserWeChatViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserWeChatViewCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserWeChatViewCell" owner:self options:nil] lastObject];
}
[cell loadCellData:indexPath user:self.user];
return cell;
}else if (indexPath.section == 2 && indexPath.row == 0){
GiGaUserSexViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiGaUserSexViewCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GiGaUserSexViewCell" owner:self options:nil] lastObject];
}
[cell loadCellData:indexPath user:self.user];
//0 女 1 男
weakify(self);
[cell userSelectSexAction:^(int sex) {
weakSelf.user.sexCode = sex;
}];
GILog(@"sex:%d",self.user.sexCode);
return cell;
}else if (indexPath.section == 2 && indexPath.row >= 1){
GIGaUserCityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserCityViewCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserCityViewCell" owner:self options:nil] lastObject];
}
[cell loadCellData:indexPath user:self.user];
return cell;
}else if (indexPath.section == 3){
static NSString *cellID = @"GiGaUserJobViewCell";
GiGaUserJobViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
if (!cell) {
cell = [[GiGaUserJobViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
[cell userSelectjobAction:^() {
self.jobSpread = !self.jobSpread;
[tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone];
}];
[cell setButtnImage:self.jobSpread with:self.jobArr];
[cell userSelectjob:^(NSString *title) {
self.user.occupationCode = title;
[tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
}];
return cell;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
GILog(@"sex:%d",self.user.sexCode);
if (indexPath.section == 2 && indexPath.row == 1) {
[LYSDatePickerController alertDatePickerWithController:self type:(LYSDatePickerTypeDay)];
[LYSDatePickerController customPickerDelegate:self];
weakify(self);
[LYSDatePickerController customdidSelectDatePicker:^(NSDate *date) {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy/MM/dd"];
NSString *currentDate = [dateFormat stringFromDate:date];
GIGaUserCityViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
weakSelf.user.birthday = currentDate;
[cell loadCellData:indexPath user:weakSelf.user];
}];
}else if (indexPath.section == 2 && indexPath.row == 2){
JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self];
weakify(self);
addressPicker.selectedItemBlock = ^(NSArray *addressArr) {
NSString *province = [addressArr objectAtIndex:0][@"name"];
NSString *city = [addressArr objectAtIndex:1][@"name"];
NSString *county = [addressArr objectAtIndex:2][@"name"];
NSString *addres = [NSString stringWithFormat:@"%@%@%@",province,city,county];
GILog(@"%@",addres);
GIGaUserCityViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
weakSelf.user.provinceName = province;
weakSelf.user.cityName = city;
weakSelf.user.areaName = county;
[cell loadCellData:indexPath user:self.user];
};
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section==0){
return 133;
}else if (indexPath.section==3){
if (self.jobArr.count <= 0) {
return 0;
}else{
return self.jobSpread == YES ? 246 : 85;
}
}else {
return 44;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 1){
return 10;
}else if (section == 2){
return 4;
}
return 0.1;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 1 || section == 2) {
UIView *view = [[UIView alloc] init];
view.backgroundColor = GIGARGB(222, 222, 222, 1);
return view;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 保存用户信息
-(void)saveUserInfo{
if (!self.user.nickName || [self.user.nickName isKindOfClass:[NSNull class]]) {
self.user.nickName = @"";
}
if (!self.user.weixin || [self.user.weixin isKindOfClass:[NSNull class]]) {
self.user.weixin = @"";
}
if (!self.user.birthday || [self.user.birthday isKindOfClass:[NSNull class]]) {
self.user.birthday = @"";
}
if (!self.user.occupationCode|| [self.user.occupationCode isKindOfClass:[NSNull class]]) {
self.user.occupationCode = @"";
}
NSDictionary *param = @{
@"provinceId":@"1_11",
@"cityId":@"1_11",
@"areaId":@"1_11_8",
@"nickName":self.user.nickName,
@"weixin":self.user.weixin,
@"sexCode":[NSString stringWithFormat:@"%d",self.user.sexCode],
@"birthday":self.user.birthday,
@"occupationCode":self.user.occupationCode
};
GiGaBaseAPiRequest *api =[GiGaBaseAPiRequest initWithRequestPath:kFixUserInfo method:RequestPostMethod parms:param];
[api requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
GIGA_ShowToast(@"修改成功");
}else{
GIGA_ShowToast(result.dic[@"msg"]);
}
}];
}
#pragma mark - 获取职业数据
-(void)getJobInfos{
NSDictionary *param = @{};
GiGaBaseAPiRequest *api =[GiGaBaseAPiRequest initWithRequestPath:kApiOccuption method:RequestPostMethod parms:param];
[api requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
GILog(@"%@",result.dic);
NSDictionary *dic = result.dic;
NSArray *josA = dic[@"list"];
for (NSDictionary *jobDic in josA ) {
[self.jobArr addObject:jobDic[@"dictLabel"]];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone];
}else{
NSString *msg = result.dic[@"msg"];
GIGA_ShowToast(msg);
}
}];
}
@end