-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyViewController.m
382 lines (286 loc) · 13.7 KB
/
MyViewController.m
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// MyViewController.m
// 访开源中国
//
// Created by 李腾芳 on 16/10/24.
// Copyright © 2016年 李腾芳. All rights reserved.
//
#import "MyViewController.h"
#import "CombinationHeadView.h"
#import "TableViewDataModel.h"
#import "LoginViewController.h"
#import "ParsingXML.h"
#import "Utils.h"
#import "SingleImageViewController.h"
#import <MJRefresh.h>
#import "SettingViewController.h"
@interface MyViewController ()<MyHeaderViewDelegate,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
CombinationHeadView *_headView;
NSArray<TableViewDataModel *> *_dataModels;
}
@property(strong,nonatomic) UITableView *tableView;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initDataModel];
[self addSubViews];
if ([UserInfo myUserInfo].user) {
[self refresh];
}
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
_tableView.mj_header.backgroundColor = RGB(50, 205, 100);
((MJRefreshNormalHeader *)_tableView.mj_header).stateLabel.textColor = [UIColor whiteColor];
((MJRefreshNormalHeader *)_tableView.mj_header).lastUpdatedTimeLabel.hidden = YES;
// [_tableView.header beginRefreshing];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUSerInfo) name:UPDATE_USER_INFO object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
_headView.user = [UserInfo myUserInfo].user;
CGRect frame = _headView.frame;
frame.size.height = _headView.suggestHeight;
_headView.frame = frame;
_tableView.tableHeaderView = _headView;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Observer's
- (void)updateUSerInfo {
[self refresh];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.imageView.image = [UIImage imageNamed:_dataModels[indexPath.row].imageName];
cell.textLabel.text = _dataModels[indexPath.row].title;
return cell;
}
#pragma mark - MyHeaderViewDelegate
- (void)login {
if ([UserInfo myUserInfo].user) {
[self showPortraitAlertView];
} else {
[self showLoginCtrl];
}
}
- (void)config {
SettingViewController *p = [[SettingViewController alloc]init];
[self.navigationController pushViewController:p animated:YES];
}
- (void)showQRCode {
if ([UserInfo myUserInfo].user == nil) {
[self showLoginCtrl];
} else {
[self showQRHud];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSLog(@"%@",info);
UIImage *image = info[@"UIImagePickerControllerEditedImage"];
[picker dismissViewControllerAnimated:YES completion:nil];
NSData *data = UIImageJPEGRepresentation(image, 0.7f);
[self updatePortraitWithData:data];
}
#pragma mark init method
- (void)addSubViews {
[self initHeadView];
[self initTableView];
}
- (void)initTableView {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(-20);
make.left.offset(0);
make.right.offset(0);
make.bottom.offset(0);
}];
_tableView.tableFooterView = [[UIView alloc]init];
_tableView.tableHeaderView = _headView;
}
- (void)initHeadView {
_headView = [[CombinationHeadView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 280)];
_headView.user = [UserInfo myUserInfo].user;
_headView.backgroundColor = [UIColor redColor];
_headView.delegate = self;
}
- (void)initDataModel {
TableViewDataModel *model0 = [[TableViewDataModel alloc]initWithTitle:@"我的消息" imageName:@"ic_my_messege" selectBlock:^{
}];
TableViewDataModel *model1 = [[TableViewDataModel alloc]initWithTitle:@"我的博客" imageName:@"ic_my_blog" selectBlock:^{
}];
TableViewDataModel *model2 = [[TableViewDataModel alloc]initWithTitle:@"我的活动" imageName:@"ic_my_event" selectBlock:^{
}];
TableViewDataModel *model3 = [[TableViewDataModel alloc]initWithTitle:@"我的团队" imageName:@"ic_my_team" selectBlock:^{
}];
_dataModels = @[model0,model1,model2,model3];
}
#pragma mark - private method
- (void)showQRHud {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
hud.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
[self.view addSubview:hud];
[hud showAnimated:YES];
hud.mode = MBProgressHUDModeCustomView;
hud.customView.backgroundColor = [UIColor whiteColor];
hud.label.text = @"扫一扫上面的二维码,加我为好友";
hud.label.font = [UIFont systemFontOfSize:13];
hud.label.textColor = [UIColor grayColor];
hud.customView = self.myQRCodeImageView;
[hud addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideHUD:)]];
}
- (void)showPortraitAlertView {
UIAlertController *p = [UIAlertController alertControllerWithTitle:@"选择操作" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 更换头像 查看大头像 取消
[p addAction:[UIAlertAction actionWithTitle:@"更换头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self showPhotoSlectAlertView];
}]];
[p addAction:[UIAlertAction actionWithTitle:@"查看大头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UserInfo myUserInfo].user.portrait.length == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"尚未设置头像"
message:nil
delegate:self
cancelButtonTitle:@"知道了"
otherButtonTitles: nil];
[alertView show];
} else {
NSArray *array1 = [[UserInfo myUserInfo].user.portrait componentsSeparatedByString:@"_"];
NSArray *array2 = [array1[1] componentsSeparatedByString:@"."];
NSString *bigPortraitURL = [NSString stringWithFormat:@"%@_200.%@", array1[0], array2[1]];
SingleImageViewController *imgViewweVC = [[SingleImageViewController alloc] initWithURL:[NSURL URLWithString:bigPortraitURL]];
[self presentViewController:imgViewweVC animated:YES completion:nil];
}
}]];
[p addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:p animated:NO completion:nil];
}
- (void)showPhotoSlectAlertView {
UIAlertController *p = [UIAlertController alertControllerWithTitle:@"选择操作" message:nil preferredStyle:UIAlertControllerStyleAlert];
[p addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self showCameraCtrl];
}]];
[p addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self showPhotoSelectCtrl];
}]];
[p addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:p animated:NO completion:nil];
}
- (void)showCameraCtrl {
UIImagePickerController *p = [[UIImagePickerController alloc]init];
p.sourceType = UIImagePickerControllerSourceTypeCamera;
p.allowsEditing = YES;
p.delegate = self;
[self presentViewController:p animated:YES completion:nil];
}
- (void)showPhotoSelectCtrl {
UIImagePickerController *p = [[UIImagePickerController alloc]init];
p.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
p.allowsEditing = YES;
p.delegate = self;
[self presentViewController:p animated:YES completion:nil];
}
- (void)showLoginCtrl {
LoginViewController *p = [[LoginViewController alloc]init];
[self.navigationController pushViewController:p animated:YES];
}
- (UIImageView *)myQRCodeImageView {
UIImage *myQRCode = [Utils createQRCodeFromString:[NSString stringWithFormat:@"http://my.oschina.net/u/%ld", (long)[UserInfo myUserInfo].user.userID]];
return [[UIImageView alloc] initWithImage:myQRCode];
}
- (void)hideHUD:(UIGestureRecognizer *)recognizer {
[(MBProgressHUD *)recognizer.view hideAnimated:YES];
}
- (NSMutableURLRequest *)getUpdatePortraitRequestWithData:(NSData *)data{
NSString *URLString = [NSString stringWithFormat:@"%@user_edit_portrait", OSCAPI_V2_PREFIX];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLString parameters:@{@"uid":@([UserInfo myUserInfo].user.userID)} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data
name:@"portrait"
fileName:@"img.jpg"
mimeType:@"image/jpeg"];
} error:nil];
return request;
}
- (NSMutableURLRequest *)getUserInfoRequest {
NSString *URLString = [NSString stringWithFormat:@"%@user_info", OSCAPI_V2_PREFIX];
return [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:nil error:nil];
}
- (void)refresh {
__weak CombinationHeadView *weakHeadView = _headView;
__weak MyViewController *weakSelf = self;
NSMutableURLRequest *request = [self getUserInfoRequest];
AFURLSessionManager *manager = defaultAPPSessionManager();
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
[Utils showHUDWithText:error.domain];
} else {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if ([dic[@"code"] isEqual:@(1)]) {
OSCUser *info = [[OSCUser alloc]initWithUserInfoDic:dic];
[UserInfo myUserInfo].user = info;
weakHeadView.user = info;
NSLog(@"%@",info);
} else {
// [Utils showHUDWithText:dic[@"message"]];
UIAlertController *p = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"刷新用户数据失败,请重新登陆" preferredStyle:UIAlertControllerStyleAlert];
[p addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf showLoginCtrl];
}]];
[weakSelf presentViewController:p animated:YES completion:nil];
}
}
[weakSelf.tableView.mj_header endRefreshing];
}];
[dataTask resume];
}
- (void)updatePortraitWithData:(NSData *)data {
AFURLSessionManager *manager = defaultAPPSessionManager();
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:[self getUpdatePortraitRequestWithData:data]
progress:^(NSProgress * _Nonnull uploadProgress) {
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if ([dic[@"code"] isEqual:@(1)]) { // sucess
OSCUser *user = [[OSCUser alloc]initWithUserInfoDic:dic];
[UserInfo myUserInfo].user = user;
_headView.user = user;
} else {
NSLog(@"%@",dic[@"message"]);
}
}
}];
[uploadTask resume];
}
@end