Skip to content

Instantly share code, notes, and snippets.

@bobspryn
Last active August 29, 2015 14:10
Show Gist options
  • Save bobspryn/3af94e7bb7065c47393b to your computer and use it in GitHub Desktop.
Save bobspryn/3af94e7bb7065c47393b to your computer and use it in GitHub Desktop.

Revisions

  1. bobspryn revised this gist Jul 9, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MVVMExample.m
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSI
    // else if the section is our loading cell
    MYLoadingCell *cell =
    [self.tableView dequeueReusableCellWithIdentifier:@"MYLoadingCell" forIndexPath:indexPath];
    [self.tableView loadMoreTweets];
    [self.viewModel loadMoreTweets];
    return cell;
    }
    }
    @@ -63,7 +63,7 @@ -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSI
    - (void) awakeFromNib {
    [super awakeFromNib];

    RAC(self.avatarImageView, image) = RACObserve(self, viewModel.userAvatarImage);
    RAC(self.avatarImageView, image) = RACObserve(self, viewModel.tweetAuthorAvatarImage);
    RAC(self.userNameLabel, text) = RACObserve(self, viewModel.tweetAuthorFullName);
    RAC(self.tweetTextLabel, text) = RACObserve(self, viewModel.tweetContent);
    }
  2. bobspryn revised this gist Dec 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MVVMExample.m
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ - (void) viewDidLoad {
    @weakify(self);
    [[[RACSignal merge:@[RACObserve(self.viewModel, tweets),
    RACObserve(self.viewModel, allTweetsLoaded)]]
    bufferWithTime:0 onScheduler:[RACScheduler mainThreadScheduler]]]
    bufferWithTime:0 onScheduler:[RACScheduler mainThreadScheduler]]
    subscribeNext:^(id value) {
    @strongify(self);
    [self.tableView reloadData];
  3. bobspryn revised this gist Dec 6, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions MVVMExample.m
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSI
    - (void) awakeFromNib {
    [super awakeFromNib];

    RAC(self.avatarImageView, image) = RACObserve(self.viewModel, userAvatarImage);
    RAC(self.userNameLabel, text) = RACObserve(self.viewModel, tweetAuthorFullName);
    RAC(self.tweetTextLabel, text) = RACObserve(self.viewModel, tweetContent);
    RAC(self.avatarImageView, image) = RACObserve(self, viewModel.userAvatarImage);
    RAC(self.userNameLabel, text) = RACObserve(self, viewModel.tweetAuthorFullName);
    RAC(self.tweetTextLabel, text) = RACObserve(self, viewModel.tweetContent);
    }
  4. bobspryn created this gist Dec 6, 2014.
    69 changes: 69 additions & 0 deletions MVVMExample.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    //
    // View Controller
    //

    - (void) viewDidLoad {
    [super viewDidLoad];

    RAC(self.viewModel, username) = [myTextfield rac_textSignal];

    RACSignal *usernameIsValidSignal = RACObserve(self.viewModel, usernameValid);

    RAC(self.goButton, alpha) = [usernameIsValidSignal
    map: ^(NSNumber *valid) {
    return valid.boolValue ? @1 : @0.5;
    }];

    RAC(self.goButton, enabled) = usernameIsValidSignal;

    RAC(self.avatarImageView, image) = RACObserve(self.viewModel, userAvatarImage);

    RAC(self.userNameLabel, text) = RACObserve(self.viewModel, userFullName);

    @weakify(self);
    [[[RACSignal merge:@[RACObserve(self.viewModel, tweets),
    RACObserve(self.viewModel, allTweetsLoaded)]]
    bufferWithTime:0 onScheduler:[RACScheduler mainThreadScheduler]]]
    subscribeNext:^(id value) {
    @strongify(self);
    [self.tableView reloadData];
    }];

    [[self.goButton rac_signalForControlEvents:UIControlEventTouchUpInside]
    subscribeNext: ^(id value) {
    @strongify(self);
    [self.viewModel getTweetsForCurrentUsername];
    }];
    }

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // if table section is the tweets section
    if (indexPath.section == 0) {
    MYTwitterUserCell *cell =
    [self.tableView dequeueReusableCellWithIdentifier:@"MYTwitterUserCell" forIndexPath:indexPath];

    // grab the cell view model from the vc view model and assign it
    cell.viewModel = self.viewModel.tweets[indexPath.row];
    return cell;
    } else {
    // else if the section is our loading cell
    MYLoadingCell *cell =
    [self.tableView dequeueReusableCellWithIdentifier:@"MYLoadingCell" forIndexPath:indexPath];
    [self.tableView loadMoreTweets];
    return cell;
    }
    }


    //
    // MYTwitterUserCell
    //

    // this could also be in cell init
    - (void) awakeFromNib {
    [super awakeFromNib];

    RAC(self.avatarImageView, image) = RACObserve(self.viewModel, userAvatarImage);
    RAC(self.userNameLabel, text) = RACObserve(self.viewModel, tweetAuthorFullName);
    RAC(self.tweetTextLabel, text) = RACObserve(self.viewModel, tweetContent);
    }