Koen's version was really helpful, I modified it to represent a working version of my code.
For my application I had the following problem: When the user logged in the home version of the videos tab was shown, but when pressing videos again, the user had to go to the videos version of that tab. And home version had a navigation in the sidemenu.
Koen's code can be shortened a bit, but it worked like a charm and I thank you for it.
struct MainView: View {
@State private var selection = 0
@Binding var shouldShowHome: Bool
@State private var resetNavigationID = UUID()
/** Navigate to tab view **/
func navigate() -> some View {
VStack {
switch selection {
case 0:
FolderVideoView(shouldShowHome: $shouldShowHome).id(self.resetNavigationID)
case 1:
ChatView()
case 2:
DocumentListView()
case 3:
NewsView()
case 4:
EventCalendarView()
default:
EmptyView()
}
}
.onAppear {
print("Navigating to \(selection)")
}
}
var body: some View {
// Proxy binding to catch taps on selected tabs
let selectable = Binding(
get: { self.selection },
set: { self.selection = $0
// If recreating navigation ID, the tab can be relected, see tabContent()
self.resetNavigationID = UUID()
})
TabView(selection: selectable) {
if GlobalData.sharedGlobal.role == "newuser" {
navigate()
.tabItem {
Image("Chat")
}
.badge(chatViewModel.unreadMessagesCount)
.tag(1)
navigate()
.tabItem {
Image("Video")
}
.tag(0)
} else {
navigate()
.tabItem {
Image("Video")
}
.tag(0)
navigate()
.tabItem {
Image("Chat")
}
.badge(chatViewModel.unreadMessagesCount)
.tag(1)
}
navigate()
.tabItem {
Image("Document")
}
.tag(2)
navigate()
.tabItem {
Image("News")
}
.tag(3)
navigate()
.tabItem {
Image("Event")
}
.tag(4)
}
}
}