Trying to build an app navigation view that does programmatic push/pop animations with given views. The main animation happens in containedView()
where I show the new view and hide the current view with animation. The problem I have is that the animation happens instantly, so it doesn't happen at all.
I am curious how to debug this or if anyone has any recommendations on how to fix this?
enum AppNavigationState
{
case root
case push(currentView: AnyView, newView: AnyView)
case pop(currentView: AnyView, newView: AnyView)
}
class AppNavigationViewModel: ObservableObject {
public var navigationStack: [AnyView]
@Published public var navigationState: AppNavigationState
init(rootView: AnyView) {
navigationStack = [rootView]
navigationState = AppNavigationState.root
}
}
struct AppNavigationView: View {
@ObservedObject var appNavigationViewModel: AppNavigationViewModel
var body: some View {
containedView()
}
private func containedView() -> AnyView {
switch(appNavigationViewModel.navigationState) {
case .root:
return appNavigationViewModel.navigationStack.last!
case let .pop(currentView, newView):
return AnyView(ZStack {
newView
currentView
.transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
.animation(.easeOut)
})
case let .push(currentView, newView):
return AnyView(ZStack {
currentView
newView
.transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
.animation(.easeOut)
})
}
}
public func pushView(view:AnyView) {
if let currentView = appNavigationViewModel.navigationStack.last {
appNavigationViewModel.navigationStack.append(view)
appNavigationViewModel.navigationState = .push(currentView: currentView, newView: view)
}
}
public func popView() {
if (appNavigationViewModel.navigationStack.count > 1) {
let currentView = appNavigationViewModel.navigationStack.removeLast()
let newView = appNavigationViewModel.navigationStack.last!
appNavigationViewModel.navigationState = .push(currentView: currentView, newView: newView)
}
}
}