How to retrieve values set in Hashable paths when using a router to navigate through views

I am using an enum as Hashable and a Router as ObservableObject to navigate through views. I send an id as parameter for the Form path and I would like to retrieve this is id in the Form view

enum Paths: Hashable{

    // An id is sent as parameter to be retrieved in the view
    case Form(id: String)
}

final class Router<T: Hashable>: ObservableObject {
    
    @Published var paths: [T] = []
    
    func push(_ path: T){
        paths.append(path)
    }
}

When I go from any view to the Form view, I would like to retrieve the value of the id sent in the Form path.

For example, I may have in any view a button that make the user go to the Form view

Button {
   router.push(.Form(id:"foo"))
}

Then, in the Form view I want to retrieve the id set when clicking on the button

struct FormView: View{

    @EnvironmentObject var router: Router<Paths>


    var body: some View{
        
        
      VStack{
            Text("\(id)") <--- How can I retrieve the id sent in the Form path?
      }
   }
}

How can I do to retrieve the value of "id"? I know I could set additional @Published fields in the router and retrieve these fields in the Form view but I think that would bloat the router and it seems more elegant to me to just set the views parameters in the paths that lead to each one of the views.

Many thanks for your insights on this

You will need to extract the id property from the Form case value using Swift's pattern matching capabilities. Here is an example of how you could achieve this:

// Add a computed property to the view
var formID: String {
    if case .Form(let id) = router.paths.last {
        id
    } else {
        "No ID" // handle the case where the ID couldn't be extracted
    }
}

// Use this id property as a string
Text(formID)

Thank you. Don't you think Paths will end up bloated if I have like 10 paths and each of them require an id to be passed into the view?

How to retrieve values set in Hashable paths when using a router to navigate through views
 
 
Q