Skip to content
Snippets Groups Projects
SearchCoordinator.swift 3.15 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
import UIKit
import Models
import XXModels
Bruno Muniz's avatar
Bruno Muniz committed
import Countries
import Presentation
import ScrollViewController
Bruno Muniz's avatar
Bruno Muniz committed

public protocol SearchCoordinating {
    func toRequests(from: UIViewController)
    func toContacts(from: UIViewController)
Bruno Muniz's avatar
Bruno Muniz committed
    func toContact(_: Contact, from: UIViewController)
    func toDrawer(_: UIViewController, from: UIViewController)
    func toNicknameDrawer(_: UIViewController, from: UIViewController)
    func toCountries(from: UIViewController, _: @escaping (Country) -> Void)
Bruno Muniz's avatar
Bruno Muniz committed
}

public struct SearchCoordinator {
    var pushPresenter: Presenting = PushPresenter()
Bruno Muniz's avatar
Bruno Muniz committed
    var bottomPresenter: Presenting = BottomPresenter()
    var replacePresenter: Presenting = ReplacePresenter()
    var fullscreenPresenter: Presenting = FullscreenPresenter()
Bruno Muniz's avatar
Bruno Muniz committed

    var contactsFactory: () -> UIViewController
    var requestsFactory: () -> UIViewController
Bruno Muniz's avatar
Bruno Muniz committed
    var contactFactory: (Contact) -> UIViewController
    var countriesFactory: (@escaping (Country) -> Void) -> UIViewController

    public init(
        contactsFactory: @escaping () -> UIViewController,
        requestsFactory: @escaping () -> UIViewController,
        contactFactory: @escaping (Contact) -> UIViewController,
        countriesFactory: @escaping (@escaping (Country) -> Void) -> UIViewController
    ) {
        self.contactFactory = contactFactory
        self.contactsFactory = contactsFactory
        self.requestsFactory = requestsFactory
        self.countriesFactory = countriesFactory
    }
Bruno Muniz's avatar
Bruno Muniz committed
}

extension SearchCoordinator: SearchCoordinating {
    public func toRequests(from parent: UIViewController) {
        let screen = requestsFactory()
        replacePresenter.present(screen, from: parent)
    }

    public func toContacts(from parent: UIViewController) {
        let screen = contactsFactory()
        replacePresenter.present(screen, from: parent)
    }

    public func toContact(_ contact: Contact, from parent: UIViewController) {
Bruno Muniz's avatar
Bruno Muniz committed
        let screen = contactFactory(contact)
        pushPresenter.present(screen, from: parent)
Bruno Muniz's avatar
Bruno Muniz committed
    }

    public func toDrawer(_ drawer: UIViewController, from parent: UIViewController) {
        bottomPresenter.present(drawer, from: parent)
Bruno Muniz's avatar
Bruno Muniz committed
    }

    public func toCountries(from parent: UIViewController, _ onChoose: @escaping (Country) -> Void) {
        let screen = countriesFactory(onChoose)
        pushPresenter.present(screen, from: parent)
Bruno Muniz's avatar
Bruno Muniz committed
    }

    public func toNicknameDrawer(_ target: UIViewController, from parent: UIViewController) {
        let screen = ScrollViewController.embedding(target)
        fullscreenPresenter.present(screen, from: parent)
    }
}

extension ScrollViewController {
    static func embedding(_ viewController: UIViewController) -> ScrollViewController {
        let scrollViewController = ScrollViewController()
        scrollViewController.addChild(viewController)
        scrollViewController.contentView = viewController.view
        scrollViewController.wrapperView.handlesTouchesOutsideContent = false
        scrollViewController.wrapperView.alignContentToBottom = true
        scrollViewController.scrollView.bounces = false

        viewController.didMove(toParent: scrollViewController)
        return scrollViewController
    }
Bruno Muniz's avatar
Bruno Muniz committed
}