CNLiveCompleteInfoView.m
13.2 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// CNLiveCompleteInfoView.m
// CNLiveLoginModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright (c) 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveCompleteInfoView.h"
#import "CNLiveLoginTextField.h"
#import "CNLiveLoginRequest.h"
#import <YYCategories/YYCategories.h>
#import <QMUIKit/QMUIKit.h>
#import <CNLiveUserSystemSDK/CNLiveUserSystemCenter.h>
#import <CNLiveUserSystemSDK/CNLiveUserSystemTypeDef.h>
#import <CNLiveUserManagement/CNUserInfoManager.h>
#import <CNLiveEnvironment/CNLiveEnvironment.h>
#import <CNLiveManager/CNLiveManager.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <CNLiveAVCamera/XFCameraController.h>
#import <CNLiveImagePickerController/CNImagePickerController.h>
#import <CNLiveIMBaseSDK/TIMAdapter.h>
#import <CNLiveIMBaseSDK/CommonLibrary.h>
#import <CNLiveCategory/CNLiveCategory.h>
@interface CNLiveCompleteInfoView()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,QMUIAlertControllerDelegate>{
UIImage *_image;
}
@property (nonatomic, weak) UIButton *head;
@property (nonatomic, weak) CNLiveLoginTextField *nickname;
@property (nonatomic, weak) UIButton *next;
@property (nonatomic, strong) IMALoginParam *loginParam;
@end
@implementation CNLiveCompleteInfoView
static NSInteger const rowHeight = 40;
static NSInteger const space = 40;
static const NSInteger timeValue = 1.5;
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
[self createSubviews:frame];
_loginParam = [[IMALoginParam alloc] init];
_image = nil;
}
return self;
}
- (void)createSubviews:(CGRect)frame{
UIButton *head = [UIButton buttonWithType:UIButtonTypeCustom];
head.frame = CGRectMake((frame.size.width-80)/2.0, 0, 80, 80);
head.layer.cornerRadius = 40;
head.layer.masksToBounds = YES;
head.adjustsImageWhenHighlighted = NO;
UIImage *image = [self getImageWithImageName:@"CNLiveBLoginModule.bundle/login_camera" bundleName:@"CNLiveBLoginModule" targetClass:[self class]];
[head setImage:image forState:UIControlStateNormal];
[head addTarget:self action:@selector(headDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:head];
self.head = head;
CNLiveLoginTextField *nickname = [[CNLiveLoginTextField alloc] initWithFrame:CGRectMake(0, head.bottom+space, frame.size.width, rowHeight)];
if(CNUserShareModel.mobile.length >= 11){
nickname.text = [CNUserShareModel.mobile stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}
nickname.placeholder = @"请输入昵称";
nickname.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
nickname.placeholderColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
nickname.font = [UIFont systemFontOfSize:16];
nickname.textAlignment = NSTextAlignmentLeft;
[nickname addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
nickname.clearButtonMode = UITextFieldViewModeWhileEditing;
nickname.delegate = self;
[self addSubview:nickname];
self.nickname = nickname;
UIButton *next = [UIButton buttonWithType:UIButtonTypeCustom];
next.frame = CGRectMake(nickname.left, nickname.bottom+space, nickname.width, rowHeight);
[next setTitle:@"下一步" forState:UIControlStateNormal];
[next setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
next.titleLabel.font = [UIFont systemFontOfSize:15.0];
UIColor *color = BusinessLive?[UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0]:[UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
next.backgroundColor = color;
next.layer.cornerRadius = 20;
next.layer.masksToBounds = YES;
next.adjustsImageWhenHighlighted = NO;
[next addTarget:self action:@selector(nextDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:next];
self.next = next;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self endEditing:YES];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([textField isEqual:_nickname]) {
return YES;
}
else {
return YES;
}
}
- (void)textFieldDidChange:(UITextField *)textField {
if (![_nickname.text isEqualToString:@""]) {
_next.enabled = YES;
}
else {
_next.enabled = NO;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isEqual:_nickname]) {
NSInteger maxTextLength = 20;
if(string.length == 0) {
//判断是不是删除键
return YES;
}
else if(textField.text.length >= maxTextLength) {
NSLog(@"输入的字符个数大于20,忽略输入");
return NO;
}
else if (textField.text.length + string.length > maxTextLength){
NSInteger length = textField.text.length;
NSInteger needLen = maxTextLength - length;
textField.text = [textField.text stringByAppendingString:[string substringToIndex:needLen]];
return NO;
}
else{
return YES;
}
}
else{
return YES;
}
}
#pragma mark - Action
- (void)headDidClicked:(UIButton *)btn{
btn.userInteractionEnabled = NO;
__weak typeof(self) weakSelf = self;
QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"拍照" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
btn.userInteractionEnabled = YES;
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
__strong typeof(strongSelf) weakSelf = strongSelf;
[CNLiveAuthorizationManager getAVAuthorizationStatus:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
XFCameraController *cameraController = [[XFCameraController alloc] init];
cameraController.isCanShootVideo = NO;
cameraController.modalPresentationStyle = UIModalPresentationFullScreen;
__weak XFCameraController *weakCameraController = cameraController;
__weak typeof(strongSelf) weakSelf = strongSelf;
cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
strongSelf->_image = image;
[strongSelf.head setImage:strongSelf->_image forState:UIControlStateNormal];
dispatch_async(dispatch_get_main_queue(), ^{
[weakCameraController dismissViewControllerAnimated:YES completion:nil];
});
};
[(UIViewController *)self.delegate presentViewController:cameraController animated:YES completion:nil];
}];
}];
QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"从手机相册中选择" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
btn.userInteractionEnabled = YES;
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
__strong typeof(strongSelf) weakSelf = strongSelf;
[CNLiveAuthorizationManager getPHAuthorizationStatus:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
CNImagePickerController *picker = [[CNImagePickerController alloc] initWithMaxImagesCount:1 delegate:strongSelf];
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.allowPickingVideo = NO;
picker.allowPreview = NO;
__weak typeof(strongSelf) weakSelf = strongSelf;
[picker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
strongSelf->_image = [photos firstObject];
[strongSelf.head setImage:strongSelf->_image forState:UIControlStateNormal];
}];
[(UIViewController *)strongSelf.delegate presentViewController:picker animated:YES completion:nil];
}];
}];
QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
alertController.delegate = self;
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:cancel];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
UIColor *color = BusinessLive?[UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0]:[UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
titleAttributs[NSForegroundColorAttributeName] = color;
NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
action1.buttonAttributes = titleAttributs;
action2.buttonAttributes = titleAttributs;
cancel.buttonAttributes = cancelAttributs;
[alertController showWithAnimated:YES];
}
- (void)didHideAlertController:(QMUIAlertController *)alertController{
self.head.userInteractionEnabled = YES;
}
#pragma mark - 响应方法
- (void)nextDidClicked:(UIButton *)btn{
[_nickname resignFirstResponder];
if ([_nickname.text isEqualToString:@""]) {
[QMUITips showError:@"请输入昵称" inView:self.superview hideAfterDelay:timeValue];
return;
}
self.next.userInteractionEnabled = NO;
[self.superview endEditing:YES];
[self modifyNickName];
}
// 修改名字
- (void)modifyNickName {
[QMUITips showLoading:@"修改中" inView:self.superview];
__weak typeof(self) weakSelf = self;
[CNLiveUserSystemCenter modifyUserInfoType:UserInfoTypeNickName uid:CNUserShareModel.uid modifyInfo:_nickname.text success:^(id result) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return;
strongSelf.next.userInteractionEnabled = YES;
CNUserShareModel.nickname = strongSelf.nickname.text;
[QMUITips hideAllToastInView:strongSelf.superview animated:YES];
if (!strongSelf->_image) {
[strongSelf callback:result];
}else{
[strongSelf modifyIcon];
}
} failure:^(NSDictionary *errorDic) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return;
strongSelf.next.userInteractionEnabled = YES;
[QMUITips hideAllToastInView:strongSelf.superview animated:YES];
[QMUITips showError:[errorDic objectForKey:@"errorMessage"] inView:strongSelf.superview hideAfterDelay:timeValue];
}];
}
// 修改头像
- (void)modifyIcon {
[QMUITips showLoading:@"修改中" inView:self.superview];
__weak typeof(self) weakSelf = self;
[CNLiveUserSystemCenter uploadIconWithUid:CNUserShareModel.uid icon:_image success:^(id result) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return;
strongSelf.next.userInteractionEnabled = YES;
[QMUITips hideAllToastInView:strongSelf.superview animated:YES];
NSLog(@"result:%@",result);
NSString *deFaceUrl = result[@"data"][@"faceUrl"];
NSString *faceUrl = @"";
if ([deFaceUrl containsString:@"/mobile/images/mobilehead/default/"]) {
faceUrl = CNLiveNetAddDefaultHead;
}else{
faceUrl = deFaceUrl;
}
CNUserShareModel.faceUrl = faceUrl;
[strongSelf callback:result];
} failure:^(NSDictionary *errorDic) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return;
strongSelf.next.userInteractionEnabled = YES;
[QMUITips hideAllToastInView:strongSelf.superview animated:YES];
[QMUITips showError:errorDic[@"errorMessage"] inView:strongSelf.superview hideAfterDelay:timeValue];
}];
}
// 回调
- (void)callback:(id)result{
// 进入tabbar
if (self.delegate && [self.delegate respondsToSelector:@selector(loginSuccess:result:)]) {
[self.delegate loginSuccess:self result:result];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end