diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/project.xcworkspace/xcuserdata/paulsohier.xcuserdatad/UserInterfaceState.xcuserstate b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/project.xcworkspace/xcuserdata/paulsohier.xcuserdatad/UserInterfaceState.xcuserstate index 253ef60..b75196e 100644 Binary files a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/project.xcworkspace/xcuserdata/paulsohier.xcuserdatad/UserInterfaceState.xcuserstate and b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/project.xcworkspace/xcuserdata/paulsohier.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/xcuserdata/paulsohier.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/xcuserdata/paulsohier.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 66c2793..a655a18 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/xcuserdata/paulsohier.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo.xcodeproj/xcuserdata/paulsohier.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -10,11 +10,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Symfony iOS Oauth Demo/Demo+Create.m" - timestampString = "425060204.708221" + timestampString = "428167633.043695" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "38" - endingLineNumber = "38" + startingLineNumber = "116" + endingLineNumber = "116" landmarkName = "+createDemo:desc:serverId:inManagedObjectContext:" landmarkType = "5"> @@ -67,5 +67,53 @@ landmarkType = "5"> + + + + + + + + + + + + diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.h b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.h index 8a1d7cf..6276c4d 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.h +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.h @@ -7,11 +7,12 @@ // #import +#import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; - +@property (nonatomic, strong) NXOAuth2Account *account; @end diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.m b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.m index 75decae..9e8da0a 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.m +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/AppDelegate.m @@ -9,6 +9,7 @@ #import "AppDelegate.h" #import "AppDelegate+radio.h" #import "DatabaseRadio.h" +#import "Demo+Create.h" #import "NXOauth2.h" @@ -53,9 +54,31 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( self.DatabaseContext = [self createMainQueueManagedObjectContext]; + [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; + + return YES; } +-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ + NSDate *fetchStart = [NSDate date]; + NSLog(@"Background fetch"); + + if (!self.account || !self.DatabaseContext) + { + completionHandler(UIBackgroundFetchResultFailed); + } + + [Demo getDataFromClient:self.account inManagedObjectContent:self.DatabaseContext refresh:nil completionHandler:^(UIBackgroundFetchResult result) { + completionHandler(result); + + NSDate *fetchEnd = [NSDate date]; + NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart]; + NSLog(@"Background Fetch Duration: %f seconds", timeElapsed); + }]; + +} + - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Base.lproj/Main.storyboard b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Base.lproj/Main.storyboard index 94619fc..ba5d2c4 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Base.lproj/Main.storyboard +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Base.lproj/Main.storyboard @@ -46,6 +46,9 @@ + + + @@ -128,6 +131,7 @@ + diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/CreateDemoViewController.m b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/CreateDemoViewController.m index 56cea4a..22dc7cc 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/CreateDemoViewController.m +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/CreateDemoViewController.m @@ -9,10 +9,12 @@ #import "CreateDemoViewController.h" #import "Demo+Create.h" #import +#import -@interface CreateDemoViewController () +@interface CreateDemoViewController () @property (weak, nonatomic) IBOutlet UITextField *demotitle; @property (weak, nonatomic) IBOutlet UITextField *demodesc; +@property (weak, nonatomic) IBOutlet UIButton *save; @end @@ -30,7 +32,7 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view. + self.save.enabled = true; } - (void)didReceiveMemoryWarning @@ -53,7 +55,6 @@ - (IBAction)createDemo:(id)sender { NSString *title = self.demotitle.text; NSString *desc = self.demodesc.text; - NSNumber *serverId = [NSNumber numberWithInt:arc4random()]; if ([title length] == 0 || [desc length] == 0) { @@ -83,11 +84,8 @@ - (IBAction)createDemo:(id)sender { } else { - [Demo createDemo:title desc:desc serverId:serverId inManagedObjectContext:self.managedObjectContext]; - - NSLog(@"Created demo with serverId %@", serverId); - - [self.navigationController popViewControllerAnimated:YES]; + self.save.enabled = FALSE; + [self createDemoOnServer:title desc:desc]; } } @@ -97,6 +95,70 @@ - (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext NSLog(@"Got a managed context"); } +- (void)createDemoOnServer:(NSString *)title + desc:(NSString *)desc +{ + NSMutableDictionary *dt = [[NSMutableDictionary alloc]init]; + [dt setValue:title forKey:@"title"]; + [dt setValue:desc forKey:@"description"]; + + NSData *jsonData; + + if ([NSJSONSerialization isValidJSONObject:dt]) { + + NSError *error; + jsonData = [NSJSONSerialization dataWithJSONObject:dt options:NSJSONWritingPrettyPrinted error:&error]; + + if (error != nil) + { + NSLog(@"Error creating JSON data: %@", error); + return; + } + } + else + { + return; + } + + NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + NSLog(@"%@", jsonString); // To verify the jsonString. + + + NXOAuth2Request *theRequest = [[NXOAuth2Request alloc] initWithResource:[NSURL URLWithString:@"http://api.ip-6.nl/demos"] + method:@"POST" + parameters:nil]; + theRequest.account = self.account; + + NSMutableURLRequest *sigendRequest = [[theRequest signedURLRequest] mutableCopy]; + + [sigendRequest setHTTPMethod:@"POST"]; + [sigendRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; + [sigendRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; + [sigendRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; + [sigendRequest setHTTPBody:jsonData]; + + + NXOAuth2Connection *connection = [[NXOAuth2Connection alloc] initWithRequest:sigendRequest + requestParameters:nil + oauthClient:self.account.oauthClient + sendingProgressHandler:nil + responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){ + NSLog(@"Done creating :) %@", error); + + [Demo getDataFromClient:self.account inManagedObjectContent:self.managedObjectContext refresh: nil completionHandler:NULL]; + self.save.enabled = true; + + if (error) + { + // Might do something here? + NSLog(@"ERR"); + } + + [self.navigationController popViewControllerAnimated:YES]; + }]; + connection.delegate = self; +} + /* #pragma mark - Navigation diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.h b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.h index 427600f..2649734 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.h +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.h @@ -8,9 +8,18 @@ #import #import "demo.h" +#import +#import @interface Demo (Create) + ++ (void)getDataFromClient:(NXOAuth2Account *)account + inManagedObjectContent:(NSManagedObjectContext *)context + refresh:(UIRefreshControl *)refresh + completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler; + + + (Demo *)createDemo:(NSString *)title desc:(NSString *)desc serverId:(NSNumber *)serverId diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.m b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.m index 2307245..5a8914c 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.m +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/Demo+Create.m @@ -7,9 +7,85 @@ // #import "Demo+Create.h" +#import @implementation Demo (Create) ++ (void)getDataFromClient:(NXOAuth2Account *)account + inManagedObjectContent:(NSManagedObjectContext *)context + refresh:(UIRefreshControl *)refresh + completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler +{ + [NXOAuth2Request performMethod:@"GET" + onResource:[NSURL URLWithString:@"http://api.ip-6.nl/demos"] + usingParameters:nil + withAccount:account + sendProgressHandler:^(unsigned long long bytesSend, unsigned long long bytesTotal) { // e.g., update a progress indicator + NSLog(@"Send %llu total %llu", bytesSend, bytesTotal); + } + responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){ + if (error) + { + if (completionHandler) + { + completionHandler(UIBackgroundFetchResultFailed); + } + return; + } + + NSError* err; + NSDictionary* json = [NSJSONSerialization + JSONObjectWithData:responseData + options:kNilOptions + error:&err]; + + NSArray* dataArray = [json objectForKey:@"demos"]; + + NSMutableArray* avail = [[NSMutableArray alloc] init]; + + for (NSDictionary *row in dataArray) + { + NSString *title = [row objectForKey:@"title"]; + NSString *desc = [row objectForKey:@"description"]; + + + NSNumber *server = [NSNumber numberWithLong: + [[row objectForKey:@"id"] integerValue]]; + + [avail addObject:server]; + [self createDemo:title desc:desc serverId:server inManagedObjectContext:context]; + } + + + // Delete old crap + NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Demo"]; + + NSError *erro; + NSArray *matches = [context executeFetchRequest:request error:&erro]; + + if ([matches count]) + { + for (Demo *dm in matches) + { + if (![avail containsObject:dm.serverId]) + { + NSLog(@"Deleting %@", dm.serverId); + [context deleteObject:dm]; + } + } + } + if (refresh) + { + [refresh endRefreshing]; + } + + if (completionHandler) + { + completionHandler(UIBackgroundFetchResultNewData); + } + }]; +} + + (Demo *)findDemo:(NSNumber *)serverId inManagedObjectContext:(NSManagedObjectContext *)context { @@ -34,6 +110,8 @@ + (Demo *)createDemo:(NSString *)title serverId:(NSNumber *)serverId inManagedObjectContext:(NSManagedObjectContext *)context { + NSLog(@"Creating a demo with title %@, desc %@ and server %@", title, desc, serverId); + Demo *demo = nil; if ([title length] && [desc length]) { diff --git a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/DemoTableViewController.m b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/DemoTableViewController.m index 797b91d..5cdabcf 100644 --- a/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/DemoTableViewController.m +++ b/Symfony iOS Oauth Demo/Symfony iOS Oauth Demo/DemoTableViewController.m @@ -11,6 +11,7 @@ #import "Demo.h" #import "CreateDemoViewController.h" #import "Demo+Create.h" +#import "AppDelegate.h" #import "NXOauth2.h" @@ -31,6 +32,10 @@ - (void)awakeFromNib self.managedObjectContext = note.userInfo[DatabaseAvailabilityContext]; }]; } +- (void)viewDidLoad +{ + [self.refreshControl addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged]; +} - (void)viewDidAppear:(BOOL)animated { @@ -40,6 +45,8 @@ - (void)viewDidAppear:(BOOL)animated self.account = acc; NSLog(@"Found a account"); //[[NXOAuth2AccountStore sharedStore] removeAccount:acc]; + + break; }; @@ -73,6 +80,9 @@ - (void)viewDidAppear:(BOOL)animated // Do something with the error NSLog(@"Received ERR: %@", error.localizedDescription); }]; + + + } } @@ -87,6 +97,9 @@ - (void)setAccount:(NXOAuth2Account *)account { _account = account; + AppDelegate* app = ((AppDelegate *)[UIApplication sharedApplication].delegate); + app.account = account; + [self updateUI]; } @@ -109,6 +122,9 @@ - (void)updateUI cacheName:nil]; self.addButton.enabled = YES; + + [self.refreshControl beginRefreshing]; + [Demo getDataFromClient:self.account inManagedObjectContent:self.managedObjectContext refresh:self.refreshControl completionHandler:NULL]; } } @@ -141,5 +157,14 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd NSNumber *serverId = demo.serverId; [Demo deleteDemo:serverId inManagedObjectContext: self.managedObjectContext]; } +- (void)refreshView:(UIRefreshControl *)refresh +{ + NSLog(@"Refresh"); + + if (self.account && self.managedObjectContext) + { + [Demo getDataFromClient:self.account inManagedObjectContent:self.managedObjectContext refresh:self.refreshControl completionHandler:NULL]; + } +} @end