Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavigationStack stops functioning once I open a fullScreenCover #49

Closed
ch05enOne opened this issue Apr 7, 2021 · 9 comments
Closed

Comments

@ch05enOne
Copy link

I have this chat tab on my app which uses a fullScreenCover. The problem is, after I open that fullScreenCover, all the Push functions of the navigationStack stop working. They animate but done push to anything, its like it is sliding itself on top of it.

@ch05enOne
Copy link
Author

I tried to stop using fullScreenCover and just using NavigationStack to display the messages but then another issue appeared. It seems that If i aim getting any environmentObject from swift, it will do the same thing. Slightly slide to the right and come back.

@matteopuc
Copy link
Owner

Hi @ch05enOne would you be able to provide me with a small example of the issue? So that I can copy-paste it on Xcode and try to debug and solve it. Thanks.
Matteo

@ch05enOne
Copy link
Author

ch05enOne commented Apr 7, 2021

Sure @matteopuc. Just to give you a better prespective, I wrote an issue on here under the name ch05en when I tried to log out the user. You quickly solved that for me. Thanks! This problem has to Do with me using snapshotListeners from Firebase Firestore. Once i log out the app, I want to remove all the snapshotListeners. This isn't a problem though. I have figured this part out. This is the manager where I add and remove the snapshotListeners.

`class StatusManager: ObservableObject{

@Published var listeners: [ListenerRegistration?] = []

func logOut(){
    
    for i in listeners{

        if i != nil{
            print(listeners.count)
            
            i?.remove()
        }
        else{
            print("Could not remove listener")
        }
    }
    
    listeners.removeAll()
    
    try? Auth.auth().signOut()
}

}`

I pass this as an environment object for all the views to use in my App.swift file. The basic functionality is that once I initialize the snapshotListener, I append it to the listeners array.

This is the View where I list all the contacts. I use a button to toggle a boolean variable. I use that bool for the PushView.

`
var body: some View {

    VStack(spacing:0){
        
        TopBar(profileTrigger: $profTrig)

    List{
        ForEach(driversArray, id: \.self) { driver in
               
            Button {
                showMessages.toggle()
            } label: {
                AdminProfileCell(email: $email, driverEmail: driver)
            }

            **PushView(destination: MessageView(email: $email, driver: driver), isActive: $showMessages) {
                
            }**

        }
    }
}
    .accentColor(.black)
    .edgesIgnoringSafeArea(.all)
    

}

`

This is the MessageView, the view that is causing the error. Once I click on the button which pushes to this view, It actually triggers the onAppear() method but comes back right away.

`struct MessageView{

@EnvironmentObject var statusManager: StatusManager
@EnvironmentObject private var navStack: NavigationStack

@Binding var email: String
@State var messagesArary: [MessageComp] = []
@State var padding: CGFloat = 0
@State var paddingDirection: Edge.Set = .trailing
@State var viewIsActive: Bool = false // Use this to solve a notification issue. not relevant
@State var messageGetterListener: ListenerRegistration? // snapshotListener
@State var isReadListener: ListenerRegistration? // snapshotListener
@State var firstInstance: Bool = true // Use this so I dont append the listeners array everytime the view appears. I initialize the listeners in onAppear()

var body: some View{

VStack{

            HStack{

                Button {
                    self.navStack.pop()
                } label: {
                    Image(systemName: "arrow.left")
                        .foregroundColor(.white)
                        .frame(width: 48, height: 48)
                }
                Spacer()
            }.padding(.horizontal)

ScrollView{

ForEach(messagesArray, id: .self){ message in

//Displays all messages

           }
      }
 }        .onDisappear(perform: {
        viewIsActive.toggle()
        firstInstance = false
    })
    .onAppear(perform: {

        viewIsActive.toggle()
        
        
        if firstInstance{

// Initialize listener
messageGetterListener = db.collection("adminData").document(email).collection("drivers").document(driver).collection("messages").whereField("cv", isEqualTo: 1).order(by: "date", descending: false).addSnapshotListener {...}

// Initialize listener
isReadListener = db.collection("adminData").document(email).collection("drivers").document(driver).collection("messages").whereField("cv", isEqualTo: 1).whereField("sender", isNotEqualTo: email).addSnapshotListener {...}

// THIS causes the bug. If I remove this, everything display wise works perfectly. Otherwise, the bug persists.
statusManager.listeners.append(messageGetterListener)
statusManager.listeners.append(isReadListener)

        }
        else{}
    })

}`

@ch05enOne
Copy link
Author

Sorry man but I just couldn't get the formatting right. Hope this helps.

@matteopuc
Copy link
Owner

Unfortunately I can't manage to make your example compile, I miss too many things. It would be really great to have a minimum viable example that reproduces the issue. I'm trying to create it, but I'm not experiencing the problem. For example, take a look at the following (you can copy paste it in your Xcode and try it yourself):

// this is the entry point file
import SwiftUI
import NavigationStack

@main
struct SwiftUIExamplesApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationStackView {
                ContentView()
            }
            .environmentObject(StatusManager())
        }
    }
}

class StatusManager: ObservableObject{
    @Published var listeners: [Int] = []
}

struct ContentView: View {
    @EnvironmentObject var statusManager: StatusManager

    var body: some View {
        VStack {
            Text("TOP BAR")
            Spacer()
            ForEach(statusManager.listeners, id: \.self) { listener in
                Text("\(listener)")
            }
            Spacer()
            PushView(destination: ChildView()) {
                Text("PUSH")
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.red)
    }
}

struct ChildView: View {
    @EnvironmentObject var statusManager: StatusManager

    var body: some View {
        VStack {
            Text("asd")
            PopView {
                Text("POP")
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.green)
        .onAppear {
            statusManager.listeners.append(10)
        }
    }
}

Since you told me that the issue is due to these two lines:

statusManager.listeners.append(messageGetterListener)
statusManager.listeners.append(isReadListener)

I tried to do something similar in the onAppear, but it seems that everything works as expected. Maybe you can take my example and change something trying to make the issue occur.

@ch05enOne
Copy link
Author

Will do @matteopuc . Thank you

@ch05enOne ch05enOne reopened this Apr 13, 2021
@ch05enOne
Copy link
Author

@matteopuc This similar library has a similar issue. reopening to see if it helps. https://github.com/rebeloper/NavigationKit/issues/6 I looked at ur injection above for the environment object btw. Not sure if this makes a change, but instead of using .environmentObject(StatusManager()), I initialized a @StateObject variable and injected that into the environment.

@matteopuc
Copy link
Owner

matteopuc commented Apr 13, 2021

Hi @ch05enOne, if you can use @StateObject (i.e. you can target iOS >= 14) it's a good idea to inject state objects from the outside. You can even inject the entire NavigationStack as a @StateObject in your NavigationStackView like this (this is just an example):

@main
struct SwiftUIExamplesApp: App {
    @StateObject var navigationStack = NavigationStack()

    var body: some Scene {
        WindowGroup {
            NavigationStackView(navigationStack: navigationStack) {
                ContentView()
            }
        }
    }
}

Then you can access and use the navigationStack:

  • as usual (i.e. from your view hierarchy by the @EnvironmentObject property)
  • or referring to it from where you store it (in the example above it is stored in the "main" struct of the app): let's say you are in a specific view of your project, you can do something like: theObjectContainingNavStack.navigationStack.push(...)

Let me know if you manage to solve your issue.
Matteo

@ln-12
Copy link

ln-12 commented Jun 10, 2021

@matteopuc I had the same issue and can confirm that using a @StateObject works for me now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants