ShareAlertView.m 5.3 KB
//
//  ShareAlertView.m
//  DreamSleep
//
//  Created by peter on 2022/4/24.
//

#import "ShareAlertView.h"

@interface ShareAlertView ()
@property (nonatomic, strong) UIView *shareView;
@end

@implementation ShareAlertView

- (instancetype)initWithDelegate:(id<ShareAlertViewDelegate>)delegate shareSource:(ShareSource)shareSource {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _delegate = delegate;
        _shareSource = shareSource;
        
        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.shareView];
        [self.shareView 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(@125);
        }];
        
        UILabel *titleLab = [UILabel dkLabWithText:@"分享到" font:BoldFont(15)];
        [self.shareView addSubview:titleLab];
        
        UIButton *wxFriendBtn = [self btnWithTitle:@"微信好友" index:1 imgName:@"wx_friend" superView:self.shareView];
        UIButton *wxTimelineBtn = [self btnWithTitle:@"朋友圈" index:2 imgName:@"wx_timeline" superView:self.shareView];
        UIButton *qqShareBtn = [self btnWithTitle:@"QQ好友" index:3 imgName:@"qq_share" superView:self.shareView];
        UIButton *dismissBtn = [UIButton dkBtnTitle:@"取消" font:BoldFont(15)];
        dismissBtn.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        [dismissBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [dismissBtn cornerRadius:12.0];
        [self addSubview:dismissBtn];
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shareView);
            make.top.equalTo(self.shareView).offset(15);
            make.height.equalTo(@21);
        }];
        [wxFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom);
            make.bottom.equalTo(self.shareView);
            make.left.equalTo(self.shareView);
        }];
        [wxTimelineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(wxFriendBtn);
            make.left.equalTo(wxFriendBtn.mas_right);
            make.size.equalTo(wxFriendBtn);
        }];
        [qqShareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(wxFriendBtn);
            make.left.equalTo(wxTimelineBtn.mas_right);
            make.size.equalTo(wxFriendBtn);
            make.right.equalTo(self.shareView);
        }];
        [dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shareView);
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.top.equalTo(self.shareView.mas_bottom).offset(20);
            make.height.equalTo(@40);
        }];
    }
    return self;
}

#pragma mark - Actions
- (void)clickAction:(UIButton *)sender {
    [self dismissShareAlertView];
    
    NSString *step = @"点击微信好友分享按钮";
    if (sender.tag == 2) {
        step = @"点击朋友圈分享按钮";
    } else if (sender.tag == 3) {
        step = @"点击QQ好友分享按钮";
    }
    [DataStatisticsUtil event:InviteFriends attributes:@{@"step":step}];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickShareBtnWithIndex:shareSource:)]) {
        [self.delegate didClickShareBtnWithIndex:(int)sender.tag shareSource:self.shareSource];
    }
}

- (void)cancelAction {
    [self dismissShareAlertView];
    
    [DataStatisticsUtil event:InviteFriends attributes:@{@"step":@"取消分享"}];
}

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

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

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

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

#pragma mark - others
- (UIButton *)btnWithTitle:(NSString *)title index:(int)index imgName:(NSString *)imgName superView:(UIView *)sv {
    UIButton *btn = [UIButton btnWithTitle:title font:SysFont(12)];
    [btn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, DSWhite, DSWhite) forState:UIControlStateNormal];
    btn.tag = index;
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn adjustLayoutWithType:UIButtonLayoutTypeUpImageBottomTitle midSpace:2 sizeToFit:YES];
    [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [sv addSubview:btn];
    return btn;
}

@end