Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Added repository watchlist. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
dbloete committed Sep 24, 2009
1 parent e493388 commit 1eb94f3
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 181 deletions.
12 changes: 1 addition & 11 deletions Classes/AppConstants.h
Expand Up @@ -27,6 +27,7 @@
#define kUserXMLFormat @"https://github.com/api/v2/xml/user/show/%@"
#define kAuthenticateUserXMLFormat @"https://github.com/api/v2/xml/user/show/%@?login=%@&token=%@"
#define kUserReposFormat @"https://github.com/api/v2/xml/repos/show/%@"
#define kUserWatchedReposFormat @"https://github.com/api/v2/xml/repos/watched/%@"
#define kUserSearchFormat @"https://github.com/api/v2/xml/user/search/%@"
#define kUserFollowingFormat @"http://github.com/api/v2/json/user/show/%@/following"
#define kUserFollowersFormat @"http://github.com/api/v2/json/user/show/%@/followers"
Expand Down Expand Up @@ -65,14 +66,3 @@
#define kResourceSavingStatusKeyPath @"savingStatus"
#define kUserLoginKeyPath @"login"
#define kUserGravatarKeyPath @"gravatar"

// NSCoding
#define kUserPersistenceFileFormat @"%@.plist"
#define kLoginKey @"login"
#define kUserKey @"user"
#define kRepositoriesKey @"repositories"
#define kRepositoriesURLKey @"repositoriesURL"
#define kWatchedRepositoriesKey @"watchedRepositories"
#define kOwnerKey @"owner"
#define kNameKey @"name"

10 changes: 9 additions & 1 deletion Classes/FeedController.m
Expand Up @@ -27,7 +27,15 @@ - (void)dealloc {
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:kResourceLoadingStatusKeyPath]) [self.tableView reloadData];
if ([keyPath isEqualToString:kResourceLoadingStatusKeyPath]) {
if (feed.isLoaded) {
[self.tableView reloadData];
} else if (feed.error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Loading error" message:@"Could not load the feed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Expand Down
2 changes: 1 addition & 1 deletion Classes/GHRepositories.h
Expand Up @@ -4,7 +4,7 @@

@class GHUser;

@interface GHRepositories : GHResource <NSCoding> {
@interface GHRepositories : GHResource {
NSMutableArray *repositories;
@private
GHUser *user;
Expand Down
14 changes: 0 additions & 14 deletions Classes/GHRepositories.m
Expand Up @@ -23,20 +23,6 @@ - (id)initWithUser:(GHUser *)theUser andURL:(NSURL *)theURL {
return self;
}

- (id)initWithCoder:(NSCoder *)coder {
[super init];
self.user = [coder decodeObjectForKey:kUserKey];
self.repositories = [coder decodeObjectForKey:kRepositoriesKey];
self.repositoriesURL = [coder decodeObjectForKey:kRepositoriesURLKey];
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:user forKey:kUserKey];
[coder encodeObject:repositories forKey:kRepositoriesKey];
[coder encodeObject:repositoriesURL forKey:kRepositoriesURLKey];
}

- (void)dealloc {
[repositoriesURL release];
[repositories release];
Expand Down
2 changes: 1 addition & 1 deletion Classes/GHRepository.h
Expand Up @@ -6,7 +6,7 @@

@class GHIssues, GHNetworks;

@interface GHRepository : GHResource <NSCoding> {
@interface GHRepository : GHResource {
NSString *name;
NSString *owner;
NSString *descriptionText;
Expand Down
13 changes: 0 additions & 13 deletions Classes/GHRepository.m
Expand Up @@ -22,19 +22,6 @@ - (id)initWithOwner:(NSString *)theOwner andName:(NSString *)theName {
return self;
}

- (id)initWithCoder:(NSCoder *)coder {
[super init];
NSString *theOwner = [coder decodeObjectForKey:kOwnerKey];
NSString *theName = [coder decodeObjectForKey:kNameKey];
[self setOwner:theOwner andName:theName];
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:owner forKey:kOwnerKey];
[coder encodeObject:name forKey:kNameKey];
}

- (void)dealloc {
[name release];
[owner release];
Expand Down
1 change: 1 addition & 0 deletions Classes/GHResourcesParserDelegate.m
Expand Up @@ -36,6 +36,7 @@ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
DebugLog(@"Parsing error: %@", parseError);
error = [parseError retain];
[self parserDidEndDocument:parser];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
Expand Down
6 changes: 2 additions & 4 deletions Classes/GHUser.h
@@ -1,13 +1,12 @@
#import <UIKit/UIKit.h>
#import "Archiving.h"
#import "GHResource.h"
#import "GHUsers.h"
#import "GHRepositories.h"


@class Archiver, GravatarLoader, GHRepository, GHFeed;
@class GravatarLoader, GHRepository, GHFeed;

@interface GHUser : GHResource <NSCoding, Archiving> {
@interface GHUser : GHResource {
NSString *name;
NSString *login;
NSString *email;
Expand All @@ -27,7 +26,6 @@
BOOL isAuthenticated;
@private
GravatarLoader *gravatarLoader;
Archiver *archiver;
}

@property (nonatomic, retain) NSString *name;
Expand Down
42 changes: 4 additions & 38 deletions Classes/GHUser.m
Expand Up @@ -6,7 +6,6 @@
#import "GHUsersParserDelegate.h"
#import "ASIFormDataRequest.h"
#import "CJSONDeserializer.h"
#import "Archiver.h"


@interface GHUser ()
Expand Down Expand Up @@ -36,22 +35,8 @@ - (id)initWithLogin:(NSString *)theLogin {
return self;
}

- (id)initWithCoder:(NSCoder *)coder {
[self init];
NSString *theLogin = [coder decodeObjectForKey:kLoginKey];
[self setLogin:theLogin];
self.watchedRepositories = [coder decodeObjectForKey:kWatchedRepositoriesKey];
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:login forKey:kLoginKey];
[coder encodeObject:watchedRepositories forKey:kWatchedRepositoriesKey];
}

- (void)dealloc {
[self removeObserver:self forKeyPath:kUserLoginKeyPath];
[archiver release];
[name release];
[login release];
[email release];
Expand Down Expand Up @@ -85,14 +70,13 @@ - (void)setLogin:(NSString *)theLogin {
[theLogin retain];
[login release];
login = theLogin;
// Archiving
NSString *fileName = [NSString stringWithFormat:kUserPersistenceFileFormat, login];
[archiver release];
archiver = [[Archiver alloc] initWithKey:kUserKey andFileName:fileName];
// Repositories
NSString *repositoriesURLString = [NSString stringWithFormat:kUserReposFormat, login];
NSString *watchedRepositoriesURLString = [NSString stringWithFormat:kUserWatchedReposFormat, login];
NSURL *repositoriesURL = [NSURL URLWithString:repositoriesURLString];
NSURL *watchedRepositoriesURL = [NSURL URLWithString:watchedRepositoriesURLString];
self.repositories = [[[GHRepositories alloc] initWithUser:self andURL:repositoriesURL] autorelease];
self.watchedRepositories = [[[GHRepositories alloc] initWithUser:self andURL:watchedRepositoriesURL] autorelease];
// Recent Activity
NSString *activityFeedURLString = [NSString stringWithFormat:kUserFeedFormat, login];
NSURL *activityFeedURL = [NSURL URLWithString:activityFeedURLString];
Expand Down Expand Up @@ -199,9 +183,7 @@ - (void)setUserFollowing:(NSArray *)args {
}

- (BOOL)isWatching:(GHRepository *)aRepository {
// FIXME Currently just stubbed out, see the issue:
// http://github.com/dbloete/ioctocat/issues#issue/6
// if (!watchedRepositories.isLoaded) [watchedRepositories loadRepositories];
if (!watchedRepositories.isLoaded) [watchedRepositories loadRepositories];
return [watchedRepositories.repositories containsObject:aRepository];
}

Expand Down Expand Up @@ -243,20 +225,4 @@ - (NSString *)cachedGravatarPath {
return [documentsPath stringByAppendingPathComponent:imageName];
}

#pragma mark -
#pragma mark Archiving

- (void)archive {
[archiver archiveObject:self];
}

- (void)restore {
GHUser *restoredUser = [archiver restoreObject];
if (restoredUser && restoredUser.watchedRepositories) {
self.watchedRepositories = restoredUser.watchedRepositories;
} else {
self.watchedRepositories = [[[GHRepositories alloc] initWithUser:self andURL:nil] autorelease];
}
}

@end
39 changes: 21 additions & 18 deletions Classes/RepositoriesController.m
Expand Up @@ -9,7 +9,7 @@


@interface RepositoriesController ()
- (void)displayRepositories;
- (void)displayRepositories:(GHRepositories *)repositories;
- (NSMutableArray *)repositoriesInSection:(NSInteger)section;
@end

Expand All @@ -28,15 +28,18 @@ - (void)viewDidLoad {
[super viewDidLoad];
if (!user) self.user = self.currentUser; // Set to currentUser in case this controller is initialized from the TabBar
[user.repositories addObserver:self forKeyPath:kResourceLoadingStatusKeyPath options:NSKeyValueObservingOptionNew context:nil];
(user.repositories.isLoaded) ? [self displayRepositories] : [user.repositories loadRepositories];
[user.watchedRepositories addObserver:self forKeyPath:kResourceLoadingStatusKeyPath options:NSKeyValueObservingOptionNew context:nil];
(user.repositories.isLoaded) ? [self displayRepositories:user.repositories] : [user.repositories loadRepositories];
(user.watchedRepositories.isLoaded) ? [self displayRepositories:user.watchedRepositories] : [user.watchedRepositories loadRepositories];
}

- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}

- (void)dealloc {
[user.repositories removeObserver:self forKeyPath:kResourceLoadingStatusKeyPath];
[user.repositories removeObserver:self forKeyPath:kResourceLoadingStatusKeyPath];
[user.watchedRepositories removeObserver:self forKeyPath:kResourceLoadingStatusKeyPath];
[noPublicReposCell release];
[noPrivateReposCell release];
[noWatchedReposCell release];
Expand All @@ -47,25 +50,28 @@ - (void)dealloc {

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:kResourceLoadingStatusKeyPath]) {
if (user.repositories.isLoaded) {
[self displayRepositories];
[self.tableView reloadData];
} else if (user.repositories.error) {
GHRepositories *repositories = object;
if (repositories.isLoaded) {
[self displayRepositories:repositories];
} else if (repositories.error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Loading error" message:@"Could not load the repositories" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}

- (void)displayRepositories {
self.privateRepositories = [NSMutableArray array];
self.publicRepositories = [NSMutableArray array];
for (GHRepository *repo in user.repositories.repositories) {
(repo.isPrivate) ? [privateRepositories addObject:repo] : [publicRepositories addObject:repo];
- (void)displayRepositories:(GHRepositories *)repositories {
if ([repositories isEqual:user.repositories]) {
self.privateRepositories = [NSMutableArray array];
self.publicRepositories = [NSMutableArray array];
for (GHRepository *repo in user.repositories.repositories) {
(repo.isPrivate) ? [privateRepositories addObject:repo] : [publicRepositories addObject:repo];
}
[self.publicRepositories sortUsingSelector:@selector(compareByName:)];
[self.privateRepositories sortUsingSelector:@selector(compareByName:)];
}
[self.publicRepositories sortUsingSelector:@selector(compareByName:)];
[self.privateRepositories sortUsingSelector:@selector(compareByName:)];
[self.tableView reloadData];
}

- (GHUser *)currentUser {
Expand Down Expand Up @@ -109,6 +115,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (!user.repositories.isLoaded) return loadingReposCell;
if (indexPath.section == 0 && self.privateRepositories.count == 0) return noPrivateReposCell;
if (indexPath.section == 1 && self.publicRepositories.count == 0) return noPublicReposCell;
if (indexPath.section == 2 && !user.watchedRepositories.isLoaded) return loadingReposCell;
if (indexPath.section == 2 && self.watchedRepositories.count == 0) return noWatchedReposCell;
RepositoryCell *cell = (RepositoryCell *)[tableView dequeueReusableCellWithIdentifier:kRepositoryCellIdentifier];
if (cell == nil) cell = [[[RepositoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kRepositoryCellIdentifier] autorelease];
Expand All @@ -127,9 +134,5 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[repoController release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return (indexPath.section == 2 && self.watchedRepositories.count == 0) ? 80.0f : 44.0f;
}

@end

10 changes: 0 additions & 10 deletions Classes/iOctocatAppDelegate.m
Expand Up @@ -4,7 +4,6 @@

@interface iOctocatAppDelegate ()
- (void)postLaunch;
- (void)saveApplicationState;
- (void)presentLogin;
- (void)dismissLogin;
- (void)showAuthenticationSheet;
Expand Down Expand Up @@ -43,10 +42,6 @@ - (void)postLaunch {
if (launchDefault) [self authenticate];
}

- (void)applicationWillTerminate:(UIApplication *)application {
[self saveApplicationState];
}

- (void)dealloc {
[tabBarController release];
[feedController release];
Expand Down Expand Up @@ -76,10 +71,6 @@ - (GHUser *)userWithLogin:(NSString *)theUsername {
return user;
}

- (void)saveApplicationState {
[self.currentUser archive];
}

- (void)clearAvatarCache {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
Expand Down Expand Up @@ -168,7 +159,6 @@ - (void)dismissAuthenticationSheet {

- (void)proceedAfterAuthentication {
[self dismissLogin];
[self.currentUser restore];
[feedController setupFeeds];
}

Expand Down

0 comments on commit 1eb94f3

Please sign in to comment.