CloseAlertView.m 7.1 KB
//
//  CloseAlertView.m
//  DreamSleep
//
//  Created by peter on 2022/4/20.
//

#import "CloseAlertView.h"

@interface CloseAlertView ()
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *cryIV;
@property (nonatomic, strong) UILabel *redLab;
@property (nonatomic, strong) UIView *dealView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) NSTimer *closeTimer;
@property (nonatomic, assign) int totalSecond;
@end

@implementation CloseAlertView

- (instancetype)initWithDelegate:(id<CloseAlertViewDelegate>)delegate {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _delegate = delegate;
        _totalSecond = 10;
        
        self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0x6F7587), DSClearColor, DSWhite);
        self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.6];
        
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tapGR];
        
        [self addSubview:self.alertView];
        [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.equalTo(@280);
            make.height.equalTo(@467);
        }];
    }
    return self;
}

- (void)dealloc {
    [_closeTimer invalidate];
    _closeTimer = nil;
}

#pragma mark - public
- (void)showCloseUserAlertView {
    [DSKeyWindow addSubview:self];
    [self.closeTimer setFireDate:[NSDate date]];
}

- (void)dismissCloseUserAlertView {
    [self removeFromSuperview];
    [self.closeTimer setFireDate:[NSDate distantFuture]];
    self.totalSecond = 10;
}

#pragma mark - actions
- (void)closeAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickCloseUserBtn)]) {
        [self.delegate didClickCloseUserBtn];
    }
}

- (void)cancelAction {
    [self dismissCloseUserAlertView];
}

- (void)tapAction:(UITapGestureRecognizer *)gesture {
    if (!CGRectContainsPoint(self.alertView.frame, [gesture locationInView:self])) {
        [self dismissCloseUserAlertView];
    }
}

#pragma mark - lazy
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [UIView new];
        [_alertView cornerRadius:20.0];
        _alertView.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, AlertDarkColor, DSWhite);
        
        [_alertView addSubview:self.titleLab];
        [_alertView addSubview:self.cryIV];
        [_alertView addSubview:self.redLab];
        [_alertView addSubview:self.dealView];
        
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_alertView).offset(20);
            make.centerX.equalTo(_alertView);
        }];
        [self.cryIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLab).offset(37);
            make.left.equalTo(_alertView).offset(44);
            make.width.equalTo(@198);
            make.height.equalTo(@186);
        }];
        [self.redLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.cryIV.mas_bottom).offset(23);
            make.left.equalTo(_alertView).offset(15);
            make.right.equalTo(_alertView).offset(-15);
        }];
        [self.dealView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_alertView);
            make.right.equalTo(_alertView);
            make.bottom.equalTo(_alertView);
            make.height.equalTo(@64);
        }];
    }
    return _alertView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel dkLabWithText:@"确定要抛弃小梦吗?" font:BoldFont(16.0)];
    }
    return _titleLab;
}

- (UIImageView *)cryIV {
    if (!_cryIV) {
        _cryIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"closeAlertIcon"]];
        _cryIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, .8);
    }
    return _cryIV;
}

- (UILabel *)redLab {
    if (!_redLab) {
        NSString *str = @"• 您仅可注销您本人申请的账号\n\n• 注销后,账号的全部权益将被清除备份\n\n• 注销后,账号下的所有数据、记录等将无法访问或找回";
        _redLab = [UILabel labWithText:str textColor:ColorFromHex(0xF04B77) font:SysFont(14.0)];
        _redLab.numberOfLines = 0;
    }
    return _redLab;
}

- (UIView *)dealView {
    if (!_dealView) {
        _dealView = [UIView new];
        
        UIButton *closeBtn = [UIButton btnWithTitle:@"注销" titleColor:BrandColor font:BoldFont(15) bgColor:DSClearColor];
        [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
        closeBtn.enabled = NO;
        [_dealView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        UIButton *cancelBtn = [UIButton btnWithTitle:@"取消" font:BoldFont(15)];
        [cancelBtn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [_dealView addSubview:cancelBtn];
        
        UIView *line = [UIView new];
        line.dk_backgroundColorPicker = DKColorPickerWithColors(DivideLineColor, DSWhite, DSWhite);
        [_dealView addSubview:line];
        
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dealView);
            make.top.equalTo(_dealView);
            make.bottom.equalTo(_dealView);
        }];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(closeBtn.mas_right);
            make.right.equalTo(_dealView);
            make.width.height.equalTo(closeBtn);
        }];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_dealView);
            make.top.equalTo(_dealView).offset(12);
            make.bottom.equalTo(_dealView).offset(-12);
            make.width.equalTo(@1);
        }];
    }
    return _dealView;
}

- (NSTimer *)closeTimer {
    if (!_closeTimer) {
        WS(weakSelf);
        _closeTimer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
            if (weakSelf.totalSecond == 0) {
                [weakSelf.closeTimer setFireDate:[NSDate distantFuture]];
                [weakSelf.closeBtn setTitle:@"注销" font:BoldFont(15)];
                [weakSelf.closeBtn setTitleColor:BrandColor forState:UIControlStateNormal];
                weakSelf.closeBtn.enabled = YES;
                return;
            }
            weakSelf.closeBtn.enabled = NO;
            [weakSelf.closeBtn setTitle:[NSString stringWithFormat:@"注销(%d)", weakSelf.totalSecond] font:BoldFont(15)];
            [weakSelf.closeBtn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite) forState:UIControlStateNormal];
            weakSelf.totalSecond--;
        }];
        [[NSRunLoop currentRunLoop] addTimer:_closeTimer forMode:NSRunLoopCommonModes];
    }
    return _closeTimer;
}

@end