Comments (3)
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.
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.
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)
- PopToRoot shows second view while transitioning from third to first view HOT 6
- How can I get access to a stack during popToRoot
- Problem with .push transition. HOT 2
- Can't I use onDismiss in route method? HOT 1
- Dismissing item issue HOT 2
- Exclude screens during deeplink HOT 1
- Issues caused by use of SwiftUI's NavigationStack HOT 2
- Can't get NavigationRouter via protocol
- Tabbar items customisation HOT 2
- dismissCoordinator animation
- dismissChild fatal optional
- How to correctly use focusFirst HOT 1
- Example just don't work
- Tabbar Item Accent Color Not working
- Presentation detents for sheets
- Memory leaks in iOS17 HOT 5
- How do I know which route I am in?
- EmptyView while push HOT 5
- hasRoot / isRoot crashes app HOT 1
- Disable tabCoordinator when show Custom Alert HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stinsen.