From f5bb2ed2c35b25ab52850d283c465e13cfac3e9f Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Sep 2020 15:49:02 -0400 Subject: [PATCH] iOS: Assorted tweaks --- BrowserCore/BrowserView.swift | 26 +++++++++++++++++--------- Gemini-iOS/BrowserViewController.swift | 18 ++++++++++++++++-- Gemini-iOS/NavigationBar.swift | 3 +++ Gemini-iOS/ToolBar.swift | 2 ++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/BrowserCore/BrowserView.swift b/BrowserCore/BrowserView.swift index e2735b4..538bc9a 100644 --- a/BrowserCore/BrowserView.swift +++ b/BrowserCore/BrowserView.swift @@ -28,18 +28,21 @@ public struct BrowserView: View { @ViewBuilder private var mainView: some View { - switch state { - case .loading: - loadingView - .onAppear(perform: self.loadDocument) - case let .error(message): - VStack { + VStack { + switch state { + case .loading: + Spacer() + loadingView + .onAppear(perform: self.loadDocument) + Spacer() + case let .error(message): Text("An error occurred") .font(.headline) Text(message) + case let .document(doc): + DocumentView(document: doc, scrollingEnabled: scrollingEnabled, changeURL: navigator.changeURL) + Spacer() } - case let .document(doc): - DocumentView(document: doc, scrollingEnabled: scrollingEnabled, changeURL: navigator.changeURL) } } @@ -79,7 +82,12 @@ public struct BrowserView: View { } private func urlChanged(_ newValue: URL) { - state = .loading + if case .loading = state { + task?.cancel() + loadDocument() + } else { + state = .loading + } } } diff --git a/Gemini-iOS/BrowserViewController.swift b/Gemini-iOS/BrowserViewController.swift index e6a38e2..cce27fb 100644 --- a/Gemini-iOS/BrowserViewController.swift +++ b/Gemini-iOS/BrowserViewController.swift @@ -8,6 +8,7 @@ import UIKit import SwiftUI import BrowserCore +import Combine class BrowserViewController: UIViewController, UIScrollViewDelegate { @@ -23,6 +24,8 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate { private var barAnimator: UIViewPropertyAnimator? + private var cancellables = [AnyCancellable]() + init(navigator: NavigationManager) { self.navigator = navigator @@ -40,6 +43,7 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate { scrollView = UIScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false + scrollView.keyboardDismissMode = .interactive view.addSubview(scrollView) scrollView.delegate = self NSLayoutConstraint.activate([ @@ -62,7 +66,7 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate { 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 - browserHost.view.heightAnchor.constraint(greaterThanOrEqualTo: view.safeAreaLayoutGuide.heightAnchor), + browserHost.view.heightAnchor.constraint(greaterThanOrEqualTo: view.heightAnchor), ]) navBarHost = UIHostingController(rootView: NavigationBar(navigator: navigator)) @@ -90,6 +94,14 @@ class BrowserViewController: UIViewController, UIScrollViewDelegate { toolBarHost.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), 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() { @@ -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. // 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 if let animator = self.barAnimator { diff --git a/Gemini-iOS/NavigationBar.swift b/Gemini-iOS/NavigationBar.swift index cc84586..dcfe782 100644 --- a/Gemini-iOS/NavigationBar.swift +++ b/Gemini-iOS/NavigationBar.swift @@ -33,6 +33,9 @@ struct NavigationBar: View { .foregroundColor(Color(white: colorScheme == .dark ? 0.25 : 0.75)) } .background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.top)) + .onReceive(navigator.$currentURL) { (newURL) in + urlFieldContents = newURL.absoluteString + } } private func commitURL() { diff --git a/Gemini-iOS/ToolBar.swift b/Gemini-iOS/ToolBar.swift index e47f645..0315aca 100644 --- a/Gemini-iOS/ToolBar.swift +++ b/Gemini-iOS/ToolBar.swift @@ -66,6 +66,8 @@ struct ToolBar: View { Spacer() } + + Spacer(minLength: 4) } .background(Color(UIColor.systemBackground).edgesIgnoringSafeArea(.bottom)) .sheet(isPresented: $showPreferencesSheet, content: {