Having an issue in my NavigationStack when opening my child, it bounces back to the root view. I think it has to do when I'm fetching a user's current messages as when I remove the function from .onAppear it navigates fine.
I didn't attach the root view, but it would just consist of a navigationStack going to Messenger Inbox. I also again believe the issue is on the function side of things and not on the actual views ( I could be wrong, but not having any issues when the messengerLogic.fetchMessages is removed from the view).
Root View:
struct TabBar: View {
@StateObject private var messengerLogic = MessengerLogic()
init() {
UITabBar.appearance().backgroundColor = UIColor(Color("PrimaryGray"))
UITabBar.appearance().barTintColor = .black
UITabBar.appearance().unselectedItemTintColor = UIColor(Color("PrimaryBlue"))
}
var body: some View {
TabView {
// Tabs for influencers
private var influencerTabs: [Tab] {
[
Tab(view: AnyView(InfluencerMainProfile().environmentObject(influencerUserModel)), iconName: "person.circle", title: "Profile"), // << view its bouncing back too
AnyView(MessengerInbox().environmentObject(messengerLogic)), iconName: "message", title: "Messages")
]
}
struct MessengerInbox: View {
@EnvironmentObject var messengerLogic: MessengerLogic
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 1) { // Padding between rows
ForEach(messengerLogic.recentMessages) { chat in
InboxRow(userId: chat.toId , userName: chat.businessName , recentMessage: chat.text , timestamp: chat.timestamp , businessProfilePicture: chat.profileImageUrl )
.environmentObject(messengerLogic)
}
.padding(.top)
}
}
Messenger View
struct MessengerMainView: View {
@EnvironmentObject var messengerLogic: MessengerLogic
var userId: String // << recipient of message
var businessName: String
var businessProfilePicture: String
@State private var newMessage: String = ""
var body: some View {
ScrollView {
VStack {
ForEach(messengerLogic.chatMessages){ message in
HStack(alignment: .top, spacing: 12) {
Image(systemName: message.profileImage )
.resizable()
VStack(alignment: .leading, spacing: 4) {
Text(message.senderName)
.font(.headline)
.foregroundColor(.primary)
Text(message.messageText)
.font(.body)
.foregroundColor(.black)
.padding(10)
}
Spacer()
}
.padding(.horizontal)
}
.padding(.top)
}
.navigationViewStyle(.stack)
.navigationTitle("Chatting with \(businessName)")
.navigationBarTitleDisplayMode(.inline)
.onAppear{
messengerLogic.fetchMessages(toId: userId)
} } }
And here is my messenger logic function:
class MessengerLogic: ObservableObject {
@Published var chatText = ""
@Published var count = 0
@Published var errorMessage = ""
@Published var chatMessages = [Message]() // << message class
@Published var recentMessages = [RecentMessage]()
func fetchMessages(toId: String) {
guard let fromId = FirebaseManager.shared.auth.currentUser?.uid else { return }
FirebaseManager.shared.firestore
.collection("messages")
.document(fromId)
.collection(toId)
.order(by: "timestamp")
.addSnapshotListener { querySnapshot, error in
if let error = error {
DispatchQueue.main.async {
self.errorMessage = "Failed to fetch messages: \(error)"
}
return
}
DispatchQueue.main.async {
self.chatMessages.removeAll() // Ensure the list resets to avoid duplicates
querySnapshot?.documents.forEach { document in
if let message = Message(documentId: document.documentID, data: document.data()) {
self.chatMessages.append(message)
print(message)
}
}
print("Updated messages: \(self.chatMessages.count)")
}
}
}
}