Created
February 18, 2014 21:50
-
-
Save bobspryn/9080987 to your computer and use it in GitHub Desktop.
TCFeedViewModel.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // TCFeedViewModel.m | |
| // Three Cents | |
| // | |
| // Created by Bob Spryn on 2/17/14. | |
| // Copyright (c) 2014 Three Cents, Inc. All rights reserved. | |
| // | |
| #import "TCFeedViewModel.h" | |
| @interface TCFeedViewModel () | |
| @property (nonatomic, strong) NSOrderedSet *popularHashtags; | |
| @property (nonatomic, strong) NSOrderedSet *popularUsers; | |
| @property (nonatomic, strong) NSOrderedSet *hashtagSearchResults; | |
| @property (nonatomic, strong) NSOrderedSet *userSearchResults; | |
| @property (nonatomic, strong) NSOrderedSet *results; | |
| // Core data search | |
| @property (nonatomic, strong) NSOrderedSet *localUserSearchResults; | |
| @property (nonatomic, assign) enum TCExploreSearchCellState searchCellState; | |
| @property (nonatomic, assign) enum TCExploreSearchCellState hashtagsSearchCellState; | |
| @property (nonatomic, assign) enum TCExploreSearchCellState usersSearchCellState; | |
| @property (nonatomic, assign, getter = isLoading) BOOL loading; | |
| @property (nonatomic, assign, getter = isHashtagsLoading) BOOL hashtagsLoading; | |
| @property (nonatomic, assign, getter = isUsersLoading) BOOL usersLoading; | |
| @end | |
| @implementation TCFeedViewModel | |
| - (id) init { | |
| self = [super init]; | |
| if (!self) return nil; | |
| self.tab = TCExploreTabHashtags; | |
| self.localUserSearchResults = [NSOrderedSet orderedSet]; | |
| self.popularHashtags = [NSOrderedSet orderedSetWithObjects:@"1", nil]; | |
| self.hashtagSearchResults = [NSOrderedSet orderedSetWithObjects:@"1", @"2", nil]; | |
| self.popularUsers = [NSOrderedSet orderedSetWithObjects:@"1", @"2", @"3", nil]; | |
| self.userSearchResults = [NSOrderedSet orderedSetWithObjects:@"1", @"2", @"3", @"4", nil]; | |
| self.results = [NSOrderedSet orderedSet]; | |
| @weakify(self); | |
| RACSignal *tabState = [RACObserve(self, tab) distinctUntilChanged]; | |
| RAC(self, searchCellState) = [tabState | |
| map:^id(NSNumber *tab) { | |
| @strongify(self); | |
| if (tab.integerValue == TCExploreTabHashtags) { | |
| return @(self.hashtagsSearchCellState); | |
| } | |
| return @(self.usersSearchCellState); | |
| }]; | |
| RACSignal *inSearchMode = [RACObserve(self, searchString) | |
| map:^id(NSString *searchString) { | |
| return @(searchString && searchString.length > 0); | |
| }]; | |
| // tabState is either 0 or 1, so it works in a nice if signal | |
| RAC(self, results) = [RACSignal if:tabState | |
| then:[RACSignal if:inSearchMode | |
| then:RACObserve(self, userSearchResults) | |
| else:RACObserve(self, popularUsers)] | |
| else:[RACSignal if:inSearchMode | |
| then:RACObserve(self, hashtagSearchResults) | |
| else:RACObserve(self, popularHashtags)]]; | |
| return self; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment