Files
NetNewsWire/iOS/MainTimeline/MainTimelineViewController.swift
T

1137 lines
39 KiB
Swift
Raw Normal View History

//
2024-12-24 12:09:41 -08:00
// MainTimelineViewController.swift
// NetNewsWire
//
// Created by Maurice Parker on 4/8/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2025-05-09 20:30:04 -07:00
import WebKit
import RSCore
import RSWeb
import Account
import Articles
final class MainTimelineViewController: UITableViewController, UndoableCommandRunner {
2019-04-16 18:08:02 -05:00
private var numberOfTextLines = 0
private var iconSize = IconSize.medium
private lazy var feedTapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(showFeedInspector(_:)))
2025-11-05 11:22:10 -08:00
private var refreshProgressView: RefreshProgressView?
@IBOutlet var markAllAsReadButton: UIBarButtonItem?
2025-11-19 10:38:18 -08:00
private lazy var filterButton = UIBarButtonItem(image: Assets.Images.filter, style: .plain, target: self, action: #selector(toggleFilter(_:)))
private lazy var firstUnreadButton = UIBarButtonItem(image: Assets.Images.nextUnread, style: .plain, target: self, action: #selector(firstUnread(_:)))
private lazy var dataSource = makeDataSource()
2019-08-31 11:50:34 -05:00
private let searchController = UISearchController(searchResultsController: nil)
2025-11-05 11:22:10 -08:00
weak var coordinator: SceneCoordinator?
2019-04-21 13:57:23 -05:00
var undoableCommands = [UndoableCommand]()
let scrollPositionQueue = CoalescingQueue(name: "Timeline Scroll Position", interval: 0.3, maxInterval: 1.0)
2019-08-31 11:50:34 -05:00
2025-11-02 12:17:44 -08:00
private var timelineFeed: SidebarItem? {
assert(coordinator != nil)
return coordinator?.timelineFeed
}
private var showIcons: Bool {
assert(coordinator != nil)
return coordinator?.showIcons ?? false
}
private var currentArticle: Article? {
assert(coordinator != nil)
return coordinator?.currentArticle
}
private var timelineMiddleIndexPath: IndexPath? {
get {
coordinator?.timelineMiddleIndexPath
}
set {
coordinator?.timelineMiddleIndexPath = newValue
}
}
private var isTimelineViewControllerPending: Bool {
get {
coordinator?.isTimelineViewControllerPending ?? false
}
set {
coordinator?.isTimelineViewControllerPending = newValue
}
}
private var timelineIconImage: IconImage? {
assert(coordinator != nil)
return coordinator?.timelineIconImage
}
private var timelineDefaultReadFilterType: ReadFilterType {
return timelineFeed?.defaultReadFilterType ?? .none
}
private var isReadArticlesFiltered: Bool {
assert(coordinator != nil)
return coordinator?.isReadArticlesFiltered ?? false
}
private var isTimelineUnreadAvailable: Bool {
assert(coordinator != nil)
return coordinator?.isTimelineUnreadAvailable ?? false
}
private var isRootSplitCollapsed: Bool {
assert(coordinator != nil)
return coordinator?.isRootSplitCollapsed ?? false
}
private var articles: ArticleArray? {
assert(coordinator != nil)
return coordinator?.articles
}
private let keyboardManager = KeyboardManager(type: .timeline)
override var keyCommands: [UIKeyCommand]? {
2025-11-05 11:22:10 -08:00
// If the first responder is the WKWebView (PreloadedWebView) we don't want to supply any keyboard
// commands that the system is looking for by going up the responder chain. They will interfere with
// the WKWebViews built in hardware keyboard shortcuts, specifically the up and down arrow keys.
guard let current = UIResponder.currentFirstResponder, !(current is PreloadedWebView) else { return nil }
return keyboardManager.keyCommands
}
2025-11-05 11:22:10 -08:00
2025-08-16 18:33:33 +08:00
private var navigationBarTitleLabel: UILabel {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .subheadline).bold()
label.isUserInteractionEnabled = true
label.numberOfLines = 1
label.textAlignment = .center
let tap = UITapGestureRecognizer(target: self, action: #selector(showFeedInspector(_:)))
label.addGestureRecognizer(tap)
let pointerInteraction = UIPointerInteraction(delegate: nil)
label.addInteraction(pointerInteraction)
return label
}
2025-11-05 11:22:10 -08:00
2025-08-16 18:33:33 +08:00
private var navigationBarSubtitleTitleLabel: UILabel {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .footnote)
label.textColor = .systemGray
label.textAlignment = .center
label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(showFeedInspector(_:)))
label.addGestureRecognizer(tap)
return label
}
2025-11-05 11:22:10 -08:00
2019-04-16 18:08:02 -05:00
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
assert(coordinator != nil)
2025-11-05 11:22:10 -08:00
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
2025-11-02 16:17:42 -08:00
NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .feedIconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
// TODO: fix this temporary hack, which will probably require refactoring image handling.
// We want to know when to possibly reconfigure our cells with a new image, and we don’t
// always know when an image is available — but watching the .htmlMetadataAvailable Notification
// lets us know that it’s time to request an image.
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .htmlMetadataAvailable, object: nil)
2025-11-18 20:22:48 -08:00
NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: .main) { [weak self] _ in
Task { @MainActor in
self?.userDefaultsDidChange()
}
}
2019-04-29 14:50:56 -05:00
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
2019-09-27 19:45:09 -05:00
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange), name: .DisplayNameDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
2025-11-05 11:22:10 -08:00
// Setup the Search Controller
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = NSLocalizedString("Search Articles", comment: "Search Articles")
searchController.searchBar.scopeButtonTitles = [
NSLocalizedString("Here", comment: "Here"),
NSLocalizedString("All Articles", comment: "All Articles")
]
navigationItem.searchController = searchController
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
if traitCollection.userInterfaceIdiom == .pad {
2025-06-13 15:53:22 +08:00
searchController.searchBar.selectedScopeButtonIndex = 1
2025-06-13 19:22:43 +08:00
navigationItem.searchBarPlacementAllowsExternalIntegration = true
2025-06-13 15:53:22 +08:00
}
definesPresentationContext = true
2025-11-05 11:22:10 -08:00
2019-08-31 11:50:34 -05:00
// Configure the table
tableView.dataSource = dataSource
2025-07-23 21:58:07 +08:00
tableView.isPrefetchingEnabled = false
2025-11-05 11:22:10 -08:00
numberOfTextLines = AppDefaults.shared.timelineNumberOfLines
iconSize = AppDefaults.shared.timelineIconSize
2025-11-05 11:22:10 -08:00
refreshControl = UIRefreshControl()
refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged)
2025-11-05 11:22:10 -08:00
configureToolbar()
resetUI(resetScroll: true)
2025-11-05 11:22:10 -08:00
// Load the table and then scroll to the saved position if available
applyChanges(animated: false) {
if let restoreIndexPath = self.timelineMiddleIndexPath {
self.tableView.scrollToRow(at: restoreIndexPath, at: .middle, animated: false)
}
}
2025-11-05 11:22:10 -08:00
2020-05-20 21:32:19 +08:00
// Disable swipe back on iPad Mice
2025-07-23 21:58:07 +08:00
guard let gesture = self.navigationController?.interactivePopGestureRecognizer as? UIPanGestureRecognizer else {
return
2020-05-20 21:32:19 +08:00
}
2025-07-23 21:58:07 +08:00
gesture.allowedScrollTypesMask = []
2025-11-05 11:22:10 -08:00
2025-08-16 18:33:33 +08:00
navigationItem.titleView = navigationBarTitleLabel
navigationItem.subtitleView = navigationBarSubtitleTitleLabel
}
2025-11-05 11:22:10 -08:00
override func viewWillAppear(_ animated: Bool) {
2025-07-23 21:58:07 +08:00
super.viewWillAppear(animated)
self.navigationController?.isToolbarHidden = false
// If the nav bar is hidden, fade it in to avoid it showing stuff as it is getting laid out
if navigationController?.navigationBar.isHidden ?? false {
navigationController?.navigationBar.alpha = 0
}
2025-08-16 18:33:33 +08:00
//navigationItem.subtitle = "" // don't inherit feeds subtitle on push
updateNavigationBarTitle(coordinator?.timelineFeed?.nameForDisplay ?? "")
updateNavigationBarSubtitle("")
}
2025-11-05 11:22:10 -08:00
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
isTimelineViewControllerPending = false
if navigationController?.navigationBar.alpha == 0 {
UIView.animate(withDuration: 0.5) {
self.navigationController?.navigationBar.alpha = 1
}
}
2025-07-23 21:58:07 +08:00
if traitCollection.userInterfaceIdiom == .phone {
if let _ = coordinator?.currentArticle {
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
coordinator?.selectArticle(nil)
}
}
}
2025-11-05 11:22:10 -08:00
2019-09-04 21:06:29 -05:00
// MARK: Actions
2025-11-05 11:22:10 -08:00
@objc func openInBrowser(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.showBrowserForCurrentArticle()
}
@objc func openInAppBrowser(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.showInAppBrowser()
}
2025-11-05 11:22:10 -08:00
2019-11-21 18:22:43 -06:00
@IBAction func toggleFilter(_ sender: Any) {
assert(coordinator != nil)
coordinator?.toggleReadArticlesFilter()
2019-11-21 18:22:43 -06:00
}
private func markAllAsReadInTimeline() {
assert(coordinator != nil)
coordinator?.markAllAsReadInTimeline()
}
@IBAction func markAllAsRead(_ sender: Any) {
let title = NSLocalizedString("Mark All as Read", comment: "Mark All as Read")
2025-11-05 11:22:10 -08:00
2020-05-14 17:10:55 +08:00
if let source = sender as? UIBarButtonItem {
MarkAsReadAlertController.confirm(self, coordinator: coordinator, confirmTitle: title, sourceType: source) { [weak self] in
self?.markAllAsReadInTimeline()
2020-05-14 17:10:55 +08:00
}
2020-05-13 12:33:51 +08:00
}
2025-11-05 11:22:10 -08:00
2020-05-14 17:10:55 +08:00
if let _ = sender as? UIKeyCommand {
guard let indexPath = tableView.indexPathForSelectedRow, let contentView = tableView.cellForRow(at: indexPath)?.contentView else {
return
}
2025-11-05 11:22:10 -08:00
2020-05-14 17:10:55 +08:00
MarkAsReadAlertController.confirm(self, coordinator: coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
self?.markAllAsReadInTimeline()
2020-05-14 17:10:55 +08:00
}
}
}
2025-11-05 11:22:10 -08:00
@IBAction func firstUnread(_ sender: Any) {
assert(coordinator != nil)
coordinator?.selectFirstUnread()
2019-04-22 18:00:26 -05:00
}
2025-11-05 11:22:10 -08:00
@objc func refreshAccounts(_ sender: Any) {
refreshControl?.endRefreshing()
// This is a hack to make sure that an error dialog doesn't interfere with dismissing the refreshControl.
// If the error dialog appears too closely to the call to endRefreshing, then the refreshControl never disappears.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
appDelegate.manualRefresh(errorHandler: ErrorHandler.present(self))
}
}
2025-11-05 11:22:10 -08:00
2019-09-04 21:06:29 -05:00
// MARK: Keyboard shortcuts
2025-11-05 11:22:10 -08:00
2019-09-04 21:06:29 -05:00
@objc func selectNextUp(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.selectPrevArticle()
2019-09-04 21:06:29 -05:00
}
@objc func selectNextDown(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.selectNextArticle()
2019-09-04 21:06:29 -05:00
}
@objc func navigateToSidebar(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.navigateToFeeds()
2019-09-04 21:06:29 -05:00
}
2025-11-05 11:22:10 -08:00
2019-09-04 21:06:29 -05:00
@objc func navigateToDetail(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.navigateToDetail()
2019-09-04 21:06:29 -05:00
}
2025-11-05 11:22:10 -08:00
@objc func showFeedInspector(_ sender: Any?) {
assert(coordinator != nil)
coordinator?.showFeedInspector()
2019-09-27 19:45:09 -05:00
}
2020-05-13 17:29:59 +05:30
// MARK: API
func restoreSelectionIfNecessary(adjustScroll: Bool) {
if let article = currentArticle, let indexPath = dataSource.indexPath(for: article) {
if adjustScroll {
tableView.selectRowAndScrollIfNotVisible(at: indexPath, animations: [])
} else {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
}
2025-11-05 11:22:10 -08:00
2025-08-16 18:33:33 +08:00
func updateNavigationBarTitle(_ text: String) {
if let label = navigationItem.titleView as? UILabel {
label.text = text
label.isUserInteractionEnabled = ((coordinator?.timelineFeed as? PseudoFeed) == nil)
label.sizeToFit()
}
}
2025-11-05 11:22:10 -08:00
2025-08-16 18:33:33 +08:00
func updateNavigationBarSubtitle(_ text: String) {
if let label = navigationItem.subtitleView as? UILabel {
label.text = text
label.isUserInteractionEnabled = ((coordinator?.timelineFeed as? PseudoFeed) == nil)
label.sizeToFit()
}
}
func reinitializeArticles(resetScroll: Bool) {
resetUI(resetScroll: resetScroll)
}
2025-11-05 11:22:10 -08:00
func reloadArticles(animated: Bool) {
applyChanges(animated: animated)
}
2025-11-05 11:22:10 -08:00
func updateArticleSelection(animations: Animations) {
if let article = currentArticle, let indexPath = dataSource.indexPath(for: article) {
if tableView.indexPathForSelectedRow != indexPath {
tableView.selectRowAndScrollIfNotVisible(at: indexPath, animations: animations)
}
2019-09-04 21:06:29 -05:00
} else {
tableView.selectRow(at: nil, animated: animations.contains(.select), scrollPosition: .none)
}
2025-11-05 11:22:10 -08:00
updateUI()
}
func updateUI() {
refreshProgressView?.update()
updateToolbar()
}
2025-11-05 11:22:10 -08:00
func hideSearch() {
navigationItem.searchController?.isActive = false
}
2019-09-01 17:41:46 -05:00
func showSearchAll() {
navigationItem.searchController?.isActive = true
navigationItem.searchController?.searchBar.selectedScopeButtonIndex = 1
navigationItem.searchController?.searchBar.becomeFirstResponder()
2019-09-01 17:41:46 -05:00
}
2025-11-05 11:22:10 -08:00
2019-09-04 21:06:29 -05:00
func focus() {
becomeFirstResponder()
}
2025-11-05 11:22:10 -08:00
// MARK: - Table view
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let article = dataSource.itemIdentifier(for: indexPath) else { return nil }
guard !article.status.read || article.isAvailableToMarkUnread else { return nil }
// Set up the read action
let readTitle = article.status.read ?
NSLocalizedString("Mark as Unread", comment: "Mark as Unread") :
NSLocalizedString("Mark as Read", comment: "Mark as Read")
2025-11-05 11:22:10 -08:00
let readAction = UIContextualAction(style: .normal, title: readTitle) { [weak self] (action, view, completion) in
self?.toggleRead(article)
completion(true)
}
2025-11-05 11:22:10 -08:00
2025-11-19 10:38:18 -08:00
readAction.image = article.status.read ? Assets.Images.circleClosed : Assets.Images.circleOpen
readAction.backgroundColor = Assets.Colors.primaryAccent
2025-11-05 11:22:10 -08:00
return UISwipeActionsConfiguration(actions: [readAction])
}
2025-11-05 11:22:10 -08:00
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
2025-11-05 11:22:10 -08:00
guard let article = dataSource.itemIdentifier(for: indexPath) else { return nil }
2025-11-05 11:22:10 -08:00
// Set up the star action
let starTitle = article.status.starred ?
NSLocalizedString("Unstar", comment: "Unstar") :
NSLocalizedString("Star", comment: "Star")
2025-11-05 11:22:10 -08:00
let starAction = UIContextualAction(style: .normal, title: starTitle) { [weak self] (action, view, completion) in
self?.toggleStar(article)
completion(true)
}
2025-11-05 11:22:10 -08:00
starAction.image = article.status.starred ? Assets.Images.starOpen : Assets.Images.starClosed
2025-11-19 10:38:18 -08:00
starAction.backgroundColor = Assets.Colors.star
2025-11-05 11:22:10 -08:00
2019-08-18 17:34:53 -05:00
// Set up the read action
let moreTitle = NSLocalizedString("More", comment: "More")
let moreAction = UIContextualAction(style: .normal, title: moreTitle) { [weak self] (action, view, completion) in
2025-11-05 11:22:10 -08:00
2019-08-18 17:34:53 -05:00
if let self = self {
2025-11-05 11:22:10 -08:00
2019-08-18 17:34:53 -05:00
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = view
popoverController.sourceRect = CGRect(x: view.frame.size.width/2, y: view.frame.size.height/2, width: 1, height: 1)
}
2020-05-13 12:33:51 +08:00
if let action = self.markAboveAsReadAlertAction(article, indexPath: indexPath, completion: completion) {
alert.addAction(action)
}
2020-05-13 12:33:51 +08:00
if let action = self.markBelowAsReadAlertAction(article, indexPath: indexPath, completion: completion) {
alert.addAction(action)
}
2025-11-05 11:22:10 -08:00
if let action = self.discloseFeedAlertAction(article, completion: completion) {
2019-08-19 15:45:52 -05:00
alert.addAction(action)
}
2025-11-05 11:22:10 -08:00
2020-05-13 12:33:51 +08:00
if let action = self.markAllInFeedAsReadAlertAction(article, indexPath: indexPath, completion: completion) {
2019-08-19 17:26:09 -05:00
alert.addAction(action)
}
if let action = self.openInBrowserAlertAction(article, completion: completion) {
2019-08-19 17:38:30 -05:00
alert.addAction(action)
}
if let action = self.shareAlertAction(article, indexPath: indexPath, completion: completion) {
2019-08-19 18:09:38 -05:00
alert.addAction(action)
}
2019-08-18 17:34:53 -05:00
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel) { _ in
completion(true)
2019-08-18 17:34:53 -05:00
})
self.present(alert, animated: true)
2025-11-05 11:22:10 -08:00
2019-08-18 17:34:53 -05:00
}
2025-11-05 11:22:10 -08:00
2019-08-18 17:34:53 -05:00
}
2025-11-05 11:22:10 -08:00
2025-11-19 10:38:18 -08:00
moreAction.image = Assets.Images.more
2019-08-18 17:34:53 -05:00
moreAction.backgroundColor = UIColor.systemGray
return UISwipeActionsConfiguration(actions: [starAction, moreAction])
2025-11-05 11:22:10 -08:00
}
override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
guard let article = dataSource.itemIdentifier(for: indexPath) else { return nil }
2025-11-05 11:22:10 -08:00
2020-01-07 16:36:32 -07:00
return UIContextMenuConfiguration(identifier: indexPath.row as NSCopying, previewProvider: nil, actionProvider: { [weak self] suggestedActions in
guard let self = self else { return nil }
2025-11-05 11:22:10 -08:00
2020-11-13 05:23:04 -06:00
var menuElements = [UIMenuElement]()
2025-11-05 11:22:10 -08:00
2020-11-13 05:23:04 -06:00
var markActions = [UIAction]()
if let action = self.toggleArticleReadStatusAction(article) {
2020-11-13 05:23:04 -06:00
markActions.append(action)
}
2020-11-13 05:23:04 -06:00
markActions.append(self.toggleArticleStarStatusAction(article))
2020-05-13 12:33:51 +08:00
if let action = self.markAboveAsReadAction(article, indexPath: indexPath) {
2020-11-13 05:23:04 -06:00
markActions.append(action)
}
2020-05-13 12:33:51 +08:00
if let action = self.markBelowAsReadAction(article, indexPath: indexPath) {
2020-11-13 05:23:04 -06:00
markActions.append(action)
2020-01-07 16:36:32 -07:00
}
2020-11-13 05:23:04 -06:00
menuElements.append(UIMenu(title: "", options: .displayInline, children: markActions))
2025-11-05 11:22:10 -08:00
2020-11-13 05:23:04 -06:00
var secondaryActions = [UIAction]()
2020-01-07 16:36:32 -07:00
if let action = self.discloseFeedAction(article) {
2020-11-13 05:23:04 -06:00
secondaryActions.append(action)
2020-01-07 16:36:32 -07:00
}
2020-05-13 12:33:51 +08:00
if let action = self.markAllInFeedAsReadAction(article, indexPath: indexPath) {
2020-11-13 05:23:04 -06:00
secondaryActions.append(action)
}
if !secondaryActions.isEmpty {
menuElements.append(UIMenu(title: "", options: .displayInline, children: secondaryActions))
2020-01-07 16:36:32 -07:00
}
2025-11-05 11:22:10 -08:00
var copyActions = [UIAction]()
if let action = self.copyArticleURLAction(article) {
copyActions.append(action)
}
if let action = self.copyExternalURLAction(article) {
copyActions.append(action)
}
if !copyActions.isEmpty {
menuElements.append(UIMenu(title: "", options: .displayInline, children: copyActions))
}
2025-11-05 11:22:10 -08:00
2020-01-07 16:36:32 -07:00
if let action = self.openInBrowserAction(article) {
2020-11-13 05:23:04 -06:00
menuElements.append(UIMenu(title: "", options: .displayInline, children: [action]))
2020-01-07 16:36:32 -07:00
}
2025-11-05 11:22:10 -08:00
2020-01-07 16:36:32 -07:00
if let action = self.shareAction(article, indexPath: indexPath) {
2020-11-13 05:23:04 -06:00
menuElements.append(UIMenu(title: "", options: .displayInline, children: [action]))
2020-01-07 16:36:32 -07:00
}
2025-11-05 11:22:10 -08:00
2020-11-13 05:23:04 -06:00
return UIMenu(title: "", children: menuElements)
2020-01-07 16:36:32 -07:00
})
2025-11-05 11:22:10 -08:00
}
override func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let row = configuration.identifier as? Int,
let cell = tableView.cellForRow(at: IndexPath(row: row, section: 0)) else {
return nil
}
2025-11-05 11:22:10 -08:00
let previewView = cell.contentView
let inset: CGFloat = 12
let visibleBounds = previewView.bounds.insetBy(dx: inset, dy: 2)
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: visibleBounds,
cornerRadius: 20)
return UITargetedPreview(view: previewView, parameters: parameters)
}
2025-11-05 11:22:10 -08:00
override func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let row = configuration.identifier as? Int,
let cell = tableView.cellForRow(at: IndexPath(row: row, section: 0)) else {
return nil
}
2025-11-05 11:22:10 -08:00
let previewView = cell.contentView
let inset: CGFloat = 0
let visibleBounds = previewView.bounds.insetBy(dx: inset, dy: 2)
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: visibleBounds,
cornerRadius: 20)
2025-11-05 11:22:10 -08:00
return UITargetedPreview(view: previewView, parameters: parameters)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
becomeFirstResponder()
let article = dataSource.itemIdentifier(for: indexPath)
coordinator?.selectArticle(article, animations: [.scroll, .select, .navigation])
}
2025-11-05 11:22:10 -08:00
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
2021-12-29 14:44:40 -08:00
scrollPositionQueue.add(self, #selector(scrollPositionDidChange))
}
2025-11-05 11:22:10 -08:00
// MARK: Notifications
@objc dynamic func unreadCountDidChange(_ notification: Notification) {
updateUI()
}
2025-11-05 11:22:10 -08:00
@objc func statusesDidChange(_ note: Notification) {
guard let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String>, !articleIDs.isEmpty else {
return
}
let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
let visibleUpdatedArticles = visibleArticles.filter { articleIDs.contains($0.articleID) }
for article in visibleUpdatedArticles {
if let indexPath = dataSource.indexPath(for: article) {
2025-07-23 21:58:07 +08:00
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell {
let cellData = configure(article: article)
cell.cellData = cellData
}
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineFeedCell {
let cellData = configure(article: article)
cell.cellData = cellData
}
}
}
}
2025-11-02 16:17:42 -08:00
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
2025-08-15 20:22:44 +08:00
2025-11-02 16:17:42 -08:00
guard let feed = note.userInfo?[UserInfoKey.feed] as? Feed else {
return
}
tableView.indexPathsForVisibleRows?.forEach { indexPath in
guard let article = dataSource.itemIdentifier(for: indexPath) else {
return
}
2025-11-02 15:12:00 -08:00
if article.feed == feed {
2025-07-23 21:58:07 +08:00
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell, let image = iconImageFor(article) {
cell.setIconImage(image)
}
}
}
}
@objc func avatarDidBecomeAvailable(_ note: Notification) {
guard showIcons, let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
return
}
tableView.indexPathsForVisibleRows?.forEach { indexPath in
guard let article = dataSource.itemIdentifier(for: indexPath), let authors = article.authors, !authors.isEmpty else {
return
}
for author in authors {
2025-08-15 20:51:43 +08:00
if author.avatarURL == avatarURL, let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell, let image = iconImageFor(article) {
cell.setIconImage(image)
}
}
}
}
@objc func faviconDidBecomeAvailable(_ note: Notification) {
if showIcons {
queueReloadAvailableCells()
}
}
2025-11-18 20:22:48 -08:00
func userDefaultsDidChange() {
if self.numberOfTextLines != AppDefaults.shared.timelineNumberOfLines || self.iconSize != AppDefaults.shared.timelineIconSize {
self.numberOfTextLines = AppDefaults.shared.timelineNumberOfLines
self.iconSize = AppDefaults.shared.timelineIconSize
self.reloadAllVisibleCells()
}
2025-11-18 20:22:48 -08:00
self.updateToolbar()
}
2019-04-29 14:50:56 -05:00
@objc func contentSizeCategoryDidChange(_ note: Notification) {
reloadAllVisibleCells()
2019-04-29 14:50:56 -05:00
}
2025-11-05 11:22:10 -08:00
2019-09-27 19:45:09 -05:00
@objc func displayNameDidChange(_ note: Notification) {
2025-08-16 18:33:33 +08:00
updateNavigationBarTitle(timelineFeed?.nameForDisplay ?? "")
2019-09-27 19:45:09 -05:00
}
2025-11-05 11:22:10 -08:00
@objc func willEnterForeground(_ note: Notification) {
updateUI()
}
2025-11-05 11:22:10 -08:00
@objc func scrollPositionDidChange() {
timelineMiddleIndexPath = tableView.middleVisibleRow()
}
2025-11-05 11:22:10 -08:00
// MARK: Reloading
2025-11-05 11:22:10 -08:00
func queueReloadAvailableCells() {
CoalescingQueue.standard.add(self, #selector(reloadAllVisibleCells))
}
@objc private func reloadAllVisibleCells() {
2024-12-04 17:52:46 -08:00
let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
reloadCells(visibleArticles)
}
2024-12-04 17:52:46 -08:00
private func reloadCells(_ articles: [Article]) {
var snapshot = dataSource.snapshot()
snapshot.reloadItems(articles)
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
self?.restoreSelectionIfNecessary(adjustScroll: false)
}
}
}
2019-08-31 15:53:47 -05:00
// MARK: Searching
2019-08-31 11:50:34 -05:00
2024-12-24 12:09:41 -08:00
extension MainTimelineViewController: UISearchControllerDelegate {
func willPresentSearchController(_ searchController: UISearchController) {
coordinator?.beginSearching()
searchController.searchBar.showsScopeBar = true
}
func willDismissSearchController(_ searchController: UISearchController) {
coordinator?.endSearching()
searchController.searchBar.showsScopeBar = false
}
}
2024-12-24 12:09:41 -08:00
extension MainTimelineViewController: UISearchResultsUpdating {
2019-08-31 15:53:47 -05:00
2019-08-31 11:50:34 -05:00
func updateSearchResults(for searchController: UISearchController) {
2019-08-31 15:53:47 -05:00
let searchScope = SearchScope(rawValue: searchController.searchBar.selectedScopeButtonIndex)!
searchArticles(searchController.searchBar.text!, searchScope)
2019-08-31 11:50:34 -05:00
}
2019-08-31 15:53:47 -05:00
}
2024-12-24 12:09:41 -08:00
extension MainTimelineViewController: UISearchBarDelegate {
2019-08-31 15:53:47 -05:00
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
let searchScope = SearchScope(rawValue: selectedScope)!
searchArticles(searchBar.text!, searchScope)
2019-08-31 15:53:47 -05:00
}
2019-08-31 11:50:34 -05:00
}
// MARK: Private
2024-12-24 12:09:41 -08:00
private extension MainTimelineViewController {
func searchArticles(_ searchString: String, _ searchScope: SearchScope) {
assert(coordinator != nil)
coordinator?.searchArticles(searchString, searchScope)
}
func configureToolbar() {
2025-07-23 21:58:07 +08:00
if traitCollection.userInterfaceIdiom == .phone {
2025-06-13 15:53:22 +08:00
toolbarItems?.insert(.flexibleSpace(), at: 1)
toolbarItems?.insert(navigationItem.searchBarPlacementBarButtonItem, at: 2)
}
}
func resetUI(resetScroll: Bool) {
switch timelineDefaultReadFilterType {
case .none, .read:
navigationItem.rightBarButtonItem = filterButton
2025-07-23 21:58:07 +08:00
navigationItem.rightBarButtonItem?.isEnabled = true
case .alwaysRead:
navigationItem.rightBarButtonItem = nil
2019-11-21 18:22:43 -06:00
}
2025-11-05 11:22:10 -08:00
if isReadArticlesFiltered {
2025-06-13 19:22:43 +08:00
filterButton.style = .prominent
2025-11-19 10:38:18 -08:00
filterButton.tintColor = Assets.Colors.primaryAccent
filterButton.accLabelText = NSLocalizedString("Selected - Filter Read Articles", comment: "Selected - Filter Read Articles")
} else {
2025-06-13 19:22:43 +08:00
filterButton.style = .plain
filterButton.tintColor = nil
filterButton.accLabelText = NSLocalizedString("Filter Read Articles", comment: "Filter Read Articles")
}
tableView.selectRow(at: nil, animated: false, scrollPosition: .top)
2020-10-18 19:59:11 -05:00
if resetScroll {
let snapshot = dataSource.snapshot()
if snapshot.sectionIdentifiers.count > 0 && snapshot.itemIdentifiers(inSection: 0).count > 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
}
2025-11-05 11:22:10 -08:00
2019-10-08 09:19:50 -05:00
updateToolbar()
2019-04-22 18:00:26 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-08 09:19:50 -05:00
func updateToolbar() {
markAllAsReadButton?.isEnabled = isTimelineUnreadAvailable
firstUnreadButton.isEnabled = isTimelineUnreadAvailable
if isRootSplitCollapsed {
if let toolbarItems = toolbarItems, toolbarItems.last != firstUnreadButton {
var items = toolbarItems
items.append(firstUnreadButton)
setToolbarItems(items, animated: false)
}
} else {
if let toolbarItems = toolbarItems, toolbarItems.last == firstUnreadButton {
let items = Array(toolbarItems[0..<toolbarItems.count - 1])
setToolbarItems(items, animated: false)
}
}
2019-04-18 14:36:22 -05:00
}
2025-11-05 11:22:10 -08:00
func applyChanges(animated: Bool, completion: (() -> Void)? = nil) {
if (articles?.count ?? 0) == 0 {
tableView.rowHeight = tableView.estimatedRowHeight
} else {
tableView.rowHeight = UITableView.automaticDimension
}
2025-11-05 11:22:10 -08:00
var snapshot = NSDiffableDataSourceSnapshot<Int, Article>()
snapshot.appendSections([0])
snapshot.appendItems(articles ?? ArticleArray(), toSection: 0)
dataSource.apply(snapshot, animatingDifferences: animated) { [weak self] in
self?.restoreSelectionIfNecessary(adjustScroll: false)
completion?()
}
}
2025-11-05 11:22:10 -08:00
func makeDataSource() -> UITableViewDiffableDataSource<Int, Article> {
let dataSource: UITableViewDiffableDataSource<Int, Article> =
2024-12-24 12:09:41 -08:00
MainTimelineDataSource(tableView: tableView, cellProvider: { [weak self] tableView, indexPath, article in
2025-07-23 21:58:07 +08:00
let cellData = self!.configure(article: article)
if self!.showIcons {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTimelineIconFeedCell", for: indexPath) as! MainTimelineIconFeedCell
2025-07-23 21:58:07 +08:00
cell.cellData = cellData
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTimelineFeedCell", for: indexPath) as! MainTimelineFeedCell
cell.cellData = cellData
return cell
}
2025-11-05 11:22:10 -08:00
})
2019-11-21 19:54:35 -06:00
dataSource.defaultRowAnimation = .middle
return dataSource
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
@discardableResult
func configure(article: Article) -> MainTimelineCellData {
let iconImage = iconImageFor(article)
let showFeedNames = coordinator?.showFeedNames ?? ShowFeedName.none
let showIcon = showIcons && iconImage != nil
2025-11-02 15:12:00 -08:00
let cellData = MainTimelineCellData(article: article, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, byline: article.byline(), iconImage: iconImage, showIcon: showIcon, numberOfLines: numberOfTextLines, iconSize: iconSize)
2025-07-23 21:58:07 +08:00
return cellData
}
2025-11-05 11:22:10 -08:00
func iconImageFor(_ article: Article) -> IconImage? {
if !showIcons {
return nil
}
return article.iconImage()
}
func toggleRead(_ article: Article) {
assert(coordinator != nil)
coordinator?.toggleRead(article)
}
func toggleArticleReadStatusAction(_ article: Article) -> UIAction? {
guard !article.status.read || article.isAvailableToMarkUnread else { return nil }
2025-11-05 11:22:10 -08:00
let title = article.status.read ?
NSLocalizedString("Mark as Unread", comment: "Mark as Unread") :
NSLocalizedString("Mark as Read", comment: "Mark as Read")
2025-11-19 10:38:18 -08:00
let image = article.status.read ? Assets.Images.circleClosed : Assets.Images.circleOpen
2019-08-18 17:34:53 -05:00
let action = UIAction(title: title, image: image) { [weak self] action in
self?.toggleRead(article)
}
2025-11-05 11:22:10 -08:00
return action
}
2025-11-05 11:22:10 -08:00
func toggleStar(_ article: Article) {
assert(coordinator != nil)
coordinator?.toggleStar(article)
}
func toggleArticleStarStatusAction(_ article: Article) -> UIAction {
let title = article.status.starred ?
NSLocalizedString("Mark as Unstarred", comment: "Mark as Unstarred") :
NSLocalizedString("Mark as Starred", comment: "Mark as Starred")
let image = article.status.starred ? Assets.Images.starOpen : Assets.Images.starClosed
2019-08-18 17:34:53 -05:00
let action = UIAction(title: title, image: image) { [weak self] action in
self?.toggleStar(article)
}
2025-11-05 11:22:10 -08:00
return action
}
func markAboveAsRead(_ article: Article) {
assert(coordinator != nil)
coordinator?.markAboveAsRead(article)
}
func canMarkAboveAsRead(for article: Article) -> Bool {
assert(coordinator != nil)
return coordinator?.canMarkAboveAsRead(for: article) ?? false
}
2020-05-13 12:33:51 +08:00
func markAboveAsReadAction(_ article: Article, indexPath: IndexPath) -> UIAction? {
guard canMarkAboveAsRead(for: article), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
return nil
}
let title = NSLocalizedString("Mark Above as Read", comment: "Mark Above as Read")
2025-11-19 10:38:18 -08:00
let image = Assets.Images.markAboveAsRead
2019-08-18 17:34:53 -05:00
let action = UIAction(title: title, image: image) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
self?.markAboveAsRead(article)
}
2019-08-18 17:34:53 -05:00
}
return action
}
func markBelowAsRead(_ article: Article) {
assert(coordinator != nil)
coordinator?.markBelowAsRead(article)
}
func canMarkBelowAsRead(for article: Article) -> Bool {
assert(coordinator != nil)
return coordinator?.canMarkBelowAsRead(for: article) ?? false
}
2020-05-13 12:33:51 +08:00
func markBelowAsReadAction(_ article: Article, indexPath: IndexPath) -> UIAction? {
guard canMarkBelowAsRead(for: article), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
return nil
}
let title = NSLocalizedString("Mark Below as Read", comment: "Mark Below as Read")
2025-11-19 10:38:18 -08:00
let image = Assets.Images.markBelowAsRead
let action = UIAction(title: title, image: image) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
self?.markBelowAsRead(article)
}
}
return action
}
2025-11-05 11:22:10 -08:00
2020-05-13 12:33:51 +08:00
func markAboveAsReadAlertAction(_ article: Article, indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard canMarkAboveAsRead(for: article), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
return nil
}
let title = NSLocalizedString("Mark Above as Read", comment: "Mark Above as Read")
let cancel = {
completion(true)
}
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView, cancelCompletion: cancel) { [weak self] in
self?.markAboveAsRead(article)
completion(true)
}
}
return action
}
2020-05-13 12:33:51 +08:00
func markBelowAsReadAlertAction(_ article: Article, indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard canMarkBelowAsRead(for: article), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
return nil
}
let title = NSLocalizedString("Mark Below as Read", comment: "Mark Below as Read")
let cancel = {
completion(true)
2019-08-18 17:34:53 -05:00
}
2025-11-05 11:22:10 -08:00
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView, cancelCompletion: cancel) { [weak self] in
self?.markBelowAsRead(article)
completion(true)
}
}
2019-08-18 17:34:53 -05:00
return action
}
2025-11-02 14:51:46 -08:00
func timelineFeedIsEqualTo(_ feed: Feed) -> Bool {
assert(coordinator != nil)
return coordinator?.timelineFeedIsEqualTo(feed) ?? false
}
2025-11-03 10:16:03 -08:00
func discloseFeed(_ feed: Feed, animations: Animations = []) {
assert(coordinator != nil)
2025-11-03 10:16:03 -08:00
coordinator?.discloseFeed(feed, animations: animations)
}
func discloseFeedAction(_ article: Article) -> UIAction? {
2025-11-03 10:16:03 -08:00
guard let feed = article.feed,
!timelineFeedIsEqualTo(feed) else { return nil }
let title = NSLocalizedString("Go to Feed", comment: "Go to Feed")
2025-11-19 10:38:18 -08:00
let action = UIAction(title: title, image: Assets.Images.openInSidebar) { [weak self] action in
2025-11-03 10:16:03 -08:00
self?.discloseFeed(feed, animations: [.scroll, .navigation])
2019-08-19 15:45:52 -05:00
}
return action
}
2025-11-05 11:22:10 -08:00
func discloseFeedAlertAction(_ article: Article, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
2025-11-03 10:16:03 -08:00
guard let feed = article.feed,
!timelineFeedIsEqualTo(feed) else { return nil }
let title = NSLocalizedString("Go to Feed", comment: "Go to Feed")
2019-08-19 15:45:52 -05:00
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
2025-11-03 10:16:03 -08:00
self?.discloseFeed(feed, animations: [.scroll, .navigation])
completion(true)
2019-08-19 15:45:52 -05:00
}
return action
}
func markAllAsRead(_ articles: ArticleArray) {
assert(coordinator != nil)
coordinator?.markAllAsRead(articles)
}
2020-05-13 12:33:51 +08:00
func markAllInFeedAsReadAction(_ article: Article, indexPath: IndexPath) -> UIAction? {
2025-11-03 10:16:03 -08:00
guard let feed = article.feed else { return nil }
guard let fetchedArticles = try? feed.fetchArticles() else {
return nil
}
let articles = Array(fetchedArticles)
2020-05-13 12:33:51 +08:00
guard articles.canMarkAllAsRead(), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
2019-08-19 17:26:09 -05:00
return nil
}
2025-11-05 11:22:10 -08:00
2019-08-19 17:26:09 -05:00
let localizedMenuText = NSLocalizedString("Mark All as Read in “%@”", comment: "Command")
2025-11-03 10:16:03 -08:00
let title = NSString.localizedStringWithFormat(localizedMenuText as NSString, feed.nameForDisplay) as String
2025-11-05 11:22:10 -08:00
2025-11-19 10:38:18 -08:00
let action = UIAction(title: title, image: Assets.Images.markAllAsRead) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
self?.markAllAsRead(articles)
}
2019-08-19 17:26:09 -05:00
}
return action
}
2020-05-13 12:33:51 +08:00
func markAllInFeedAsReadAlertAction(_ article: Article, indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
2025-11-03 10:16:03 -08:00
guard let feed = article.feed else { return nil }
guard let fetchedArticles = try? feed.fetchArticles() else {
return nil
}
2025-11-05 11:22:10 -08:00
let articles = Array(fetchedArticles)
2020-05-13 12:33:51 +08:00
guard articles.canMarkAllAsRead(), let contentView = self.tableView.cellForRow(at: indexPath)?.contentView else {
2019-08-19 17:26:09 -05:00
return nil
}
2025-11-05 11:22:10 -08:00
2019-08-19 17:38:30 -05:00
let localizedMenuText = NSLocalizedString("Mark All as Read in “%@”", comment: "Mark All as Read in Feed")
2025-11-03 10:16:03 -08:00
let title = NSString.localizedStringWithFormat(localizedMenuText as NSString, feed.nameForDisplay) as String
let cancel = {
completion(true)
}
2025-11-05 11:22:10 -08:00
2019-08-19 17:26:09 -05:00
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
2020-05-13 12:33:51 +08:00
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView, cancelCompletion: cancel) { [weak self] in
self?.markAllAsRead(articles)
completion(true)
}
2019-08-19 17:26:09 -05:00
}
return action
}
2025-11-05 11:22:10 -08:00
func copyArticleURLAction(_ article: Article) -> UIAction? {
guard let url = article.preferredURL else { return nil }
let title = NSLocalizedString("Copy Article URL", comment: "Copy Article URL")
let action = UIAction(title: title, image: Assets.Images.copy) { action in
UIPasteboard.general.url = url
}
return action
}
2025-11-05 11:22:10 -08:00
func copyExternalURLAction(_ article: Article) -> UIAction? {
guard let externalLink = article.externalLink, externalLink != article.preferredLink, let url = URL(string: externalLink) else { return nil }
let title = NSLocalizedString("Copy External URL", comment: "Copy External URL")
let action = UIAction(title: title, image: Assets.Images.copy) { action in
UIPasteboard.general.url = url
}
return action
}
func showBrowserForArticle(_ article: Article) {
assert(coordinator != nil)
coordinator?.showBrowserForArticle(article)
}
2019-08-19 17:38:30 -05:00
func openInBrowserAction(_ article: Article) -> UIAction? {
guard let _ = article.preferredURL else { return nil }
2019-08-19 17:38:30 -05:00
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
2025-11-19 10:38:18 -08:00
let action = UIAction(title: title, image: Assets.Images.safari) { [weak self] action in
self?.showBrowserForArticle(article)
2019-08-19 17:38:30 -05:00
}
return action
}
func openInBrowserAlertAction(_ article: Article, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let _ = article.preferredURL else { return nil }
2021-08-26 10:27:23 +08:00
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
2019-08-19 17:38:30 -05:00
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
self?.showBrowserForArticle(article)
completion(true)
2019-08-19 17:38:30 -05:00
}
return action
}
2025-11-05 11:22:10 -08:00
2019-08-19 18:09:38 -05:00
func shareDialogForTableCell(indexPath: IndexPath, url: URL, title: String?) {
let activityViewController = UIActivityViewController(url: url, title: title, applicationActivities: nil)
2025-11-05 11:22:10 -08:00
2019-08-19 18:09:38 -05:00
guard let cell = tableView.cellForRow(at: indexPath) else { return }
let popoverController = activityViewController.popoverPresentationController
popoverController?.sourceView = cell
popoverController?.sourceRect = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height)
2025-11-05 11:22:10 -08:00
2019-08-19 18:09:38 -05:00
present(activityViewController, animated: true)
}
2025-11-05 11:22:10 -08:00
func shareAction(_ article: Article, indexPath: IndexPath) -> UIAction? {
guard let url = article.preferredURL else { return nil }
2019-08-19 18:09:38 -05:00
let title = NSLocalizedString("Share", comment: "Share")
2025-11-19 10:38:18 -08:00
let action = UIAction(title: title, image: Assets.Images.share) { [weak self] action in
2019-08-19 18:09:38 -05:00
self?.shareDialogForTableCell(indexPath: indexPath, url: url, title: article.title)
}
return action
}
2025-11-05 11:22:10 -08:00
func shareAlertAction(_ article: Article, indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let url = article.preferredURL else { return nil }
2019-08-19 18:09:38 -05:00
let title = NSLocalizedString("Share", comment: "Share")
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
completion(true)
2019-08-19 18:09:38 -05:00
self?.shareDialogForTableCell(indexPath: indexPath, url: url, title: article.title)
}
return action
}
2025-11-05 11:22:10 -08:00
}