// // ReadViewController.swift // Reader // // Created by Shadowfacts on 1/9/22. // import UIKit import WebKit import HTMLEntities import SafariServices class ReadViewController: UIViewController { private static let publishedFormatter: DateFormatter = { let f = DateFormatter() f.dateStyle = .medium f.timeStyle = .medium return f }() let fervorController: FervorController let item: Item #if targetEnvironment(macCatalyst) private var itemReadObservation: NSKeyValueObservation? #endif override var prefersStatusBarHidden: Bool { navigationController?.isNavigationBarHidden ?? false } override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { .slide } init(item: Item, fervorController: FervorController) { self.fervorController = fervorController self.item = item super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() navigationItem.largeTitleDisplayMode = .never view.backgroundColor = .appBackground view.addInteraction(StretchyMenuInteraction(delegate: self)) let webView = WKWebView() webView.translatesAutoresizingMaskIntoConstraints = false webView.navigationDelegate = self webView.uiDelegate = self // transparent background required to prevent white flash in dark mode, just using .appBackground doesn't work webView.isOpaque = false webView.backgroundColor = .clear if let content = itemContentHTML() { webView.loadHTMLString(content, baseURL: item.url) } view.addSubview(webView) NSLayoutConstraint.activate([ webView.leadingAnchor.constraint(equalTo: view.leadingAnchor), webView.trailingAnchor.constraint(equalTo: view.trailingAnchor), webView.topAnchor.constraint(equalTo: view.topAnchor), webView.bottomAnchor.constraint(equalTo: view.bottomAnchor), ]) if let url = item.url { activityItemsConfiguration = UIActivityItemsConfiguration(objects: [url as NSURL]) } #if targetEnvironment(macCatalyst) itemReadObservation = item.observe(\.read) { [unowned self] _, _ in self.updateToggleReadToolbarImage() } #endif } private static let css = try! String(contentsOf: Bundle.main.url(forResource: "read", withExtension: "css")!) private static let js = try! String(contentsOf: Bundle.main.url(forResource: "read", withExtension: "js")!) private func itemContentHTML() -> String? { guard let content = item.content else { return nil } var info = "" if let title = item.title, !title.isEmpty { info += "

" if let url = item.url { info += "" } info += title.htmlEscape() if item.url != nil { info += "" } info += "

" } if let feedTitle = item.feed!.title, !feedTitle.isEmpty { info += "

\(feedTitle.htmlEscape())

" } if let author = item.author, !author.isEmpty { info += "

\(author)

" } if let published = item.published { let formatted = ReadViewController.publishedFormatter.string(from: published) info += "

\(formatted)

" } return """
\(info)
\(content)
""" } private func createSafariVC(url: URL) -> SFSafariViewController { let vc = SFSafariViewController(url: url) vc.preferredControlTintColor = .appTintColor return vc } #if targetEnvironment(macCatalyst) @objc func toggleItemRead(_ item: NSToolbarItem) { Task { await fervorController.markItem(self.item, read: !self.item.read) updateToggleReadToolbarImage() } } private func updateToggleReadToolbarImage() { guard let titlebar = view.window?.windowScene?.titlebar, let item = titlebar.toolbar?.items.first(where: { $0.itemIdentifier == .toggleItemRead }) else { return } item.image = UIImage(systemName: self.item.read ? "checkmark.circle.fill" : "checkmark.circle") } #endif } extension ReadViewController: WKNavigationDelegate { func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy { if navigationAction.navigationType == .linkActivated { let url = navigationAction.request.url! if url.fragment != nil { var components = URLComponents(url: url, resolvingAgainstBaseURL: false)! components.fragment = nil if components.url == item.url { return .allow } } present(createSafariVC(url: url), animated: true) return .cancel } else { return .allow } } } extension ReadViewController: WKUIDelegate { func webView(_ webView: WKWebView, contextMenuConfigurationFor elementInfo: WKContextMenuElementInfo) async -> UIContextMenuConfiguration? { guard let url = elementInfo.linkURL, ["http", "https"].contains(url.scheme?.lowercased()) else { return nil } return UIContextMenuConfiguration(identifier: nil) { [unowned self] in self.createSafariVC(url: url) } actionProvider: { _ in return UIMenu(children: [ UIAction(title: "Open in Safari", image: UIImage(systemName: "safari"), handler: { [weak self] _ in guard let self = self else { return } self.present(self.createSafariVC(url: url), animated: true) }), UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up"), handler: { [weak self] _ in self?.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true) }), ]) } } func webView(_ webView: WKWebView, contextMenuForElement elementInfo: WKContextMenuElementInfo, willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) { if let vc = animator.previewViewController as? SFSafariViewController { animator.preferredCommitStyle = .pop animator.addCompletion { self.present(vc, animated: true) } } } } extension ReadViewController: StretchyMenuInteractionDelegate { func stretchyMenuTitle() -> String? { return nil } func stretchyMenuItems() -> [StretchyMenuItem] { guard let url = item.url else { return [] } var items = [ StretchyMenuItem(title: "Open in Safari", subtitle: nil, action: { [unowned self] in self.present(createSafariVC(url: url), animated: true) }), StretchyMenuItem(title: item.read ? "Mark as Unread" : "Mark as Read", subtitle: nil, action: { [unowned self] in Task { await self.fervorController.markItem(item, read: !item.read) } }), ] #if !targetEnvironment(macCatalyst) items.insert(StretchyMenuItem(title: "Share", subtitle: nil, action: { [unowned self] in self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true) }), at: 1) #endif return items } }