struct MyRoutesSectionViewObject: Hashable {
var title: String
var iconString: String
var count: Int
var badgeIconString: String?
}
protocol MyRoutesSectionViewControllerRepresentable {
var myRoutesSectionViewObject: MyRoutesSectionViewObject { get set }
}
enum Destination: CaseIterable, Hashable {
case savedRoutes
case plannedRoutes
case recordedRoutes
case offlineRoutes
}
// This obviously throws a compile error
struct MyRoutesSectionView: View {
var body: some View {
ForEach(Destination.allCases, id: \.self) { destination in
if let destinationView = self.destinationView(for: destination) as? MyRoutesSectionViewControllerRepresentable {
NavigationLink(destination: destinationView) {
MyRoutesNavigationLinkView(myRoutesObject: destinationView.myRoutesObject)
}
} else {
assert(false, "Destination view needs to conform to `MyRoutesSectionViewControllerRepresentable`")
return AnyView(EmptyView())
}
}
}
}
private extension MyRoutesSectionView {
@ViewBuilder
func destinationView(for destination: Destination) -> some View {
switch destination {
case .savedRoutes:
BMSavedRoutesView()
case .plannedRoutes:
BMPlannedRoutesView()
case .recordedRoutes:
BMRecordedRoutesView()
case .offlineRoutes:
BMOfflineRoutesView()
}
}
}
First, I don’t know what you’re representing, with count, so can’t give targeted advice.
But a struct instance is not an object; “object” specifically means “class instance”. Maybe that’s the right solution: have both the struct and class hold a reference to it.
If not, then a Coordinator could work. You’ll need to use the delegate pattern for it. I never thought the delegate pattern was modern, so Bindings or Combine would be my preference.