Code Monkey home page Code Monkey logo

Comments (3)

rundfunk47 avatar rundfunk47 commented on May 28, 2024 2

Hi! Here is a quick and dirty example:

class BurgerOpener: ObservableObject {
    @Published var value: Bool
    
    init(value: Bool) {
        self.value = value
    }
}

class SplitViewCoordinator<Master: Coordinatable, Detail: Coordinatable>: Coordinatable {
    typealias Router = SplitViewRouter<Master, Detail>

    public let master: Master
    public let detail: Detail
    var opened = BurgerOpener(value: false)

    public func dismissChild(coordinator: AnyCoordinatable, action: (() -> Void)?) {
        fatalError("Not implemented")
    }
    
    public weak var parent: AnyCoordinatable?

    public func view() -> AnyView {
        AnyView(
            SplitViewCoordinatorView(
                coordinator: self
            )
        )
    }
    
    public init(detail: Detail, master: Master) {
        self.detail = detail
        self.master = master
        self.detail.parent = self
        self.master.parent = self
    }
    
    func toggle() {
        opened.value.toggle()
    }
}

struct SplitViewCoordinatorView<Master: Coordinatable, Detail: Coordinatable>: View {
    @ObservedObject var opened: BurgerOpener
    let coordinator: SplitViewCoordinator<Master, Detail>
    let router: SplitViewRouter<Master, Detail>
    
    var body: some View {
        Group {
            if opened.value == true {
                HStack {
                    coordinator.master.view()
                    coordinator.detail.view()
                }
            } else {
                coordinator.master.view()
            }
        }
        .environmentObject(router)
    }
    
    init(coordinator: SplitViewCoordinator<Master, Detail>) {
        self.opened = coordinator.opened
        self.coordinator = coordinator
        self.router = SplitViewRouter(coordinator: coordinator)
        RouterStore.shared.store(router: router)
    }
}

class SplitViewRouter<Master: Coordinatable, Detail: Coordinatable>: Routable {
    fileprivate weak var coordinator: SplitViewCoordinator<Master, Detail>!
    
    init(coordinator: SplitViewCoordinator<Master, Detail>) {
        self.coordinator = coordinator
    }
    
    func toggle() {
        coordinator.toggle()
    }
    
    var detail: Detail {
        coordinator.detail
    }
    
    var master: Master {
        coordinator.master
    }
}

to use it:

struct StinsenApp: App {
    var body: some Scene {
        WindowGroup {
            SplitViewCoordinator(
                detail: MainCoordinator(),
                master: MainCoordinator()
            ).view()
        }
    }
}

To fetch the router:

    @EnvironmentObject private var splitViewRouter: SplitViewCoordinator<MainCoordinator, MainCoordinator>.Router

Hopefully this is easier to follow than in version 1.

To you need to switch detail/master, thanks to the new system in version 2, if you replace the first MainCoordinator in with for instance

class DetailCoordinator: NavigationCoordinatable {
   var stack = NavigationStack(initial: \.a)

   @Root var a = makeA
   @Root var b = makeB
[...]

You can switch using:

splitViewRouter.detail.setRoot(\.b)

I think SplitView is a common enough use case, so maybe this should be provided as an example or even built into Stinsen itself - but I still haven't decided on the best course of action here... Suggestions? :)

from stinsen.

jblanco-applaudostudios avatar jblanco-applaudostudios commented on May 28, 2024 1

Thanks for your help! I was able to use the sample code you provided to create a custom SplitView on my App. I only found one issue though, I have this SplitViewCoordinator inside a custom SidebarCoordinator that I made, which works exactly the same as the TabViewCoordinatable (I dug into the source and replicated the code, changing the view). When changing navigating through my menu, when I change the option and return to the one I was in before, the SplitViewCoordinator's view reinitializes the viewmodel. In this case, this is not desirable. Is there any way to persist the views so they don't change? 🤔

By the way... I noticed that when I set the master and detail for the SplitViewCoordinator, if any of the views in the coordinators has a large navigation bar title, it doesn't display correctly (The status bar loses the color only on the part in which the large navigation title is).

Sure, I have suggestions 😄 Let me create a separate issue for that!

from stinsen.

rundfunk47 avatar rundfunk47 commented on May 28, 2024

The reinitialization of the ViewModel sounds more like a SwiftUI issue than a Stinsen one (although it might be, but I checked the code quickly and it seems like we should persist views rather than recreate...) since SwiftUI will recreate views very often. Perhaps you can persist the viewmodel in the parent view, or maybe you can just try toggling the opacity/changing the offset instead of removing the view completely?

from stinsen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.