GraniteNavigation.Router
public final class GraniteNavigation: ObservableObject {
public struct Router {
public let id: String
public init(id: String) {
self.id = id
}
public var navigation: GraniteNavigation {
GraniteNavigation.instance(for: id)
}
public func push<C: View>(style: GraniteNavigationDestinationStyle = .init(),
window: GraniteRouteWindowProperties? = nil,
@ViewBuilder _ content: @escaping () -> C) {
self.navigation.push(destinationStyle: style,
window: window,
content)
}
public func push<C: GraniteNavigationDestination>(window: GraniteRouteWindowProperties? = nil,
@ViewBuilder _ content: @escaping () -> C) {
self.navigation.push(window: window,
content)
}
public func pop() {
self.navigation.pop()
}
}
...
}यह असल में वह मुख्य राउटर है जो आपके ऐप्लिकेशन के Views को EnvironmentValues के रूप में उपलब्ध कराया जाता है, ताकि जहाँ भी यह मौजूद हो, आप views को प्रोग्रामैटिक तरीक़े से खुलकर push और pop कर सकें।
struct PostCardView: View {
@Environment(\.graniteRouter) var router
...
var body: some View {
Button {
router.push {
PostDisplayView(context: _context,
updatedModel: currentModel)
}
} label: {
Text("Push")
}
SomeView()
.route {
PostDisplayView(context: _context,
updatedModel: currentModel)
} with: { router }
}
}बस इतना ही आसान है। रूट की सुविधा तक आसानी से पहुँचने के लिए कई view एक्सटेंशन उपलब्ध हैं, या आप चलाने के लिए सीधे push या pop बुला सकते हैं। और पकड़ा गया View या GraniteComponent कॉल करने वाले के ऊपर सरककर आ जाएगा।
self.navigation.push(window: window, content)Router struct अपना प्रतिनिधित्व करने वाला GraniteNavigation इंस्टेंस ढूँढ लेता है, ताकि उसके push/pop फ़ंक्शन इस्तेमाल कर सके।
public extension GraniteNavigation {
func push<Component: View>(destinationStyle style: GraniteNavigationDestinationStyle = .init(),
window: GraniteRouteWindowProperties? = nil,
@ViewBuilder _ component: @escaping (() -> Component)) {
let component = NavigationComponent<Component>(component)
let memadd = self.set(destinationStyle: style) {
component
}
self.push(memadd, window: window)
}
}एक्सटेंशन ख़ुद कॉम्पोनेंट को NavigationComponent में लपेटता, कॉम्पोनेंट को सेट करता और एक heap address लौटाता दिखता है, ताकि तैयार होने पर push को निशाना बनाया जा सके।
public func routeButton<C: GraniteNavigationDestination>(title: String = "",
window: GraniteRouteWindowProperties? = nil,
@ViewBuilder component : @escaping (() -> C),
with router: @escaping (() -> GraniteNavigation.Router)) -> some View {
let router = router()
let memadd = router.navigation.set {
NavigationComponent<C>(component)
}
return Button {
#if os(iOS)
UIImpactFeedbackGenerator(style: .light).impactOccurred()
#endif
router.navigation.push(memadd, window: window)
} label: {
self
}
}set को push से अलग रखा गया है उन मामलों के लिए जहाँ हम चाहते हैं कि push से पहले कोई शर्त पूरी हो, लेकिन साथ ही हम कॉम्पोनेंट को सेट कर देना चाहते हैं ताकि प्रस्तुति का समय सबसे कम रहे।
ऊपर जैसे view एक्सटेंशन बस इसलिए हैं कि एक सामान्य View के var body: some View बिल्डर के भीतर कोड साफ़-सुथरा रहे। सिद्धांत रूप में, आप पूरे ऐप के लिए सीधे राउटर का ही इस्तेमाल कर सकते हैं।
ऊपर GraniteNavigation में set फ़ंक्शन को बुलाए जाने का असली स्निपेट।
func set<Component : GraniteComponent>(destinationStyle: GraniteNavigationDestinationStyle = .init(),
@ViewBuilder _ component: @escaping (() -> Component)) -> String {
let screen = NavigationPassthroughComponent<Component, EmptyGranitePayload>.Screen<Component, EmptyGranitePayload>.init(component)
let addr = NSString(format: "%p", addressHeap(o: screen)) as String
paths[addr] = { AnyView(NavigationPassthroughComponent<Component,
EmptyGranitePayload>(screen: screen)
.environment(\.graniteNavigationDestinationStyle, destinationStyle)) }
isActive[addr] = false
return addr
}screen GraniteNavigationDestinationStyle सेट करने के लिए अहम है, जहाँ views प्रस्तुत करते समय, आपकी तरफ़ से, ट्रेलिंग नेविगेशन बार आइटम और दूसरी प्रॉपर्टीज़ कस्टमाइज़ की जा सकती हैं। यह प्रस्तुति के लाइफ़साइकल को सँभालने और “segues” को एनिमेट करने में भी मदद करती है।