-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigationManager.swift
More file actions
138 lines (117 loc) · 4.19 KB
/
Copy pathNavigationManager.swift
File metadata and controls
138 lines (117 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import SwiftUI
import Combine
enum NavigationDestination: Equatable {
case home
case loading
case extraDetails(source: ExtraDetailsView.NavigationSource)
case popularDishes
case nutritionAnalytics(Recipe?)
case recipeDisplay(Recipe)
case savedRecipes
}
@MainActor
class NavigationManager: ObservableObject {
static let shared = NavigationManager()
private var history: [NavigationDestination] = [.home]
@Published private(set) var currentDestination: NavigationDestination = .home
func updateRecipe(_ updatedRecipe: Recipe) {
if case .recipeDisplay = currentDestination {
withAnimation {
currentDestination = .recipeDisplay(updatedRecipe)
}
}
}
func navigate(to destination: NavigationDestination) {
withAnimation(.easeInOut(duration: 0.3)) {
history.append(destination)
currentDestination = destination
}
}
var canGoBack: Bool { history.count > 1 }
func goBack() {
withAnimation(.easeInOut(duration: 0.3)) {
guard canGoBack else { return }
_ = history.popLast()
currentDestination = history.last ?? .home
}
}
func navigateToRoot() {
history = [.home]
currentDestination = .home
}
}
struct SwipeBackModifier: ViewModifier {
@EnvironmentObject private var navigationManager: NavigationManager
let customAction: (() -> Void)?
func body(content: Content) -> some View {
content.highPriorityGesture(
DragGesture(minimumDistance: 20, coordinateSpace: .local)
.onEnded { value in
let isLeftEdge = value.startLocation.x <= 30
let isHorizontal = abs(value.translation.height) < 50
let isRightSwipe = value.translation.width > 80
if isLeftEdge && isHorizontal && isRightSwipe {
if let action = customAction {
action()
} else {
navigationManager.goBack()
}
}
}
)
}
}
struct ToolbarBackModifier: ViewModifier {
@EnvironmentObject private var navigationManager: NavigationManager
var title: String?
let customAction: (() -> Void)?
func body(content: Content) -> some View {
content.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(action: { (customAction ?? { navigationManager.goBack() })() }) {
HStack(spacing: 4) {
Image(systemName: "chevron.left")
Text(title ?? "Back")
}
}
}
}
}
}
extension View {
func swipeBackGesture() -> some View {
modifier(SwipeBackModifier(customAction: nil))
}
func swipeBackGesture(action: @escaping () -> Void) -> some View {
modifier(SwipeBackModifier(customAction: action))
}
func toolbarBackButton(_ title: String? = nil) -> some View {
modifier(ToolbarBackModifier(title: title, customAction: nil))
}
func toolbarBackButton(_ title: String? = nil, action: @escaping () -> Void) -> some View {
modifier(ToolbarBackModifier(title: title, customAction: action))
}
}
struct UnifiedBackButton: View {
let action: () -> Void
var body: some View {
Button(action: action) {
Image(systemName: "chevron.left")
.imageScale(.large)
.frame(width: 44, height: 44)
.background(.ultraThinMaterial)
.clipShape(Circle())
}
.buttonStyle(PressableButtonStyle())
}
}
struct PressableButtonStyle: ButtonStyle {
var scale: CGFloat = 0.95
var pressedOpacity: Double = 0.8
func makeBody(configuration: ButtonStyleConfiguration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? scale : 1.0)
.opacity(configuration.isPressed ? pressedOpacity : 1.0)
.animation(.easeInOut(duration: 0.1), value: configuration.isPressed)
}
}