johnpatrickmorgan / FlowStacks

FlowStacks allows you to hoist SwiftUI navigation and presentation state into a Coordinator
MIT License
857 stars 66 forks source link

The screen closes automatically after opening #82

Closed lisindima closed 1 month ago

lisindima commented 2 months ago

Hello, after upgrading to 0.7.0, in cases where after closing the sheet and opening the screen in the stack it began to close itself, on version 0.6.4 this problem is not observed

import SwiftUI
import FlowStacks

enum Path: Hashable {
    case sport(Int, FirstCoordinator)
    case challenge(String, FirstCoordinator)
}

class FirstCoordinator: ObservableObject {
    @Published var path: Routes<Path> = []

    func presentSheet() {
        path.presentSheet(.sport(1, self))
    }

    func closeSheetAndPush() {
        path.goBack()
        path.push(.challenge("1", self))
    }

    func goBackToRoot() {
        path.goBackToRoot()
    }
}

struct ContentView: View {
    @ObservedObject var coordinator: FirstCoordinator

    var body: some View {
        FlowStack($coordinator.path, withNavigation: true) {
            VStack {
                Button {
                    coordinator.presentSheet()
                } label: {
                    Text("Present Sheet")
                }
            }
            .navigationTitle("ROOT")
            .flowDestination(for: Path.self) { screen in
                switch screen {
                case let .sport(value, coordinator):
                    SportView(coordinator: coordinator, value: value)
                case let .challenge(value, coordinator):
                    ChallengeView(coordinator: coordinator, value: value)
                }
            }
        }
    }
}

extension FirstCoordinator: Identifiable, Hashable, Equatable {
    static func == (lhs: FirstCoordinator, rhs: FirstCoordinator) -> Bool {
        lhs.id == rhs.id
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

struct SportView: View {
    @ObservedObject var coordinator: FirstCoordinator

    let value: Int

    var body: some View {
        VStack {
            Button {
                coordinator.closeSheetAndPush()
            } label: {
                Text("push")
            }
            Text(value.description)
        }
        .navigationTitle("Sport")
    }
}

struct ChallengeView: View {
    @ObservedObject var coordinator: FirstCoordinator

    let value: String

    var body: some View {
        VStack {
            Text(value)
            Button {
                coordinator.goBackToRoot()
            } label: {
                Text("goBackToRoot")
            }
        }
        .navigationTitle("Challenge")
    }
}

https://github.com/user-attachments/assets/77a87211-3a4d-468a-a34c-58d025f9fb1e

johnpatrickmorgan commented 2 months ago

Thanks for raising this issue @lisindima!

I think this change fixes it:

https://github.com/johnpatrickmorgan/FlowStacks/compare/bugfix%2Fissue%2D82

I'll just need to do some more testing before I can merge this fix.

lisindima commented 2 months ago

Yes, that solved the problem.

johnpatrickmorgan commented 1 month ago

Released as v0.8.2. Thanks @lisindima !