Usando DynamicNavigationDestinationLink
28 Jun 2019Por causa de uma thread no Twitter, eu criei um pequeno gist com uma tentativa de simular o use de DynamicNavigationDestinationLink para mostrar um conteúdo carregado de uma fonte externa. Isso pode ser útil para quem está em busca da fonte de um crash.
import SwiftUI
import PlaygroundSupport
struct RootView: View {
var body: some View {
NavigationView {
MainView()
}
}
}
struct MainView: View {
let destination = DynamicNavigationDestinationLink(id: \String.self) { state in
SecondaryView(input: state)
}
@State var loading: Bool = false
var body: some View {
VStack {
Text(loading ? "Loading" : "Main View")
if !loading {
Button(action: self.navigate) {
Text("Random number")
}
}
}
}
func navigate() {
self.loading = true
let number = (1...100).randomElement() ?? 0
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.loading = false
self.destination.presentedData?.value = "\(number)"
}
}
}
struct SecondaryView: View {
@State var input: String
var body: some View {
Text(input)
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: RootView())
PlaygroundPage.current.needsIndefiniteExecution = true
P.s.: Esse código foi feito em um Playground porque eu prefiro essa ferramenta para executar testes/exemplos de SwiftUI no Mojave.