CNLivePrivacyTableViewCell.m
5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// 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