[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.
This commit is contained in:
AntonieSander 2024-05-22 14:08:14 +02:00
parent ae374b07b0
commit 3aeab9a91e
3 changed files with 85 additions and 24 deletions

View File

@ -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")
}

View File

@ -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
}
}

View File

@ -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 {