2020-01-01 00:55:39 +01:00
|
|
|
//
|
|
|
|
// WebViewController.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 12/28/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import WebKit
|
2020-01-21 19:05:47 +01:00
|
|
|
import RSCore
|
2020-01-01 00:55:39 +01:00
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
import SafariServices
|
2020-05-03 15:33:57 +02:00
|
|
|
import MessageUI
|
2020-01-01 00:55:39 +01:00
|
|
|
|
2021-02-06 00:06:45 +01:00
|
|
|
protocol WebViewControllerDelegate: AnyObject {
|
2020-01-01 00:55:39 +01:00
|
|
|
func webViewController(_: WebViewController, articleExtractorButtonStateDidUpdate: ArticleExtractorButtonState)
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebViewController: UIViewController {
|
|
|
|
|
|
|
|
private struct MessageName {
|
|
|
|
static let imageWasClicked = "imageWasClicked"
|
|
|
|
static let imageWasShown = "imageWasShown"
|
2020-04-29 04:26:57 +02:00
|
|
|
static let showFeedInspector = "showFeedInspector"
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private var topShowBarsView: UIView!
|
|
|
|
private var bottomShowBarsView: UIView!
|
|
|
|
private var topShowBarsViewConstraint: NSLayoutConstraint!
|
|
|
|
private var bottomShowBarsViewConstraint: NSLayoutConstraint!
|
|
|
|
|
2020-02-26 00:10:51 +01:00
|
|
|
private var webView: PreloadedWebView? {
|
|
|
|
return view.subviews[0] as? PreloadedWebView
|
2020-02-01 05:07:08 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
private lazy var contextMenuInteraction = UIContextMenuInteraction(delegate: self)
|
|
|
|
private var isFullScreenAvailable: Bool {
|
2020-07-02 04:47:45 +02:00
|
|
|
return AppDefaults.shared.articleFullscreenAvailable && traitCollection.userInterfaceIdiom == .phone && coordinator.isRootSplitCollapsed
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
private lazy var transition = ImageTransition(controller: self)
|
|
|
|
private var clickedImageCompletion: (() -> Void)?
|
|
|
|
|
|
|
|
private var articleExtractor: ArticleExtractor? = nil
|
2020-01-29 19:30:52 +01:00
|
|
|
var extractedArticle: ExtractedArticle? {
|
|
|
|
didSet {
|
|
|
|
windowScrollY = 0
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 01:08:44 +01:00
|
|
|
var isShowingExtractedArticle = false
|
2020-01-01 00:55:39 +01:00
|
|
|
|
|
|
|
var articleExtractorButtonState: ArticleExtractorButtonState = .off {
|
|
|
|
didSet {
|
2020-01-08 19:22:11 +01:00
|
|
|
delegate?.webViewController(self, articleExtractorButtonStateDidUpdate: articleExtractorButtonState)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
weak var coordinator: SceneCoordinator!
|
2020-01-08 19:22:11 +01:00
|
|
|
weak var delegate: WebViewControllerDelegate?
|
2020-01-01 00:55:39 +01:00
|
|
|
|
2020-03-14 12:31:14 +01:00
|
|
|
private(set) var article: Article?
|
2020-01-01 00:55:39 +01:00
|
|
|
|
2020-05-15 17:45:05 +02:00
|
|
|
let scrollPositionQueue = CoalescingQueue(name: "Article Scroll Position", interval: 0.3, maxInterval: 0.3)
|
2020-02-01 05:07:08 +01:00
|
|
|
var windowScrollY = 0
|
2021-09-13 08:11:23 +02:00
|
|
|
private var restoreWindowScrollY: Int?
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
|
2021-09-12 22:44:24 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(currentArticleThemeDidChangeNotification(_:)), name: .CurrentArticleThemeDidChangeNotification, object: nil)
|
|
|
|
|
2020-02-01 05:07:08 +01:00
|
|
|
// Configure the tap zones
|
|
|
|
configureTopShowBarsView()
|
|
|
|
configureBottomShowBarsView()
|
|
|
|
|
|
|
|
loadWebView()
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
2021-05-05 04:44:55 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func avatarDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func faviconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
2021-09-12 22:44:24 +02:00
|
|
|
@objc func currentArticleThemeDidChangeNotification(_ note: Notification) {
|
|
|
|
loadWebView()
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@objc func showBars(_ sender: Any) {
|
|
|
|
showBars()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
2020-03-14 12:31:14 +01:00
|
|
|
func setArticle(_ article: Article?, updateView: Bool = true) {
|
|
|
|
stopArticleExtractor()
|
|
|
|
|
|
|
|
if article != self.article {
|
|
|
|
self.article = article
|
|
|
|
if updateView {
|
|
|
|
if article?.webFeed?.isArticleExtractorAlwaysOn ?? false {
|
|
|
|
startArticleExtractor()
|
|
|
|
}
|
|
|
|
windowScrollY = 0
|
|
|
|
loadWebView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-13 08:11:23 +02:00
|
|
|
func setScrollPosition(isShowingExtractedArticle: Bool, articleWindowScrollY: Int) {
|
|
|
|
if isShowingExtractedArticle {
|
|
|
|
switch articleExtractor?.state {
|
|
|
|
case .ready:
|
|
|
|
restoreWindowScrollY = articleWindowScrollY
|
|
|
|
startArticleExtractor()
|
|
|
|
case .complete:
|
|
|
|
windowScrollY = articleWindowScrollY
|
|
|
|
loadWebView()
|
|
|
|
case .processing:
|
|
|
|
restoreWindowScrollY = articleWindowScrollY
|
|
|
|
default:
|
|
|
|
restoreWindowScrollY = articleWindowScrollY
|
|
|
|
startArticleExtractor()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
windowScrollY = articleWindowScrollY
|
|
|
|
loadWebView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func focus() {
|
2020-02-01 05:07:08 +01:00
|
|
|
webView?.becomeFirstResponder()
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func canScrollDown() -> Bool {
|
2020-02-01 05:07:08 +01:00
|
|
|
guard let webView = webView else { return false }
|
2020-07-10 20:51:41 +02:00
|
|
|
return webView.scrollView.contentOffset.y < finalScrollPosition(scrollingUp: false)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 20:51:41 +02:00
|
|
|
func canScrollUp() -> Bool {
|
|
|
|
guard let webView = webView else { return false }
|
|
|
|
return webView.scrollView.contentOffset.y > finalScrollPosition(scrollingUp: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func scrollPage(up scrollingUp: Bool) {
|
2020-02-01 05:07:08 +01:00
|
|
|
guard let webView = webView else { return }
|
2020-07-10 20:51:41 +02:00
|
|
|
|
2020-06-08 02:05:56 +02:00
|
|
|
let overlap = 2 * UIFont.systemFont(ofSize: UIFont.systemFontSize).lineHeight * UIScreen.main.scale
|
2020-01-01 00:55:39 +01:00
|
|
|
let scrollToY: CGFloat = {
|
2020-07-10 20:51:41 +02:00
|
|
|
let scrollDistance = webView.scrollView.layoutMarginsGuide.layoutFrame.height - overlap;
|
|
|
|
let fullScroll = webView.scrollView.contentOffset.y + (scrollingUp ? -scrollDistance : scrollDistance)
|
|
|
|
let final = finalScrollPosition(scrollingUp: scrollingUp)
|
|
|
|
return (scrollingUp ? fullScroll > final : fullScroll < final) ? fullScroll : final
|
2020-01-01 00:55:39 +01:00
|
|
|
}()
|
2020-07-10 20:51:41 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
let convertedPoint = self.view.convert(CGPoint(x: 0, y: 0), to: webView.scrollView)
|
|
|
|
let scrollToPoint = CGPoint(x: convertedPoint.x, y: scrollToY)
|
|
|
|
webView.scrollView.setContentOffset(scrollToPoint, animated: true)
|
|
|
|
}
|
2020-07-10 20:51:41 +02:00
|
|
|
|
|
|
|
func scrollPageDown() {
|
|
|
|
scrollPage(up: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrollPageUp() {
|
|
|
|
scrollPage(up: true)
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func hideClickedImage() {
|
|
|
|
webView?.evaluateJavaScript("hideClickedImage();")
|
|
|
|
}
|
|
|
|
|
|
|
|
func showClickedImage(completion: @escaping () -> Void) {
|
|
|
|
clickedImageCompletion = completion
|
|
|
|
webView?.evaluateJavaScript("showClickedImage();")
|
|
|
|
}
|
|
|
|
|
|
|
|
func fullReload() {
|
2020-05-07 00:35:38 +02:00
|
|
|
loadWebView(replaceExistingWebView: true)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func showBars() {
|
2020-07-02 04:47:45 +02:00
|
|
|
AppDefaults.shared.articleFullscreenEnabled = false
|
2020-01-10 05:22:29 +01:00
|
|
|
coordinator.showStatusBar()
|
|
|
|
topShowBarsViewConstraint?.constant = 0
|
|
|
|
bottomShowBarsViewConstraint?.constant = 0
|
|
|
|
navigationController?.setNavigationBarHidden(false, animated: true)
|
|
|
|
navigationController?.setToolbarHidden(false, animated: true)
|
|
|
|
configureContextMenuInteraction()
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func hideBars() {
|
|
|
|
if isFullScreenAvailable {
|
2020-07-02 04:47:45 +02:00
|
|
|
AppDefaults.shared.articleFullscreenEnabled = true
|
2020-01-01 00:55:39 +01:00
|
|
|
coordinator.hideStatusBar()
|
2020-01-08 18:54:41 +01:00
|
|
|
topShowBarsViewConstraint?.constant = -44.0
|
|
|
|
bottomShowBarsViewConstraint?.constant = 44.0
|
2020-01-01 00:55:39 +01:00
|
|
|
navigationController?.setNavigationBarHidden(true, animated: true)
|
|
|
|
navigationController?.setToolbarHidden(true, animated: true)
|
|
|
|
configureContextMenuInteraction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleArticleExtractor() {
|
|
|
|
|
|
|
|
guard let article = article else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard articleExtractor?.state != .processing else {
|
|
|
|
stopArticleExtractor()
|
2020-02-14 01:08:44 +01:00
|
|
|
loadWebView()
|
2020-01-01 00:55:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard !isShowingExtractedArticle else {
|
|
|
|
isShowingExtractedArticle = false
|
2020-02-14 01:08:44 +01:00
|
|
|
loadWebView()
|
2020-01-01 00:55:39 +01:00
|
|
|
articleExtractorButtonState = .off
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let articleExtractor = articleExtractor {
|
|
|
|
if article.preferredLink == articleExtractor.articleLink {
|
|
|
|
isShowingExtractedArticle = true
|
2020-02-14 01:08:44 +01:00
|
|
|
loadWebView()
|
2020-01-01 00:55:39 +01:00
|
|
|
articleExtractorButtonState = .on
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
startArticleExtractor()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:30:52 +01:00
|
|
|
func stopArticleExtractorIfProcessing() {
|
|
|
|
if articleExtractor?.state == .processing {
|
|
|
|
stopArticleExtractor()
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 20:08:38 +01:00
|
|
|
|
|
|
|
func stopWebViewActivity() {
|
|
|
|
if let webView = webView {
|
|
|
|
stopMediaPlayback(webView)
|
|
|
|
cancelImageLoad(webView)
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 19:30:52 +01:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func showActivityDialog(popOverBarButtonItem: UIBarButtonItem? = nil) {
|
2021-04-25 23:28:19 +02:00
|
|
|
guard let url = article?.preferredURL else { return }
|
2021-04-03 16:06:51 +02:00
|
|
|
let activityViewController = UIActivityViewController(url: url, title: article?.title, applicationActivities: [FindInArticleActivity(), OpenInBrowserActivity()])
|
2020-01-01 00:55:39 +01:00
|
|
|
activityViewController.popoverPresentationController?.barButtonItem = popOverBarButtonItem
|
|
|
|
present(activityViewController, animated: true)
|
|
|
|
}
|
2020-05-15 09:09:33 +02:00
|
|
|
|
|
|
|
func openInAppBrowser() {
|
2021-04-25 23:28:19 +02:00
|
|
|
guard let url = article?.preferredURL else { return }
|
2021-08-26 04:27:23 +02:00
|
|
|
if AppDefaults.shared.useSystemBrowser {
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
} else {
|
2021-08-22 03:42:06 +02:00
|
|
|
let vc = SFSafariViewController(url: url)
|
|
|
|
present(vc, animated: true)
|
|
|
|
}
|
2020-05-15 09:09:33 +02:00
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: ArticleExtractorDelegate
|
|
|
|
|
|
|
|
extension WebViewController: ArticleExtractorDelegate {
|
|
|
|
|
|
|
|
func articleExtractionDidFail(with: Error) {
|
|
|
|
stopArticleExtractor()
|
|
|
|
articleExtractorButtonState = .error
|
2020-02-01 05:07:08 +01:00
|
|
|
loadWebView()
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func articleExtractionDidComplete(extractedArticle: ExtractedArticle) {
|
|
|
|
if articleExtractor?.state != .cancelled {
|
|
|
|
self.extractedArticle = extractedArticle
|
2021-09-13 08:11:23 +02:00
|
|
|
if let restoreWindowScrollY = restoreWindowScrollY {
|
|
|
|
windowScrollY = restoreWindowScrollY
|
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
isShowingExtractedArticle = true
|
2020-02-14 01:08:44 +01:00
|
|
|
loadWebView()
|
2020-01-01 00:55:39 +01:00
|
|
|
articleExtractorButtonState = .on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIContextMenuInteractionDelegate
|
|
|
|
|
|
|
|
extension WebViewController: UIContextMenuInteractionDelegate {
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: contextMenuPreviewProvider) { [weak self] suggestedActions in
|
|
|
|
guard let self = self else { return nil }
|
2020-11-13 17:18:48 +01:00
|
|
|
|
|
|
|
var menus = [UIMenu]()
|
2020-01-01 00:55:39 +01:00
|
|
|
|
2020-11-13 17:18:48 +01:00
|
|
|
var navActions = [UIAction]()
|
2020-01-01 00:55:39 +01:00
|
|
|
if let action = self.prevArticleAction() {
|
2020-11-13 17:18:48 +01:00
|
|
|
navActions.append(action)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
if let action = self.nextArticleAction() {
|
2020-11-13 17:18:48 +01:00
|
|
|
navActions.append(action)
|
|
|
|
}
|
|
|
|
if !navActions.isEmpty {
|
|
|
|
menus.append(UIMenu(title: "", options: .displayInline, children: navActions))
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
2020-11-13 17:18:48 +01:00
|
|
|
|
|
|
|
var toggleActions = [UIAction]()
|
2020-02-18 22:49:29 +01:00
|
|
|
if let action = self.toggleReadAction() {
|
2020-11-13 17:18:48 +01:00
|
|
|
toggleActions.append(action)
|
2020-02-18 22:49:29 +01:00
|
|
|
}
|
2020-11-13 17:18:48 +01:00
|
|
|
toggleActions.append(self.toggleStarredAction())
|
|
|
|
menus.append(UIMenu(title: "", options: .displayInline, children: toggleActions))
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
if let action = self.nextUnreadArticleAction() {
|
2020-11-13 17:18:48 +01:00
|
|
|
menus.append(UIMenu(title: "", options: .displayInline, children: [action]))
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
2020-11-13 17:18:48 +01:00
|
|
|
|
|
|
|
menus.append(UIMenu(title: "", options: .displayInline, children: [self.toggleArticleExtractorAction()]))
|
|
|
|
menus.append(UIMenu(title: "", options: .displayInline, children: [self.shareAction()]))
|
2020-01-01 00:55:39 +01:00
|
|
|
|
2020-11-13 17:18:48 +01:00
|
|
|
return UIMenu(title: "", children: menus)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
coordinator.showBrowserForCurrentArticle()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WKNavigationDelegate
|
|
|
|
|
|
|
|
extension WebViewController: WKNavigationDelegate {
|
2020-05-06 21:53:09 +02:00
|
|
|
|
2020-05-07 00:35:38 +02:00
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
2020-06-16 15:07:54 +02:00
|
|
|
for (index, view) in view.subviews.enumerated() {
|
|
|
|
if index != 0, let oldWebView = view as? PreloadedWebView {
|
|
|
|
oldWebView.removeFromSuperview()
|
|
|
|
}
|
2020-05-07 00:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
|
|
|
|
|
if navigationAction.navigationType == .linkActivated {
|
|
|
|
guard let url = navigationAction.request.url else {
|
|
|
|
decisionHandler(.allow)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
|
|
|
|
if components?.scheme == "http" || components?.scheme == "https" {
|
|
|
|
decisionHandler(.cancel)
|
2021-08-26 04:27:23 +02:00
|
|
|
if AppDefaults.shared.useSystemBrowser {
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
} else {
|
2021-09-01 21:13:04 +02:00
|
|
|
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { didOpen in
|
|
|
|
guard didOpen == false else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let vc = SFSafariViewController(url: url)
|
|
|
|
self.present(vc, animated: true)
|
|
|
|
}
|
2021-08-22 03:42:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:33:57 +02:00
|
|
|
} else if components?.scheme == "mailto" {
|
|
|
|
decisionHandler(.cancel)
|
|
|
|
|
2021-01-08 23:09:50 +01:00
|
|
|
guard let emailAddress = url.percentEncodedEmailAddress else {
|
2020-05-03 15:33:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:56:13 +01:00
|
|
|
if UIApplication.shared.canOpenURL(emailAddress) {
|
|
|
|
UIApplication.shared.open(emailAddress, options: [.universalLinksOnly : false], completionHandler: nil)
|
2020-05-03 15:33:57 +02:00
|
|
|
} else {
|
2020-05-04 07:24:35 +02:00
|
|
|
let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: NSLocalizedString("This device cannot send emails.", comment: "This device cannot send emails."), preferredStyle: .alert)
|
|
|
|
alert.addAction(.init(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil))
|
2020-05-03 15:33:57 +02:00
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
2020-05-03 16:37:01 +02:00
|
|
|
} else if components?.scheme == "tel" {
|
|
|
|
decisionHandler(.cancel)
|
|
|
|
|
|
|
|
if UIApplication.shared.canOpenURL(url) {
|
|
|
|
UIApplication.shared.open(url, options: [.universalLinksOnly : false], completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
} else {
|
|
|
|
decisionHandler(.allow)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
decisionHandler(.allow)
|
|
|
|
}
|
2020-05-06 21:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
|
|
|
|
fullReload()
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
2020-03-17 23:18:24 +01:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WKUIDelegate
|
|
|
|
|
|
|
|
extension WebViewController: WKUIDelegate {
|
2021-07-29 23:48:42 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func webView(_ webView: WKWebView, contextMenuForElement elementInfo: WKContextMenuElementInfo, willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
// We need to have at least an unimplemented WKUIDelegate assigned to the WKWebView. This makes the
|
2022-01-04 23:25:20 +01:00
|
|
|
// link preview launch Safari when the link preview is tapped. In theory, you should be able to get
|
2020-01-01 00:55:39 +01:00
|
|
|
// the link from the elementInfo above and transition to SFSafariViewController instead of launching
|
|
|
|
// Safari. As the time of this writing, the link in elementInfo is always nil. ¯\_(ツ)_/¯
|
|
|
|
}
|
2021-07-29 23:48:42 +02:00
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
|
|
|
|
guard let url = navigationAction.request.url else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
openURL(url)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WKScriptMessageHandler
|
|
|
|
|
|
|
|
extension WebViewController: WKScriptMessageHandler {
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
|
switch message.name {
|
|
|
|
case MessageName.imageWasShown:
|
|
|
|
clickedImageCompletion?()
|
|
|
|
case MessageName.imageWasClicked:
|
|
|
|
imageWasClicked(body: message.body as? String)
|
2020-04-29 04:26:57 +02:00
|
|
|
case MessageName.showFeedInspector:
|
|
|
|
if let webFeed = article?.webFeed {
|
|
|
|
coordinator.showFeedInspector(for: webFeed)
|
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewControllerTransitioningDelegate
|
|
|
|
|
|
|
|
extension WebViewController: UIViewControllerTransitioningDelegate {
|
|
|
|
|
|
|
|
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
|
|
|
transition.presenting = true
|
|
|
|
return transition
|
|
|
|
}
|
|
|
|
|
|
|
|
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
|
|
|
transition.presenting = false
|
|
|
|
return transition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:05:47 +01:00
|
|
|
// MARK:
|
|
|
|
|
|
|
|
extension WebViewController: UIScrollViewDelegate {
|
|
|
|
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
scrollPositionQueue.add(self, #selector(scrollPositionDidChange))
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func scrollPositionDidChange() {
|
2020-03-12 01:45:45 +01:00
|
|
|
webView?.evaluateJavaScript("window.scrollY") { (scrollY, error) in
|
|
|
|
guard error == nil else { return }
|
|
|
|
let javascriptScrollY = scrollY as? Int ?? 0
|
|
|
|
// I don't know why this value gets returned sometimes, but it is in error
|
|
|
|
guard javascriptScrollY != 33554432 else { return }
|
|
|
|
self.windowScrollY = javascriptScrollY
|
2020-01-21 19:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:15:16 +01:00
|
|
|
|
2020-05-18 10:05:59 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: JSON
|
|
|
|
|
|
|
|
private struct ImageClickMessage: Codable {
|
|
|
|
let x: Float
|
|
|
|
let y: Float
|
|
|
|
let width: Float
|
|
|
|
let height: Float
|
2020-01-01 02:06:27 +01:00
|
|
|
let imageTitle: String?
|
2020-01-01 00:55:39 +01:00
|
|
|
let imageURL: String
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension WebViewController {
|
|
|
|
|
2020-05-07 00:35:38 +02:00
|
|
|
func loadWebView(replaceExistingWebView: Bool = false) {
|
2020-02-01 05:07:08 +01:00
|
|
|
guard isViewLoaded else { return }
|
|
|
|
|
2020-05-07 00:35:38 +02:00
|
|
|
if !replaceExistingWebView, let webView = webView {
|
2020-03-17 23:18:24 +01:00
|
|
|
self.renderPage(webView)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:07:08 +01:00
|
|
|
coordinator.webViewProvider.dequeueWebView() { webView in
|
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
webView.ready {
|
|
|
|
|
|
|
|
// Add the webview
|
|
|
|
webView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
self.view.insertSubview(webView, at: 0)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
self.view.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
|
|
|
|
self.view.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
|
|
|
|
self.view.topAnchor.constraint(equalTo: webView.topAnchor),
|
|
|
|
self.view.bottomAnchor.constraint(equalTo: webView.bottomAnchor)
|
|
|
|
])
|
|
|
|
|
|
|
|
// UISplitViewController reports the wrong size to WKWebView which can cause horizontal
|
|
|
|
// rubberbanding on the iPad. This interferes with our UIPageViewController preventing
|
|
|
|
// us from easily swiping between WKWebViews. This hack fixes that.
|
|
|
|
webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: -1, bottom: 0, right: 0)
|
2020-02-05 22:46:59 +01:00
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
webView.scrollView.setZoomScale(1.0, animated: false)
|
2020-02-06 19:36:39 +01:00
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
self.view.setNeedsLayout()
|
|
|
|
self.view.layoutIfNeeded()
|
2020-02-01 05:07:08 +01:00
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
// Configure the webview
|
|
|
|
webView.navigationDelegate = self
|
|
|
|
webView.uiDelegate = self
|
|
|
|
webView.scrollView.delegate = self
|
|
|
|
self.configureContextMenuInteraction()
|
2020-02-01 05:07:08 +01:00
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
|
|
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown)
|
|
|
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.showFeedInspector)
|
2020-02-01 05:07:08 +01:00
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
self.renderPage(webView)
|
|
|
|
|
|
|
|
}
|
2020-02-01 05:07:08 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 00:10:51 +01:00
|
|
|
func renderPage(_ webView: PreloadedWebView?) {
|
2020-01-18 19:59:24 +01:00
|
|
|
guard let webView = webView else { return }
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
let theme = ArticleThemesManager.shared.currentTheme
|
2020-01-18 19:59:24 +01:00
|
|
|
let rendering: ArticleRenderer.Rendering
|
|
|
|
|
|
|
|
if let articleExtractor = articleExtractor, articleExtractor.state == .processing {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.loadingHTML(theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
} else if let articleExtractor = articleExtractor, articleExtractor.state == .failedToParse, let article = article {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
} else if let article = article, let extractedArticle = extractedArticle {
|
|
|
|
if isShowingExtractedArticle {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, extractedArticle: extractedArticle, theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
} else {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
}
|
|
|
|
} else if let article = article {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
} else {
|
2021-09-07 23:58:06 +02:00
|
|
|
rendering = ArticleRenderer.noSelectionHTML(theme: theme)
|
2020-01-18 19:59:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
let substitutions = [
|
|
|
|
"title": rendering.title,
|
|
|
|
"baseURL": rendering.baseURL,
|
|
|
|
"style": rendering.style,
|
|
|
|
"body": rendering.html,
|
|
|
|
"windowScrollY": String(windowScrollY)
|
|
|
|
]
|
|
|
|
|
|
|
|
let html = try! MacroProcessor.renderedText(withTemplate: ArticleRenderer.page.html, substitutions: substitutions)
|
|
|
|
webView.loadHTMLString(html, baseURL: ArticleRenderer.page.baseURL)
|
2020-01-18 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:51:41 +02:00
|
|
|
func finalScrollPosition(scrollingUp: Bool) -> CGFloat {
|
2020-02-01 05:07:08 +01:00
|
|
|
guard let webView = webView else { return 0 }
|
2020-07-10 20:51:41 +02:00
|
|
|
|
|
|
|
if scrollingUp {
|
|
|
|
return -webView.scrollView.safeAreaInsets.top
|
|
|
|
} else {
|
|
|
|
return webView.scrollView.contentSize.height - webView.scrollView.bounds.height + webView.scrollView.safeAreaInsets.bottom
|
|
|
|
}
|
2020-01-17 03:40:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func startArticleExtractor() {
|
2021-09-13 08:11:23 +02:00
|
|
|
guard articleExtractor == nil else { return }
|
2020-01-01 00:55:39 +01:00
|
|
|
if let link = article?.preferredLink, let extractor = ArticleExtractor(link) {
|
|
|
|
extractor.delegate = self
|
|
|
|
extractor.process()
|
|
|
|
articleExtractor = extractor
|
|
|
|
articleExtractorButtonState = .animated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopArticleExtractor() {
|
|
|
|
articleExtractor?.cancel()
|
|
|
|
articleExtractor = nil
|
|
|
|
isShowingExtractedArticle = false
|
|
|
|
articleExtractorButtonState = .off
|
|
|
|
}
|
|
|
|
|
|
|
|
func reloadArticleImage() {
|
2020-01-28 00:27:13 +01:00
|
|
|
guard let article = article else { return }
|
2020-02-09 22:08:11 +01:00
|
|
|
|
|
|
|
var components = URLComponents()
|
|
|
|
components.scheme = ArticleRenderer.imageIconScheme
|
|
|
|
components.path = article.articleID
|
|
|
|
|
|
|
|
if let imageSrc = components.string {
|
|
|
|
webView?.evaluateJavaScript("reloadArticleImage(\"\(imageSrc)\")")
|
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func imageWasClicked(body: String?) {
|
2020-02-01 05:07:08 +01:00
|
|
|
guard let webView = webView,
|
|
|
|
let body = body,
|
2020-01-01 00:55:39 +01:00
|
|
|
let data = body.data(using: .utf8),
|
|
|
|
let clickMessage = try? JSONDecoder().decode(ImageClickMessage.self, from: data),
|
|
|
|
let range = clickMessage.imageURL.range(of: ";base64,")
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
let base64Image = String(clickMessage.imageURL.suffix(from: range.upperBound))
|
|
|
|
if let imageData = Data(base64Encoded: base64Image), let image = UIImage(data: imageData) {
|
|
|
|
|
|
|
|
let y = CGFloat(clickMessage.y) + webView.safeAreaInsets.top
|
|
|
|
let rect = CGRect(x: CGFloat(clickMessage.x), y: y, width: CGFloat(clickMessage.width), height: CGFloat(clickMessage.height))
|
|
|
|
transition.originFrame = webView.convert(rect, to: nil)
|
|
|
|
|
|
|
|
if navigationController?.navigationBar.isHidden ?? false {
|
|
|
|
transition.maskFrame = webView.convert(webView.frame, to: nil)
|
|
|
|
} else {
|
|
|
|
transition.maskFrame = webView.convert(webView.safeAreaLayoutGuide.layoutFrame, to: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
transition.originImage = image
|
|
|
|
|
2020-01-01 02:06:27 +01:00
|
|
|
coordinator.showFullScreenImage(image: image, imageTitle: clickMessage.imageTitle, transitioningDelegate: self)
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-20 19:46:23 +01:00
|
|
|
|
2020-02-18 18:49:54 +01:00
|
|
|
func stopMediaPlayback(_ webView: WKWebView) {
|
|
|
|
webView.evaluateJavaScript("stopMediaPlayback();")
|
2020-01-20 19:46:23 +01:00
|
|
|
}
|
2020-02-18 18:49:54 +01:00
|
|
|
|
|
|
|
func cancelImageLoad(_ webView: WKWebView) {
|
|
|
|
webView.evaluateJavaScript("cancelImageLoad();")
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func configureTopShowBarsView() {
|
|
|
|
topShowBarsView = UIView()
|
|
|
|
topShowBarsView.backgroundColor = .clear
|
|
|
|
topShowBarsView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(topShowBarsView)
|
|
|
|
|
2020-07-02 04:47:45 +02:00
|
|
|
if AppDefaults.shared.articleFullscreenEnabled {
|
2020-01-01 00:55:39 +01:00
|
|
|
topShowBarsViewConstraint = view.topAnchor.constraint(equalTo: topShowBarsView.bottomAnchor, constant: -44.0)
|
|
|
|
} else {
|
|
|
|
topShowBarsViewConstraint = view.topAnchor.constraint(equalTo: topShowBarsView.bottomAnchor, constant: 0.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
topShowBarsViewConstraint,
|
|
|
|
view.leadingAnchor.constraint(equalTo: topShowBarsView.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: topShowBarsView.trailingAnchor),
|
|
|
|
topShowBarsView.heightAnchor.constraint(equalToConstant: 44.0)
|
|
|
|
])
|
|
|
|
topShowBarsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showBars(_:))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureBottomShowBarsView() {
|
|
|
|
bottomShowBarsView = UIView()
|
|
|
|
topShowBarsView.backgroundColor = .clear
|
|
|
|
bottomShowBarsView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(bottomShowBarsView)
|
2020-07-02 04:47:45 +02:00
|
|
|
if AppDefaults.shared.articleFullscreenEnabled {
|
2020-01-01 00:55:39 +01:00
|
|
|
bottomShowBarsViewConstraint = view.bottomAnchor.constraint(equalTo: bottomShowBarsView.topAnchor, constant: 44.0)
|
|
|
|
} else {
|
|
|
|
bottomShowBarsViewConstraint = view.bottomAnchor.constraint(equalTo: bottomShowBarsView.topAnchor, constant: 0.0)
|
|
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
bottomShowBarsViewConstraint,
|
|
|
|
view.leadingAnchor.constraint(equalTo: bottomShowBarsView.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: bottomShowBarsView.trailingAnchor),
|
|
|
|
bottomShowBarsView.heightAnchor.constraint(equalToConstant: 44.0)
|
|
|
|
])
|
|
|
|
bottomShowBarsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showBars(_:))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureContextMenuInteraction() {
|
|
|
|
if isFullScreenAvailable {
|
|
|
|
if navigationController?.isNavigationBarHidden ?? false {
|
|
|
|
webView?.addInteraction(contextMenuInteraction)
|
|
|
|
} else {
|
|
|
|
webView?.removeInteraction(contextMenuInteraction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contextMenuPreviewProvider() -> UIViewController {
|
|
|
|
let previewProvider = UIStoryboard.main.instantiateController(ofType: ContextMenuPreviewViewController.self)
|
|
|
|
previewProvider.article = article
|
|
|
|
return previewProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func prevArticleAction() -> UIAction? {
|
|
|
|
guard coordinator.isPrevArticleAvailable else { return nil }
|
|
|
|
let title = NSLocalizedString("Previous Article", comment: "Previous Article")
|
|
|
|
return UIAction(title: title, image: AppAssets.prevArticleImage) { [weak self] action in
|
|
|
|
self?.coordinator.selectPrevArticle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextArticleAction() -> UIAction? {
|
|
|
|
guard coordinator.isNextArticleAvailable else { return nil }
|
|
|
|
let title = NSLocalizedString("Next Article", comment: "Next Article")
|
|
|
|
return UIAction(title: title, image: AppAssets.nextArticleImage) { [weak self] action in
|
|
|
|
self?.coordinator.selectNextArticle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 22:49:29 +01:00
|
|
|
func toggleReadAction() -> UIAction? {
|
|
|
|
guard let article = article, !article.status.read || article.isAvailableToMarkUnread else { return nil }
|
|
|
|
|
|
|
|
let title = article.status.read ? NSLocalizedString("Mark as Unread", comment: "Mark as Unread") : NSLocalizedString("Mark as Read", comment: "Mark as Read")
|
|
|
|
let readImage = article.status.read ? AppAssets.circleClosedImage : AppAssets.circleOpenImage
|
2020-01-01 00:55:39 +01:00
|
|
|
return UIAction(title: title, image: readImage) { [weak self] action in
|
|
|
|
self?.coordinator.toggleReadForCurrentArticle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleStarredAction() -> UIAction {
|
|
|
|
let starred = article?.status.starred ?? false
|
|
|
|
let title = starred ? NSLocalizedString("Mark as Unstarred", comment: "Mark as Unstarred") : NSLocalizedString("Mark as Starred", comment: "Mark as Starred")
|
|
|
|
let starredImage = starred ? AppAssets.starOpenImage : AppAssets.starClosedImage
|
|
|
|
return UIAction(title: title, image: starredImage) { [weak self] action in
|
|
|
|
self?.coordinator.toggleStarredForCurrentArticle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextUnreadArticleAction() -> UIAction? {
|
|
|
|
guard coordinator.isAnyUnreadAvailable else { return nil }
|
|
|
|
let title = NSLocalizedString("Next Unread Article", comment: "Next Unread Article")
|
|
|
|
return UIAction(title: title, image: AppAssets.nextUnreadArticleImage) { [weak self] action in
|
|
|
|
self?.coordinator.selectNextUnread()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleArticleExtractorAction() -> UIAction {
|
|
|
|
let extracted = articleExtractorButtonState == .on
|
|
|
|
let title = extracted ? NSLocalizedString("Show Feed Article", comment: "Show Feed Article") : NSLocalizedString("Show Reader View", comment: "Show Reader View")
|
|
|
|
let extractorImage = extracted ? AppAssets.articleExtractorOffSF : AppAssets.articleExtractorOnSF
|
|
|
|
return UIAction(title: title, image: extractorImage) { [weak self] action in
|
|
|
|
self?.toggleArticleExtractor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shareAction() -> UIAction {
|
|
|
|
let title = NSLocalizedString("Share", comment: "Share")
|
|
|
|
return UIAction(title: title, image: AppAssets.shareImage) { [weak self] action in
|
|
|
|
self?.showActivityDialog()
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 23:48:42 +02:00
|
|
|
|
|
|
|
// If the resource cannot be opened with an installed app, present the web view.
|
|
|
|
func openURL(_ url: URL) {
|
|
|
|
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { didOpen in
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
guard didOpen == false else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let vc = SFSafariViewController(url: url)
|
|
|
|
self.present(vc, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
}
|
2020-05-11 22:08:01 +02:00
|
|
|
|
|
|
|
// MARK: Find in Article
|
|
|
|
|
|
|
|
private struct FindInArticleOptions: Codable {
|
|
|
|
var text: String
|
|
|
|
var caseSensitive = false
|
2020-05-13 11:29:09 +02:00
|
|
|
var regex = false
|
2020-05-11 22:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal struct FindInArticleState: Codable {
|
|
|
|
struct WebViewClientRect: Codable {
|
|
|
|
let x: Double
|
|
|
|
let y: Double
|
|
|
|
let width: Double
|
|
|
|
let height: Double
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FindInArticleResult: Codable {
|
|
|
|
let rects: [WebViewClientRect]
|
|
|
|
let bounds: WebViewClientRect
|
|
|
|
let index: UInt
|
|
|
|
let matchGroups: [String]
|
|
|
|
}
|
|
|
|
|
|
|
|
let index: UInt?
|
|
|
|
let results: [FindInArticleResult]
|
|
|
|
let count: UInt
|
|
|
|
}
|
|
|
|
|
|
|
|
extension WebViewController {
|
2020-05-14 13:20:40 +02:00
|
|
|
|
2020-05-11 22:08:01 +02:00
|
|
|
func searchText(_ searchText: String, completionHandler: @escaping (FindInArticleState) -> Void) {
|
|
|
|
guard let json = try? JSONEncoder().encode(FindInArticleOptions(text: searchText)) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let encoded = json.base64EncodedString()
|
|
|
|
|
|
|
|
webView?.evaluateJavaScript("updateFind(\"\(encoded)\")") {
|
|
|
|
(result, error) in
|
|
|
|
guard error == nil,
|
|
|
|
let b64 = result as? String,
|
|
|
|
let rawData = Data(base64Encoded: b64),
|
|
|
|
let findState = try? JSONDecoder().decode(FindInArticleState.self, from: rawData) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
completionHandler(findState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func endSearch() {
|
|
|
|
webView?.evaluateJavaScript("endFind()")
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectNextSearchResult() {
|
|
|
|
webView?.evaluateJavaScript("selectNextResult()")
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectPreviousSearchResult() {
|
|
|
|
webView?.evaluateJavaScript("selectPreviousResult()")
|
|
|
|
}
|
2020-05-18 10:05:59 +02:00
|
|
|
|
2020-05-11 22:08:01 +02:00
|
|
|
}
|