NickNameController.m 4.2 KB
//
//  NickNameController.m
//  DreamSleep
//
//  Created by peter on 2022/4/25.
//

#import "NickNameController.h"

@interface NickNameController ()
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UIImageView *portraitIV;
@property (nonatomic, strong) UIImageView *cameraIV;
@property (nonatomic, strong) UITextField *nickTF;
@property (nonatomic, strong) UIButton *saveBtn;
@end

@implementation NickNameController

- (instancetype)initWithBlock:(SaveBlock)block {
    if (self = [super init]) {
        _block = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改昵称";
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    [self.view addSubview:self.mainView];
    [self.mainView addSubview:self.portraitIV];
    [self.mainView addSubview:self.cameraIV];
    [self.mainView addSubview:self.nickTF];
    [self.mainView addSubview:self.saveBtn];
    
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(83);
    }];
    
    [self.portraitIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(145, 145));
        make.top.equalTo(self.mainView);
        make.centerX.equalTo(self.mainView);
    }];
    [self.cameraIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.portraitIV).offset(-6);
        make.bottom.equalTo(self.portraitIV).offset(-6);
    }];
    [self.nickTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainView);
        make.top.equalTo(self.portraitIV.mas_bottom).offset(60);
        make.size.mas_equalTo(CGSizeMake(155, 40));
    }];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainView);
        make.size.equalTo(self.nickTF);
        make.top.equalTo(self.nickTF.mas_bottom).offset(15);
        make.bottom.equalTo(self.mainView);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - Actions
- (void)saveAction {
    [DSProgressHUD showProgressHUDWithInfo:@""];
    NSDictionary *data = @{@"nick_name" : self.nickTF.text};
    WS(weakSelf);
    [UserRequestModel updateUserInfoWithData:data completion:^(UserRequestModel * _Nonnull requestModel) {
        [DSProgressHUD dissmissProgressHUD];
        if (requestModel.resCode == DSResCodeSuccess) {
            [DSProgressHUD showToast:@"修改成功"];
            [LoginUtils updateUserInfo:data];
            if (weakSelf.block) { weakSelf.block(); }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}

#pragma mark - lazy
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
    }
    return _mainView;
}

- (UIImageView *)portraitIV {
    if (!_portraitIV) {
        _portraitIV = [UIImageView new];
        [_portraitIV yy_setImageWithURL:[NSURL URLWithString:[LoginUtils getFaceImg]] placeholder:[UIImage imageNamed:@"portrait"]];
        [_portraitIV cornerRadius:145/2.0];
    }
    return _portraitIV;
}

- (UIImageView *)cameraIV {
    if (!_cameraIV) {
        _cameraIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cameraIcon"]];
    }
    return _cameraIV;
}

- (UITextField *)nickTF {
    if (!_nickTF) {
        _nickTF = [UITextField new];
        _nickTF.font = BoldFont(16);
        _nickTF.dk_textColorPicker = DKColorPickerWithKey(TEXT);
        UserModel *model = [LoginUtils getUserLoginData];
        _nickTF.text = model.nick_name;
        _nickTF.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
        _nickTF.textAlignment = NSTextAlignmentCenter;
        [_nickTF cornerRadius:20];
    }
    return _nickTF;
}

- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [UIButton btnWithTitle:@"保存" titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
        [_saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
        [_saveBtn cornerRadius:20];
    }
    return _saveBtn;
}

@end