Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Last active December 20, 2015 09:58
Show Gist options
  • Save oleksii-demedetskyi/6111627 to your computer and use it in GitHub Desktop.
Save oleksii-demedetskyi/6111627 to your computer and use it in GitHub Desktop.
Example of if-less coding
- (void)switchPanel:(UIView*)currentPanel toPanel:(UIView*)newPanel;
{
if (currentPanel == newPanel) return;
/// This part of code describes all transitions between panels.
/// empty mean that panel is null;
/// non-empty : vice versa.
NSString *empty = @"empty", *non_empty = @"non-empty";
NSDictionary* switches = @{
empty : @{
non_empty : ^{
[self.panelContainer addSubview:newPanel];
}},
non_empty : @{
empty : ^{
[currentPanel removeFromSuperview];
},
non_empty : ^{
newPanel.alpha = 0;
[self.panelContainer addSubview:newPanel];
[UIView animateWithDuration:0.5
animations:^{
newPanel.alpha = 1;
currentPanel.alpha = 0;
} completion:^(BOOL finished) {
[currentPanel removeFromSuperview];
}];
}}
};
void(^block)() = ({
NSString* currentPanelStatus = (currentPanel == nil) ? empty : non_empty;
NSString* newPanelStatus = (newPanel == nil) ? empty : non_empty;
switches[currentPanelStatus][newPanelStatus];
});
NSAssert(block != NULL, @"Uncovered condition detected");
block();
}
- (void)reloadView
{
NSAssert(self.panelContainer.subviews.count < 2, @"Inconsistent container content");
UIView* currentPanel = self.panelContainer.subviews[0];
UIView* newPanel = @{
@(PTLoginViewModelCredentialsState): self.credentialsPanel,
@(PTLoginViewModelLoadingState) : self.loadingPanel
}[@(self.model.state)];
[self switchPanel:currentPanel toPanel:newPanel];
[self.loginTextField setText:self.model.login];
[self.passTextField setText:self.model.password];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment