skip to Main Content

I could use some help. I am writing an app for non profit and I have come to a stand still. Have a list of meetings created and stored at meetings on firebase. Each meeting has sub-collections. I have it fixed so a user can select a meeting from a list and it presents a screen with that meeting info. Easy enough. I have buttons on the bottom of that screen that takes a user to sub-collection meetings. On those views its easy to pass the main meeting details. my problem is i am having a hard time figuring out how to pass my master meeting id ( selected in list ) into viewModel so i can easily search sub-collection of that meeting to display on the sub collection meeting views.

So..My database looks like this.

Meeting (meeting) collection

  • SubCollection1 (breakouts)
    *breakout1
    *breakout2
    *breakout3

  • SubCollection2 (tours)
    *tour1
    *tour2

Again, user selects a meeting from list.
goes to meeting detail screen. (i now have meetingId)
user selects button to go to "Subcollection1" (i still have meetingID)
(i just need to know how to pass that meetingID into viewModel so i can fetch Subcollection1 info.) :-/

Here i pass "meet" into next view. How can i inject "meet" into my viewModel to access its uid/id

                List{
                ForEach(viewModel.meetings) { meet in
                    ZStack {
                        NavigationLink(value: meet) {
                            EmptyView()
                        }.opacity(0.0)
                        
                        MeetingsRowView(meeting: meet, viewModel: viewModel)
                            .onAppear {
                                if meet == viewModel.meetings.last {
                                    print("DEBUG: Paginate here..")
                                }
                            }
                    }
                }.onAppear(perform: {
                    Task { try await viewModel.fetchAllMeetings()}
                })
            }

Here is the view I am trying to pull "meet" from

struct LongProductsView: View {
@StateObject var viewModel = MeetingViewModel()
@State private var showNewMeetingView = false
@State private var showMeeting = false
@Environment(.dismiss) var dismiss

var meet: Meeting
var body: some View {
    //  I can show the meeting selected details here in text.  I need to inject
    // this "meet" into viewModel to use as variable for firebase subcollection lookup.
    Text(meet.title)
    Text(meet.datestart)
        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                HStack{
                    Button(action: {
                        dismiss()
                    }, label: {
                        Image(systemName: "arrowshape.turn.up.backward")
                            .font(.footnote)
                            .foregroundColor(.blue)
                    }
                    ).padding(.vertical)
                }
            }
        }
}

}

Any help or references to similar issues would be much appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Well... I fixed my viewModel like this:

    Question is when i set breakouts to my found item, what do i put in "selectedMeeting( ?? ). *Meeting gives an error

    @Published var breakouts = [BreakOuts]()
    func lp() async throws{
        self.breakouts = try await selectedMeeting( Meeting)
    }
    
    func selectedMeeting(_ meet: Meeting) async throws -> [BreakOuts]{
        let snapshot = try await FirestoreConstants.MeetingsCollection.document(meet.uid).collection("LongProducts").getDocuments()
        return snapshot.documents.compactMap ({ try? $0.data(as: BreakOuts.self) })
    }
    

    Then in my view i am using:

        struct WednesdayView: View {
        @StateObject var viewModel = MeetingViewModel()
        @State private var showNewMeetingView = false
        @State private var showMeeting = false
        @Environment(.dismiss) var dismiss
        var meet: Meeting
        var lpmeet: BreakOuts
    //    var breakouts: BreakOuts
        var body: some View {
            NavigationStack{
                Text("Wednesday View")
                Text(viewModel.currentBreakout?.uid ?? "")
                Text(meet.datestart)
                Text(lpmeet.Agenda)
                    .toolbar{
                        ToolbarItem(placement: .navigationBarLeading) {
                            HStack{
                                Button(action: {
                                    dismiss()
                                }, label: {
                                    Image(systemName: "arrowshape.turn.up.backward")
                                        .font(.footnote)
                                        .foregroundColor(.blue)
                                }
                                ).padding(.vertical)
                            }
                        }
                    }
            }
               .onAppear {
                Task { try await viewModel.selectedMeeting(meet) }
    //            viewModel.selectedMeeting(meet) // <-- pass meet to the viewModel
             }
        }
    }
    

  2. You could try this approach, where in your MeetingViewModel you have a function such as, func selectedMeeting(_ meet: Meeting) {...} and a variable
    that you update.

    And in your LongProductsView you could add .onAppear { ... }.

    Alternatively, you can try to set the MeetingViewModel in the init() method. I prefer the .onAppear.

    For example:

    struct LongProductsView: View {
        //....
        var body: some View {
        //...
        .onAppear { 
            viewModel.selectedMeeting(meet) // <-- pass meet to the viewModel
         }
       }
    }
    

    And

    class MeetingViewModel: ObservableObject {
         //...
         var selected: Meeting?
    
         func selectedMeeting(_ meet: Meeting) {
            selected = meet
            // some other code to look up that meeting
            // and returning the sub-collection if you want
         } 
    }
    

    Depending on what you want to do, in your MeetingViewModel you could
    make a @Published var selected: Meeting? and use/set that directly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search