Last active
December 13, 2016 11:19
-
-
Save dodikk/f993dca3e4d4da3c4b630747f8e2f310 to your computer and use it in GitHub Desktop.
MVVM router example
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
@interface HPQuizRouter | |
- (void)navigateToSelectedQuiz:(id<HPQuizState>)quizState from:(UIViewController*)screen; | |
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender; | |
@end |
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
@implementation HPQuizRouter | |
- (void)navigateToSelectedQuiz:(id<HPQuizState>)quizState from:(UIViewController*)screen | |
{ | |
// wraps segue ids. They should not appear in the client code | |
if ([quizState isCompleted]) | |
{ | |
[screen performSegueWithIdentifier:kSegueToAlreadyCompletedQuizScreen sender:screen]; | |
} | |
else | |
{ | |
[screen performSegueWithIdentifier:kSegueToSelectedQuiz sender:screen]; | |
} | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender | |
{ | |
if ([segue.identifier isEqualToString:kSegueToSelectedQuiz]) | |
{ | |
MySelectedQuizVC* nextVC = objc_member_of_cast<MySelectedQuizVC>(segue.destinationController); | |
nextVC.viewModel = [MyFactory buildVmForXYZ]; | |
nextVC.router = [MySelectedQuizVCRouter new]; | |
} | |
} | |
@end | |
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
@interface QuizQuestionVC : UIViewController | |
// to be injected | |
@property (nonatomic, strong) HPQuizRouter* quizRouter; | |
@property (nonatomic, strong) id<HPQuizListVM> quizVM; | |
@end |
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
- (void)tableView:(UITableView*)tableView | |
didSelectRowAtIndexPath:(NSIndexPath*)indexPath | |
{ | |
// user's action | |
[self.router navigateToSelectedQuiz:self]; | |
} | |
-(void)viewModelDidLoadSomethingAndShouldNavigate:(id)myViewModel | |
{ | |
// data flow driven | |
[self.router navigateToSelectedQuiz:self]; | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender | |
{ | |
// navigation logic and dependency injection delegated to a separate class | |
[self.router prepareForSegue:segue sender:self]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// lights out if injection has not been done properly | |
NSParameterAssert(nil != self.quizRouter); | |
NSParameterAssert(nil != self.quizVM); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment