Add rudimentary navigation
This commit is contained in:
parent
968837a6da
commit
29bd87f287
|
@ -17,7 +17,22 @@ struct ContentView: View {
|
|||
@State var errorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
mainView
|
||||
VStack {
|
||||
HStack {
|
||||
Button(action: navigator.back) {
|
||||
Image(systemName: "chevron.left")
|
||||
}.disabled(navigator.backStack.isEmpty)
|
||||
Button(action: navigator.forward) {
|
||||
Image(systemName: "chevron.right")
|
||||
}.disabled(navigator.forwardStack.isEmpty)
|
||||
TextField("URL", text: Binding<String>(get: {
|
||||
self.navigator.currentURL.absoluteString
|
||||
}, set: { (newValue) in
|
||||
self.navigator.currentURL = URL(string: newValue)!
|
||||
}))
|
||||
}.padding([.leading, .trailing])
|
||||
mainView.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
.frame(minWidth: 480, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)
|
||||
.onReceive(navigator.$currentURL, perform: self.urlChanged)
|
||||
}
|
||||
|
|
|
@ -10,13 +10,29 @@ import Foundation
|
|||
class NavigationManager: ObservableObject {
|
||||
|
||||
@Published var currentURL: URL
|
||||
@Published var backStack = [URL]()
|
||||
@Published var forwardStack = [URL]()
|
||||
|
||||
init(url: URL) {
|
||||
self.currentURL = url
|
||||
}
|
||||
|
||||
func changeURL(_ url: URL) {
|
||||
self.currentURL = url
|
||||
backStack.append(currentURL)
|
||||
currentURL = url
|
||||
forwardStack = []
|
||||
}
|
||||
|
||||
func back() {
|
||||
guard !backStack.isEmpty else { return }
|
||||
forwardStack.insert(currentURL, at: 0)
|
||||
currentURL = backStack.removeLast()
|
||||
}
|
||||
|
||||
func forward() {
|
||||
guard !forwardStack.isEmpty else { return }
|
||||
backStack.append(currentURL)
|
||||
currentURL = forwardStack.removeFirst()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue