[457] - Immersive reading view

- Hiding bars when scrolling down WebView & showing when scrolling up.
- Showing bars when device rotates into landscape mode.
This commit is contained in:
AntonieSander 2024-05-21 01:34:33 +02:00
parent 0f1e3c0e8d
commit ce2dde898c

View File

@ -62,7 +62,9 @@ class WebViewController: UIViewController {
private var topSafeAreaConstraint: NSLayoutConstraint?
private var layoutSubject = PassthroughSubject<Void, Never>()
private var layoutCancellable: AnyCancellable?
private var currentScrollViewOffset: CGFloat = 0.0
private var compactViewNavigationController: UINavigationController?
init(webView: WKWebView) {
self.webView = webView
self.pageZoomObserver = Defaults.observe(.webViewPageZoom) { change in
@ -74,10 +76,20 @@ class WebViewController: UIViewController {
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self,
selector: #selector(self.onOrientationChange),
name: UIDevice.orientationDidChangeNotification,
object: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
compactViewNavigationController = parent?.navigationController ?? UINavigationController()
webView.scrollView.delegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
webView.alpha = 0
@ -117,6 +129,60 @@ class WebViewController: UIViewController {
}
}
// MARK: - UIScrollViewDelegate
extension WebViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let navigationController = compactViewNavigationController else {
debugPrint("compactViewNavigationController not set")
return
}
var isScrollingDown: Bool {
scrollView.contentOffset.y > currentScrollViewOffset
}
if scrollView.isDragging {
isScrollingDown ?
hideBars(on: navigationController) : showBars(on: navigationController)
}
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
currentScrollViewOffset = scrollView.contentOffset.y
}
func hideBars(on navigationController: UINavigationController) {
navigationController.setNavigationBarHidden(true, animated: true)
navigationController.setToolbarHidden(true, animated: true)
}
func showBars(on navigationController: UINavigationController) {
navigationController.setNavigationBarHidden(false, animated: true)
navigationController.setToolbarHidden(false, animated: true)
}
}
// MARK: - Screen orientation change
extension WebViewController {
@objc func onOrientationChange() {
guard let navigationController = compactViewNavigationController else {
debugPrint("compactViewNavigationController not set")
return
}
switch UIDevice.current.orientation {
case .landscapeLeft:
showBars(on: navigationController)
case .landscapeRight:
showBars(on: navigationController)
default:
showBars(on: navigationController)
}
}
}
extension WKWebView {
func adjustTextSize(pageZoom: Double? = nil) {
let pageZoom = pageZoom ?? Defaults[.webViewPageZoom]