This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
GHPullRefreshTableViewController.m
157 lines (130 loc) · 4.26 KB
/
GHPullRefreshTableViewController.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
//
// Copyright 2010-2012 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// GHPullRefreshTableViewController.m
// Greenhouse
//
// Created by Roy Clarkson on 8/27/10.
//
#import "GHPullRefreshTableViewController.h"
#import "EGORefreshTableHeaderView.h"
#import "GHDataRefreshController.h"
@interface GHPullRefreshTableViewController()
@property (nonatomic, strong) EGORefreshTableHeaderView *refreshHeaderView;
@end
@implementation GHPullRefreshTableViewController
@synthesize tableView;
@synthesize refreshHeaderView;
@synthesize reloading = _reloading;
@synthesize lastRefreshKey;
@dynamic lastRefreshDate;
@dynamic lastRefreshExpired;
- (NSDate *)lastRefreshDate
{
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:self.lastRefreshKey];
if (date == nil)
{
date = [GHDataRefreshController defaultDate];
}
DLog(@"%@", date);
return date;
}
- (void)setLastRefreshDate:(NSDate *)date
{
[[NSUserDefaults standardUserDefaults] setObject:date forKey:self.lastRefreshKey];
}
- (BOOL)lastRefreshExpired
{
// if the last refresh was older than the configured time, then expire the data
NSDate *expireDate = [NSDate dateWithTimeIntervalSinceNow:-[GHUserSettings dataExpiration]];
DLog(@"last refresh: %@", self.lastRefreshDate);
DLog(@"expire: %@", expireDate);
return ([self.lastRefreshDate compare:expireDate] == NSOrderedAscending);
}
- (void)reloadTableViewDataSource
{
// implement in inherited class
}
- (void)dataSourceDidFinishLoadingNewData
{
_reloading = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[UIView commitAnimations];
[refreshHeaderView setState:EGOOPullRefreshNormal];
self.lastRefreshDate = [NSDate date];
[refreshHeaderView setLastUpdateLabel:self.lastRefreshDate];
}
#pragma mark -
#pragma mark UIScrollViewDelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.isDragging)
{
if (refreshHeaderView.state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_reloading)
{
[refreshHeaderView setState:EGOOPullRefreshNormal];
}
else if (refreshHeaderView.state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_reloading)
{
[refreshHeaderView setState:EGOOPullRefreshPulling];
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (scrollView.contentOffset.y <= - 65.0f && !_reloading)
{
_reloading = YES;
[self reloadTableViewDataSource];
[refreshHeaderView setState:EGOOPullRefreshLoading];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
[UIView commitAnimations];
}
}
#pragma mark -
#pragma mark UIViewController methods
- (void)viewDidLoad
{
[super viewDidLoad];
DLog(@"");
if (refreshHeaderView == nil)
{
CGRect frame = CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, 320.0f, self.tableView.bounds.size.height);
self.refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:frame];
refreshHeaderView.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
[self.tableView addSubview:refreshHeaderView];
self.tableView.showsVerticalScrollIndicator = YES;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
DLog(@"");
[refreshHeaderView setLastUpdateLabel:self.lastRefreshDate];
}
- (void)viewDidUnload
{
[super viewDidUnload];
DLog(@"");
self.refreshHeaderView = nil;
self.tableView = nil;
self.lastRefreshKey = nil;
}
@end