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
|
//
// MasterViewController.m
// Juick
//
// Created by Vitaly Takmazov on 26.10.13.
// Copyright (c) 2013 com.juick. All rights reserved.
//
#import "SWRevealViewController.h"
#import "MessagesViewController.h"
#import "MessageCell.h"
#import "Message.h"
#import "ColorScheme.h"
#import "NewPostViewController.h"
#import "NSURL+PathParameters.h"
static NSString *CellIdentifier = @"MessageCell";
@interface MessagesViewController ();
@property(nonatomic, strong) NSMutableArray *messages;
@property(nonatomic, assign) Boolean dataLoading;
@property(nonatomic, strong) NSString *path;
@property(nonatomic, strong) NSMutableDictionary *params;
@property(nonatomic, strong) MessageCell *sizingCell;
@end
@implementation MessagesViewController
- (void)loadFromPath:(NSString *)messagesPath withParams:(NSDictionary *)params withTitle:(NSString *)title
{
[self setPath:messagesPath];
[self setParams:params];
self.title = title;
[self refreshData:nil];
}
- (void) refreshData:(UIRefreshControl *)refresh {
[self.messages removeAllObjects];
[self.tableView reloadData];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.dataLoading = YES;
[Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.messages addObjectsFromArray:next];
[self.tableView reloadData];
[refresh endRefreshing];
self.dataLoading = NO;
});
}];
});
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.path isEqualToString:[Message threadUrl]]) {
return;
}
CGPoint pos = scrollView.contentOffset;
CGFloat contentHeight = scrollView.contentSize.height - scrollView.contentSize.height / 3;
if (pos.y >= contentHeight && !self.dataLoading) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.dataLoading = YES;
if (self.params == nil) {
self.params = [[NSMutableDictionary alloc] init];
}
Message * lastMessage = [self.messages lastObject];
if (lastMessage != nil) {
[self.params setObject:lastMessage.MID forKey:@"before_mid"];
}
[Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.messages addObjectsFromArray:next];
[self.tableView reloadData];
self.dataLoading = NO;
});
}];
});
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataLoading = NO;
[self.view setBackgroundColor:[ColorScheme mainBackground]];
self.messages = [NSMutableArray array];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
[refresh addTarget:self action:@selector(refreshData:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
SWRevealViewController *revealController = [self revealViewController];
if (revealController) {
// TODO: add to thread view too
[self.view addGestureRecognizer:[revealController panGestureRecognizer]];
}
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1) {
self.navigationController.navigationBar.tintColor = [ColorScheme navbarBackground];
}
[self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier];
self.sizingCell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
self.sizingCell.hidden = YES;
[self.tableView addSubview:self.sizingCell];
self.sizingCell.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 0);
}
- (void) composePressed {
CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:[[NewPostViewController alloc] init] animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell updateFonts];
Message *msg = [_messages objectAtIndex:indexPath.row];
[cell bind:msg];
[cell.contentView layoutIfNeeded];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.sizingCell updateFonts];
Message *msg = [_messages objectAtIndex:indexPath.row];
[self.sizingCell bind:msg];
[self.sizingCell setNeedsLayout];
[self.sizingCell layoutIfNeeded];
CGFloat calculatedHeight = [self.sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return calculatedHeight;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* if (self.dataLoading) {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}*/
return 500.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.path isEqualToString:[Message threadUrl]])
return;
Message *msg = [_messages objectAtIndex:indexPath.row];
MessagesViewController *threadViewController = [[MessagesViewController alloc] init];
[threadViewController loadFromPath:[Message threadUrl] withParams:[NSDictionary dictionaryWithObjectsAndKeys:msg.MID, @"mid", nil] withTitle:@"Thread"];
[self.navigationController pushViewController:threadViewController animated:NO];
}
-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
self.navigationItem.leftBarButtonItem = nil;
}
-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
self.navigationItem.leftBarButtonItem = barButtonItem;
}
@end
|