ExceptionDefaultView.m 5.2 KB
//
//  ExceptionDefaultView.m
//  DreamSleep
//
//  Created by peter on 2022/4/25.
//

#import "ExceptionDefaultView.h"

@interface ExceptionDefaultView ()
@property (nonatomic, strong) UIImageView *exceptionIV;
@property (nonatomic, strong) UILabel *exceptLab;
@property (nonatomic, strong) UIButton *dealBtn;
@property (nonatomic, copy)   DealBlock dealBlock;
@property (nonatomic, assign) ExceptionType type;
@end

@implementation ExceptionDefaultView

- (instancetype)initWithType:(ExceptionType)type block:(DealBlock)block superView:(UIView *)superView {
    if (self = [super initWithFrame:CGRectZero]) {
        _dealBlock = block;
        _type = type;
        
        self.hidden = YES;
        
        [superView addSubview:self];
        [self addSubview:self.exceptLab];
        [self addSubview:self.dealBtn];
        
        if (type == ExceptionTypeSmall) {
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.bottom.right.equalTo(superView);
            }];
            [self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.left.equalTo(self).offset(5);
                make.right.equalTo(self).offset(-5);
                make.centerY.equalTo(self).offset(-30);
            }];
            [self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.size.mas_equalTo(CGSizeMake(155, 40));
                make.centerY.equalTo(self).offset(30);
            }];
        } else {
            [self addSubview:self.exceptionIV];
            
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(superView);
                make.center.equalTo(superView);
            }];
            [self.exceptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.top.equalTo(self);
            }];
            [self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.left.equalTo(self).offset(5);
                make.right.equalTo(self).offset(-5);
                make.top.equalTo(self.exceptionIV.mas_bottom).offset(42);
            }];
            [self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.size.mas_equalTo(CGSizeMake(155, 40));
                make.top.equalTo(self.exceptLab.mas_bottom).offset(42);
                make.bottom.equalTo(self).offset(-30);
            }];
        }
    }
    return self;
}

- (void)updateExceptionViewWithType:(ExceptionType)type {
    self.exceptionIV.image = [UIImage imageNamed:[self getExceptionImgWithType:type]];
    self.exceptLab.text = [self getExceptionInfoWithType:type];
    [self.dealBtn setTitle:[self getDealBtnTitleWithType:self.type] forState:UIControlStateNormal];
}

- (void)showServerErrInfo:(NSString *)errInfo {
    self.exceptLab.text = (errInfo && [errInfo isKindOfClass:[NSString class]]) ? errInfo : @"服务器异常";
}

#pragma mark - Actions
- (void)dealAction {
    if (self.dealBlock) { self.dealBlock(); }
}

#pragma mark - lazy
- (UIImageView *)exceptionIV {
    if (!_exceptionIV) {
        _exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self getExceptionImgWithType:self.type]]];
    }
    return _exceptionIV;
}

- (UILabel *)exceptLab {
    if (!_exceptLab) {
        _exceptLab = [UILabel labWithText:[self getExceptionInfoWithType:self.type] font:SysFont(14) fit:YES];
        _exceptLab.textAlignment = NSTextAlignmentCenter;
        _exceptLab.dk_textColorPicker = DKColorPickerWithKey(SubTEXT);
    }
    return _exceptLab;
}

- (UIButton *)dealBtn {
    if (!_dealBtn) {
        _dealBtn = [UIButton btnWithTitle:[self getDealBtnTitleWithType:self.type] titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
        [_dealBtn addTarget:self action:@selector(dealAction) forControlEvents:UIControlEventTouchUpInside];
        [_dealBtn cornerRadius:20];
    }
    return _dealBtn;
}

#pragma mark - private
- (NSString *)getExceptionImgWithType:(ExceptionType)type {
    NSString *imgName = @"netDefault";
    if (type == ExceptionTypeNet) {
        imgName = @"netDefault";
    } else if (type == ExceptionTypeSmall) {
        imgName = @"netDefault";
    } else if (type == ExceptionTypeNoData) {
        imgName = @"noData";
    }
    return imgName;
}

- (NSString *)getExceptionInfoWithType:(ExceptionType)type {
    NSString *info = @"";
    if (type == ExceptionTypeNet) {
        info = @"当前网络环境较差,点击刷新重新加载~";
    } else if (type == ExceptionTypeSmall) {
        info = @"当前网络环境较差,点击刷新重新加载~";
    } else if (type == ExceptionTypeNoData) {
        info = @"空空如也哦~";
    }
    return info;
}

- (NSString *)getDealBtnTitleWithType:(ExceptionType)type {
    NSString *title = @"";
    if (type == ExceptionTypeNet) {
        title = @"刷新";
    } else if (type == ExceptionTypeSmall) {
        title = @"刷新";
    } else if (type == ExceptionTypeNoData) {
        title = @"重新获取";
    }
    return title;
}

@end