Coordinatorパターンの実践

 Coordinatorパターンの実践

Ac3c0c2b458cdf0fe6a6e98a46e34322?s=128

Yoshikuni Kato

March 22, 2017
Tweet

Transcript

  1. Coordinatorύλʔϯͷ࣮ફ @yoshikuni_kato potato%ps #38 2017/03/22 1

  2. Who am I? • Ճ౻༝܇ʢYoshikuni Katoʣ @yoshikuni_kato • iOSΤϯδχΞ •

    Yahoo! Japan -> Φϋί • ʮϥδ΁͐ʯ͘Μ → • Interests: ઃܭ / FRP / Coordinator PaCern / UI࣮૷ 2
  3. Agenda • Coordinatorύλʔϯͷ࣮૷ྫΛ঺հ͍ͨ͠ • Coordinator: ը໘ભҠͷϩδοΫΛVC͔Β੾Γग़ͨ͠΋ͷ • աڈͷࢿྉʢը໘ભҠͷ؅ཧͱMVVMʣΛࢀর͍ͩ͘͞ 3

  4. ιʔείʔυ h"ps:/ /github.com/yoching/CoordinatorSample 4

  5. 5

  6. 6

  7. Coordinatorͷ࡞੒୯Ґ • ʮதʹෳ਺ͷVCΛ࣋ͭVCʯ୯ҐͰCoordinatorΛ࡞Δͱ෼͔Γ ΍͍͢ • UINaviga0onController • UITabBarController • containerViewΛར༻͍ͯ͠ΔViewController

    • UXతͳ෼ׂͱ΋Ұக͢Δ͸ͣ 7
  8. Coordinator protocol protocol Coordinator { var presenter: UIViewController { get

    } func start() } 8
  9. Naviga&onCoordinator protocol protocol NavigationCoordinator: Coordinator { var navigationController: UINavigationController {

    get } } extension NavigationCoordinator { var presenter: UIViewController { return navigationController as UIViewController } } 9
  10. TabBarCoordinator protocol protocol TabBarCoordinator: Coordinator { var tabBarController: UITabBarController {

    get } } extension TabBarCoordinator { var presenter: UIViewController { return tabBarController as UIViewController } } 10
  11. AppCoordinator final class AppCoordinator { private let window: UIWindow private

    let rootCoordinator: Coordinator init(window: UIWindow, rootCoordinator: Coordinator) { self.window = window self.rootCoordinator = rootCoordinator } func start() { rootCoordinator.start() window.rootViewController = rootCoordinator.presenter window.makeKeyAndVisible() } } 11
  12. MainTabCoordinator final class MainTabCoordinator: TabBarCoordinator { let tabBarController: UITabBarController private

    let childCoordinators: [Coordinator] init(presenter: UITabBarController, childCoordinators: [Coordinator]) { self.tabBarController = presenter self.childCoordinators = childCoordinators } func start() { childCoordinators.forEach { coordinator in coordinator.start() } tabBarController.setViewControllers( childCoordinators.map { $0.presenter }, animated: false ) } } 12
  13. 2ͭͷNaviga'onͰڞ௨͢ΔભҠ͕͋Δ • ભҠ ɹ→ protocol-orientedʹղܾ 13

  14. DetailsPresentable protocol protocol DetailsPresentable { func showItemDetail(item: Item) func showUserDetail(user:

    User) } 14
  15. DetailsPresentable extension extension DetailsPresentable where Self: NavigationCoordinator { func showItemDetail(item:

    Item) { let itemDetailVC = StoryboardScene.ItemDetailViewController.initialViewController() itemDetailVC.item = item itemDetailVC.userTapped = showUserDetail navigationController.pushViewController(itemDetailVC, animated: true) } func showUserDetail(user: User) { // ... } } ※ VC→Coordinatorͷ௨৴͸ɺclosureΛհ͢ / delegate / Observable(FRP) ͳͲ 15
  16. FeedCoordinator final class FeedCoordinator: NavigationCoordinator, DetailsPresentable { let navigationController: UINavigationController

    init(presenter: UINavigationController) { self.navigationController = presenter presenter.title = "Feed" } func start() { let feedViewController = StoryboardScene.FeedViewController.initialViewController() feedViewController.itemSelected = showItemDetail navigationController.pushViewController(feedViewController, animated: false) } } 16
  17. Summary • Coordinatorͷ࡞੒ྫͷ঺հ • h+ps:/ /github.com/yoching/CoordinatorSample • Ͳ͏෼ׂ͢Δ͔ • ڞ௨ॲཧΛprotocol-orientedʹղܾ͢Δ

    17
  18. Thank you! 18

  19. ࢀߟ • ը໘ભҠͷ؅ཧͱMVVM • Presen)ng Coordinators by Soroush Khanlou, NSSpain(2015)

    • Boundaries in Prac)ce by Nonaka Ayaka, try!SwiH(2016) • MVVM-C In Prac)ce by Steve ScoM, UIKonf(2016) • Connec)ng View Controllers at SwiH Talk(objc.io), 2016 19