Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:06
Show Gist options
  • Save laser/66fd54c0d077cd34348e to your computer and use it in GitHub Desktop.
Save laser/66fd54c0d077cd34348e to your computer and use it in GitHub Desktop.
LoginViewController.m
@implementation WMListingsViewModel
- (id)init {
// combine bounding box and type signals and then feed through api to get current listings
_currentListings = [[[[[RACSignal
combineLatest:@[allBoundingBoxSources,
_currentType,
combinedFilters]
reduce:toListingQuery]
throttle:0.5]
ignore:nil]
flattenMap:toFetchListings]
replayLast];
RACSignal *loginAttempts = [[[RACObserve(self, loginCredentials) ignore:nil] flattenMap:^RACStream *(RACTuple *values) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
GROAuth2SessionManager *sessionManager = [GROAuth2SessionManager
managerWithBaseURL:[NSURL URLWithString:WM_BASE_URL]
clientID:OAUTH_CLIENT_ID
secret:OAUTH_SECRET];
[sessionManager
authenticateUsingOAuthWithPath:@"/oauth/token"
username:values[0]
password:values[1]
success:^(AFOAuthCredential *credential) {
NSLog(@"loginAttempt, success");
[subscriber sendNext:@[@"success", credential]];
[subscriber sendCompleted];
}
failure:^(NSError *error) {
[subscriber sendNext:@[@"fail", error]];
[subscriber sendCompleted];
}
];
return [RACDisposable disposableWithBlock:^{}];
}];
}] replayLast];
_loginSuccesses = [[[loginAttempts
filter:^BOOL(RACTuple *values) {
NSLog(@"loginSuccess, filter");
return [values[0] isEqualToString:@"success"];
}]
map:^id(RACTuple *values) {
NSLog(@"loginSuccess, map");
return values[1];
}] replayLast];
@implementation WMLoginViewController
- (void)viewDidLoad {
[self rac_liftSelector:@selector(handleSuccess:) withSignalsFromArray:@[[_viewModel.loginSuccesses take:1]]];
[self rac_liftSelector:@selector(handleFailure:) withSignalsFromArray:@[[_viewModel.loginFailures takeUntil:_viewModel.loginSuccesses]]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment