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
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
Then in my view i am using:
You could try this approach, where in your
MeetingViewModel
you have a function such as,func selectedMeeting(_ meet: Meeting) {...}
and a variablethat you update.
And in your
LongProductsView
you could add.onAppear { ... }
.Alternatively, you can try to set the
MeetingViewModel
in theinit()
method. I prefer the.onAppear
.For example:
And
Depending on what you want to do, in your
MeetingViewModel
you couldmake a
@Published var selected: Meeting?
and use/set that directly.