Add rudimentary navigation

This commit is contained in:
Shadowfacts 2020-07-15 09:18:37 -04:00
parent 968837a6da
commit 29bd87f287
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 33 additions and 2 deletions

View File

@ -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)
}

View File

@ -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()
}
}