Gemini/BrowserCore/NavigationManager.swift

111 lines
2.9 KiB
Swift
Raw Normal View History

2020-07-15 03:15:56 +00:00
//
// NavigationManager.swift
// Gemini
//
// Created by Shadowfacts on 7/14/20.
//
import Foundation
import Combine
2020-07-15 03:15:56 +00:00
public protocol NavigationManagerDelegate: AnyObject {
2020-09-28 02:26:44 +00:00
func loadNonGeminiURL(_ url: URL)
}
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?
@Published public var currentURL: URL
@Published public var backStack = [URL]()
@Published public var forwardStack = [URL]()
2020-07-15 03:15:56 +00:00
public let navigationOperation = PassthroughSubject<Operation, Never>()
2020-09-29 20:28:05 +00:00
public var displayURL: String {
var components = URLComponents(url: currentURL, resolvingAgainstBaseURL: false)!
if components.port == 1965 {
components.port = nil
}
return components.string!
}
public init(url: URL) {
2020-07-15 03:15:56 +00:00
self.currentURL = url
}
public func changeURL(_ url: URL) {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
if let scheme = url.scheme {
if scheme != "gemini" {
delegate?.loadNonGeminiURL(url)
return
}
} else {
components.scheme = "gemini"
}
// Foundation parses bare hosts (e.g. `example.com`) as having no host and a path of `example.com`
if components.host == nil {
components.host = components.path
components.path = "/"
2020-09-28 02:26:44 +00:00
}
2020-09-29 20:28:17 +00:00
// Some Gemini servers break on empty paths
if components.path.isEmpty {
components.path = "/"
}
let url = components.url!
2020-07-15 13:18:37 +00:00
backStack.append(currentURL)
currentURL = url
2020-07-15 13:18:37 +00:00
forwardStack = []
navigationOperation.send(.go)
2020-07-15 13:18:37 +00:00
}
2020-12-19 20:19:32 +00:00
@objc public func reload() {
2020-09-28 02:11:34 +00:00
let url = currentURL
currentURL = url
2020-12-21 03:27:59 +00:00
navigationOperation.send(.reload)
2020-09-28 02:11:34 +00:00
}
2020-12-19 20:19:32 +00:00
@objc public func goBack() {
back(count: 1)
2020-07-15 13:18:37 +00:00
}
public func back(count: Int) {
guard count <= backStack.count else { return }
var removed = backStack.suffix(count)
backStack.removeLast(count)
forwardStack.insert(currentURL, at: 0)
currentURL = removed.removeFirst()
forwardStack.insert(contentsOf: removed, at: 0)
navigationOperation.send(.backward(count: count))
}
2020-12-19 20:19:32 +00:00
@objc public func goForward() {
forward(count: 1)
2020-07-15 03:15:56 +00:00
}
public func forward(count: Int) {
guard count <= forwardStack.count else { return }
var removed = forwardStack.prefix(count)
forwardStack.removeFirst(count)
backStack.append(currentURL)
currentURL = removed.removeLast()
backStack.append(contentsOf: removed)
navigationOperation.send(.forward(count: count))
}
2020-07-15 03:15:56 +00:00
}
public extension NavigationManager {
enum Operation {
2020-12-21 03:27:59 +00:00
case go, reload, forward(count: Int), backward(count: Int)
}
}