Skip to content

Instantly share code, notes, and snippets.

@jem-computer
Created July 15, 2014 15:19
Show Gist options
  • Save jem-computer/9ed0d757d7ff2ce1b5f3 to your computer and use it in GitHub Desktop.
Save jem-computer/9ed0d757d7ff2ce1b5f3 to your computer and use it in GitHub Desktop.
RAC example for ben
//
// IATBenHowdleViewController.m
// Visit
//
// Created by Jon Gold on 15/07/2014.
// Copyright (c) 2014 Internet & Things. All rights reserved.
//
#import "IATBenHowdleViewController.h"
#import "IATStore.h"
@interface IATBenHowdleViewController ()
@end
@implementation IATBenHowdleViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
// actually this should be passed in somewhere else but you get the idea - maybe '-initWithStore:(IATStore)store' or something from the previous view controller.
IATStore *store = [[IATStore alloc] init];
UILabel *tweetCount = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[self.view addSubview:tweetCount];
RAC(tweetCount, text) = [store.tweetCount map:^id(NSNumber *count) {
return [NSString stringWithFormat:@"%@ tweets", count];
}];
UIButton *sendTweet = [[UIButton alloc] initWithFrame:CGRectMake(0, 60, 320, 40)];
sendTweet.titleLabel.text = @"Send a tweet!";
sendTweet.rac_command = store.updateTweets;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
//
// IATStore.h
// Visit
//
// Created by Jon Gold on 15/07/2014.
// Copyright (c) 2014 Internet & Things. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface IATStore : NSObject
@property (nonatomic, strong) RACSignal *tweetCount;
@property (nonatomic, strong) RACCommand *updateTweets;
@end
//
// IATStore.m
// Visit
//
// Created by Jon Gold on 15/07/2014.
// Copyright (c) 2014 Internet & Things. All rights reserved.
//
#import "IATStore.h"
@interface IATStore ()
@property (nonatomic, strong) NSArray *tweets;
@property (nonatomic, strong) NSNumber *userLoggedIn;
@end
@implementation IATStore
- (instancetype)init {
if (self = [super init]) {
self.tweets = @[
@{@"from": @"benhowdle",
@"text": @"I love Jon. Jon is awesome"}
];
// move this to an account class or something
self.userLoggedIn = @(YES);
// observe tweets array, map it into an NSNumber count
self.tweetCount = [RACObserve(self, tweets) map:^id(NSArray *tweets) {
return @([tweets count]);
}];
RACSignal *userLoggedInSignal = RACObserve(self, userLoggedIn);
self.updateTweets = [[RACCommand alloc] initWithEnabled:userLoggedInSignal signalBlock:^RACSignal *(id input) {
// Update the store here I guess
return [RACSignal empty];
}];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment