Permalink
Browse files

Adding pull to refresh in posts view.

  • Loading branch information...
1 parent 850382d commit b536891b74f55bf30bf69be0326e4c228aeaf4e8 @jsakuda jsakuda committed Jul 20, 2012
@@ -17,6 +17,7 @@
09359FEB1588974400B4EA3E /* line@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 09359FEA1588974400B4EA3E /* line@2x.png */; };
09359FED1588977500B4EA3E /* line.png in Resources */ = {isa = PBXBuildFile; fileRef = 09359FEC1588977500B4EA3E /* line.png */; };
09517A83158AF4320016EDF8 /* Post.m in Sources */ = {isa = PBXBuildFile; fileRef = 09517A82158AF4320016EDF8 /* Post.m */; };
+ 098B1F6815B921CB00ACFAD1 /* ODRefreshControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 098B1F6715B921CB00ACFAD1 /* ODRefreshControl.m */; };
09A4459C1567A6CD00634C53 /* NSDate+TKCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 09A4458E1567A6CD00634C53 /* NSDate+TKCategory.m */; };
09A4459D1567A6CD00634C53 /* TapkuLibrary.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 09A4458F1567A6CD00634C53 /* TapkuLibrary.bundle */; };
09A4459E1567A6CD00634C53 /* TKCalendarMonthTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 09A445911567A6CD00634C53 /* TKCalendarMonthTableViewController.m */; };
@@ -105,6 +106,8 @@
09359FEC1588977500B4EA3E /* line.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = line.png; sourceTree = "<group>"; };
09517A81158AF4320016EDF8 /* Post.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Post.h; sourceTree = "<group>"; };
09517A82158AF4320016EDF8 /* Post.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Post.m; sourceTree = "<group>"; };
+ 098B1F6615B921CB00ACFAD1 /* ODRefreshControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ODRefreshControl.h; sourceTree = "<group>"; };
+ 098B1F6715B921CB00ACFAD1 /* ODRefreshControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ODRefreshControl.m; sourceTree = "<group>"; };
09A4458D1567A6CD00634C53 /* NSDate+TKCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDate+TKCategory.h"; sourceTree = "<group>"; };
09A4458E1567A6CD00634C53 /* NSDate+TKCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSDate+TKCategory.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
09A4458F1567A6CD00634C53 /* TapkuLibrary.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = TapkuLibrary.bundle; sourceTree = "<group>"; };
@@ -237,6 +240,16 @@
path = Model;
sourceTree = "<group>";
};
+ 098B1F6515B921CB00ACFAD1 /* ODRefreshControl */ = {
+ isa = PBXGroup;
+ children = (
+ 098B1F6615B921CB00ACFAD1 /* ODRefreshControl.h */,
+ 098B1F6715B921CB00ACFAD1 /* ODRefreshControl.m */,
+ );
+ name = ODRefreshControl;
+ path = vendor/ODRefreshControl;
+ sourceTree = "<group>";
+ };
09A4458C1567A6CD00634C53 /* TapkuLibrary */ = {
isa = PBXGroup;
children = (
@@ -283,6 +296,7 @@
09B27FF1157D55A00015FF1A /* SVProgressHUD */,
09A4458C1567A6CD00634C53 /* TapkuLibrary */,
A39E34281564E0D40076ED6D /* MKNetworkKit */,
+ 098B1F6515B921CB00ACFAD1 /* ODRefreshControl */,
A39E33F2155FA29F0076ED6D /* HI Capacity */,
A39E3413155FA29F0076ED6D /* HI CapacityTests */,
A39E33EB155FA29F0076ED6D /* Frameworks */,
@@ -602,6 +616,7 @@
0907D664158721E2009C395E /* AboutViewController.m in Sources */,
0907D67115873DF6009C395E /* Location.m in Sources */,
09517A83158AF4320016EDF8 /* Post.m in Sources */,
+ 098B1F6815B921CB00ACFAD1 /* ODRefreshControl.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -7,9 +7,11 @@
//
#import <UIKit/UIKit.h>
+#import "ODRefreshControl.h"
@interface PostsViewController : UITableViewController<UITableViewDelegate> {
MKNetworkOperation *runningOp;
+ ODRefreshControl *refreshControl;
}
@property (strong, nonatomic) NSMutableArray *posts;
@@ -40,25 +40,10 @@ - (void)viewDidLoad
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
- // Start loading spinner
- [self showLoading];
-
- NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
- [headerFields setValue:@"iOS" forKey:@"x-client-identifier"];
- [headerFields setValue:@"application/json" forKey:@"Accept"];
- HTTPEngine *httpEngine = [[HTTPEngine alloc] initWithHostName:@"hicapacity.org" customHeaderFields:headerFields];
- runningOp = [httpEngine posts:nil :^(NSMutableArray *returnedPosts) {
- posts = returnedPosts;
- [postTableView reloadData];
- [self dismissLoading:NO]; // Stop loading spinner
- runningOp = nil;
- }
- onError:^(NSError *error) {
- // please handle the error
- [self dismissLoading:YES]; // Stop loading spinner
- runningOp = nil;
- }];
+ refreshControl = [[ODRefreshControl alloc] initInScrollView:self.tableView];
+ [refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
+ [self reloadPosts];
}
- (void)viewDidUnload
@@ -89,6 +74,42 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
+- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
+{
+ double delayInSeconds = 0.0;
+ dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
+ dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
+ [self reloadPosts];
+ });
+}
+
+- (void)reloadPosts {
+ // Start loading spinner
+ [self showLoading];
+
+ NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
+ [headerFields setValue:@"iOS" forKey:@"x-client-identifier"];
+ [headerFields setValue:@"application/json" forKey:@"Accept"];
+ HTTPEngine *httpEngine = [[HTTPEngine alloc] initWithHostName:@"hicapacity.org" customHeaderFields:headerFields];
+ runningOp = [httpEngine posts:nil :^(NSMutableArray *returnedPosts) {
+ posts = returnedPosts;
+ [postTableView reloadData];
+ [self dismissLoading:NO]; // Stop loading spinner
+ if ([refreshControl refreshing]) {
+ [refreshControl endRefreshing]; // Stop the refreshing
+ }
+ runningOp = nil;
+ }
+ onError:^(NSError *error) {
+ // please handle the error
+ [self dismissLoading:YES]; // Stop loading spinner
+ if ([refreshControl refreshing]) {
+ [refreshControl endRefreshing]; // Stop the refreshing
+ }
+ runningOp = nil;
+ }];
+}
+
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
@@ -136,7 +157,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
- (void) showLoading {
[SVProgressHUD showWithStatus:@"Loading" maskType:SVProgressHUDMaskTypeNone];
- [[[self view] subviews]makeObjectsPerformSelector:@selector(setUserInteractionEnabled:) withObject:[NSNumber numberWithBool:FALSE]];
+ [[self view] setUserInteractionEnabled:NO];
}
- (void) dismissLoading:(BOOL)error {
@@ -146,7 +167,7 @@ - (void) dismissLoading:(BOOL)error {
else {
[SVProgressHUD dismiss];
}
- [[[self view] subviews]makeObjectsPerformSelector:@selector(setUserInteractionEnabled:) withObject:[NSNumber numberWithBool:TRUE]];
+ [[self view] setUserInteractionEnabled:YES];
}
@end
@@ -0,0 +1,34 @@
+//
+// ODRefreshControl.h
+// ODRefreshControl
+//
+// Created by Fabio Ritrovato on 6/13/12.
+// Copyright (c) 2012 orange in a day. All rights reserved.
+//
+// https://github.com/Sephiroth87/ODRefreshControl
+//
+
+#import <UIKit/UIKit.h>
+#import <QuartzCore/QuartzCore.h>
+
+@interface ODRefreshControl : UIControl {
+ CAShapeLayer *_shapeLayer;
+ CAShapeLayer *_arrowLayer;
+ CAShapeLayer *_highlightLayer;
+ UIActivityIndicatorView *_activity;
+ BOOL _refreshing;
+ BOOL _canRefresh;
+}
+
+@property (nonatomic, readonly) BOOL refreshing;
+@property (nonatomic, strong) UIColor *tintColor;
+
+- (id)initInScrollView:(UIScrollView *)scrollView;
+
+// Tells the control that a refresh operation was started programmatically
+- (void)beginRefreshing;
+
+// Tells the control the refresh operation has ended
+- (void)endRefreshing;
+
+@end
Oops, something went wrong.

0 comments on commit b536891

Please sign in to comment.