Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LeonidKokhnovich/4933e43a1bc96985047a1c8b6a9cdad3 to your computer and use it in GitHub Desktop.
Save LeonidKokhnovich/4933e43a1bc96985047a1c8b6a9cdad3 to your computer and use it in GitHub Desktop.
ReThinkingCoordinators-RefactorToCoordinators
class NewsFeedCoordinator {
// #1
func start(presenter: UIViewController) {
let newsFeedVC = NewsfeedViewController()
newsFeedVC.onCreateNewPostOptionSelected = { [weak newsFeedVC] in
guard let newsFeedVC = newsFeedVC else { return }
// #2
self.showCreateNewPostFlow(presenter: newsFeedVC)
}
presenter.present(newsFeedVC, animated: true, completion: nil)
}
private func showCreateNewPostFlow(presenter: UIViewController) {
let coordinator = CreateNewPostCoordinator()
coordinator.start(presenter: presenter)
}
// …
}
class CreateNewPostCoordinator {
// #3
func start(presenter: UIViewController) {
let createNewPostVC = CreateNewPostViewController()
createNewPostVC.onEditPostContentSettings = { [weak createNewPostVC] contentSettings in
guard let createNewPostVC = createNewPostVC else { return }
// #4
self.presentEditPostContentSettings(presenter: createNewPostVC, contentSettings: contentSettings)
}
presenter.present(createNewPostVC, animated: true, completion: nil)
}
private func presentEditPostContentSettings(presenter: UIViewController, contentSettings: PostContentSettings) {
let editPostContentSettingsVC = EditPostContentSettingsViewController()
editPostContentSettingsVC.contentSettings = contentSettings
presenter.present(editPostContentSettingsVC, animated: true, completion: nil)
}
// …
}
class NewsfeedViewController: UIViewController {
// …
var onCreateNewPostOptionSelected: (() -> Void)?
// …
}
// …
class CreateNewPostViewController: UIViewController {
// …
var contentSettings = PostContentSettings(expiryDate: nil, isPublic: true)
var onEditPostContentSettings: ((PostContentSettings) -> Void)?
// …
}
// …
class EditPostContentSettingsViewController: UIViewController {
// …
var contentSettings: PostContentSettings?
// …
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment