// // EditProfileViewController.m // Youth // // Created by mambaxie on 2022/1/2. // #import "EditProfileViewController.h" #import "AvatarView.h" #import "TZImagePickerController+MTImagePicker.h" #import "EditSloganViewController.h" #import "EditNameViewController.h" #import "MTPickerView.h" #import "EditProfileViewController.h" #import "EditSloganViewController.h" #import "GenderPickerView.h" @interface EditProfileViewController () @property (nonatomic, strong) AvatarView *avatarView; @property (nonatomic, strong) PYImageView *bgView; @property (nonatomic, strong) GenderPickerView *genderPickerView; @end @implementation EditProfileViewController - (BOOL)mt_nagationBarTransparent { return NO; } - (void)viewDidLoad { [super viewDidLoad]; User *user = [UserService currentUser]; UIView *headerView = [[UIView alloc] init]; headerView.width = SCREEN_WIDTH; headerView.height = FIX_SIZE(150); PYImageView *bgView = [[PYImageView alloc] init]; [bgView sd_setImageWithURL:[NSURL URLWithString:user.cover_img] placeholderImage:ImageNamed(@"profile_bg")]; bgView.frame = headerView.bounds; [headerView addSubview:bgView]; self.bgView = bgView; AvatarView *avatarView = [AvatarView avatarWithUser:user]; avatarView.size = CGSizeMake(FIX_SIZE(84), FIX_SIZE(84)); avatarView.layer.cornerRadius = FIX_SIZE(15); avatarView.centerY = headerView.height * 0.5; avatarView.centerX = headerView.width * 0.5; self.avatarView = avatarView; WeakSelf(self); [avatarView addTapWithAction:^{ TZImagePickerController *picker = [TZImagePickerController mt_imagePickerWithMaxImagesCount:1 didFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { if (photos.count) { UIImage *image = photos.firstObject; [PYHTTPManager uploadFileWithScene:AliOSSUploadSceneAvatar data:compressImageToDataIfNeed(image) completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error && rsp) { NSString *avatarUrl = rsp; [UserService updateUserToRemoteWithParams:@{ @"avatar": avatarUrl ?: @"" } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.avatar = avatarUrl; user.is_default_avatar = false; weakself.avatarView.image = image; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; } }]; } }]; [weakself presentViewController:picker animated:YES completion:nil]; }]; [headerView addSubview:avatarView]; PYImageView *editIconView = [[PYImageView alloc] initWithImage:ImageNamed(@"TH_profile_edit_camera_icon")]; editIconView.size = CGSizeMake(FIX_SIZE(36), FIX_SIZE(36)); editIconView.center = avatarView.center; [headerView addSubview:editIconView]; UIButton *editBgButton = [UIButton buttonWithType:UIButtonTypeCustom]; editBgButton.size = CGSizeMake(FIX_SIZE(73), FIX_SIZE(28)); editBgButton.layer.cornerRadius = FIX_SIZE(12); editBgButton.backgroundColor = COLOR_WITH_RGB_A(0x222222, 0.3); editBgButton.clipsToBounds = YES; [editBgButton setImage:ImageNamed(@"TH_profile_edit_icon") forState:UIControlStateNormal]; [editBgButton setTitle:@"更换封面" forState:UIControlStateNormal]; [editBgButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; editBgButton.titleLabel.font = MT_FONT_REGULAR_SIZE(11); editBgButton.right = headerView.width - FIX_SIZE(15); editBgButton.bottom = headerView.height - FIX_SIZE(10); [editBgButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) { TZImagePickerController *picker = [TZImagePickerController mt_imagePickerWithMaxImagesCount:1 didFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { if (photos.count) { UIImage *image = photos.firstObject; [PYHTTPManager uploadFileWithScene:AliOSSUploadSceneAvatar data:compressImageToDataIfNeed(image) completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error && rsp) { NSString *coverImageUrl = rsp; [UserService updateUserToRemoteWithParams:@{ @"cover_img": coverImageUrl ?: @"" } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.cover_img = coverImageUrl; weakself.bgView.image = image; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; } }]; } }]; [weakself presentViewController:picker animated:YES completion:nil]; }]; [headerView addSubview:editBgButton]; self.tableView.tableHeaderView = headerView; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self reloadData]; } - (MTCommonListTableConfig *)tableConfigForTableViewContorller { WeakSelf(self); MTCommonListTableConfig *config = [MTCommonListTableConfig defaultConfigWithCellStyle:MTCommonListTableCellStyleDisclosureIndicator rowsCountForSections:@[@(6)]]; config.cellHeight = FIX_SIZE(48); config.cellTitles = @[@"昵称", @"属性", @"想认识的人", @"生日", @"所在地", @"个人简介"]; config.tableTitle = @"编辑资料"; config.sectionFooterHeight = FIX_SIZE(10); config.sectionHeaderHeight = 0.001; User *user = [UserService currentUser]; [config resetCellConfigForSection:0 row:0 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = user.nickname; cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { [PYAppService pushViewControllerAnimated:[EditNameViewController new]]; }; }]; UITextField *genderTextField = [[UITextField alloc] init]; [self.view addSubview:genderTextField]; genderTextField.delegate = self; genderTextField.text = user.gender_str; genderTextField.inputView = [MTPickerView pickerViewWithDatas:@[@[@"CD", @"TS", @"ZN"]] defaultValues:@[genderTextField.text] valueDidChange:nil done:^(NSArray * _Nonnull selectedValues) { NSString *gender_str = selectedValues.firstObject; [UserService updateUserToRemoteWithParams:@{ @"gender_str": selectedValues.firstObject } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.gender_str = gender_str; user.gender_change = @"1"; [weakself reloadData]; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; }]; [config resetCellConfigForSection:0 row:1 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = genderTextField.text; if([user.gender_change isEqualToString:@"0"]){ // cellConfig.style = MTCommonListTableCellStyleNone; }else{ cellConfig.style = MTCommonListTableCellStyleNone; } cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { if([user.gender_change isEqualToString:@"0"]){ [SVProgressHUD showImage:[UIImage new] status:@"属性只能更正一次,请谨慎操作"]; [genderTextField becomeFirstResponder]; } }; }]; UITextField *wantGenderTextField = [[UITextField alloc] init]; [self.view addSubview:wantGenderTextField]; wantGenderTextField.delegate = self; GenderPickerView *genderPickerView = [GenderPickerView pickerViewWithMultiple:YES]; self.genderPickerView = genderPickerView; wantGenderTextField.inputView = [MTPickerView pickerViewWithCustomView:genderPickerView done:^(NSArray * _Nonnull selectedValues) { [UserService updateUserToRemoteWithParams:@{ @"want_genders": genderPickerView.selectedItems ?: @[], } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.want_genders = genderPickerView.selectedItems ?: @[]; [weakself reloadData]; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; }]; [config resetCellConfigForSection:0 row:2 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = [user.want_genders componentsJoinedByString:@"、"]; cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { [wantGenderTextField becomeFirstResponder]; NSMutableArray *defaultIndexs = [NSMutableArray array]; for (NSString *gender in user.want_genders) { NSInteger index = [weakself.genderPickerView.originalModels indexOfObject:gender]; if (index >= 0 && index < weakself.genderPickerView.originalModels.count) { [defaultIndexs addObject:@(index)]; } } [weakself.genderPickerView selectItemAtIndexs:defaultIndexs]; }; }]; UITextField *locationTextField = [[UITextField alloc] init]; [self.view addSubview:locationTextField]; locationTextField.delegate = self; locationTextField.text = user.location ?: @"选择城市-省市"; locationTextField.inputView = [MTPickerView pickerViewWithType:MTPickerViewTypeLocation defaultValues:[user.location ?: @"北京市-北京市-东城区" componentsSeparatedByString:@"-"] valueDidChange:^(NSArray * _Nonnull values) { NSLog(@"改成数据.%@",values); } done:^(NSArray * _Nonnull selectedValues) { NSLog(@"选择."); if (selectedValues.count == 3) { [UserService updateUserToRemoteWithParams:@{ @"province": selectedValues[0]?:@"", @"city": selectedValues[1]?:@"", @"district": selectedValues[2]?:@"", } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.province = selectedValues[0]; user.city = selectedValues[1]; user.district = selectedValues[2]; [weakself reloadData]; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; } }]; UITextField *birthdayTextField = [[UITextField alloc] init]; [self.view addSubview:birthdayTextField]; birthdayTextField.delegate = self; birthdayTextField.text = user.birthday.length > 0 ? user.birthday : nil; birthdayTextField.inputView = [MTPickerView pickerViewWithType:MTPickerViewTypeBirthday defaultValues:[(birthdayTextField.text.length > 0 ? birthdayTextField.text : @"2000-01-01") componentsSeparatedByString:@"-"] valueDidChange:nil done:^(NSArray * _Nonnull selectedValues) { NSString *birthday = [selectedValues componentsJoinedByString:@"-"]; [UserService updateUserToRemoteWithParams:@{ @"birthday": birthday?:@"" } completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { user.birthday = birthday; [weakself reloadData]; }else{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]]; }); } }]; }]; [config resetCellConfigForSection:0 row:3 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = birthdayTextField.text; cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { [birthdayTextField becomeFirstResponder]; }; }]; [config resetCellConfigForSection:0 row:4 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = locationTextField.text; cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { [locationTextField becomeFirstResponder]; }; }]; [config resetCellConfigForSection:0 row:5 config:^(MTCommonListTableCellConfig *cellConfig) { cellConfig.accessoryTitle = user.introduce; cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { [weakself.navigationController pushViewController:[EditSloganViewController new] animated:YES]; }; }]; return config; } @end