Skip to content

Instantly share code, notes, and snippets.

@kastiglione
Forked from bobspryn/signalForSelector.m
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastiglione/10025034 to your computer and use it in GitHub Desktop.
Save kastiglione/10025034 to your computer and use it in GitHub Desktop.
Delegate dance
- (void)viewDidLoad
{
[super viewDidLoad];
@weakify(self);
self.exploreSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, mTCTableViewFrameWidth, 44)];
self.exploreSearchBar.placeholder = @"Search";
[self.exploreSearchBar setBackgroundImage:[UIImage squareImageWithColor:mTCLightTanColor dimension:1] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
self.tableView.tableHeaderView = self.exploreSearchBar;
RACSignal *enterSearchViewSignal = [[self rac_signalForSelector:@selector(searchBarTextDidBeginEditing:)] logNext];
RACSignal *cancelSearch = [self rac_signalForSelector:@selector(searchBarCancelButtonClicked:)];
RACSignal *textChange = [[self rac_signalForSelector:@selector(searchBar:textDidChange:)]
reduceEach:^id(UISearchBar *searchBar, NSString *text) {
return text;
}];
// search string
RAC(self, viewModel.searchString) = [RACSignal merge:@[textChange, [cancelSearch mapReplace:@""]]];
RAC(self.exploreSearchBar, text) = [cancelSearch mapReplace:@""];
RACSignal *showSearchInterfaceSignal = [[RACSignal merge:@[[enterSearchViewSignal mapReplace:@YES], [cancelSearch mapReplace:@NO]]] distinctUntilChanged];
[self.exploreSearchBar rac_liftSelector:@selector(setShowsCancelButton:animated:) withSignals:showSearchInterfaceSignal, [RACSignal return:@YES], nil];
// end editing on cancel click
[self.exploreSearchBar rac_liftSelector:@selector(endEditing:) withSignals:[cancelSearch mapReplace:@YES], nil];
RACSignal *clickSearch = [self rac_signalForSelector:@selector(searchBarSearchButtonClicked:)];
[self.viewModel rac_liftSelector:@selector(performSearch:) withSignals:clickSearch, nil];
// show navigation bar
// cancel button tapped or
// will disappear
RACSignal *showWhenDisappearing = [[self rac_signalForSelector:@selector(viewWillDisappear:)]
mapReplace:@NO];
// hide navigation bar
// enter search or
// view will appear and search string isn't empty
RACSignal *viewWillAppearWithSearchText = [[[self rac_signalForSelector:@selector(viewWillAppear:)]
filter:^BOOL(id value) {
@strongify(self);
return self.viewModel.searchString.length > 0;
}]
mapReplace:@YES];
RACSignal *showHideNavBar = [RACSignal merge:@[showSearchInterfaceSignal, viewWillAppearWithSearchText, showWhenDisappearing]];
[self.navigationController rac_liftSelector:@selector(setNavigationBarHidden:animated:) withSignals:showHideNavBar, [RACSignal return:@YES], nil];
RACSignal *presented = [[RACSignal
merge:@[
[[self rac_signalForSelector:@selector(viewWillAppear:)] mapReplace:@YES],
[[self rac_signalForSelector:@selector(viewWillDisappear:)] mapReplace:@NO]
]]
setNameWithFormat:@"%@ presented", self];
RACSignal *appActive = [[[RACSignal
merge:@[
[[NSNotificationCenter.defaultCenter rac_addObserverForName:UIApplicationDidBecomeActiveNotification object:nil] mapReplace:@YES],
[[NSNotificationCenter.defaultCenter rac_addObserverForName:UIApplicationWillResignActiveNotification object:nil] mapReplace:@NO]
]]
startWith:@YES]
setNameWithFormat:@"%@ appActive", self];
RAC(self, viewModel.active) = [[[RACSignal
combineLatest:@[ presented, appActive ]]
and]
setNameWithFormat:@"%@ active", self];
self.tableView.backgroundColor = mTCLightTanColor;
[self.tableView registerClass:[TCExploreSearchHeaderView class] forHeaderFooterViewReuseIdentifier:@"TCExploreSearchHeaderView"];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.tableView registerClass:[TCProfileUserCell class] forCellReuseIdentifier:@"TCProfileUserCell"];
[self.tableView registerClass:[TCExploreSearchLoadingCell class] forCellReuseIdentifier:@"TCExploreSearchLoadingCell"];
[self.tableView registerClass:[TCExploreHashtagCell class] forCellReuseIdentifier:@"TCExploreHashtagCell"];
[self.tableView registerClass:[TCLoadingCell class] forCellReuseIdentifier:@"TCLoadingCell"];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// reload the search table on signal
[[RACObserve(self, viewModel.tableUpdateSignal)
switchToLatest]
subscribeNext:^(id x) {
@strongify(self);
[self.tableView reloadData];
}];
[[RACObserve(self, viewModel.exploreErrorSignal)
switchToLatest]
subscribeNext:^(NSString *errorDescription) {
[[TWMessageBarManager sharedInstance] showMessageWithTitle:@"Error" description:errorDescription type:TWMessageBarMessageTypeError];
}];
self.exploreSearchBar.delegate = nil;
self.exploreSearchBar.delegate = self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment