Compare commits

...

7 Commits

8 changed files with 304 additions and 8 deletions

View File

@ -7,8 +7,14 @@
import Foundation
public protocol NavigationManagerDelegate: class {
func loadNonGeminiURL(_ url: URL)
}
public class NavigationManager: NSObject, ObservableObject {
public weak var delegate: NavigationManagerDelegate?
@Published public var currentURL: URL
@Published public var backStack = [URL]()
@Published public var forwardStack = [URL]()
@ -18,11 +24,21 @@ public class NavigationManager: NSObject, ObservableObject {
}
public func changeURL(_ url: URL) {
guard url.scheme == "gemini" else {
delegate?.loadNonGeminiURL(url)
return
}
backStack.append(currentURL)
currentURL = cannonicalizeURL(url)
forwardStack = []
}
public func reload() {
let url = currentURL
currentURL = url
}
private func cannonicalizeURL(_ url: URL) -> URL {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
if components.scheme == "gemini" && components.port == 1965 {

View File

@ -9,15 +9,118 @@ import SwiftUI
import BrowserCore
struct ContentView: View {
let navigator: NavigationManager
@ObservedObject private var navigator: NavigationManager
@State private var urlFieldContents: String
@State private var showPreferencesSheet = false
private let shareCurrentURL: () -> Void
@Environment(\.colorScheme) var colorScheme: ColorScheme
init(navigator: NavigationManager, shareCurrentURL: @escaping () -> Void) {
self.navigator = navigator
self._urlFieldContents = State(initialValue: navigator.currentURL.absoluteString)
self.shareCurrentURL = shareCurrentURL
}
var body: some View {
VStack(spacing: 0) {
urlBar
barBorder
Spacer(minLength: 0)
BrowserView(navigator: navigator)
Spacer(minLength: 0)
barBorder
bottomBar
}
.onAppear(perform: tweakAppearance)
.onReceive(navigator.$currentURL, perform: { (new) in
urlFieldContents = new.absoluteString
})
.sheet(isPresented: $showPreferencesSheet, content: {
PreferencesView(presented: $showPreferencesSheet)
})
}
private var barBorder: some View {
Rectangle()
.frame(height: 1)
.foregroundColor(Color(white: colorScheme == .dark ? 0.25 : 0.75))
}
private var urlBar: some View {
TextField("URL", text: $urlFieldContents, onCommit: commitURL)
.keyboardType(.URL)
.autocapitalization(.none)
.disableAutocorrection(true)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.leading, .trailing, .bottom])
}
private var bottomBar: some View {
HStack {
// use a group because this exceeds the 10 view limit :/
Group {
Spacer()
Button(action: navigator.back) {
Image(systemName: "arrow.left")
.font(.system(size: 24))
}
.disabled(navigator.backStack.isEmpty)
Spacer()
Button(action: navigator.forward) {
Image(systemName: "arrow.right")
.font(.system(size: 24))
}
.disabled(navigator.forwardStack.isEmpty)
Spacer()
Button(action: navigator.reload) {
Image(systemName: "arrow.clockwise")
.font(.system(size: 24))
}
Spacer()
Button(action: shareCurrentURL) {
Image(systemName: "square.and.arrow.up")
.font(.system(size: 24))
}
Spacer()
Button(action: {
showPreferencesSheet = true
}, label: {
Image(systemName: "gear")
.font(.system(size: 24))
})
}
Spacer()
}
.padding(.top, 4)
}
private func tweakAppearance() {
UIScrollView.appearance().keyboardDismissMode = .interactive
}
private func commitURL() {
guard let url = URL(string: urlFieldContents) else { return }
navigator.changeURL(url)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(navigator: NavigationManager(url: URL(string: "gemini://localhost/overview.gmi")!))
ContentView(navigator: NavigationManager(url: URL(string: "gemini://localhost/overview.gmi")!), shareCurrentURL: {})
}
}

View File

@ -16,6 +16,19 @@
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>net.shadowfacts.gemini</string>
<key>CFBundleURLSchemes</key>
<array>
<string>gemini</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>

View File

@ -0,0 +1,56 @@
//
// Preferences.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/27/20.
//
import Foundation
class Preferences: Codable, ObservableObject {
static let shared: Preferences = load()
private static var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private static var archiveURL = Preferences.documentsDirectory.appendingPathComponent("Preferences").appendingPathExtension("plist")
static func save() {
let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection)
}
static func load() -> Preferences {
let decoder = PropertyListDecoder()
if let data = try? Data(contentsOf: archiveURL),
let prefs = try? decoder.decode(Preferences.self, from: data) {
return prefs
}
return Preferences()
}
private init() {}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
useReaderMode = try container.decode(Bool.self, forKey: .useReaderMode)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(useInAppSafari, forKey: .useInAppSafari)
try container.encode(useReaderMode, forKey: .useReaderMode)
}
@Published var useInAppSafari = false
@Published var useReaderMode = false
enum CodingKeys: String, CodingKey {
case useInAppSafari
case useReaderMode
}
}

View File

@ -0,0 +1,61 @@
//
// PreferencesView.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/27/20.
//
import SwiftUI
struct PreferencesView: View {
@ObservedObject var preferences: Preferences = .shared
@Binding var presented: Bool
var body: some View {
NavigationView {
List {
safariSection
}
.navigationBarTitle("Preferences")
.insetOrGroupedListStyle()
.navigationBarItems(trailing: doneButton)
}
}
private var doneButton: some View {
Button(action: {
presented = false
}, label: {
Text("Done")
})
}
private var safariSection: some View {
Section(header: Text("Safari")) {
Toggle("Use In-App Safari", isOn: $preferences.useInAppSafari)
Toggle("Use Reader Mode", isOn: $preferences.useReaderMode)
.disabled(!preferences.useInAppSafari)
}
}
}
fileprivate extension View {
@ViewBuilder
func insetOrGroupedListStyle() -> some View {
if #available(iOS 14.0, *) {
self.listStyle(InsetGroupedListStyle())
} else {
self.listStyle(GroupedListStyle())
}
}
}
struct PreferencesView_Previews: PreviewProvider {
@State static var presented = true
static var previews: some View {
PreferencesView(presented: $presented)
}
}

View File

@ -8,20 +8,32 @@
import UIKit
import SwiftUI
import BrowserCore
import SafariServices
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let navigationManager = NavigationManager(url: URL(string: "gemini://gemini.circumlunar.space/")!)
var navigationManager: NavigationManager!
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
let initialURL: URL
if let context = connectionOptions.urlContexts.first {
initialURL = context.url
} else {
initialURL = URL(string: "gemini://gemini.circumlunar.space/")!
}
navigationManager = NavigationManager(url: initialURL)
navigationManager.delegate = self
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(navigator: navigationManager)
let contentView = ContentView(navigator: navigationManager, shareCurrentURL: self.shareCurrentURL)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
@ -32,6 +44,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let context = URLContexts.first {
navigationManager.changeURL(context.url)
}
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
@ -60,6 +78,27 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// to restore the scene back to its current state.
}
private func shareCurrentURL() {
let vc = UIActivityViewController(activityItems: [navigationManager.currentURL], applicationActivities: nil)
window?.rootViewController?.present(vc, animated: true)
}
}
extension SceneDelegate: NavigationManagerDelegate {
func loadNonGeminiURL(_ url: URL) {
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (success) in
if !success {
if Preferences.shared.useInAppSafari {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = Preferences.shared.useReaderMode
let vc = SFSafariViewController(url: url, configuration: config)
self.window?.rootViewController?.present(vc, animated: true)
} else {
UIApplication.shared.open(url, options: [:])
}
}
}
}
}

View File

@ -40,6 +40,8 @@
D664673624BD07F700B0B741 /* RenderingBlock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D664673524BD07F700B0B741 /* RenderingBlock.swift */; };
D664673824BD086F00B0B741 /* RenderingBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D664673724BD086F00B0B741 /* RenderingBlockView.swift */; };
D664673A24BD0B8E00B0B741 /* Fonts.swift in Sources */ = {isa = PBXBuildFile; fileRef = D664673924BD0B8E00B0B741 /* Fonts.swift */; };
D691A64E25217C6F00348C4B /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D691A64D25217C6F00348C4B /* Preferences.swift */; };
D691A66725217FD800348C4B /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D691A66625217FD800348C4B /* PreferencesView.swift */; };
D69F00AC24BE9DD300E37622 /* GeminiDataTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = D69F00AB24BE9DD300E37622 /* GeminiDataTask.swift */; };
D69F00AE24BEA29100E37622 /* GeminiResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D69F00AD24BEA29100E37622 /* GeminiResponse.swift */; };
D6E1529824BFAAA400FDF9D3 /* BrowserWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E1529624BFAAA400FDF9D3 /* BrowserWindowController.swift */; };
@ -272,6 +274,8 @@
D664673524BD07F700B0B741 /* RenderingBlock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RenderingBlock.swift; sourceTree = "<group>"; };
D664673724BD086F00B0B741 /* RenderingBlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RenderingBlockView.swift; sourceTree = "<group>"; };
D664673924BD0B8E00B0B741 /* Fonts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fonts.swift; sourceTree = "<group>"; };
D691A64D25217C6F00348C4B /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
D691A66625217FD800348C4B /* PreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
D69F00AB24BE9DD300E37622 /* GeminiDataTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeminiDataTask.swift; sourceTree = "<group>"; };
D69F00AD24BEA29100E37622 /* GeminiResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeminiResponse.swift; sourceTree = "<group>"; };
D69F00AF24BEA84D00E37622 /* NavigationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationManager.swift; sourceTree = "<group>"; };
@ -523,6 +527,8 @@
D6E152A424BFFDF500FDF9D3 /* AppDelegate.swift */,
D6E152A624BFFDF500FDF9D3 /* SceneDelegate.swift */,
D6E152A824BFFDF500FDF9D3 /* ContentView.swift */,
D691A64D25217C6F00348C4B /* Preferences.swift */,
D691A66625217FD800348C4B /* PreferencesView.swift */,
D6E152AA24BFFDF600FDF9D3 /* Assets.xcassets */,
D6E152AF24BFFDF600FDF9D3 /* LaunchScreen.storyboard */,
D6E152B224BFFDF600FDF9D3 /* Info.plist */,
@ -1021,8 +1027,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D691A66725217FD800348C4B /* PreferencesView.swift in Sources */,
D6E152A524BFFDF500FDF9D3 /* AppDelegate.swift in Sources */,
D6E152A724BFFDF500FDF9D3 /* SceneDelegate.swift in Sources */,
D691A64E25217C6F00348C4B /* Preferences.swift in Sources */,
D6E152A924BFFDF500FDF9D3 /* ContentView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -7,7 +7,7 @@
<key>BrowserCore.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>4</integer>
</dict>
<key>Gemini-iOS.xcscheme_^#shared#^_</key>
<dict>
@ -27,7 +27,7 @@
<key>GeminiProtocol.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
<integer>2</integer>
</dict>
<key>GeminiRenderer.xcscheme_^#shared#^_</key>
<dict>