2019-04-15 22:03:05 +02:00
|
|
|
//
|
2019-09-24 11:29:15 +02:00
|
|
|
// ArticleViewController.swift
|
2019-04-15 22:03:05 +02:00
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/8/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import WebKit
|
|
|
|
import Account
|
|
|
|
import Articles
|
2019-04-21 13:41:59 +02:00
|
|
|
import SafariServices
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
enum ArticleViewState: Equatable {
|
2019-09-24 02:23:23 +02:00
|
|
|
case noSelection
|
|
|
|
case multipleSelection
|
|
|
|
case loading
|
|
|
|
case article(Article)
|
|
|
|
case extracted(Article, ExtractedArticle)
|
|
|
|
}
|
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
class ArticleViewController: UIViewController {
|
2019-10-12 21:45:44 +02:00
|
|
|
|
|
|
|
private struct MessageName {
|
|
|
|
static let imageWasClicked = "imageWasClicked"
|
2019-10-16 23:40:49 +02:00
|
|
|
static let imageWasShown = "imageWasShown"
|
2019-10-12 21:45:44 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-08-31 21:30:01 +02:00
|
|
|
@IBOutlet private weak var nextUnreadBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var prevArticleBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var nextArticleBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var readBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var starBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var actionBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var browserBarButtonItem: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var webViewContainer: UIView!
|
2019-09-27 18:20:26 +02:00
|
|
|
|
2019-09-27 21:09:28 +02:00
|
|
|
private var articleExtractorButton: ArticleExtractorButton = {
|
|
|
|
let button = ArticleExtractorButton(type: .system)
|
2019-09-28 00:32:13 +02:00
|
|
|
button.frame = CGRect(x: 0, y: 0, width: 44.0, height: 44.0)
|
2019-09-27 21:09:28 +02:00
|
|
|
button.setImage(AppAssets.articleExtractorOff, for: .normal)
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2019-08-31 21:30:01 +02:00
|
|
|
private var webView: WKWebView!
|
2019-10-16 23:40:49 +02:00
|
|
|
private lazy var transition = ImageTransition(controller: self)
|
|
|
|
private var clickedImageCompletion: (() -> Void)?
|
2019-08-31 21:30:01 +02:00
|
|
|
|
2019-09-01 19:43:07 +02:00
|
|
|
weak var coordinator: SceneCoordinator!
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
var state: ArticleViewState = .noSelection {
|
2019-09-24 02:23:23 +02:00
|
|
|
didSet {
|
|
|
|
if state != oldValue {
|
|
|
|
updateUI()
|
|
|
|
reloadHTML()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentArticle: Article? {
|
|
|
|
switch state {
|
|
|
|
case .article(let article):
|
|
|
|
return article
|
|
|
|
case .extracted(let article, _):
|
|
|
|
return article
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 01:08:13 +02:00
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
var articleExtractorButtonState: ArticleExtractorButtonState {
|
|
|
|
get {
|
|
|
|
return articleExtractorButton.buttonState
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
articleExtractorButton.buttonState = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 21:37:07 +02:00
|
|
|
private let keyboardManager = KeyboardManager(type: .detail)
|
2019-09-05 04:06:29 +02:00
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return keyboardManager.keyCommands
|
|
|
|
}
|
|
|
|
|
2019-08-31 21:53:03 +02:00
|
|
|
deinit {
|
2019-10-01 10:51:05 +02:00
|
|
|
if webView != nil {
|
2019-10-30 17:06:55 +01:00
|
|
|
webView?.evaluateJavaScript("cancelImageLoad();")
|
|
|
|
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked)
|
|
|
|
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasShown)
|
2019-10-01 10:51:05 +02:00
|
|
|
webView.removeFromSuperview()
|
|
|
|
ArticleViewControllerWebViewProvider.shared.enqueueWebView(webView)
|
|
|
|
webView = nil
|
|
|
|
}
|
2019-08-31 21:53:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2019-08-31 21:30:01 +02:00
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-04-15 22:03:05 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
|
2019-04-27 17:49:54 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange(_:)), name: UIContentSizeCategory.didChangeNotification, object: nil)
|
2019-09-28 00:23:38 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
articleExtractorButton.addTarget(self, action: #selector(toggleArticleExtractor(_:)), for: .touchUpInside)
|
2019-09-27 18:20:26 +02:00
|
|
|
navigationItem.titleView = articleExtractorButton
|
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
ArticleViewControllerWebViewProvider.shared.dequeueWebView() { webView in
|
2019-09-21 17:37:21 +02:00
|
|
|
|
|
|
|
self.webView = webView
|
|
|
|
self.webViewContainer.addChildAndPin(webView)
|
2019-09-21 19:43:15 +02:00
|
|
|
webView.navigationDelegate = self
|
2019-10-01 18:09:46 +02:00
|
|
|
webView.uiDelegate = self
|
2019-10-12 21:45:44 +02:00
|
|
|
|
2019-10-16 18:31:20 +02:00
|
|
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
2019-10-16 23:40:49 +02:00
|
|
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown)
|
2019-10-12 21:45:44 +02:00
|
|
|
|
2019-09-21 19:43:15 +02:00
|
|
|
// Even though page.html should be loaded into this webview, we have to do it again
|
|
|
|
// to work around this bug: http://www.openradar.me/22855188
|
2019-10-19 23:25:06 +02:00
|
|
|
let url = Bundle.main.url(forResource: "page", withExtension: "html")!
|
|
|
|
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
|
2019-09-21 19:43:15 +02:00
|
|
|
|
2019-09-21 17:37:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
func updateUI() {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-09-24 02:23:23 +02:00
|
|
|
guard let article = currentArticle else {
|
2019-09-24 23:34:11 +02:00
|
|
|
articleExtractorButton.isEnabled = false
|
2019-04-22 00:42:26 +02:00
|
|
|
nextUnreadBarButtonItem.isEnabled = false
|
|
|
|
prevArticleBarButtonItem.isEnabled = false
|
|
|
|
nextArticleBarButtonItem.isEnabled = false
|
2019-04-15 22:03:05 +02:00
|
|
|
readBarButtonItem.isEnabled = false
|
|
|
|
starBarButtonItem.isEnabled = false
|
|
|
|
browserBarButtonItem.isEnabled = false
|
|
|
|
actionBarButtonItem.isEnabled = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:45:39 +02:00
|
|
|
nextUnreadBarButtonItem.isEnabled = coordinator.isAnyUnreadAvailable
|
|
|
|
prevArticleBarButtonItem.isEnabled = coordinator.isPrevArticleAvailable
|
|
|
|
nextArticleBarButtonItem.isEnabled = coordinator.isNextArticleAvailable
|
2019-04-22 00:42:26 +02:00
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
articleExtractorButton.isEnabled = true
|
2019-04-15 22:03:05 +02:00
|
|
|
readBarButtonItem.isEnabled = true
|
|
|
|
starBarButtonItem.isEnabled = true
|
|
|
|
browserBarButtonItem.isEnabled = true
|
|
|
|
actionBarButtonItem.isEnabled = true
|
|
|
|
|
|
|
|
let readImage = article.status.read ? AppAssets.circleOpenImage : AppAssets.circleClosedImage
|
|
|
|
readBarButtonItem.image = readImage
|
|
|
|
|
|
|
|
let starImage = article.status.starred ? AppAssets.starClosedImage : AppAssets.starOpenImage
|
|
|
|
starBarButtonItem.image = starImage
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func reloadHTML() {
|
2019-09-24 02:23:23 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
let style = ArticleStylesManager.shared.currentStyle
|
2019-09-24 02:23:23 +02:00
|
|
|
let rendering: ArticleRenderer.Rendering
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case .noSelection:
|
|
|
|
rendering = ArticleRenderer.noSelectionHTML(style: style)
|
|
|
|
case .multipleSelection:
|
|
|
|
rendering = ArticleRenderer.multipleSelectionHTML(style: style)
|
|
|
|
case .loading:
|
|
|
|
rendering = ArticleRenderer.loadingHTML(style: style)
|
|
|
|
case .article(let article):
|
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, style: style)
|
|
|
|
case .extracted(let article, let extractedArticle):
|
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, extractedArticle: extractedArticle, style: style)
|
|
|
|
}
|
2019-09-21 03:33:28 +02:00
|
|
|
|
|
|
|
let templateData = TemplateData(style: rendering.style, body: rendering.html)
|
2019-04-22 00:42:26 +02:00
|
|
|
|
2019-09-21 03:33:28 +02:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
var render = "error();"
|
|
|
|
if let data = try? encoder.encode(templateData) {
|
|
|
|
let json = String(data: data, encoding: .utf8)!
|
|
|
|
render = "render(\(json));"
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:52:49 +01:00
|
|
|
webView?.scrollView.setZoomScale(1.0, animated: false)
|
2019-09-24 02:23:23 +02:00
|
|
|
webView?.evaluateJavaScript(render)
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc dynamic func unreadCountDidChange(_ notification: Notification) {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
@objc func statusesDidChange(_ note: Notification) {
|
|
|
|
guard let articles = note.userInfo?[Account.UserInfoKey.articles] as? Set<Article> else {
|
|
|
|
return
|
|
|
|
}
|
2019-09-24 02:23:23 +02:00
|
|
|
if articles.count == 1 && articles.first?.articleID == currentArticle?.articleID {
|
2019-04-23 11:35:48 +02:00
|
|
|
updateUI()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 17:49:54 +02:00
|
|
|
@objc func contentSizeCategoryDidChange(_ note: Notification) {
|
|
|
|
reloadHTML()
|
|
|
|
}
|
|
|
|
|
2019-09-28 00:23:38 +02:00
|
|
|
// Don't delete this even though it looks like it isn't doing anything. This is to work
|
|
|
|
// around a bug (don't know if it is an Apple one or ours) that happens when the root
|
|
|
|
// split view controller is not collapsed. When the app goes to the background and then
|
|
|
|
// comes back to the foreground the article extractor button will disappear. The
|
|
|
|
// navigationItem will still have a reference to the articleExtractorButton, but if you
|
|
|
|
// check in the view debugger, the button isn't in the view hierarchy anymore.
|
|
|
|
// Setting the titleView to nil and then back to the articleExtractorButton hides that
|
|
|
|
// this happened. If we move the articleExtractorButton to someplace other than the
|
|
|
|
// titleView, then this code can be safely deleted.
|
|
|
|
@objc func willEnterForeground(_ note: Notification) {
|
|
|
|
navigationItem.titleView = nil
|
|
|
|
navigationItem.titleView = articleExtractorButton
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
// MARK: Actions
|
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
@IBAction func toggleArticleExtractor(_ sender: Any) {
|
2019-09-24 13:46:53 +02:00
|
|
|
coordinator.toggleArticleExtractor()
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
@IBAction func nextUnread(_ sender: Any) {
|
2019-07-06 00:45:39 +02:00
|
|
|
coordinator.selectNextUnread()
|
2019-04-22 00:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func prevArticle(_ sender: Any) {
|
2019-07-06 18:32:19 +02:00
|
|
|
coordinator.selectPrevArticle()
|
2019-04-22 00:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func nextArticle(_ sender: Any) {
|
2019-07-06 18:32:19 +02:00
|
|
|
coordinator.selectNextArticle()
|
2019-04-22 00:42:26 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
@IBAction func toggleRead(_ sender: Any) {
|
2019-07-06 18:49:53 +02:00
|
|
|
coordinator.toggleReadForCurrentArticle()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func toggleStar(_ sender: Any) {
|
2019-09-05 22:43:01 +02:00
|
|
|
coordinator.toggleStarredForCurrentArticle()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func openBrowser(_ sender: Any) {
|
2019-07-06 18:49:53 +02:00
|
|
|
coordinator.showBrowserForCurrentArticle()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func showActivityDialog(_ sender: Any) {
|
2019-09-24 02:23:23 +02:00
|
|
|
guard let preferredLink = currentArticle?.preferredLink, let url = URL(string: preferredLink) else {
|
2019-08-20 01:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 02:23:23 +02:00
|
|
|
let itemSource = ArticleActivityItemSource(url: url, subject: currentArticle!.title)
|
2019-08-20 01:13:21 +02:00
|
|
|
let activityViewController = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil)
|
|
|
|
activityViewController.popoverPresentationController?.barButtonItem = actionBarButtonItem
|
|
|
|
present(activityViewController, animated: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-08-25 18:38:04 +02:00
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
// MARK: Keyboard Shortcuts
|
|
|
|
@objc func navigateToTimeline(_ sender: Any?) {
|
|
|
|
coordinator.navigateToTimeline()
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:38:04 +02:00
|
|
|
// MARK: API
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
func focus() {
|
|
|
|
webView.becomeFirstResponder()
|
|
|
|
}
|
2019-08-25 18:38:04 +02:00
|
|
|
|
2019-09-06 04:14:19 +02:00
|
|
|
func finalScrollPosition() -> CGFloat {
|
|
|
|
return webView.scrollView.contentSize.height - webView.scrollView.bounds.size.height + webView.scrollView.contentInset.bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
func canScrollDown() -> Bool {
|
|
|
|
return webView.scrollView.contentOffset.y < finalScrollPosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrollPageDown() {
|
|
|
|
let scrollToY: CGFloat = {
|
|
|
|
let fullScroll = webView.scrollView.contentOffset.y + webView.scrollView.bounds.size.height
|
|
|
|
let final = finalScrollPosition()
|
2019-09-06 04:19:00 +02:00
|
|
|
return fullScroll < final ? fullScroll : final
|
2019-09-06 04:14:19 +02: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)
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:40:49 +02:00
|
|
|
func hideClickedImage() {
|
|
|
|
webView?.evaluateJavaScript("hideClickedImage();")
|
|
|
|
}
|
|
|
|
|
|
|
|
func showClickedImage(completion: @escaping () -> Void) {
|
|
|
|
clickedImageCompletion = completion
|
|
|
|
webView?.evaluateJavaScript("showClickedImage();")
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 03:33:28 +02:00
|
|
|
// MARK: WKNavigationDelegate
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
extension ArticleViewController: WKNavigationDelegate {
|
2019-04-15 22:03:05 +02: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" {
|
2019-04-21 13:41:59 +02:00
|
|
|
let vc = SFSafariViewController(url: url)
|
|
|
|
present(vc, animated: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
decisionHandler(.cancel)
|
|
|
|
} else {
|
|
|
|
decisionHandler(.allow)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
decisionHandler(.allow)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-21 19:43:15 +02:00
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
2019-09-24 02:23:23 +02:00
|
|
|
self.updateUI()
|
|
|
|
self.reloadHTML()
|
2019-09-21 19:43:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-23 14:26:35 +02:00
|
|
|
|
2019-10-01 18:09:46 +02:00
|
|
|
// MARK: WKUIDelegate
|
|
|
|
|
|
|
|
extension ArticleViewController: WKUIDelegate {
|
|
|
|
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
|
|
|
|
// link preview launch Safari when the link preview is tapped. In theory, you shoud be able to get
|
|
|
|
// 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. ¯\_(ツ)_/¯
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 21:45:44 +02:00
|
|
|
// MARK: WKScriptMessageHandler
|
|
|
|
|
|
|
|
extension ArticleViewController: WKScriptMessageHandler {
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
2019-10-16 23:40:49 +02:00
|
|
|
switch message.name {
|
|
|
|
case MessageName.imageWasShown:
|
|
|
|
clickedImageCompletion?()
|
|
|
|
case MessageName.imageWasClicked:
|
|
|
|
imageWasClicked(body: message.body as? String)
|
|
|
|
default:
|
|
|
|
return
|
2019-10-12 21:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 02:41:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:31:20 +02:00
|
|
|
class WrapperScriptMessageHandler: NSObject, WKScriptMessageHandler {
|
|
|
|
|
|
|
|
// We need to wrap a message handler to prevent a circlular reference
|
|
|
|
private weak var handler: WKScriptMessageHandler?
|
|
|
|
|
|
|
|
init(_ handler: WKScriptMessageHandler) {
|
|
|
|
self.handler = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
|
handler?.userContentController(userContentController, didReceive: message)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-16 01:08:13 +02:00
|
|
|
// MARK: UIViewControllerTransitioningDelegate
|
|
|
|
|
|
|
|
extension ArticleViewController: 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
// MARK: JSON
|
2019-10-16 01:08:13 +02:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
private struct TemplateData: Codable {
|
|
|
|
let style: String
|
|
|
|
let body: String
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct ImageClickMessage: Codable {
|
|
|
|
let x: Float
|
|
|
|
let y: Float
|
|
|
|
let width: Float
|
|
|
|
let height: Float
|
|
|
|
let imageURL: String
|
2019-10-12 21:45:44 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 03:33:28 +02:00
|
|
|
// MARK: Private
|
|
|
|
|
2019-09-24 11:29:15 +02:00
|
|
|
private extension ArticleViewController {
|
2019-04-23 14:26:35 +02:00
|
|
|
|
2019-10-16 23:40:49 +02:00
|
|
|
func imageWasClicked(body: String?) {
|
|
|
|
guard let body = body,
|
|
|
|
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 rect = CGRect(x: CGFloat(clickMessage.x), y: CGFloat(clickMessage.y), width: CGFloat(clickMessage.width), height: CGFloat(clickMessage.height))
|
|
|
|
transition.originFrame = webView.convert(rect, to: nil)
|
2019-10-17 02:32:33 +02:00
|
|
|
transition.maskFrame = webView.convert(webView.frame, to: nil)
|
2019-10-16 23:40:49 +02:00
|
|
|
transition.originImage = image
|
|
|
|
|
2019-10-17 03:20:36 +02:00
|
|
|
coordinator.showFullScreenImage(image: image, transitioningDelegate: self)
|
2019-10-16 23:40:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|