InfoModifyAlertView.m 5.1 KB
//
//  InfoModifyAlertView.m
//  DreamSleep
//
//  Created by peter on 2022/4/25.
//

#import "InfoModifyAlertView.h"
#import <AVFoundation/AVFoundation.h>

@interface InfoModifyAlertView ()
@property (nonatomic, strong) UIView *infoView;
@end

@implementation InfoModifyAlertView

- (instancetype)initWithDelegate:(id<InfoModifyAlertViewDelegate>)delegate type:(AlertType)type {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _delegate = delegate;
        _type = type;
        
        self.backgroundColor = AlertDarkColor;
        self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.6];
        
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tapGR];
        
        [self addSubview:self.infoView];
        UIButton *btn1 = [self btnWithType:type index:1 superView:self.infoView];
        UIButton *btn2 = [self btnWithType:type index:2 superView:self.infoView];
        UIButton *dismissBtn = [self btnWithType:type index:3 superView:self];
        [dismissBtn cornerRadius:12.0];
        
        [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.bottom.equalTo(self).offset(- 118 - Bottom_SafeArea_Height);
            make.height.equalTo(@106);
        }];
        
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.infoView);
            make.centerX.equalTo(self.infoView);
            make.left.right.equalTo(self.infoView);
        }];
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.infoView);
            make.left.right.equalTo(self.infoView);
            make.size.equalTo(btn1);
            make.top.equalTo(btn1.mas_bottom);
            make.bottom.equalTo(self.infoView);
        }];
        [dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.infoView);
            make.left.right.equalTo(btn1);
            make.size.equalTo(btn1);
            make.top.equalTo(self.infoView.mas_bottom).offset(20);
        }];
    }
    return self;
}

#pragma mark - Actions
- (void)clickAction:(UIButton *)sender {
    [self dismissPhotoAlertView];
    
    switch (self.type) {
        case AlertTypePhoto:
            if (sender.tag == 1) { // 拍照
                // 相机鉴权
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
                    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
                        [DSProgressHUD showDetailInfo:@"请前往iPhone的“设置-隐私-相机”,允许APP访问您的相机。" ];
                        return;
                    }
                } else {
                    [DSProgressHUD showDetailInfo:@"您的设备无摄像头!" ];
                    return;
                }
                [self didClickInfoModifyAlertView:AlertTypePhoto index:1];
            } else if (sender.tag == 2) { // 从相册中选取
                [self didClickInfoModifyAlertView:AlertTypePhoto index:2];
            }
            break;
        case AlertTypeGender:
        {
            if (sender.tag != 3) {
                [self didClickInfoModifyAlertView:AlertTypeGender index:(int)sender.tag];
            }
        }
        default:
            break;
    }
}

- (void)didClickInfoModifyAlertView:(AlertType)type index:(int)index {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectWithType:index:)]) {
        [self.delegate didSelectWithType:type index:index];
    }
}

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

#pragma mark - public
- (void)showInfoModifyAlertView {
    [DSKeyWindow addSubview:self];
}

- (void)dismissPhotoAlertView {
    [self removeFromSuperview];
}

#pragma mark - lazy
- (UIView *)infoView {
    if (!_infoView) {
        _infoView = [UIView new];
        [_infoView cornerRadius:12.0];
        _infoView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
    }
    return _infoView;
}

#pragma mark - others
- (UIButton *)btnWithType:(AlertType)type index:(int)index superView:(UIView *)sv {
    NSString *title = @"取消";
    if (index == 1) {
        title = type == AlertTypePhoto ? @"拍照" : @"男";
    } else if (index == 2) {
        title = type == AlertTypePhoto ? @"从相册中选择" : @"女";
    }
    UIButton *btn = [UIButton dkBtnTitle:title font:BoldFont(15)];
    btn.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
    btn.tag = index;
    [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [sv addSubview:btn];
    return btn;
}

@end