Skip to content

Repository files navigation

What each piece is doing (and why) Coordinator (protocol): a tiny interface so all coordinators expose start() and hold a UINavigationController. That’s it.

BaseCoordinator (optional): just avoids copy-pasting child-coordinator bookkeeping (childCoordinators, add/remove). You can delete it if you don’t need multiple nested flows.

AuthCoordinator.onFinish: this is the output of the auth flow. The parent (AppCoordinator) needs to know β€œlogin succeeded/cancelled” to switch to Main. We use a callback so Auth doesn’t call parent APIs directly (decoupling + easy to unit test).

Bridge: LoginViewController.onLogin β†’ AuthCoordinator.onFinish(.success) β†’ AppCoordinator swaps to Main.

MainCoordinator.onLogout: the mirror of the above. β€œUser wants out,” bubble that up to the parent so it can swap back to Auth.

Bridge: HomeViewController.onLogout β†’ MainCoordinator.onLogout() β†’ AppCoordinator shows Auth.

Think of VC closures as intent signals (β€œuser tapped X”), and coordinator closures as flow outputs (β€œthis flow is done”). That separation keeps VCs dumb and flows testable.

Ref: https://chatgpt.com/s/t_68dc6754197c819180ce346f11515086


  1.   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚  Coordinator  β”‚  (protocol)
      β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
     conforms β”‚
              β–Ό
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚  BaseCoordinator β”‚  (concrete, child mgmt)
      β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”˜
          β”‚         β”‚
          β”‚         β”‚
    

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β” β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚AppCoordinatorβ”‚ β”‚AuthCoordinatorβ”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚MainCoordinatorβ”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

SceneDelegate β”‚ creates β–Ό AppCoordinator (root) β”‚ holds strong refs to child coordinators β”œβ”€β”€ AuthCoordinator ── presents β†’ LoginViewController └── MainCoordinator ── shows β†’ HomeViewController β”œβ”€ push β†’ DetailsViewController └─ push β†’ SettingsViewController

[App/Scene launch] └─> SceneDelegate makes UIWindow + UINavigationController └─> creates AppCoordinator(nav), calls start() └─> decides by auth state:

             if NOT authenticated:
                AppCoordinator β†’ starts AuthCoordinator
                AuthCoordinator β†’ shows LoginViewController  ← (start screen)

             if authenticated:
                AppCoordinator β†’ starts MainCoordinator
                MainCoordinator β†’ shows HomeViewController   ← (start screen)

User taps "Log In" on LoginViewController └─ LoginVC calls onLogin?() └─ AuthCoordinator invokes onFinish(.success) └─ AppCoordinator removes Auth, starts Main └─ MainCoordinator shows HomeViewController

"Details" button β†’ HomeVC.onDetails?() β†’ MainCoordinator.showDetails() β†’ push DetailsVC "Settings" button β†’ HomeVC.onSettings?() β†’ MainCoordinator.showSettings() β†’ push SettingsVC "Log out" button β†’ HomeVC.onLogout?() β†’ MainCoordinator.onLogout?() β†’ AppCoordinator shows Auth again

Ref: https://chatgpt.com/s/t_68dc5be2dc248191aeea8d9c5bd57864

About

Coordinator Pattern in Swift Learning and Implementation

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages