Add fullscreen image previewing and zooming

This commit is contained in:
Maurice Parker
2019-10-12 14:45:44 -05:00
parent 5ed508b709
commit 3ee0506b4a
6 changed files with 546 additions and 3 deletions

View File

@@ -21,6 +21,10 @@ enum ArticleViewState: Equatable {
}
class ArticleViewController: UIViewController {
private struct MessageName {
static let imageWasClicked = "imageWasClicked"
}
@IBOutlet private weak var nextUnreadBarButtonItem: UIBarButtonItem!
@IBOutlet private weak var prevArticleBarButtonItem: UIBarButtonItem!
@@ -102,7 +106,10 @@ class ArticleViewController: UIViewController {
self.webViewContainer.addChildAndPin(webView)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked)
webView.configuration.userContentController.add(self, name: MessageName.imageWasClicked)
// 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
webView.loadHTMLString(ArticleRenderer.page.html, baseURL: ArticleRenderer.page.baseURL)
@@ -337,6 +344,20 @@ extension ArticleViewController: WKUIDelegate {
}
}
// MARK: WKScriptMessageHandler
extension ArticleViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == MessageName.imageWasClicked, let link = message.body as? String, let url = URL(string: link) {
let imageVC = UIStoryboard.main.instantiateController(ofType: ImageViewController.self)
imageVC.url = url
imageVC.modalPresentationStyle = .fullScreen
present(imageVC, animated: true)
}
}
}
// MARK: Private
private extension ArticleViewController {