카테고리 없음

[SwiftUI] Navigator with NavigationStack, PopoupView

insub4067 2023. 11. 26. 12:58
import SwiftUI
import PopupView

struct ContentView: View {
    
    @StateObject var navigator = Navigator()
    
    var body: some View {
        NavigationStack {
            VStack {
                Button("show detail") {
                    navigator.route(NavigationItem.detail)
                }
                Button("show setting") {
                    navigator.route(NavigationItem.setting)
                }
                Button("show popup") {
                    navigator.route(PopupItem.default)
                }
                
                Button("show") {
                    navigator.route(BottomsheetItem.default)
                }
            }
            .navigationDestination(item: $navigator.navigationItem) { item in
                switch item {
                case .detail:
                    Color.gray
                case .setting:
                    Color.green
                }
            }
        }
        .popup(item: $navigator.popupItem) { destination in
            Color.white.frame(width: 200, height: 100)
        } customize: {
            $0
                .type(.toast)
                .position(.center)
                .backgroundColor(.gray.opacity(0.6))
                .closeOnTapOutside(true)
                .isOpaque(true)
        }
        .popup(item: $navigator.bottomsheetItem) { destination in
            Color.white.frame(height: 300)
        } customize: {
            $0
                .type(.toast)
                .position(.bottom)
                .backgroundColor(.gray.opacity(0.6))
                .closeOnTapOutside(true)
                .isOpaque(true)
        }
    }
}
import Foundation

@MainActor class Navigator: ObservableObject {
    
    @Published var navigationItem: NavigationItem?
    @Published var popupItem: PopupItem?
    @Published var bottomsheetItem: BottomsheetItem?
    
    func route(_ destination: Routable) {
        if let destination = destination as? NavigationItem  {
            self.navigationItem = destination
        } else if let destination = destination as? PopupItem {
            self.popupItem = destination
        } else if let destination = destination as? BottomsheetItem {
            self.bottomsheetItem = destination
        }
    }
}

protocol Routable { }

enum NavigationItem: Routable {
    case detail, setting
}

enum PopupItem: Routable {
    case `default`
}

enum BottomsheetItem: Routable {
    case `default`
}