commit 554edc963c3c99f23f69f94dfc9c48979a02de6c
parent 6a90ed51d65f0bdda2979def3d3f7021151e520c
Author: FIGBERT <figbert@figbert.com>
Date: Wed, 14 Sep 2022 15:01:12 -0700
Implement basic history traversal
Diffstat:
3 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/Shared/BrowserFunctions.swift b/Shared/BrowserFunctions.swift
@@ -11,7 +11,6 @@ import SwiftGemtext
@MainActor
class BrowserData: ObservableObject {
- @Published var url = ""
@Published var tab = Tab()
@Published var currentView: GemenonView = .Capsule
@@ -25,7 +24,16 @@ extension BrowserData {
func goToStartPage() {
guard !self.tab.home else { return }
self.tab = Tab(prev: self.tab)
- self.url = ""
+ }
+ func goBack() {
+ guard self.tab.prev != nil else { return }
+ let next = self.tab
+ self.tab = self.tab.prev!
+ self.tab.next = next
+ }
+ func goForward() {
+ guard self.tab.next != nil else { return }
+ self.tab = self.tab.next!
}
}
@@ -33,7 +41,6 @@ extension BrowserData {
func openURL(_ url: URL) async {
let response = try! await self.engine.request(url)
self.tab = Tab(url, prev: self.tab, response: response)
- self.url = url.absoluteString
self.currentView = .Capsule
}
}
@@ -43,6 +50,7 @@ class Tab: Identifiable {
let timestamp = Date.now
var home: Bool
+ var urlBar: String
var url: URL?
var next: Tab?
var prev: Tab?
@@ -50,14 +58,17 @@ class Tab: Identifiable {
init() {
self.home = true
+ self.urlBar = ""
}
init(prev: Tab) {
self.home = true
+ self.urlBar = ""
self.prev = prev
}
init(_ url: URL, prev: Tab, response: SwiftGemini.GeminiResponse) {
self.home = false
self.url = url
+ self.urlBar = url.absoluteString
self.prev = prev
self.response = response
}
diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift
@@ -45,13 +45,13 @@ struct ContentView: View {
}
.toolbar {
ToolbarItem(placement: .principal) {
- TextField("Search or enter website name", text: $data.url)
+ TextField("Search or enter website name", text: $data.tab.urlBar)
.frame(minWidth: 500)
.textFieldStyle(.roundedBorder)
.autocorrectionDisabled(true)
.onSubmit {
Task {
- if let url = URL(string: data.url) {
+ if let url = URL(string: data.tab.urlBar) {
if var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
if components.scheme?.isEmpty ?? true {
if components.host?.isEmpty ?? true {
@@ -67,7 +67,7 @@ struct ContentView: View {
}
} else {
var components = URLComponents(string: "gemini://geminispace.info/search")
- components?.query = data.url
+ components?.query = data.tab.urlBar
if let url = components?.url {
await data.openURL(url)
}
@@ -76,12 +76,22 @@ struct ContentView: View {
}
}
ToolbarItem(placement: .navigation) {
- Button(action: {
- data.goToStartPage()
- }) {
+ Button(action: { data.goToStartPage() }) {
Image(systemName: "house")
}
}
+ ToolbarItem(placement: .navigation) {
+ Button(action: { data.goBack() }) {
+ Image(systemName: "chevron.backward")
+ }
+ .disabled(data.tab.prev == nil)
+ }
+ ToolbarItem(placement: .navigation) {
+ Button(action: { data.goForward() }) {
+ Image(systemName: "chevron.forward")
+ }
+ .disabled(data.tab.next == nil)
+ }
}
.handlesExternalEvents(preferring: ["gemini://*"], allowing: ["*"])
.onOpenURL(perform: { url in
diff --git a/Shared/HistoryView.swift b/Shared/HistoryView.swift
@@ -26,10 +26,21 @@ struct HistoryView: View {
}
}
.onAppear {
- appendToURLs(data.tab, list: &tabs)
+ genHistory(data.tab, list: &tabs)
}
}
+ func genHistory(_ tab: Tab, list: inout [Tab]) {
+ let start = seekEndOfHistory(tab)
+ appendToURLs(start, list: &list)
+ }
+ func seekEndOfHistory(_ tab: Tab) -> Tab {
+ if let next = tab.next {
+ return next
+ } else {
+ return tab
+ }
+ }
func appendToURLs(_ tab: Tab, list: inout [Tab]) {
if !tab.home {
list.append(tab)