From 3aeab9a91e0b2e41103c1ab0f0c543c5c85bb6c7 Mon Sep 17 00:00:00 2001 From: AntonieSander Date: Wed, 22 May 2024 14:08:14 +0200 Subject: [PATCH] [457] Add check to only enable immersive reading for iPhone devices - Added WebView to Log. - Added TargetDevice enum to easily check for the current device type. --- Model/Utilities/Log.swift | 1 + Model/Utilities/TargetDevice.swift | 44 ++++++++++++++++++++ Views/BuildingBlocks/WebView.swift | 64 +++++++++++++++++++----------- 3 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 Model/Utilities/TargetDevice.swift diff --git a/Model/Utilities/Log.swift b/Model/Utilities/Log.swift index 42130830..d0db9237 100644 --- a/Model/Utilities/Log.swift +++ b/Model/Utilities/Log.swift @@ -25,4 +25,5 @@ struct Log { static let OPDS = OSLog(subsystem: subsystem, category: "OPDS") static let URLSchemeHandler = OSLog(subsystem: subsystem, category: "URLSchemeHandler") static let Branding = OSLog(subsystem: subsystem, category: "Branding") + static let WebView = OSLog(subsystem: subsystem, category: "WebView") } diff --git a/Model/Utilities/TargetDevice.swift b/Model/Utilities/TargetDevice.swift new file mode 100644 index 00000000..e8dcd489 --- /dev/null +++ b/Model/Utilities/TargetDevice.swift @@ -0,0 +1,44 @@ +// This file is part of Kiwix for iOS & macOS. +// +// Kiwix is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// any later version. +// +// Kiwix is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Kiwix; If not, see https://www.gnu.org/licenses/. + +import Foundation +import UIKit + +enum TargetDevice { + case nativeMac + case iPad + case iPhone + case iWatch + + public static var currentDevice: Self { + var currentDeviceModel = UIDevice.current.model + #if targetEnvironment(macCatalyst) + currentDeviceModel = "nativeMac" + #elseif os(watchOS) + currentDeviceModel = "watchOS" + #endif + + if currentDeviceModel.starts(with: "iPhone") { + return .iPhone + } + if currentDeviceModel.starts(with: "iPad") { + return .iPad + } + if currentDeviceModel.starts(with: "watchOS") { + return .iWatch + } + return .nativeMac + } +} diff --git a/Views/BuildingBlocks/WebView.swift b/Views/BuildingBlocks/WebView.swift index c9c43633..5c57d0d5 100644 --- a/Views/BuildingBlocks/WebView.swift +++ b/Views/BuildingBlocks/WebView.swift @@ -17,8 +17,8 @@ import Combine import CoreData import SwiftUI import WebKit - import Defaults +import os #if os(macOS) struct WebView: NSViewRepresentable { @@ -76,19 +76,9 @@ 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) - webView.scrollView.delegate = self webView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(webView) webView.alpha = 0 @@ -119,12 +109,7 @@ class WebViewController: UIViewController { self?.topSafeAreaConstraint?.isActive = false self?.view.topAnchor.constraint(equalTo: webView.topAnchor).isActive = true } - - if parent?.navigationController != nil { - compactViewNavigationController = parent?.navigationController - } else { - debugPrint("compactViewNavigationController not set") - } + configureImmersiveReading() } override func viewDidLayoutSubviews() { @@ -135,11 +120,22 @@ class WebViewController: UIViewController { } // MARK: - UIScrollViewDelegate - extension WebViewController: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { + if TargetDevice.currentDevice == .iPhone { + configureBars(on: scrollView) + } + } + + func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { + if TargetDevice.currentDevice == .iPhone { + currentScrollViewOffset = scrollView.contentOffset.y + } + } + + private func configureBars(on scrollView: UIScrollView) { guard let navigationController = compactViewNavigationController else { - debugPrint("compactViewNavigationController not set") + os_log("compactViewNavigationController not set", log: Log.WebView, type: .debug) return } @@ -156,10 +152,6 @@ extension WebViewController: UIScrollViewDelegate { } } - func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { - currentScrollViewOffset = scrollView.contentOffset.y - } - func hideBars(on navigationController: UINavigationController) { navigationController.setNavigationBarHidden(true, animated: true) navigationController.setToolbarHidden(true, animated: true) @@ -176,7 +168,7 @@ extension WebViewController: UIScrollViewDelegate { extension WebViewController { @objc func onOrientationChange() { guard let navigationController = compactViewNavigationController else { - debugPrint("compactViewNavigationController not set") + os_log("compactViewNavigationController not set", log: Log.WebView, type: .debug) return } @@ -189,6 +181,30 @@ extension WebViewController { showBars(on: navigationController) } } + + private func configureImmersiveReading() { + if TargetDevice.currentDevice == .iPhone { + configureDeviceOrientationNotifications() + configureNavigationController() + } + + func configureDeviceOrientationNotifications() { + UIDevice.current.beginGeneratingDeviceOrientationNotifications() + NotificationCenter.default.addObserver(self, + selector: #selector(self.onOrientationChange), + name: UIDevice.orientationDidChangeNotification, + object: nil) + } + + func configureNavigationController() { + webView.scrollView.delegate = self + if parent?.navigationController != nil { + compactViewNavigationController = parent?.navigationController + } else { + os_log("compactViewNavigationController not set", log: Log.WebView, type: .debug) + } + } + } } extension WKWebView {