GithubHelp home page GithubHelp logo

coordinatorswiftui's Introduction

Coordinator SwiftUI

Simple coordinator with router

Navigation types

  • Change tab TabView
  • Navigation by NavigationPath
  • Present sheet and with dynamically height sheet
  • Present full screen cover
  • Alert with error
  • Route from widget through coordinator

Router

protocol NavigationRouter: Hashable, Identifiable {
associatedtype V: View
var style: TransitionStyle? { get }
@ViewBuilder
func view() -> V
}

Coordinator

class Coordinator<Router: NavigationRouter>: ObservableObject {
@Published var path = NavigationPath()
@Published var sheet: Router?
@Published var cover: Router?
@Published var hasError: Bool = false
var error: LocalizedError?
func push(_ page: Router) {
path.append(page)
}
func pop() {
path.removeLast()
}
func popToRoot() {
path.removeLast(path.count)
}
func present(sheet: Router) {
self.sheet = sheet
}
func dismissSheet() {
sheet = nil
}
func present(cover: Router) {
self.cover = cover
}
func dismissCover() {
cover = nil
}
func presentAlert(error: LocalizedError) {
self.error = error
hasError = true
}
@ViewBuilder
func build(_ route: Router) -> some View {
route.view()
}
}

TabCoordinator

class TabCoordinator<Router: NavigationRouter>: ObservableObject {
@Published var tab: Router
init(tab: Router) {
self.tab = tab
}
func change(_ route: Router) {
self.tab = route
}
@ViewBuilder
func build(_ route: Router) -> some View {
route.view()
.tag(route)
}
}

Example

Navigation in App Navigation from Widget
CoordinatorDemonstration.mp4
Widget.route.mov

Coordinator

struct FruitsCoordinatorView: View {
@StateObject var coordinator: Coordinator<FruitsRouter>
var body: some View {
NavigationStack(path: $coordinator.path) {
coordinator.build(.fruits)
.navigationTitle("Fruits ๐Ÿ›’")
.navigationDestination(for: FruitsRouter.self) { route in
coordinator.build(route)
}
.sheet(item: $coordinator.sheet) { route in
coordinator.build(route)
}
.fullScreenCover(item: $coordinator.cover) { route in
coordinator.build(route)
}
}
.environmentObject(coordinator)
}
}

Router

enum FruitsRouter: NavigationRouter {
case fruits
case cherry(count: Int)
case lemon(count: Int)
case watermelon(count: Int)
var id: Self {
return self
}
var style: TransitionStyle? {
return nil
}
@ViewBuilder
func view() -> some View {
switch self {
case .fruits:
FruitsView()
case .cherry(let count):
CherryView(count: count)
case .lemon(let count):
LemonView(count: count)
case .watermelon(let count):
WatermelonView(count: count)
}
}
}

Used materials

coordinatorswiftui's People

Contributors

dendmitriev avatar

Stargazers

 avatar

Watchers

 avatar

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.