Skip to content

Instantly share code, notes, and snippets.

@rnystrom
Created June 21, 2013 14:53
Show Gist options
  • Select an option

  • Save rnystrom/5831736 to your computer and use it in GitHub Desktop.

Select an option

Save rnystrom/5831736 to your computer and use it in GitHub Desktop.
Login to Github and display user info
- (void)viewDidLoad {
[super viewDidLoad];
RAC(self.nameLabel, text) = RACAble([GHDataStore sharedStore], client.user.name);
RAC(self.emailLabel, text) = RACAble([GHDataStore sharedStore], client.user.email);
RAC(self.companyLabel, text) = RACAble([GHDataStore sharedStore], client.user.company);
RAC(self.loginLabel, text) = RACAble([GHDataStore sharedStore], client.user.login);
RAC(self.reposLabel, text) = [[RACSignal combineLatest:@[
RACAble([GHDataStore sharedStore], client.user.publicRepoCount),
RACAble([GHDataStore sharedStore], client.user.privateRepoCount)
] reduce:^NSString *(NSNumber *public, NSNumber *private) {
NSUInteger total = public.integerValue + private.integerValue;
NSLog(@"%i",total);
if (total > 0) {
return [NSString stringWithFormat:@"%i",total];
}
return @"No repos";
}] deliverOn:RACScheduler.mainThreadScheduler];
[RACAble([GHDataStore sharedStore], client.user.avatarURL) subscribeNext:^(NSURL *url) {
[self.imageView setImageWithURL:url];
}];
RAC(self.loginButton, enabled) = [RACSignal combineLatest:@[
self.usernameTextField.rac_textSignal,
self.passwordTextField.rac_textSignal]
reduce:^(NSString *username, NSString *password) {
return @([username length] > 0 && [password length] > 0);
}];
[[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *sender) {
[self.activity startAnimating];
OCTUser *user = [OCTUser userWithLogin:self.usernameTextField.text server:OCTServer.dotComServer];
OCTClient *client = [OCTClient authenticatedClientWithUser:user password:self.passwordTextField.text];
[[[client fetchUserInfo] deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(OCTUser *fetchedUser) {
[self.activity stopAnimating];
[[GHDataStore sharedStore] persistUser:fetchedUser withPassword:self.passwordTextField.text];
}
error:^(NSError *error) {
[self.activity stopAnimating];
NSLog(@"Fetching error: %@",error.localizedDescription);
}];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment