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
|
|
|
class ArticleViewController: UIViewController {
|
2019-10-12 21:45:44 +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!
|
2020-01-01 00:55:39 +01:00
|
|
|
|
|
|
|
private var pageViewController: UIPageViewController!
|
|
|
|
|
|
|
|
private var currentWebViewController: WebViewController? {
|
|
|
|
return pageViewController?.viewControllers?.first as? WebViewController
|
|
|
|
}
|
2019-11-18 03:20:50 +01: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-11-26 02:43:43 +01:00
|
|
|
private var isFullScreenAvailable: Bool {
|
|
|
|
return traitCollection.userInterfaceIdiom == .phone && coordinator.isRootSplitCollapsed
|
|
|
|
}
|
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
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
var article: Article? {
|
2019-09-24 02:23:23 +02:00
|
|
|
didSet {
|
2020-01-01 00:55:39 +01:00
|
|
|
if let controller = currentWebViewController, controller.article != article {
|
|
|
|
controller.article = article
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
// You have to set the view controller to clear out the UIPageViewController child controller cache.
|
|
|
|
// You also have to do it in an async call or you will get a strange assertion error.
|
|
|
|
self.pageViewController.setViewControllers([controller], direction: .forward, animated: false, completion: nil)
|
|
|
|
}
|
2019-09-24 02:23:23 +02:00
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
updateUI()
|
2019-09-24 23:34:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-11-19 02:12:24 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2019-12-26 20:21:56 +01:00
|
|
|
let fullScreenTapZone = UIView()
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
fullScreenTapZone.widthAnchor.constraint(equalToConstant: 150),
|
|
|
|
fullScreenTapZone.heightAnchor.constraint(equalToConstant: 44)
|
|
|
|
])
|
|
|
|
fullScreenTapZone.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapNavigationBar)))
|
|
|
|
navigationItem.titleView = fullScreenTapZone
|
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
articleExtractorButton.addTarget(self, action: #selector(toggleArticleExtractor(_:)), for: .touchUpInside)
|
2019-11-06 15:08:08 +01:00
|
|
|
toolbarItems?.insert(UIBarButtonItem(customView: articleExtractorButton), at: 6)
|
2019-09-27 18:20:26 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])
|
|
|
|
pageViewController.delegate = self
|
|
|
|
pageViewController.dataSource = self
|
2019-11-18 03:20:50 +01:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
view.addSubview(pageViewController.view)
|
|
|
|
addChild(pageViewController!)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.leadingAnchor.constraint(equalTo: pageViewController.view.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: pageViewController.view.trailingAnchor),
|
|
|
|
view.topAnchor.constraint(equalTo: pageViewController.view.topAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: pageViewController.view.bottomAnchor)
|
|
|
|
])
|
|
|
|
|
|
|
|
let controller = createWebViewController(article)
|
|
|
|
pageViewController.setViewControllers([controller], direction: .forward, animated: false, completion: nil)
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
updateUI()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 21:18:58 +01:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
if AppDefaults.articleFullscreenEnabled {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.hideBars()
|
2019-11-24 21:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 02:12:24 +01:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(true)
|
|
|
|
coordinator.isArticleViewControllerPending = false
|
|
|
|
}
|
|
|
|
|
2019-11-24 10:42:38 +01:00
|
|
|
override func viewSafeAreaInsetsDidChange() {
|
2019-11-24 20:41:32 +01:00
|
|
|
// This will animate if the show/hide bars animation is happening.
|
|
|
|
view.layoutIfNeeded()
|
2019-11-24 10:42:38 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
func updateUI() {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
guard let article = article 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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-17 07:45:59 +01:00
|
|
|
guard let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String> else {
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
guard let article = article else {
|
2019-12-17 07:45:59 +01:00
|
|
|
return
|
|
|
|
}
|
2020-01-01 00:55:39 +01:00
|
|
|
if articleIDs.contains(article.articleID) {
|
2019-04-23 11:35:48 +02:00
|
|
|
updateUI()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 02:12:24 +01:00
|
|
|
@objc func willEnterForeground(_ note: Notification) {
|
2019-11-24 21:49:44 +01:00
|
|
|
// The toolbar will come back on you if you don't hide it again
|
|
|
|
if AppDefaults.articleFullscreenEnabled {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.hideBars()
|
2019-11-24 21:49:44 +01:00
|
|
|
}
|
2019-11-19 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
// MARK: Actions
|
2019-11-18 03:20:50 +01:00
|
|
|
|
2019-12-26 20:21:56 +01:00
|
|
|
@objc func didTapNavigationBar() {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.hideBars()
|
2019-12-26 20:21:56 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 02:12:24 +01:00
|
|
|
@objc func showBars(_ sender: Any) {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.showBars()
|
2019-11-18 03:20:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
@IBAction func toggleArticleExtractor(_ sender: Any) {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.toggleArticleExtractor()
|
2019-09-24 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
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 showActivityDialog(_ sender: Any) {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.showActivityDialog(popOverBarButtonItem: actionBarButtonItem)
|
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() {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.focus()
|
2019-09-05 04:06:29 +02:00
|
|
|
}
|
2019-08-25 18:38:04 +02:00
|
|
|
|
2019-09-06 04:14:19 +02:00
|
|
|
func finalScrollPosition() -> CGFloat {
|
2020-01-01 00:55:39 +01:00
|
|
|
return currentWebViewController?.finalScrollPosition() ?? 0.0
|
2019-09-06 04:14:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func canScrollDown() -> Bool {
|
2020-01-01 00:55:39 +01:00
|
|
|
return currentWebViewController?.canScrollDown() ?? false
|
2019-09-06 04:14:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func scrollPageDown() {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.scrollPageDown()
|
2019-10-16 23:40:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 01:16:54 +01:00
|
|
|
func fullReload() {
|
2020-01-01 00:55:39 +01:00
|
|
|
currentWebViewController?.fullReload()
|
2019-11-26 02:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: WebViewControllerDelegate
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
extension ArticleViewController: WebViewControllerDelegate {
|
|
|
|
func webViewController(_ webViewController: WebViewController, articleExtractorButtonStateDidUpdate buttonState: ArticleExtractorButtonState) {
|
|
|
|
if webViewController === currentWebViewController {
|
|
|
|
articleExtractorButton.buttonState = buttonState
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-09-21 19:43:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-23 14:26:35 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: UIPageViewControllerDataSource
|
2019-10-12 21:45:44 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
extension ArticleViewController: UIPageViewControllerDataSource {
|
2019-10-14 02:41:34 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
|
|
|
|
guard let article = coordinator.prevArticle else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return createWebViewController(article)
|
2019-10-16 18:31:20 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
|
|
|
|
guard let article = coordinator.nextArticle else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return createWebViewController(article)
|
2019-10-16 18:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
// MARK: UIPageViewControllerDelegate
|
2019-10-16 01:08:13 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
extension ArticleViewController: UIPageViewControllerDelegate {
|
2019-10-16 01:08:13 +02:00
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
|
|
|
|
guard finished, completed else { return }
|
|
|
|
guard let article = currentWebViewController?.article else { return }
|
|
|
|
coordinator.selectArticle(article)
|
|
|
|
articleExtractorButton.buttonState = currentWebViewController?.articleExtractorButtonState ?? .off
|
2019-10-16 01:08:13 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-01 00:55:39 +01:00
|
|
|
func createWebViewController(_ article: Article?) -> WebViewController {
|
|
|
|
let controller = WebViewController()
|
|
|
|
controller.coordinator = coordinator
|
|
|
|
controller.delegate = self
|
|
|
|
controller.article = article
|
|
|
|
return controller
|
2019-11-18 03:20:50 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|