CNLivePrivacyTableViewCell.m 5.29 KB
//
//  CNLivePrivacyTableViewCell.m
//  CNLiveSettingModule
//
//  Created by CNLive-zxw on 2019/4/23.
//  Copyright © 2019 cnlive. All rights reserved.
//

#import "CNLivePrivacyTableViewCell.h"
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import <Masonry/Masonry.h>

#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"
#import "CNUserInfoManager.h"
#import "CNLiveCategory.h"

#import "CNLivePrivacyModel.h"

@interface CNLivePrivacyTableViewCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UISwitch *notifiSwitch;
@property (nonatomic, strong) UIImageView *arrowImage;

@end

@implementation CNLivePrivacyTableViewCell
static const NSInteger margin = 12;

#pragma mark - Init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubView];
    }
    return self;
}

#pragma mark - UI
- (void)initSubView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.arrowImage];
    [self.contentView addSubview:self.notifiSwitch];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(margin);
    }];
    [self.arrowImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-margin);
    }];
    [self.notifiSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-margin);
        make.width.mas_equalTo(47);
        make.height.mas_equalTo(29);
    }];
}

- (void)setFrame:(CGRect)frame {
    frame.origin.y += _model.lineHeight;
    frame.size.height -= _model.lineHeight;
    [super setFrame:frame];
}

#pragma mark - Data
- (void)setModel:(CNLivePrivacyModel *)model {
    _model = model;
    _titleLabel.text = model.title;
    if (model.cellType == CNLivePrivacyCellTypeList) { //0 隐私的列表
        _arrowImage.hidden = NO;
        _notifiSwitch.hidden = YES;
        
    }
    if (model.cellType == CNLivePrivacyCellTypeSee) { //1 是否允许陌生人看生活圈
        _arrowImage.hidden = YES;
        _notifiSwitch.hidden = NO;
        
//        _notifiSwitch.enabled = [[KSYReachability reachabilityForInternetConnection] currentReachabilityStatus];//交互性
        [_notifiSwitch setOn:[model.status isEqualToString:@"access"]];

    }
    
}

#pragma mark - Action
- (void)switchAction:(UISwitch *)sender {
    if (_model.cellType == CNLivePrivacyCellTypeSee) { //1 是否允许陌生人看生活圈
        sender.userInteractionEnabled = NO;
        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"CNLiveConnectSuccNotification"] boolValue]) {
            [sender setOn:[_model.status isEqualToString:@"access"]];
            sender.userInteractionEnabled = YES;
            return;
        };
        __weak typeof(self) weakSelf = self;
        NSDictionary *params = @{@"appId":AppId,
                                 @"type":[_model.status isEqualToString:@"access"]?@"reject":@"access",
                                 @"sid":CNUserShareModel.uid};
        [CNLiveNetworking setAllowRequestDefaultArgument:YES];
        [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNSwitchAllowStrangerUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
            if ([responseObject isKindOfClass:[NSDictionary class]] && [responseObject[@"errorCode"] isEqualToString:@"0"]) {
                weakSelf.model.status = [weakSelf.model.status isEqualToString:@"access"]?@"reject":@"access";
                [[NSUserDefaults standardUserDefaults] setValue:weakSelf.model.status forKey:@"findStrangerSwitch"];
                
            }else{
                [sender setOn:[weakSelf.model.status isEqualToString:@"access"]];

            }
            sender.userInteractionEnabled = YES;
            
        }];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

#pragma mark - Lazy Loading
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorMake(40, 40, 40);
        _titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel;
}

- (UIImageView *)arrowImage {
    if (!_arrowImage) {
        _arrowImage = [[UIImageView alloc] init];
        UIImage *xw_image = [self getImageWithImageName:@"mine_info_front" bundleName:@"CNLiveSettingModule" targetClass:[CNLivePrivacyTableViewCell class]];
        _arrowImage.image = xw_image;
    }
    return _arrowImage;
}

- (UISwitch *)notifiSwitch {
    if (!_notifiSwitch) {
        _notifiSwitch = [[UISwitch alloc] init];
        [_notifiSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _notifiSwitch;
}

@end