Giter VIP home page Giter VIP logo

swiftuicoordinator's Introduction

SwiftUICoordinator

Swift Version License

A Reusable Coordinator for pure SwiftUI project. No more need for using UIKit for coordinators to separate navigation logic from scenes. All you need to do is to create a subclass of a desired coordinator type (SwiftUICoordinator, NavigationSwiftUICoordinator, TabSwiftUICoordinator), provide a scene in a form of SwiftUI's View and coordinate to other coordinators.

Requirements

  • XCode 14 and above
  • iOS 16 and above
  • Swift 5.7 and above
  • Swift Package Manager

Installation

You can either download ZIP and add the package locally or use SPM.

SPM

  • Go to your project > Package Dependencies
  • Tap add
  • Paste the repo link
  • Choose a desired version/branch/commit. (e.g., Up to next major 2.0.0-beta.2)

Or you can do it inside your Package.swift dependencies array. e.g.

dependencies: [
    .package(url: "[email protected]:kakhaberikiknadze/SwiftUICoordinator.git", from: "2.0.0-beta.2")
]

Example

Creating a specific coordinator is as easy as just subclassing SwiftUICoordinator

final class HomeCoordinator: SwiftUICoordinator<Void> {
    override init(
        id: String
    ) {
        super.init(id: id)
        tabItem = .init(title: "Home", image: .local(.system("house"))) // Tab item used in case it's inside tab view.
    }
    
    override func createScene() -> AnyView {
        let viewModel = HomeViewModel(coordinator: self)
        return AnyView(HomeScene(viewModel: viewModel))
    }
    
    func openAccount() -> AnyPublisher<String, Never> {
        let coordinator = AccountCoordinator(id: "[ACCOUNT_COORDINATOR " + UUID().uuidString + "]")
        return coordinate(to: coordinator, presentationStyle: .custom(.scale))
    }
}

If you need to return a non-Void result, you can change it to a desired type. e.g., String. Result is sent using a publisher which you can subscribe and handle it once coordinator is dismissed. Also, if the coordinator is inside navigation stack, you can use navigationRouter: NavigationPushing to push the coordinator instead of modally presenting it. navigationRouter will be nil if it's not inside navigation, so you can fallback to a modal presentation in this case.

final class AccountCoordinator: SwiftUICoordinator<String> {
    override func createScene() -> AnyView {
        let viewModel = AccountViewModel(coordinator: self)
        return AnyView(AccountScene(viewModel: viewModel))
    }
    
    func dismiss(result: String) {
        finish(result: result)
    }
    
    func openChat() -> AnyPublisher<Void, Never> {
        if let router = navigationRouter {
            let coordinator = ChatCoordinator(id: "[CHAT_COORDINATOR " + UUID().uuidString + "]")
            return router.push(coordinator)
        } else {
            let coordinator = ChatCoordinator(id: "[CHAT_COORDINATOR " + UUID().uuidString + "]")
            return coordinate(to: coordinator)
        }
    }
}

Navigation Coordinator

To create a navigation coordinator all you need to do is to subclass NavigationSwiftUICoordinator instead of SwiftUICoordinator and your scene will automatically be wrapped inside NavigationStack. You won't need to push through navigationRouter in this case as your coordinator is a navigation router itself. So you can simply call push method.

final class HomeCoordinator: NavigationSwiftUICoordinator<Void> {
    override init(
        id: String
    ) {
        super.init(id: id)
        tabItem = .init(title: "Home", image: .local(.system("house"))) // Tab item used in case it's inside tab view.
    }
    
    override func createScene() -> AnyView {
        let viewModel = HomeViewModel(coordinator: self)
        return AnyView(HomeScene(viewModel: viewModel))
    }
    
    func openAccount() -> AnyPublisher<String, Never> {
        let coordinator = AccountCoordinator(id: "[ACCOUNT_COORDINATOR " + UUID().uuidString + "]")
        return push(coordinator)
    }
}

Tab coordinator

To create a tabbed experience you need to subclass TabSwiftUICoordinator and provide other coordinators as tabs.

final class MainCoordinator: TabSwiftUICoordinator<String> {
    let homeCoordinator = HomeCoordinator(id: "HOME_COORDINATOR")
    
    let chatCoordinator = ChatCoordinator(id: "CHAT_COORDINATOR")
    
    init(id: String) {
        super.init(id: id)
        setTabs([homeCoordinator, chatCoordinator])
    }
}

Start initial coordinator

Starting initial coordinator is also pretty straightforward.

import SwiftUI

@main
struct MyApp: App {
    private let rootScene = MainCoordinator(
        id: "[MAIN_COORDINATOR " + UUID().uuidString + "]"
    ).getRoot()
    
    var body: some Scene {
        WindowGroup {
            rootScene
        }
    }
}

Meta

Kakhaberi Kiknadze โ€“ @Kakhi26 - swiftlyblogging.com

Distributed under the MIT license. See LICENSE for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.