From 29bd87f28761a8d8591d55df18bf28f6936dfb1f Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 15 Jul 2020 09:18:37 -0400 Subject: [PATCH] Add rudimentary navigation --- Gemini/ContentView.swift | 17 ++++++++++++++++- Gemini/NavigationManager.swift | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Gemini/ContentView.swift b/Gemini/ContentView.swift index 96db521..fe8a64a 100644 --- a/Gemini/ContentView.swift +++ b/Gemini/ContentView.swift @@ -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(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) } diff --git a/Gemini/NavigationManager.swift b/Gemini/NavigationManager.swift index 2bddc5b..be0731f 100644 --- a/Gemini/NavigationManager.swift +++ b/Gemini/NavigationManager.swift @@ -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() } }