iOS: Assorted tweaks

This commit is contained in:
Shadowfacts 2020-09-28 15:49:02 -04:00
parent cea2076244
commit f5bb2ed2c3
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 38 additions and 11 deletions

View File

@ -28,18 +28,21 @@ public struct BrowserView: View {
@ViewBuilder @ViewBuilder
private var mainView: some View { private var mainView: some View {
VStack {
switch state { switch state {
case .loading: case .loading:
Spacer()
loadingView loadingView
.onAppear(perform: self.loadDocument) .onAppear(perform: self.loadDocument)
Spacer()
case let .error(message): case let .error(message):
VStack {
Text("An error occurred") Text("An error occurred")
.font(.headline) .font(.headline)
Text(message) Text(message)
}
case let .document(doc): case let .document(doc):
DocumentView(document: doc, scrollingEnabled: scrollingEnabled, changeURL: navigator.changeURL) DocumentView(document: doc, scrollingEnabled: scrollingEnabled, changeURL: navigator.changeURL)
Spacer()
}
} }
} }
@ -79,8 +82,13 @@ public struct BrowserView: View {
} }
private func urlChanged(_ newValue: URL) { private func urlChanged(_ newValue: URL) {
if case .loading = state {
task?.cancel()
loadDocument()
} else {
state = .loading state = .loading
} }
}
} }
extension BrowserView { extension BrowserView {

View File

@ -8,6 +8,7 @@
import UIKit import UIKit
import SwiftUI import SwiftUI
import BrowserCore import BrowserCore
import Combine
class BrowserViewController: UIViewController, UIScrollViewDelegate { class BrowserViewController: UIViewController, UIScrollViewDelegate {
@ -23,6 +24,8 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate {
private var barAnimator: UIViewPropertyAnimator? private var barAnimator: UIViewPropertyAnimator?
private var cancellables = [AnyCancellable]()
init(navigator: NavigationManager) { init(navigator: NavigationManager) {
self.navigator = navigator self.navigator = navigator
@ -40,6 +43,7 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate {
scrollView = UIScrollView() scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.keyboardDismissMode = .interactive
view.addSubview(scrollView) view.addSubview(scrollView)
scrollView.delegate = self scrollView.delegate = self
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
@ -62,7 +66,7 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate {
browserHost.view.widthAnchor.constraint(equalTo: view.widthAnchor), browserHost.view.widthAnchor.constraint(equalTo: view.widthAnchor),
// make sure the browser host view is at least the screen height so the loading indicator appears centered // make sure the browser host view is at least the screen height so the loading indicator appears centered
browserHost.view.heightAnchor.constraint(greaterThanOrEqualTo: view.safeAreaLayoutGuide.heightAnchor), browserHost.view.heightAnchor.constraint(greaterThanOrEqualTo: view.heightAnchor),
]) ])
navBarHost = UIHostingController(rootView: NavigationBar(navigator: navigator)) navBarHost = UIHostingController(rootView: NavigationBar(navigator: navigator))
@ -90,6 +94,14 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate {
toolBarHost.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), toolBarHost.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
toolBarHost.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), toolBarHost.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
]) ])
navigator.$currentURL
.sink { (_) in
self.scrollView.contentOffset = .zero
self.navBarHost.view.transform = .identity
self.toolBarHost.view.transform = .identity
}
.store(in: &cancellables)
} }
override func viewDidLayoutSubviews() { override func viewDidLayoutSubviews() {
@ -116,7 +128,9 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate {
// When certain state changes happen, the scroll view seems to "scroll" by top the safe area inset. // When certain state changes happen, the scroll view seems to "scroll" by top the safe area inset.
// It's not actually user scrolling, and this screws up our animation, so we ignore it. // It's not actually user scrolling, and this screws up our animation, so we ignore it.
guard abs(scrollViewDelta) != view.safeAreaInsets.top, scrollViewDelta != 0 else { return } guard abs(scrollViewDelta) != view.safeAreaInsets.top,
scrollViewDelta != 0,
scrollView.contentOffset.y > 0 else { return }
let barAnimator: UIViewPropertyAnimator let barAnimator: UIViewPropertyAnimator
if let animator = self.barAnimator { if let animator = self.barAnimator {

View File

@ -33,6 +33,9 @@ struct NavigationBar: View {
.foregroundColor(Color(white: colorScheme == .dark ? 0.25 : 0.75)) .foregroundColor(Color(white: colorScheme == .dark ? 0.25 : 0.75))
} }
.background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.top)) .background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.top))
.onReceive(navigator.$currentURL) { (newURL) in
urlFieldContents = newURL.absoluteString
}
} }
private func commitURL() { private func commitURL() {

View File

@ -66,6 +66,8 @@ struct ToolBar: View {
Spacer() Spacer()
} }
Spacer(minLength: 4)
} }
.background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.bottom)) .background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.bottom))
.sheet(isPresented: $showPreferencesSheet, content: { .sheet(isPresented: $showPreferencesSheet, content: {