I have a view that has a loading animation, basically when that call is done I want to automatically navigate to the following screen. Before with the isActive parameter I could create an empty view for the link and easily just toggle the bool value at the end of the call.
I have doneLoading being toggled so that once true, the app will navigate to ContentView since isPresented is being satisfied. However when I run the app and the final animation has ended, the view starts over at the beginning animation.
@State var show = false
@State var isDone: Bool = false
@State var doneLoading = false
@StateObject var viewModel: BumperScreenViewModel
@StateObject var sheetManager = SheetManager()
init(viewModel: @autoclosure @escaping () -> BumperScreenViewModel) {
self._viewModel = .init(wrappedValue: viewModel())
}
var body: some View {
NavigationStack {
VStack {
ZStack {
Color(.trulliGold)
.ignoresSafeArea()
VStack {
if show {
Spacer()
loadAnimation
.frame(width: 150, height: 150)
.task {
try? await viewModel.getDataFromAPI()
try? await Task.sleep(for: Duration.seconds(1))
doneLoading.toggle()
show.toggle()
print("Done")
}
Spacer()
} else {
launchAnimation
}
}
}
}
}
.navigationDestination(isPresented: $doneLoading) {
ContentView()
.environmentObject(sheetManager)
}
}
The only effective "Navigation" that I've been able to accomplish is calling up ContentView with the environment object as an additional if else so it looks more like this (everything else is the same, didn't want to add even more code):
doneLoading.toggle()
show.toggle()
print("Done")
}
Spacer()
} else if doneLoading {
ContentView()
.environmentObject(sheetManager)
} else {
launchAnimation
}
}
which is a hack that I am not proud of nor want in an app.