Was working on a refactor of this method when I realized it was doing too much.

- (void)showAppropriateView
{
    if ([self isOffline]) {
        [self.viewManager showOfflineView];
    } else if (!self.nullstateScreenHasBeenDisplayed || [self searchBarHasNoText]) {
        [self.viewManager showNullstateView];
    } else if (self.errorHasOccurred) {
        [self.viewManager showErrorView];
    } else if (self.tracks.count > 0) {
        [self.collectionView reloadData];
        [self.viewManager showContentView];
    } else {
        [self.viewManager showNoResultsView];
        [self updateNoResultsSearchText];
    }
}

On the surface it doesn’t look that bad. Just figuring out what view to display, and then displaying it.

But there are really x2 things going on with this method. One is displaying the view. But the other is doing the other things as a result of that view being displayed.

Before breaking I had to pull out the non display view stuff. Then I was free to have another method focus on the view logic itself.

Final result looked like this.

- (void)showAppropriateView
{
    if (self.tracks.count > 0) {
        [self.collectionView reloadData];
    } else if (self.tracks.count == 0) {
        [self updateNoResultsSearchText];
    }

    [self.viewManager showAppropriateViewWhenOffline:[self isOffline]
                     nullstateScreenHasBeenDisplayed:!self.nullstateScreenHasBeenDisplayed
                                  searchBarHasNoText: [self searchBarHasNoText]
                                    errorHasOccurred:self.errorHasOccurred
                                          trackCount:self.tracks.count];
}
Advertisements