Skip to content

Commit

Permalink
Navigating without providing next view
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaal111 committed Feb 26, 2024
1 parent 19fbf56 commit fea0e65
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ContentView: View {
var body: some View {
NavigationStackView(
stack: [] as [Screens],
root: { screen in MainView(screen: screen) },
root: { screen in MainView(screen: screen, displayMode: .large) },
subView: { screen in MainView(screen: screen, displayMode: .inline) },
sidebar: { Sidebar() }
)
Expand Down
5 changes: 5 additions & 0 deletions Example/Example/Screens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Kamaal M Farah on 23/04/2023.
//

import SwiftUI
import KamaalNavigation

enum Screens: Hashable, Codable, CaseIterable, NavigatorStackValue {
Expand Down Expand Up @@ -62,5 +63,9 @@ enum Screens: Hashable, Codable, CaseIterable, NavigatorStackValue {
}
}

func view(_ isSub: Bool) -> some View {
MainView(screen: self, displayMode: isSub ? .inline : .large)
}

static let root: Screens = .home
}
5 changes: 1 addition & 4 deletions Example/Example/Views/Screens/CoreDataScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ struct CoreDataScreen: View {
EmptyItemsView(itemName: "items")
}
ForEach(self.items, id: \.id) { item in
StackNavigationLink(
destination: Screens.coreDataChild(parentID: item.id),
nextView: { screen in MainView(screen: screen) }
) {
StackNavigationLink(destination: Screens.coreDataChild(parentID: item.id)) {
TimestampView(time: item.timestamp)
.foregroundColor(.accentColor)
}
Expand Down
10 changes: 2 additions & 8 deletions Example/Example/Views/Screens/HomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,10 @@ struct HomeScreen: View {
}) {
Text("Hud popup")
}
StackNavigationLink(
destination: Screens.coreData,
nextView: { screen in MainView(screen: screen, displayMode: .inline) }
) {
StackNavigationLink(destination: Screens.coreData) {
Text("Go to core data screen")
}
StackNavigationLink(
destination: Screens.other,
nextView: { screen in MainView(screen: screen, displayMode: .inline) }
) {
StackNavigationLink(destination: Screens.other) {
Text("Go to other screen")
}
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/Views/SupportingViews/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct MainView: View {
let screen: Screens
let displayMode: DisplayMode

init(screen: Screens, displayMode: DisplayMode = .large) {
init(screen: Screens, displayMode: DisplayMode) {
self.screen = screen
self.displayMode = displayMode
}
Expand All @@ -40,6 +40,6 @@ struct MainView: View {

struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView(screen: .home)
MainView(screen: .home, displayMode: .large)
}
}
18 changes: 17 additions & 1 deletion Sources/KamaalNavigation/Protocols/NavigatorStackValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
// Created by Kamaal M Farah on 04/01/2023.
//

import Foundation
import SwiftUI
import KamaalUI

public protocol NavigatorStackValue: Codable, Hashable, CaseIterable {
associatedtype ScreenView: SwiftUI.View

var isTabItem: Bool { get }
var imageSystemName: String { get }
var title: String { get }

@ViewBuilder
@MainActor
func view(_ isSub: Bool) -> Self.ScreenView

static var root: Self { get }
}

Expand All @@ -25,6 +32,15 @@ enum PreviewScreenType: NavigatorStackValue, Equatable {
var imageSystemName: String { "person" }
var title: String { "Screen" }

func view(_ isSub: Bool) -> some View {
KJustStack {
switch self {
case .screen: Text("Screen").navigationTitle(title: "Screen", displayMode: isSub ? .inline : .large)
case .sub: Text("Sub").navigationTitle(title: "Sub", displayMode: isSub ? .inline : .large)
}
}
}

static let root: Self = .screen
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@

import SwiftUI

public struct StackNavigationLink<Content: View, NextView: View, Destination: NavigatorStackValue>: View {
public struct StackNavigationLink<Content: View, Destination: NavigatorStackValue>: View {
@EnvironmentObject private var navigator: Navigator<Destination>

let destination: Destination
let nextView: (Destination) -> NextView
let content: () -> Content

public init(
destination: Destination,
@ViewBuilder nextView: @escaping (Destination) -> NextView,
@ViewBuilder content: @escaping () -> Content
) {
public init(destination: Destination, @ViewBuilder content: @escaping () -> Content) {
self.destination = destination
self.nextView = nextView
self.content = content
}

Expand All @@ -30,7 +24,7 @@ public struct StackNavigationLink<Content: View, NextView: View, Destination: Na
self.content()
}
#else
NavigationLink(destination: { self.nextView(self.destination) }) {
NavigationLink(destination: { self.destination.view(true) }) {
self.content()
}
#endif
Expand All @@ -40,7 +34,7 @@ public struct StackNavigationLink<Content: View, NextView: View, Destination: Na
#if DEBUG
struct StackNavigationLink_Previews: PreviewProvider {
static var previews: some View {
StackNavigationLink(destination: PreviewScreenType.screen, nextView: { _ in Text("s") }) {
StackNavigationLink(destination: PreviewScreenType.screen) {
Text("Link")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kamaal M Farah on 25/12/2022.
//

import Foundation
import SwiftUI
import KamaalNavigation

enum ScreenSelection: Codable, Hashable, CaseIterable, NavigatorStackValue {
Expand Down Expand Up @@ -47,6 +47,10 @@ enum ScreenSelection: Codable, Hashable, CaseIterable, NavigatorStackValue {
}
}

func view(_ isSub: Bool) -> some View {
MainView(screen: self).navigationTitle(title: self.title, displayMode: isSub ? .inline : .large)
}

static var allCases: [ScreenSelection] {
[
.acknowledgements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ struct NavigationLinkRow<Value: View>: View {
}

var body: some View {
StackNavigationLink(
destination: self.destination,
nextView: { screen in
MainView(screen: screen)
.environment(\.settingsConfiguration, self.settingsConfiguration)
.environmentObject(self.store)
}
) {
StackNavigationLink(destination: self.destination) {
self.valueView
}
.buttonStyle(.plain)
Expand Down

0 comments on commit fea0e65

Please sign in to comment.