2020-07-15 03:15:56 +00:00
|
|
|
//
|
|
|
|
// NavigationManager.swift
|
|
|
|
// Gemini
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 7/14/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-09-28 02:26:44 +00:00
|
|
|
public protocol NavigationManagerDelegate: class {
|
|
|
|
func loadNonGeminiURL(_ url: URL)
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
public class NavigationManager: NSObject, ObservableObject {
|
2020-07-15 03:15:56 +00:00
|
|
|
|
2020-09-28 02:26:44 +00:00
|
|
|
public weak var delegate: NavigationManagerDelegate?
|
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
@Published public var currentURL: URL
|
|
|
|
@Published public var backStack = [URL]()
|
|
|
|
@Published public var forwardStack = [URL]()
|
2020-07-15 03:15:56 +00:00
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
public init(url: URL) {
|
2020-07-15 03:15:56 +00:00
|
|
|
self.currentURL = url
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
public func changeURL(_ url: URL) {
|
2020-09-28 02:26:44 +00:00
|
|
|
guard url.scheme == "gemini" else {
|
|
|
|
delegate?.loadNonGeminiURL(url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-15 13:18:37 +00:00
|
|
|
backStack.append(currentURL)
|
2020-07-16 03:08:56 +00:00
|
|
|
currentURL = cannonicalizeURL(url)
|
2020-07-15 13:18:37 +00:00
|
|
|
forwardStack = []
|
|
|
|
}
|
|
|
|
|
2020-09-28 02:11:34 +00:00
|
|
|
public func reload() {
|
|
|
|
let url = currentURL
|
|
|
|
currentURL = url
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:08:56 +00:00
|
|
|
private func cannonicalizeURL(_ url: URL) -> URL {
|
|
|
|
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
|
|
|
|
if components.scheme == "gemini" && components.port == 1965 {
|
|
|
|
components.port = nil
|
|
|
|
}
|
|
|
|
return components.url!
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
@objc public func back() {
|
2020-07-15 13:18:37 +00:00
|
|
|
guard !backStack.isEmpty else { return }
|
|
|
|
forwardStack.insert(currentURL, at: 0)
|
|
|
|
currentURL = backStack.removeLast()
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:45:37 +00:00
|
|
|
@objc public func forward() {
|
2020-07-15 13:18:37 +00:00
|
|
|
guard !forwardStack.isEmpty else { return }
|
|
|
|
backStack.append(currentURL)
|
|
|
|
currentURL = forwardStack.removeFirst()
|
2020-07-15 03:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|