FlowStacks icon indicating copy to clipboard operation
FlowStacks copied to clipboard

[iOS 14] View pops back if Router is inside of other

Open rigamikhail27 opened this issue 3 years ago • 6 comments

Hi everybody and thanks a lot @johnpatrickmorgan for this job! I was looking chance to organise my app code with MVVMc + SwiftUI and draw a conclusion, that FlowStacks is a best solution!

Here is a code example with weird behaviour when put one Router inside other leads to popping instead of pushing and only on iOS <= 14.5. (routes.push(.second) -> pops back to .root(.main) in CoordinatingViewB)

import SwiftUI
import FlowStacks

enum ScreensA {

    case main, bFlow

}

struct CoordinatingViewA: View {
    
    @State private var routes: Routes<ScreensA> = [.root(.main)]

    var body: some View {
        NavigationView {
            Router($routes) { screen, _ in
                switch screen {
                case .main:
                    Button {
                        routes.push(.bFlow)
                    } label: {
                        Text("push bFlow")
                    }
                    .navigationTitle("Main A")
                case .bFlow:
                    CoordinatingViewB()
                }
            }
            .navigationBarTitleDisplayMode(.inline)
        }
        .navigationViewStyle(.stack)
    }
    
}

enum ScreensB {

    case main, first, second

}

struct CoordinatingViewB: View {
    
    @State private var routes: Routes<ScreensB> = [.root(.main)]

    var body: some View {
        Router($routes) { screen, _ in
            switch screen {
            case .main:
                Button {
                    routes.push(.first)
                } label: {
                    Text("push First B")
                }
                .navigationTitle("Main B")
            case .first:
                Button {
                    routes.push(.second)
                } label: {
                    Text("push second B")
                }
                .navigationTitle("First B")
            case .second:
                Text("Finish")
                    .navigationTitle("Second B")
            }
        }
    }
}

struct TestFlowStack_Previews: PreviewProvider {
    static var previews: some View {
        CoordinatingViewA()
    }
}

Of course, I may use it incorrectly, that's why need your experience and waiting for it)

Thanks!

rigamikhail27 avatar Sep 04 '22 07:09 rigamikhail27

Hey! Got the same issue. It'd be nice to fix that. Thanks in advance!

oleh-petrenko avatar Sep 05 '22 07:09 oleh-petrenko

Thanks @rigamikhail27 for raising this issue and for the clear reproduction. I see the same issue on iOS 14, and you are using the library as intended, so this is definitely something to be fixed. I've done a little investigation but so far I haven't developed any insight, but will keep you posted.

johnpatrickmorgan avatar Sep 05 '22 22:09 johnpatrickmorgan

@johnpatrickmorgan any idea?)

rigamikhail27 avatar Oct 03 '22 05:10 rigamikhail27

Hi @rigamikhail27, sorry I haven't had any breakthrough. I checked older FlowStacks versions and the issue has been present since the inception of the library. I'm afraid it might be the case that FlowStacks' approach to nesting coordinators is not compatible with SwiftUI on iOS 14, at least for push navigation (presentation seems to work as expected).

I'm considering a significant rewrite of the library to bring the API more in line with the new navigation APIs in iOS 16 (point 2 here). I believe that would allow a more robust approach to nesting coordinators. Sorry I can't offer a better solution.

johnpatrickmorgan avatar Oct 05 '22 22:10 johnpatrickmorgan

Got it, thanks! Current implementation is useful, so I will better up min support version to 15🙂

rigamikhail27 avatar Oct 14 '22 21:10 rigamikhail27

Follow-up: here's an early peek at the possible rewrite: #51

johnpatrickmorgan avatar Oct 28 '22 22:10 johnpatrickmorgan