rundfunk47 / stinsen

Coordinators in SwiftUI. Simple, powerful and elegant.
MIT License
820 stars 87 forks source link

SwiftUI Previews #70

Open j-cheung opened 2 years ago

j-cheung commented 2 years ago

Hi,

I've been searching through all the examples and haven't been able to find one which shows how we can use SwiftUI Previews with this? Or is it not possible?

Any help is appreciated, thanks!

Thanks.

rundfunk47 commented 2 years ago

Hi! What exactly do you wish to accomplish with SwiftUI previews? Do you wish to be able to navigate through a preview just as you should with an app?

There shouldn't be any issues using previews...

j-cheung commented 2 years ago

Hi!

Not much, i just want to be able to use preview as normal, not really needing to navigate.

It's telling me the following:

No ObservableObject of type NavigationRouter<AuthenticatedCoordinator> found. A View.environmentObject(_:) for NavigationRouter<AuthenticatedCoordinator> may be missing as an ancestor of this view.

----------------------------------------

CrashReportError: `fatalError` in EnvironmentObject.swift

(my app) crashed due to fatalError in EnvironmentObject.swift at line 70.

No ObservableObject of type NavigationRouter<AuthenticatedCoordinator> found. A View.environmentObject(_:) for NavigationRouter<AuthenticatedCoordinator> may be missing as an ancestor of this view.

because I don't know what i should pass in .environmentObject() for the preview.

this is the example which failed, i suspect it is because of the variable index which is trying to access authenticatedRouter.coordinator.tabIndex

import SwiftUI

struct ProcessingScreen: View {
    @EnvironmentObject var authenticatedRouter: AuthenticatedCoordinator.Router
    var index: Int {
        authenticatedRouter.coordinator.tabIndex
    }
    var body: some View {
        VStack {
            NavigationHeader(
                title:"處理中",
                subtitle: "\(index)",
                foregroundColor : .white,
                backgroundColor: .blue)
        }
    }
}

struct ProcessingScreen_Previews: PreviewProvider {
    static var previews: some View {
        ProcessingScreen()
.previewInterfaceOrientation(.landscapeLeft)
    }
}

and this works

import SwiftUI

struct ProcessingScreen: View {
    @EnvironmentObject var authenticatedRouter: AuthenticatedCoordinator.Router
//    @EnvironmentObject var store: AutomateProStore
    var body: some View {
        VStack {
            NavigationHeader(
                title:"處理中",
                subtitle: "hihi",
//                subtitle: "\(store.state.orderState.arrivingOrderIds.count)",
                foregroundColor : .white,
                backgroundColor: .blue)
//            HorizontalOrderList(orders:store.state.orderState.arrivingOrders)
        }
    }
}

struct ProcessingScreen_Previews: PreviewProvider {
    static var previews: some View {
        ProcessingScreen()
.previewInterfaceOrientation(.landscapeLeft)
    }
}

thanks!