简体   繁体   中英

Display custom information according to NavigationLink Text Swift, Cloud Firestore

I have this code to get fields "spty" with autoID from collection "specialities":

struct Spty: Identifiable{
var id: String = UUID().uuidString
var spty: String

}

 class SptyViewModel: NSObject, ObservableObject{


@Published var specialities = [Spty]()

func fetchData(){
    let db = Firestore.firestore()
    db.collection("specialities").addSnapshotListener { (querySnapshot, error) in
       guard let documents = querySnapshot?.documents else {return }
        self.specialities = documents.map { (queryDocumentSnapshot) -> Spty in
          let data = queryDocumentSnapshot.data()
            
            let spty = data["spty"] as? String ?? ""
            
            return Spty(spty: spty)
        }
    }
}
}

They are displayed by using ForEach:

    @StateObject var sptyModel = SptyViewModel()


    ForEach(sptyModel.specialities){ spty in

           NavigationLink(destination: More()) {
                    
               Text(spty.spty).foregroundColor(.black)
                       

As in the code, each Text(spty.spty) is a NavigationLink, so what I want is to display different information according to the Text that I pressed.

Im currently working on this getDocuments:

 struct Testtt: Identifiable{

   var id: String = UUID().uuidString
   var name: String

 }


class TesteViewModel: NSObject,ObservableObject{

   @StateObject var LocationModel = LocationViewModel()
   let db = Firestore.firestore()
   @Published var testsss = [Testtt]()
   @Published var sptyModel = SptyViewModel()
   @Published var SptyNameee = [Spty]()


  func testeApp(){

    db.collection("Test").whereField("Los Angeles", isEqualTo: true).getDocuments { (querySnapshot, err) in
        guard let documents = querySnapshot?.documents else {return }
        self.testsss = documents.map { (queryDocumentSnapshot) -> Testtt in
           let data = queryDocumentSnapshot.data()
            
            let name = data["Name"] as? String ?? ""
            
            return Testtt(name: name)
        }
    }
    
}

}
  • ignore .whereField

What I've been thinking is that to answer this question I need to relate collection() in testeApp() with spty.spty , but didn't managed to do it.

Any ideas?

EDIT:

图 1 2 图 3

[![2 ][4]][4]

To clarify my question I added screenshots on how Im currently managing the data:

I get 3 NavLinks in Home View accordion to collection "specialities" and the code given above: Cardiologista , Cirurgião Geral and Oftalmoloista

Then, if I press any of the NavLinks, I got to More View

There, what I want is: if the user presses Cardiologista , the name "Mateus Neves" will appear for him. But, I he presses Oftalmologista , the name "Vitor Souza" will appear.

I am going to attempt address a comment in the question first, which may lead to the answer.

If you click “Top Paid Apps”, X apps will be displayed. If you click on “Top Free Apps”, Y apps will be displayed

Here's a structure that supports that UI

allApps (a collection)
   app_0 (a document)
     app_type = "Free"
   app_1
     app_type = "Paid"

As you can see, if a user clicks on Free apps, a query is run against Firestore for all documents where app_type isEqual(to: "Free")

So then let's expand on that. Lets take specialty types and store them in a collection - this would be the list presented to the user that can select from

specialties
   type_0
      name: "Cardiologist"
   type_1
      name: "General"
   type_2
      name: "Anesthesiologist"

then the doctors would be stored in a separate collection

doctors
   doctor_0
      name: "Dr. Smith"
      type: "type_0"
   doctor_1
      name: "Dr. Jones"
      type: "type_1"
   doctor_2
      name: "Dr. Jenkins"
      type: "type_2"

So when the user clicks on Cardiologist in the list, you know that's type_0 and then query the doctors node for all documents where type isEqual(to: "type_0") which returns Dr. Smith.

Note

Make sure you map the documents into an object in code that will hold both the type (the documentId) as the name so you can use that as a tableView datasouce.

class MyType {
   var type = "" //the firestore documentId
   var name = ""
}

var myTableViewDataSourceArrayOfTypes = [MyType]() //the tableView datasource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM