forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweetDetailViewController.m
More file actions
444 lines (396 loc) · 16.2 KB
/
TweetDetailViewController.m
File metadata and controls
444 lines (396 loc) · 16.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// TweetDetailViewController.m
// Coding_iOS
//
// Created by 王 原闯 on 14-9-24.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "TweetDetailViewController.h"
#import "Coding_NetAPIManager.h"
#import "TweetDetailCell.h"
#import "TweetDetailCommentCell.h"
#import "UserInfoViewController.h"
#import "LikersViewController.h"
#import "RegexKitLite.h"
#import "TopicDetailViewController.h"
#import "ProjectViewController.h"
#import "MJPhotoBrowser.h"
#import "EditTaskViewController.h"
#import "WebViewController.h"
#import "ReportIllegalViewController.h"
#import "TweetSendLocationDetailViewController.h"
@interface TweetDetailViewController ()
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) ODRefreshControl *refreshControl;
//评论
@property (nonatomic, strong) UIMessageInputView *myMsgInputView;
@property (nonatomic, strong) Comment *toComment;
@property (nonatomic, strong) UIView *commentSender;
//TTTAttributedLabel
@property (strong, nonatomic) HtmlMediaItem *clickedItem;
@property (strong, nonatomic) NSString *clickedAutoLinkStr;
@end
@implementation TweetDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"冒泡详情";
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"moreBtn_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(rightNavBtnClicked)] animated:NO];
// 添加myTableView
_myTableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[TweetDetailCell class] forCellReuseIdentifier:kCellIdentifier_TweetDetail];
[tableView registerClass:[TweetDetailCommentCell class] forCellReuseIdentifier:kCellIdentifier_TweetDetailComment];
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView;
});
_refreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_refreshControl addTarget:self action:@selector(refreshTweet) forControlEvents:UIControlEventValueChanged];
//评论
_myMsgInputView = [UIMessageInputView messageInputViewWithType:UIMessageInputViewContentTypeTweet];
_myMsgInputView.isAlwaysShow = YES;
_myMsgInputView.delegate = self;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,CGRectGetHeight(_myMsgInputView.frame), 0.0);
self.myTableView.contentInset = contentInsets;
self.myTableView.scrollIndicatorInsets = contentInsets;
if (!_curTweet.content
|| (_curTweet.likes.integerValue > 0 && _curTweet.like_users.count == 0)) {
[self refreshTweet];
}else{
_myMsgInputView.commentOfId = _curTweet.id;
if (_curTweet.comments.integerValue > _curTweet.comment_list.count) {
[self refreshComments];//加载等多评论
}
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (_myMsgInputView) {
[_myMsgInputView prepareToDismiss];
}
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// 键盘
if (_myMsgInputView) {
if (!_myMsgInputView.toUser) {
_myMsgInputView.toUser = nil;
}
[_myMsgInputView prepareToShow];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)rightNavBtnClicked{
if (self.curTweet.id && [self.curTweet.id isKindOfClass:[NSNumber class]]) {
[_myMsgInputView isAndResignFirstResponder];
@weakify(self);
[[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"举报"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
if (index == 0) {
@strongify(self);
[self goToReport];
}
}] showInView:self.view];
}
}
- (void)goToReport{
if (self.curTweet.id && [self.curTweet.id isKindOfClass:[NSNumber class]]) {
NSNumber *tweet_id = self.curTweet.id;
[ReportIllegalViewController showReportWithIllegalContent:tweet_id.stringValue andType:IllegalContentTypeTweet];
}else{
[self showHudTipStr:@"冒泡为空"];
}
}
#pragma mark UIMessageInputViewDelegate
- (void)messageInputView:(UIMessageInputView *)inputView sendText:(NSString *)text{
[self sendCommentMessage:text];
}
- (void)messageInputView:(UIMessageInputView *)inputView heightToBottomChenged:(CGFloat)heightToBottom{
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
UIEdgeInsets contentInsets= UIEdgeInsetsMake(0.0, 0.0, MAX(CGRectGetHeight(inputView.frame), heightToBottom), 0.0);;
CGFloat msgInputY = kScreen_Height - heightToBottom - 64;
self.myTableView.contentInset = contentInsets;
self.myTableView.scrollIndicatorInsets = contentInsets;
if ([_commentSender isKindOfClass:[UIView class]] && !self.myTableView.isDragging && heightToBottom > 60) {
UIView *senderView = _commentSender;
CGFloat senderViewBottom = [_myTableView convertPoint:CGPointZero fromView:senderView].y+ CGRectGetMaxY(senderView.bounds);
CGFloat contentOffsetY = MAX(0, senderViewBottom- msgInputY);
[self.myTableView setContentOffset:CGPointMake(0, contentOffsetY) animated:YES];
}
} completion:nil];
}
#pragma mark refresh
- (void)refreshTweet{
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_Tweet_Detail_WithObj:_curTweet andBlock:^(id data, NSError *error) {
if (data) {
if (weakSelf.curTweet.contentHeight > 1) {
((Tweet *)data).contentHeight = weakSelf.curTweet.contentHeight;
}
weakSelf.curTweet = data;
weakSelf.myMsgInputView.commentOfId = weakSelf.curTweet.id;
weakSelf.myMsgInputView.toUser = nil;
[weakSelf.myTableView reloadData];
[weakSelf refreshComments];
}else{
[weakSelf.refreshControl endRefreshing];
}
}];
}
- (void)refreshComments{
if (_curTweet.isLoading) {
return;
}
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_Tweet_Comments_WithObj:_curTweet andBlock:^(id data, NSError *error) {
[weakSelf.refreshControl endRefreshing];
if (data) {
weakSelf.curTweet.comment_list = data;
weakSelf.curTweet.comments = [NSNumber numberWithInteger:weakSelf.curTweet.comment_list.count];
[weakSelf.myTableView reloadData];
}
}];
}
#pragma mark TableM
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger row = 0;
if (_curTweet && _curTweet.comment_list) {
row = 1+ [_curTweet.comment_list count];
}else{
row = 1;
}
return row;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
TweetDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TweetDetail forIndexPath:indexPath];
cell.tweet = _curTweet;
cell.commentClickedBlock = ^(id sender){
[self doCommentToComment:nil sender:sender];
};
cell.likeBtnClickedBlock = ^(){
[self.myTableView reloadData];
};
cell.deleteClickedBlock = ^(){
if ([self.myMsgInputView isAndResignFirstResponder]) {
return ;
}
ESWeakSelf;
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"删除此冒泡" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
ESStrongSelf
if (index == 0) {
[_self deleteTweet:_self.curTweet];
}
}];
[actionSheet showInView:self.view];
};
cell.userBtnClickedBlock = ^(User *curUser){
[self goToUserInfo:curUser];
};
cell.moreLikersBtnClickedBlock = ^(){
LikersViewController *vc = [[LikersViewController alloc] init];
vc.curTweet = _curTweet;
[self.navigationController pushViewController:vc animated:YES];
};
cell.cellHeightChangedBlock = ^(){
[self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
};
cell.loadRequestBlock = ^(NSURLRequest *curRequest){
[self loadRequest:curRequest];
};
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:0];
return cell;
}else{
TweetDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TweetDetailComment forIndexPath:indexPath];
Comment *curComment = [_curTweet.comment_list objectAtIndex:indexPath.row-1];
cell.toComment = curComment;
cell.commentToCommentBlock = ^(Comment *toComment, id sender){
[self doCommentToComment:toComment sender:sender];
};
[cell.ownerIconView addTapBlock:^(id obj) {
[self goToUserInfo:curComment.owner];
}];
cell.contentLabel.delegate = self;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:50];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat row = 0;
if (indexPath.row == 0) {
row = [TweetDetailCell cellHeightWithObj:_curTweet];
}else{
row = [TweetDetailCommentCell cellHeightWithObj:[_curTweet.comment_list objectAtIndex:indexPath.row-1]];
}
return row;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row != 0) {
Comment *toComment = [_curTweet.comment_list objectAtIndex:indexPath.row-1];
[self doCommentToComment:toComment sender:[tableView cellForRowAtIndexPath:indexPath]];
}
}
#pragma mark Table Copy
//
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
// return indexPath.row != 0;
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
return YES;
}
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
if (indexPath.row == 0) {
[UIPasteboard generalPasteboard].string = _curTweet.content? _curTweet.content: @"";
}else{
Comment *curComment = [_curTweet.comment_list objectAtIndex:indexPath.row-1];
[UIPasteboard generalPasteboard].string = curComment.content? curComment.content: @"";
}
}
}
#pragma mark Comment To Tweet
- (void)doCommentToComment:(Comment *)toComment sender:(id)sender{
if ([self.myMsgInputView isAndResignFirstResponder]) {
return ;
}
_toComment = toComment;
_commentSender = sender;
_myMsgInputView.toUser = toComment.owner;
if (_toComment) {
if ([Login isLoginUserGlobalKey:_toComment.owner.global_key]) {
ESWeakSelf;
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"删除此评论" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
ESStrongSelf
if (index == 0) {
[_self deleteComment:_self.toComment ofTweet:_self.curTweet];
}
}];
[actionSheet showInView:self.view];
return;
}
}
[_myMsgInputView notAndBecomeFirstResponder];
}
- (void)sendCommentMessage:(id)obj{
if (_toComment) {
_curTweet.nextCommentStr = [NSString stringWithFormat:@"@%@ %@", _toComment.owner.name, obj];
}else{
_curTweet.nextCommentStr = obj;
}
[self sendCurComment:_curTweet];
{
_toComment = nil;
_commentSender = nil;
}
self.myMsgInputView.toUser = nil;
[self.myMsgInputView isAndResignFirstResponder];
}
- (void)sendCurComment:(Tweet *)commentObj{
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_Tweet_DoComment_WithObj:commentObj andBlock:^(id data, NSError *error) {
if (data) {
Comment *resultCommnet = (Comment *)data;
resultCommnet.owner = [Login curLoginUser];
[commentObj addNewComment:resultCommnet];
[weakSelf.myTableView reloadData];
}
}];
}
#pragma mark deleteTweet
- (void)deleteTweet:(Tweet *)curTweet{
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_Tweet_Delete_WithObj:curTweet andBlock:^(id data, NSError *error) {
if (data) {
if (weakSelf.deleteTweetBlock) {
weakSelf.deleteTweetBlock(curTweet);
}
[self.navigationController popViewControllerAnimated:YES];
}
}];
}
- (void)deleteComment:(Comment *)comment ofTweet:(Tweet *)tweet{
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_TweetComment_Delete_WithTweet:tweet andComment:comment andBlock:^(id data, NSError *error) {
if (data) {
[tweet deleteComment:comment];
[weakSelf.myTableView reloadData];
}
}];
}
#pragma mark ScrollView Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (scrollView == _myTableView) {
[self.myMsgInputView isAndResignFirstResponder];
}
}
#pragma mark TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components{
DebugLog(@"%@", components.description);
_clickedItem = [components objectForKey:@"value"];
[self analyseLinkStr:_clickedItem.href];
}
#pragma mark to VC
- (void)goToUserInfo:(User *)curUser{
UserInfoViewController *vc = [[UserInfoViewController alloc] init];
vc.curUser = curUser;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark loadCellRequest
- (void)loadRequest:(NSURLRequest *)curRequest{
NSString *linkStr = curRequest.URL.absoluteString;
[self analyseLinkStr:linkStr];
}
- (void)analyseLinkStr:(NSString *)linkStr{
if (linkStr.length <= 0) {
return;
}
UIViewController *vc = [BaseViewController analyseVCFromLinkStr:linkStr];
if (vc) {
[self.navigationController pushViewController:vc animated:YES];
}else{
//可能是图片链接
HtmlMedia *htmlMedia = self.curTweet.htmlMedia;
if (htmlMedia.imageItems.count > 0) {
for (HtmlMediaItem *item in htmlMedia.imageItems) {
if ((item.src.length > 0 && [item.src isEqualToString:linkStr])
|| (item.href.length > 0 && [item.href isEqualToString:linkStr])) {
[MJPhotoBrowser showHtmlMediaItems:htmlMedia.imageItems originalItem:item];
return;
}
}
}
//跳转去网页
WebViewController *webVc = [WebViewController webVCWithUrlStr:linkStr];
[self.navigationController pushViewController:webVc animated:YES];
}
}
- (void)dealloc
{
_myTableView.delegate = nil;
_myTableView.dataSource = nil;
}
@end