I'm trying to express the following scenario in ReactiveCocoa and MVVM.
- There's a table view which shows a list of Bluetooth devices nearby
- On row selection we start a process of connecting to the selected device and display an activity indicator as an accessoryView of the selected cell.
Now we have alternative endings:
- When connected successfully we dismiss the table view controller and pass device handle to the parent view controller or rather parent view model.
- When during connecting process user taps another table view cell then we cancel the previous process and start a new one with the selected device.
- On error show a message.
I have a problem with ending no 2. I came up with RACCommand in my view model that triggers the process of connection. Then in didSelectRowAtIndexPath I execute that command.
ViewModel:
- (RACCommand *)selectionCommand {
if (!_selectionCommand) {
_selectionCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [self selectionSignal];
}];
}
return _selectionCommand;
}
- (RACSignal *)selectionSignal {
// not implemented for real
return [[[RACSignal return:@"ASDF"] delay:2.0] logAll];
}
ViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicatorView startAnimating];
cell.accessoryView = activityIndicatorView;
[[self.viewModel.selectionCommand execute:indexPath] subscribeCompleted:^{
[activityIndicatorView stopAnimating];
cell.accessoryView = nil;
}];
}
This shows and hides the activity view during the connection process but only when I wait for it to finish without tapping on other cells. I ask for a guidance on how such behaviour could be completed. (It also feels like this isn't the right place to subscribe to the signal, right? Should it go to viewDidLoad?)