Swiftpack.co - UnrealGlitch/UG_Coordinator as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
UnrealGlitch/UG_Coordinator
MVVM + Coordinator
.package(url: "https://github.com/UnrealGlitch/UG_Coordinator.git", from: "1.1.0")

UG_Coordinator

MVVM + Coordinator

How to use

Create view controller


let settingsVm = SettingsViewModel(output: self)
let settingsVc = SettingsViewController(viewModel: settingsVm)

Create coordinator


final class AppCoordinator: BaseCoordinator {
    
    // MARK: - Private properties
    
    private weak var window: UIWindow?
    private let router: Router
    
    // MARK: - Life cycle
    
    init(window: UIWindow) {
        self.window = window
        let rootViewController = UINavigationController()
        
        window.rootViewController = rootViewController
        
        router = DefaultRouter(rootController: rootViewController)
        
        super.init()
    }
    
    // MARK: - Internal functions
    
    override func start() {
        startMainFlowCoordinator()
    }
    
}

// MARK: - Private

private extension AppCoordinator {
    
    func startMainFlowCoordinator() {
        let coordinator = MainFlowCoordinator(router: router)
        
        addDependency(coordinator)
        coordinator.start()
    }
    
}

Navigation


import Routing

final class MainFlowCoordinator: BaseCoordinator {
    
    // MARK: - Private properties
    
    private let router: Router?
    private var gameCoordinator: GameFlowCoordinator?
    private var settingsCoordinator: SettingsFlowCoordinator?
    
    // MARK: - Life cycle
    
    init(router: Router) {
        self.router = router
        
        super.init()
    }
    
    // MARK: - Internal
    
    override func start() {
        super.start()
        
        showMainScreen()
    }
    
}

// MARK: - Private

private extension MainFlowCoordinator {
    
    func showMainScreen() {
        let mainScreenVm = MainScreenViewModel(output: self)
        let mainScreenVc = MainScreenViewController(viewModel: mainScreenVm)
        router?.push(mainScreenVc, animated: true)
    }
    
}

// MARK: - MainScreenViewModelOutput

extension MainFlowCoordinator: MainScreenViewModelOutput {
    
    func onMainScreenPlayButtonTap(_ viewModel: MainScreenViewModel) {
        guard let router = router else {
            return
        }
        let gameCoordinator = GameFlowCoordinator(router: router)
        addDependency(gameCoordinator)
        
        gameCoordinator.flowCompletion = { [weak self] coordinator in
            guard let self = self, let router = self.router else {
                return
            }
            
            router.pop(animated: true)
            self.removeDependency(coordinator)
        }
        
        gameCoordinator.start()
        
        self.gameCoordinator = gameCoordinator
    }
    
    func onMainScreenSettingsButtonTap(_ viewModel: MainScreenViewModel) {
        guard let router = router else {
            return
        }
        let settingsCoordinator = SettingsFlowCoordinator(router: router, output: self)
        addDependency(settingsCoordinator)
        
        settingsCoordinator.flowCompletion = { [weak self] coordinator in
            guard let self = self, let router = self.router else {
                return
            }
            
            router.pop(animated: true)
            self.removeDependency(coordinator)
        }
        
        settingsCoordinator.start()
        
        self.settingsCoordinator = settingsCoordinator
    }
    
    func onMainScreenBackButtonTap(_ viewModel: MainScreenViewModel) {
        router?.pop(animated: true)
    }
    
}

// MARK: - SettingsFlowCoordinatorOutput

extension MainFlowCoordinator: SettingsFlowCoordinatorOutput {
    
    func onSettingsFinishFlow() {
        guard 
            let settingsCoordinator = settingsCoordinator, 
            let completion = settingsCoordinator.flowCompletion 
        else {
            return
        }
        
        completion(settingsCoordinator)
    }
    
}

GitHub

link
Stars: 0
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Add extension
3 weeks ago

Add extension of UIViewController

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics