forked from reddit-archive/iReddit
/
SubredditTableViewDelegate.m
104 lines (85 loc) · 3.33 KB
/
SubredditTableViewDelegate.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
//
// SubredditTableViewDelegate.m
// iReddit
//
// Created by Ryan Bruels on 12/12/10.
//
#import "SubredditTableViewDelegate.h"
#import "SubredditDataSource.h"
@interface SubredditTableViewDelegate ()
- (void)teardownRefreshingView;
- (void)refreshingHideAnimationStopped;
@end
static const CGFloat kRefreshingViewHeight = 22;
@implementation SubredditTableViewDelegate
// load more stories if we're at the bottom
- (id)initWithController:(TTTableViewController*)controller
{
if(self = [super initWithController:controller])
{
[self.controller.dataSource.model.delegates addObject:self];
}
return self;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[super scrollViewDidEndDecelerating:scrollView];
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height)
if ([self.controller.dataSource.model respondsToSelector:@selector(canLoadMore)])
if ([((SubredditDataModel *)self.controller.dataSource.model) canLoadMore])
{
if(!_refreshingView)
{
_refreshingView = [[TTActivityLabel alloc] initWithFrame:
CGRectMake(0, self.controller.tableView.frame.size.height + self.controller.tableView.frame.origin.y, self.controller.tableView.bounds.size.width, kRefreshingViewHeight)
style:TTActivityLabelStyleBlackBox text:@"Updating..."];
_refreshingView.userInteractionEnabled = NO;
_refreshingView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
_refreshingView.font = [UIFont boldSystemFontOfSize:12];
NSInteger tableIndex = [self.controller.view.subviews indexOfObject:self.controller.tableView];
[self.controller.view insertSubview:_refreshingView atIndex:tableIndex+1];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_TRANSITION_DURATION];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_refreshingView.frame = CGRectOffset(_refreshingView.frame, 0, -kRefreshingViewHeight);
[UIView commitAnimations];
}
[self.controller.dataSource.model load:TTURLRequestCachePolicyNoCache more:YES];
}
}
- (void)modelDidFinishLoad:(id<TTModel>)model
{
[self teardownRefreshingView];
}
- (void)model:(id<TTModel>)model didFailLoadWithError:(NSError*)error
{
[self teardownRefreshingView];
}
- (void)modelDidCancelLoad:(id<TTModel>)model
{
[self teardownRefreshingView];
}
- (void)teardownRefreshingView
{
if(_refreshingView)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_TRANSITION_DURATION];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(refreshingHideAnimationStopped)];
_refreshingView.alpha = 0;
[UIView commitAnimations];
}
}
- (void)refreshingHideAnimationStopped
{
[_refreshingView removeFromSuperview];
[self.controller.tableView flashScrollIndicators];
TT_RELEASE_SAFELY(_refreshingView);
}
- (void)dealloc
{
[self.controller.dataSource.model.delegates removeObject:self];
[super dealloc];
}
@end