Successfully reported this slideshow.
Your SlideShare is downloading. ×

Coordinators

Ad

NSSpain 2015
Soroush Khanlou

Ad

Overstuffed App Delegate

Ad

@interface SKTabBarController : UITabBarController

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Check these out next

1 of 61 Ad
1 of 61 Ad
Advertisement

Coordinators

  1. 1. NSSpain 2015 Soroush Khanlou
  2. 2. Overstuffed App Delegate
  3. 3. @interface SKTabBarController : UITabBarController
  4. 4. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER
  5. 5. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  6. 6. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  7. 7. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  8. 8. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER
  9. 9. Controller User Input Model Domain State View Display and Layout
  10. 10. Adapter Mediates Model-View Interaction Model Domain State and Behavior View Display and Layout Mutate Update View Notify User Action
  11. 11. View Controller Mediates Model-View Interaction Model Domain State and Behavior View Display and Layout Mutate Update View Notify User Action
  12. 12. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  13. 13. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  14. 14. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  15. 15. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  16. 16. PhotoSelectionViewController StraighteningViewController FilteringViewController CaptionViewController
  17. 17. Frameworks vs Libraries
  18. 18. Frameworks call you vs Libraries
  19. 19. Frameworks call you You call vs Libraries
  20. 20. UIView UIViewController
  21. 21. UIView UIViewController CALayer
  22. 22. UIView UIViewController CALayer ???
  23. 23. UIView UIViewController CALayer Coordinators
  24. 24. App Coordinator
  25. 25. App Coordinator aka Application Controller
  26. 26. What does a coordinator do?
  27. 27. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  28. 28. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  29. 29. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  30. 30. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  31. 31. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  32. 32. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  33. 33. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  34. 34. How do you make a coordinator?
  35. 35. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  36. 36. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  37. 37. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  38. 38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  39. 39. @interface AppCoordinator : NSObject
  40. 40. - (instancetype)initWithNavigationController: (UINavigationController *)navigationController { self = [super init]; if (!self) return nil; _navigationController = navigationController; return self; }
  41. 41. - (void)start { if ([self isLoggedIn]) { [self showContent]; } else { [self showAuthentication]; } }
  42. 42. - (void)showAuthentication { AuthCoordinator *authCoordinator = [[AuthCoordinator alloc] initWithNavigationController: self.navigationController]; authCoordinator.delegate = self; [authCoordinator start]; [self.childCoordinators addObject:authCoordinator]; }
  43. 43. - (void)coordinatorDidAuth:(AuthCoordinator *)coordinator { [self.childCoordinators removeObject:coordinator]; [self showContent]; }
  44. 44. @implementation AuthCoordinator - (instancetype)initWithNavigationController: (UINavigationController *)navigationController { self = [super init]; if (!self) return nil; _navigationController = navigationController; return self; }
  45. 45. - (void)start { FirstRunViewController *firstRun = [FirstRunViewController new]; firstRun.delegate = self; [self.navigationController pushViewController:firstRun animated:NO]; }
  46. 46. - (void)firstRunViewControllerDidTapSignup: (FirstRunViewController *)firstRun { SignUpViewController *signup = [[SignUpViewController alloc] init]; signup.delegate = self; [self.navigationController pushViewController:signup animated:YES]; }
  47. 47. - (void)signUpViewController:(SignUpViewController *)signup didTapSignupWithEmail:(NSString *)email password:(NSString *)password { //... } Etc.
  48. 48. Why are coordinators great?
  49. 49. 1. View controllers are isolated
  50. 50. A B C D
  51. 51. A B C D Coordinator
  52. 52. 1. View controllers are isolated 2. View controllers are reusable
  53. 53. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated
  54. 54. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated 4. Display-binding is separate from side effects
  55. 55. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated 4. Display-binding is separate from side effects 5. Coordinators are fully in your control
  56. 56. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  57. 57. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  58. 58. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  59. 59. Thanks! @khanlou

×