From cb0e29c25585e7d82fbd99a9d642a2c326269425 Mon Sep 17 00:00:00 2001 From: Mike Lee Date: Thu, 1 Aug 2013 18:07:25 +0200 Subject: [PATCH] ## GHStore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Moved all save calls to inside the success block to ensure things happen in the right order. * Add API to add and delete issues and comments on the GitHub server * Implement these API, except for `deleteComment` * Also I haven’t really tested these at all. Refs #29 Refs #35 --- Lemacs/GitHub/GHStore.h | 8 +++- Lemacs/GitHub/GHStore.m | 90 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/Lemacs/GitHub/GHStore.h b/Lemacs/GitHub/GHStore.h index fc22468..3c7ea50 100644 --- a/Lemacs/GitHub/GHStore.h +++ b/Lemacs/GitHub/GHStore.h @@ -6,7 +6,7 @@ // Copyright (c) 2013 New Lemurs. All rights reserved. // -@class GHIssue, GHUser, UAGithubEngine; +@class GHComment, GHIssue, GHUser, UAGithubEngine; @interface GHStore : NSObject @@ -24,8 +24,14 @@ - (void)logInWithUsername:(NSString *)username password:(NSString *)password; // Issues +- (void)addIssue:(GHIssue *)issue; +- (void)deleteIssue:(GHIssue *)issue; - (void)loadIssues:(BOOL)freshStart; - (void)loadCommentsForIssue:(GHIssue *)issue; - (void)loadUser:(GHUser *)user; +// Comments +- (void)addComment:(GHComment *)comment toIssue:(GHIssue *)issue; +- (void)deleteComment:(GHComment *)comment; + @end diff --git a/Lemacs/GitHub/GHStore.m b/Lemacs/GitHub/GHStore.m index ae0e642..71bd7fb 100644 --- a/Lemacs/GitHub/GHStore.m +++ b/Lemacs/GitHub/GHStore.m @@ -249,6 +249,51 @@ - (IBAction)sync; #pragma mark Loading +- (void)addIssue:(GHIssue *)issue; +{ + assert((IsEmpty(issue.body) && IsEmpty(issue.title))); // Otherwise defer to changeIssue: + if (IsEmpty(issue.plainBody) || IsEmpty(issue.plainTitle)) + return; // Let the user know it's not ready yet or hide save button + + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; + + NSDictionary *valuesForGitHubKeys = [issue dictionaryWithValuesForKeys:nil]; + [self.GitHub addIssueForRepository:self.repositoryPath withDictionary:valuesForGitHubKeys success:^(id results) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + assert([results isKindOfClass:[NSArray class]]); + NSDictionary *dictionary = [results lastObject]; + assert([results isKindOfClass:[NSDictionary class]]); + [issue setValuesForKeysWithDictionary:dictionary]; + [[GHStore sharedStore] save]; + } failure:^(NSError *error) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + NSLog(@"Failure %@", error.localizedDescription); + }]; +} + +- (void)deleteIssue:(GHIssue *)issue; +{ + if (IsEmpty(issue.body)) { + [issue.managedObjectContext deleteObject:issue]; + [self save]; + return ; + } + + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; + + [self.GitHub deleteIssue:issue.number inRepository:self.repositoryPath success:^(BOOL success) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + if (!success) + return; // ???: What does this state represent? + + [issue.managedObjectContext deleteObject:issue]; + [[GHStore sharedStore] save]; + } failure:^(NSError *error) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + NSLog(@"Failure %@", error.localizedDescription); + }]; +} + - (void)loadIssues:(BOOL)freshStart; { if (freshStart) @@ -268,12 +313,12 @@ - (void)loadIssues:(BOOL)freshStart; [issue setValuesForKeysWithDictionary:dictionary]; } }]; + [[GHStore sharedStore] save]; } failure:^(NSError *error) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSLog(@"Failure %@", error.localizedDescription); }]; - [self save]; } - (void)loadCommentsForIssue:(GHIssue *)issue; @@ -291,19 +336,20 @@ - (void)loadCommentsForIssue:(GHIssue *)issue; [self.GitHub commentsForIssue:issue.number forRepository:self.repositoryPath success:^(id results) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + assert([results isKindOfClass:[NSArray class]]); [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger index, BOOL *stop) { GHComment *comment = [NSEntityDescription insertNewObjectForEntityForName:kGHCommentEntityName inManagedObjectContext:issue.managedObjectContext]; [comment setValuesForKeysWithDictionary:dictionary]; [comment setValue:issue forKey:kGHCommentIssuePropertyName]; [comments addObject:comment]; }]; + issue.comments = [NSOrderedSet orderedSetWithArray:comments]; + [[GHStore sharedStore] save]; } failure:^(NSError *error) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSLog(@"%@ %@", NSStringFromSelector(_cmd), error.localizedDescription); }]; - issue.comments = [NSOrderedSet orderedSetWithArray:comments]; - [self save]; // [self.talkList reloadList]; } @@ -316,17 +362,49 @@ - (void)loadUser:(GHUser *)user; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; - [self.GitHub user:user.userName success:^(id result) { + [self.GitHub user:user.userName success:^(id results) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; - assert([result isKindOfClass:[NSArray class]]); - NSDictionary *dictionary = [result lastObject]; + assert([results isKindOfClass:[NSArray class]]); + NSDictionary *dictionary = [results lastObject]; + assert([results isKindOfClass:[NSDictionary class]]); [user setValuesForKeysWithDictionary:dictionary]; + [[GHStore sharedStore] save]; } failure:^(NSError *error) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSLog(@"%@ %@", NSStringFromSelector(_cmd), error.localizedDescription); }]; } + +#pragma mark Comments + +- (void)addComment:(GHComment *)comment toIssue:(GHIssue *)issue; +{ + // TODO: clean up placeholder data + + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; + [self.GitHub addComment:comment.body toIssue:issue.number forRepository:self.repositoryPath success:^(id results) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + assert([results isKindOfClass:[NSArray class]]); + NSLog(@"%@", results); + NSDictionary *dictionary = [results lastObject]; + assert([results isKindOfClass:[NSDictionary class]]); + [comment setValuesForKeysWithDictionary:dictionary]; + comment.changes = nil; + NSLog(@"%@", dictionary); + [[GHStore sharedStore] save]; + } failure:^(NSError *error) { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + NSLog(@"%@ %@", NSStringFromSelector(_cmd), error.localizedDescription); + }]; +} + +- (void)deleteComment:(GHComment *)comment; +{ + // ???: Do we have to call validateForDelete: or is that involked automatically? + [self.managedObjectContext deleteObject:comment]; +} + @end