2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterViewController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/8/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
2019-04-17 01:25:55 +02:00
|
|
|
import Articles
|
2019-04-15 22:03:05 +02:00
|
|
|
import RSCore
|
|
|
|
import RSTree
|
|
|
|
|
2019-07-27 21:49:07 +02:00
|
|
|
class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-01-08 01:39:45 +01:00
|
|
|
@IBOutlet weak var filterButton: UIBarButtonItem!
|
2019-10-30 10:04:13 +01:00
|
|
|
private var refreshProgressView: RefreshProgressView?
|
2020-01-08 01:39:45 +01:00
|
|
|
@IBOutlet weak var addNewItemButton: UIBarButtonItem!
|
2019-04-23 11:35:48 +02:00
|
|
|
|
2019-11-21 03:28:24 +01:00
|
|
|
lazy var dataSource = makeDataSource()
|
2019-04-17 01:25:55 +02:00
|
|
|
var undoableCommands = [UndoableCommand]()
|
2019-09-01 19:43:07 +02:00
|
|
|
weak var coordinator: SceneCoordinator!
|
2019-08-29 01:06:27 +02:00
|
|
|
|
2019-09-05 21:37:07 +02:00
|
|
|
private let keyboardManager = KeyboardManager(type: .sidebar)
|
2019-09-04 23:24:16 +02:00
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return keyboardManager.keyCommands
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:36:22 +01:00
|
|
|
var restoreSelection = false
|
2019-04-17 01:25:55 +02:00
|
|
|
override var canBecomeFirstResponder: Bool {
|
|
|
|
return true
|
|
|
|
}
|
2019-09-04 23:24:16 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-08-23 19:27:45 +02:00
|
|
|
if traitCollection.userInterfaceIdiom == .phone {
|
|
|
|
navigationController?.navigationBar.prefersLargeTitles = true
|
|
|
|
}
|
|
|
|
|
2019-09-15 02:51:23 +02:00
|
|
|
// If you don't have an empty table header, UIKit tries to help out by putting one in for you
|
|
|
|
// that makes a gap between the first section header and the navigation bar
|
|
|
|
var frame = CGRect.zero
|
|
|
|
frame.size.height = .leastNormalMagnitude
|
|
|
|
tableView.tableHeaderView = UIView(frame: frame)
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
tableView.register(MasterFeedTableViewSectionHeader.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
|
2019-08-29 01:06:27 +02:00
|
|
|
tableView.dataSource = dataSource
|
2019-11-21 03:28:24 +01:00
|
|
|
tableView.dragDelegate = self
|
|
|
|
tableView.dropDelegate = self
|
|
|
|
tableView.dragInteractionEnabled = true
|
2019-10-29 01:52:50 +01:00
|
|
|
resetEstimatedRowHeight()
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
|
2019-11-15 03:11:41 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webFeedSettingDidChange(_:)), name: .WebFeedSettingDidChange, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webFeedMetadataDidChange(_:)), name: .WebFeedMetadataDidChange, object: nil)
|
2019-04-18 16:42:41 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil)
|
2019-04-28 17:31:35 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
|
2019-11-13 20:43:02 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2019-04-22 23:25:16 +02:00
|
|
|
|
2020-01-03 16:42:43 +01:00
|
|
|
refreshControl = UIRefreshControl()
|
|
|
|
refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged)
|
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
configureToolbar()
|
2019-09-06 17:52:21 +02:00
|
|
|
becomeFirstResponder()
|
2019-11-13 22:22:22 +01:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
2019-10-26 00:07:40 +02:00
|
|
|
updateUI()
|
2019-04-15 22:03:05 +02:00
|
|
|
super.viewWillAppear(animated)
|
|
|
|
}
|
2019-11-01 17:55:11 +01:00
|
|
|
|
2019-11-30 00:36:22 +01:00
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
|
|
|
// We have to delay the selection of the Feed until the subviews have been layed out
|
|
|
|
// so that the visible area is correct and we scroll correctly.
|
|
|
|
if restoreSelection {
|
|
|
|
restoreSelectionIfNecessary(adjustScroll: true)
|
|
|
|
restoreSelection = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:35:16 +02:00
|
|
|
// MARK: Notifications
|
2019-04-17 01:25:55 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2019-04-28 19:37:53 +02:00
|
|
|
updateUI()
|
2019-08-29 21:35:18 +02:00
|
|
|
|
|
|
|
guard let representedObject = note.object else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let account = representedObject as? Account {
|
|
|
|
if let node = coordinator.rootNode.childNodeRepresentingObject(account) {
|
|
|
|
let sectionIndex = coordinator.rootNode.indexOfChild(node)!
|
|
|
|
if let headerView = tableView.headerView(forSection: sectionIndex) as? MasterFeedTableViewSectionHeader {
|
|
|
|
headerView.unreadCount = account.unreadCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var node: Node? = nil
|
2019-11-15 13:19:14 +01:00
|
|
|
if let coordinator = representedObject as? SceneCoordinator, let fetcher = coordinator.timelineFeed {
|
2019-08-29 21:35:18 +02:00
|
|
|
node = coordinator.rootNode.descendantNodeRepresentingObject(fetcher as AnyObject)
|
|
|
|
} else {
|
|
|
|
node = coordinator.rootNode.descendantNodeRepresentingObject(representedObject as AnyObject)
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:31:14 +01:00
|
|
|
// Only do the reload of the node when absolutely necessary. It can stop programatic scrolling from
|
|
|
|
// completing if called to soon after a selectRow where scrolling is necessary. See discloseFeed.
|
|
|
|
if let node = node,
|
|
|
|
let indexPath = dataSource.indexPath(for: node),
|
|
|
|
let cell = tableView.cellForRow(at: indexPath) as? MasterFeedTableViewCell,
|
|
|
|
let unreadCountProvider = node.representedObject as? UnreadCountProvider {
|
|
|
|
|
|
|
|
if cell.unreadCount != unreadCountProvider.unreadCount {
|
2019-11-13 22:22:22 +01:00
|
|
|
self.reloadNode(node)
|
|
|
|
}
|
2019-11-13 22:31:14 +01:00
|
|
|
|
2019-08-29 21:35:18 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func faviconDidBecomeAvailable(_ note: Notification) {
|
2019-11-06 01:05:57 +01:00
|
|
|
applyToAvailableCells(configureIcon)
|
2019-10-29 02:57:26 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
guard let webFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed else {
|
2019-11-06 02:57:15 +01:00
|
|
|
return
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
applyToCellsForRepresentedObject(webFeed, configureIcon(_:_:))
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
@objc func webFeedSettingDidChange(_ note: Notification) {
|
|
|
|
guard let webFeed = note.object as? WebFeed, let key = note.userInfo?[WebFeed.WebFeedSettingUserInfoKey] as? String else {
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
if key == WebFeed.WebFeedSettingKey.homePageURL || key == WebFeed.WebFeedSettingKey.faviconURL {
|
|
|
|
configureCellsForRepresentedObject(webFeed)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
@objc func webFeedMetadataDidChange(_ note: Notification) {
|
2019-09-23 04:20:01 +02:00
|
|
|
reloadAllVisibleCells()
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:42:41 +02:00
|
|
|
@objc func userDidAddFeed(_ notification: Notification) {
|
2019-11-15 03:11:41 +01:00
|
|
|
guard let webFeed = notification.userInfo?[UserInfoKey.webFeed] as? WebFeed else {
|
2019-04-18 16:42:41 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
discloseFeed(webFeed, animated: true)
|
2019-04-18 16:42:41 +02:00
|
|
|
}
|
2019-05-20 02:18:28 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
@objc func contentSizeCategoryDidChange(_ note: Notification) {
|
2019-10-28 23:18:44 +01:00
|
|
|
resetEstimatedRowHeight()
|
2019-11-19 18:16:43 +01:00
|
|
|
applyChanges(animated: false)
|
2019-04-28 17:31:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 20:43:02 +01:00
|
|
|
@objc func willEnterForeground(_ note: Notification) {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: Table View
|
|
|
|
|
2019-04-21 01:20:25 +02:00
|
|
|
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
2019-04-28 17:31:35 +02:00
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
guard let nameProvider = coordinator.rootNode.childAtIndex(section)?.representedObject as? DisplayNameProvider else {
|
2019-04-28 17:31:35 +02:00
|
|
|
return 44
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerView = MasterFeedTableViewSectionHeader()
|
|
|
|
headerView.name = nameProvider.nameForDisplay
|
|
|
|
|
2019-04-28 18:25:21 +02:00
|
|
|
let size = headerView.sizeThatFits(CGSize(width: tableView.bounds.width, height: 0.0))
|
2019-04-28 17:31:35 +02:00
|
|
|
return size.height
|
|
|
|
|
2019-04-21 01:20:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
guard let nameProvider = coordinator.rootNode.childAtIndex(section)?.representedObject as? DisplayNameProvider else {
|
2019-04-17 20:35:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-18 14:24:55 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! MasterFeedTableViewSectionHeader
|
2019-04-18 14:24:55 +02:00
|
|
|
headerView.name = nameProvider.nameForDisplay
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
guard let sectionNode = coordinator.rootNode.childAtIndex(section) else {
|
2019-04-21 01:20:25 +02:00
|
|
|
return headerView
|
|
|
|
}
|
|
|
|
|
|
|
|
if let account = sectionNode.representedObject as? Account {
|
2019-04-18 14:24:55 +02:00
|
|
|
headerView.unreadCount = account.unreadCount
|
2019-04-18 17:49:31 +02:00
|
|
|
} else {
|
|
|
|
headerView.unreadCount = 0
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:38:38 +02:00
|
|
|
headerView.tag = section
|
2019-11-25 01:29:00 +01:00
|
|
|
headerView.disclosureExpanded = coordinator.isExpanded(sectionNode)
|
2019-11-01 17:40:52 +01:00
|
|
|
|
|
|
|
if section == tableView.numberOfSections - 1 {
|
|
|
|
headerView.isLastSection = true
|
|
|
|
} else {
|
|
|
|
headerView.isLastSection = false
|
|
|
|
}
|
2019-04-21 01:20:25 +02:00
|
|
|
|
2019-12-11 22:29:32 +01:00
|
|
|
headerView.gestureRecognizers?.removeAll()
|
2019-04-18 18:38:38 +02:00
|
|
|
let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:)))
|
|
|
|
headerView.addGestureRecognizer(tap)
|
|
|
|
|
2019-12-11 22:42:45 +01:00
|
|
|
headerView.interactions.removeAll()
|
2019-10-24 02:58:18 +02:00
|
|
|
if section != 0 {
|
|
|
|
headerView.addInteraction(UIContextMenuInteraction(delegate: self))
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
return headerView
|
|
|
|
|
2019-04-17 20:35:16 +02:00
|
|
|
}
|
2019-04-21 01:20:25 +02:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
|
|
|
|
return CGFloat.leastNormalMagnitude
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
|
|
|
|
return UIView(frame: CGRect.zero)
|
|
|
|
}
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-08-16 02:46:31 +02:00
|
|
|
var actions = [UIContextualAction]()
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
// Set up the delete action
|
|
|
|
let deleteTitle = NSLocalizedString("Delete", comment: "Delete")
|
2019-12-15 01:14:55 +01:00
|
|
|
let deleteAction = UIContextualAction(style: .normal, title: deleteTitle) { [weak self] (action, view, completion) in
|
2019-04-15 22:03:05 +02:00
|
|
|
self?.delete(indexPath: indexPath)
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-08-16 02:46:31 +02:00
|
|
|
deleteAction.backgroundColor = UIColor.systemRed
|
|
|
|
actions.append(deleteAction)
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
// Set up the rename action
|
|
|
|
let renameTitle = NSLocalizedString("Rename", comment: "Rename")
|
2019-12-15 01:14:55 +01:00
|
|
|
let renameAction = UIContextualAction(style: .normal, title: renameTitle) { [weak self] (action, view, completion) in
|
2019-04-15 22:03:05 +02:00
|
|
|
self?.rename(indexPath: indexPath)
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-08-16 02:46:31 +02:00
|
|
|
renameAction.backgroundColor = UIColor.systemOrange
|
|
|
|
actions.append(renameAction)
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
if let webFeed = dataSource.itemIdentifier(for: indexPath)?.representedObject as? WebFeed {
|
2019-08-16 02:46:31 +02:00
|
|
|
let moreTitle = NSLocalizedString("More", comment: "More")
|
2019-12-15 01:14:55 +01:00
|
|
|
let moreAction = UIContextualAction(style: .normal, title: moreTitle) { [weak self] (action, view, completion) in
|
2019-08-16 02:46:31 +02:00
|
|
|
|
|
|
|
if let self = self {
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
let alert = UIAlertController(title: webFeed.nameForDisplay, message: nil, preferredStyle: .actionSheet)
|
2019-08-16 02:46:31 +02:00
|
|
|
if let popoverController = alert.popoverPresentationController {
|
|
|
|
popoverController.sourceView = view
|
2019-08-16 19:14:55 +02:00
|
|
|
popoverController.sourceRect = CGRect(x: view.frame.size.width/2, y: view.frame.size.height/2, width: 1, height: 1)
|
2019-08-16 02:46:31 +02:00
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
if let action = self.getInfoAlertAction(indexPath: indexPath, completion: completion) {
|
2019-09-28 14:00:18 +02:00
|
|
|
alert.addAction(action)
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
if let action = self.homePageAlertAction(indexPath: indexPath, completion: completion) {
|
2019-08-16 02:46:31 +02:00
|
|
|
alert.addAction(action)
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
if let action = self.copyFeedPageAlertAction(indexPath: indexPath, completion: completion) {
|
2019-08-16 02:46:31 +02:00
|
|
|
alert.addAction(action)
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
if let action = self.copyHomePageAlertAction(indexPath: indexPath, completion: completion) {
|
2019-08-16 02:46:31 +02:00
|
|
|
alert.addAction(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
|
|
|
|
alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel) { _ in
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-08-16 02:46:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
self.present(alert, animated: true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
moreAction.backgroundColor = UIColor.systemGray
|
|
|
|
actions.append(moreAction)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: actions)
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
}
|
2019-08-15 20:19:02 +02:00
|
|
|
|
2019-08-26 03:00:34 +02:00
|
|
|
override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
2019-12-30 09:33:06 +01:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath) else {
|
2019-08-26 03:00:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
if node.representedObject is WebFeed {
|
2019-11-24 05:15:29 +01:00
|
|
|
return makeFeedContextMenu(node: node, indexPath: indexPath, includeDeleteRename: true)
|
2019-12-30 09:33:06 +01:00
|
|
|
} else if node.representedObject is Folder {
|
2019-11-24 05:15:29 +01:00
|
|
|
return makeFolderContextMenu(node: node, indexPath: indexPath)
|
2019-12-30 09:33:06 +01:00
|
|
|
} else if node.representedObject is PseudoFeed {
|
|
|
|
return makePseudoFeedContextMenu(node: node, indexPath: indexPath)
|
|
|
|
} else {
|
|
|
|
return nil
|
2019-08-26 03:00:34 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 05:15:29 +01:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
guard let nodeUniqueId = configuration.identifier as? Int,
|
|
|
|
let node = coordinator.rootNode.descendantNode(where: { $0.uniqueID == nodeUniqueId }),
|
|
|
|
let indexPath = dataSource.indexPath(for: node),
|
|
|
|
let cell = tableView.cellForRow(at: indexPath) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return UITargetedPreview(view: cell, parameters: CroppingPreviewParameters(view: cell))
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2019-09-04 23:24:16 +02:00
|
|
|
becomeFirstResponder()
|
2019-10-10 04:24:56 +02:00
|
|
|
coordinator.selectFeed(indexPath, animated: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-20 03:03:02 +02:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
|
|
|
|
|
|
|
|
// Adjust the index path so that it will never be in the smart feeds area
|
|
|
|
let destIndexPath: IndexPath = {
|
|
|
|
if proposedDestinationIndexPath.section == 0 {
|
|
|
|
return IndexPath(row: 0, section: 1)
|
|
|
|
}
|
2019-09-03 19:07:18 +02:00
|
|
|
return coordinator.cappedIndexPath(proposedDestinationIndexPath)
|
2019-04-20 03:03:02 +02:00
|
|
|
}()
|
|
|
|
|
2019-11-05 14:12:51 +01:00
|
|
|
guard let draggedNode = dataSource.itemIdentifier(for: sourceIndexPath) else {
|
2019-04-20 03:03:02 +02:00
|
|
|
assertionFailure("This should never happen")
|
|
|
|
return sourceIndexPath
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:12:51 +01:00
|
|
|
// If there is no destination node, we are dragging onto an empty Account
|
|
|
|
guard let destNode = dataSource.itemIdentifier(for: destIndexPath), let parentNode = destNode.parent else {
|
|
|
|
return proposedDestinationIndexPath
|
|
|
|
}
|
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
// If this is a folder and isn't expanded or doesn't have any entries, let the users drop on it
|
2019-11-25 01:29:00 +01:00
|
|
|
if destNode.representedObject is Folder && (destNode.numberOfChildNodes == 0 || !coordinator.isExpanded(destNode)) {
|
2019-11-23 02:59:25 +01:00
|
|
|
return proposedDestinationIndexPath
|
2019-04-20 03:03:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are dragging around in the same container, just return the original source
|
|
|
|
if parentNode.childNodes.contains(draggedNode) {
|
|
|
|
return sourceIndexPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suggest to the user the best place to drop the feed
|
|
|
|
// Revisit if the tree controller can ever be sorted in some other way.
|
|
|
|
let nodes = parentNode.childNodes + [draggedNode]
|
|
|
|
var sortedNodes = nodes.sortedAlphabeticallyWithFoldersAtEnd()
|
|
|
|
let index = sortedNodes.firstIndex(of: draggedNode)!
|
|
|
|
|
2019-11-26 20:42:25 +01:00
|
|
|
sortedNodes.remove(at: index)
|
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
if index == 0 {
|
2019-04-20 16:07:54 +02:00
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
if parentNode.representedObject is Account {
|
|
|
|
return IndexPath(row: 0, section: destIndexPath.section)
|
|
|
|
} else {
|
2019-11-26 20:42:25 +01:00
|
|
|
let candidateIndexPath = dataSource.indexPath(for: sortedNodes[index])!
|
|
|
|
let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0
|
|
|
|
return IndexPath(row: candidateIndexPath.row - movementAdjustment, section: candidateIndexPath.section)
|
2019-04-20 03:03:02 +02:00
|
|
|
}
|
2019-04-20 16:07:54 +02:00
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
} else {
|
2019-04-20 16:07:54 +02:00
|
|
|
|
2019-11-21 20:22:33 +01:00
|
|
|
if index >= sortedNodes.count {
|
2019-09-11 12:45:35 +02:00
|
|
|
let lastSortedIndexPath = dataSource.indexPath(for: sortedNodes[sortedNodes.count - 1])!
|
2019-11-23 02:59:25 +01:00
|
|
|
let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0
|
|
|
|
return IndexPath(row: lastSortedIndexPath.row + movementAdjustment, section: lastSortedIndexPath.section)
|
2019-04-20 16:07:54 +02:00
|
|
|
} else {
|
2019-11-23 02:59:25 +01:00
|
|
|
let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0
|
|
|
|
return dataSource.indexPath(for: sortedNodes[index - movementAdjustment])!
|
2019-04-20 16:07:54 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: Actions
|
|
|
|
|
2019-04-25 13:05:49 +02:00
|
|
|
@IBAction func settings(_ sender: UIBarButtonItem) {
|
2019-07-06 19:25:45 +02:00
|
|
|
coordinator.showSettings()
|
2019-04-17 14:00:32 +02:00
|
|
|
}
|
2019-04-17 01:25:55 +02:00
|
|
|
|
2020-01-08 01:39:45 +01:00
|
|
|
@IBAction func toggleFilter(_ sender: Any) {
|
2019-11-27 03:23:12 +01:00
|
|
|
if coordinator.isReadFeedsFiltered {
|
2020-01-09 22:38:25 +01:00
|
|
|
setFilterButtonToInactive()
|
2019-11-21 22:55:50 +01:00
|
|
|
coordinator.showAllFeeds()
|
|
|
|
} else {
|
2020-01-09 22:38:25 +01:00
|
|
|
setFilterButtonToActive()
|
2019-11-27 03:23:12 +01:00
|
|
|
coordinator.hideReadFeeds()
|
2019-11-21 22:55:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 01:39:45 +01:00
|
|
|
@IBAction func add(_ sender: UIBarButtonItem) {
|
2019-09-05 22:07:35 +02:00
|
|
|
coordinator.showAdd(.feed)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:38:38 +02:00
|
|
|
@objc func toggleSectionHeader(_ sender: UITapGestureRecognizer) {
|
|
|
|
|
|
|
|
guard let sectionIndex = sender.view?.tag,
|
2019-06-29 20:35:12 +02:00
|
|
|
let sectionNode = coordinator.rootNode.childAtIndex(sectionIndex),
|
2019-04-23 15:00:27 +02:00
|
|
|
let headerView = sender.view as? MasterFeedTableViewSectionHeader
|
2019-04-18 18:38:38 +02:00
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
if coordinator.isExpanded(sectionNode) {
|
2019-04-21 01:20:25 +02:00
|
|
|
headerView.disclosureExpanded = false
|
2019-09-11 23:53:27 +02:00
|
|
|
coordinator.collapse(sectionNode)
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true)
|
2019-04-18 18:38:38 +02:00
|
|
|
} else {
|
2019-04-21 01:20:25 +02:00
|
|
|
headerView.disclosureExpanded = true
|
2019-09-11 23:53:27 +02:00
|
|
|
coordinator.expand(sectionNode)
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true)
|
2019-04-18 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
@objc func refreshAccounts(_ sender: Any) {
|
2020-01-03 16:42:43 +01:00
|
|
|
refreshControl?.endRefreshing()
|
2020-01-11 02:14:21 +01:00
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
// 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) {
|
2020-01-11 02:14:21 +01:00
|
|
|
AccountManager.shared.refreshAll(errorHandler: ErrorHandler.present(self)) {
|
|
|
|
if AppDefaults.refreshClearsReadArticles {
|
|
|
|
self.coordinator.refreshTimeline(resetScroll: false)
|
|
|
|
}
|
|
|
|
}
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-19 22:45:52 +02:00
|
|
|
|
2019-09-04 23:24:16 +02:00
|
|
|
// MARK: Keyboard shortcuts
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
@objc func selectNextUp(_ sender: Any?) {
|
|
|
|
coordinator.selectPrevFeed()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func selectNextDown(_ sender: Any?) {
|
|
|
|
coordinator.selectNextFeed()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func navigateToTimeline(_ sender: Any?) {
|
|
|
|
coordinator.navigateToTimeline()
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:24:16 +02:00
|
|
|
@objc func openInBrowser(_ sender: Any?) {
|
|
|
|
coordinator.showBrowserForCurrentFeed()
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:54:58 +02:00
|
|
|
@objc override func delete(_ sender: Any?) {
|
|
|
|
if let indexPath = coordinator.currentFeedIndexPath {
|
|
|
|
delete(indexPath: indexPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 23:04:07 +02:00
|
|
|
@objc func expandSelectedRows(_ sender: Any?) {
|
2019-09-11 23:53:27 +02:00
|
|
|
if let indexPath = coordinator.currentFeedIndexPath, let node = dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
coordinator.expand(node)
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true) {
|
2019-09-09 17:06:13 +02:00
|
|
|
self.reloadAllVisibleCells()
|
|
|
|
}
|
2019-09-05 23:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func collapseSelectedRows(_ sender: Any?) {
|
2019-09-11 23:53:27 +02:00
|
|
|
if let indexPath = coordinator.currentFeedIndexPath, let node = dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
coordinator.collapse(node)
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true) {
|
2019-09-09 17:06:13 +02:00
|
|
|
self.reloadAllVisibleCells()
|
|
|
|
}
|
2019-09-05 23:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 23:38:33 +02:00
|
|
|
@objc func expandAll(_ sender: Any?) {
|
|
|
|
coordinator.expandAllSectionsAndFolders()
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true) {
|
2019-09-09 17:06:13 +02:00
|
|
|
self.reloadAllVisibleCells()
|
|
|
|
}
|
2019-09-05 23:38:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func collapseAllExceptForGroupItems(_ sender: Any?) {
|
|
|
|
coordinator.collapseAllFolders()
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true) {
|
2019-09-09 17:06:13 +02:00
|
|
|
self.reloadAllVisibleCells()
|
|
|
|
}
|
2019-09-05 23:38:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 22:45:52 +02:00
|
|
|
// MARK: API
|
|
|
|
|
2019-09-10 23:38:59 +02:00
|
|
|
func restoreSelectionIfNecessary(adjustScroll: Bool) {
|
|
|
|
if let indexPath = coordinator.masterFeedIndexPathForCurrentTimeline() {
|
|
|
|
if adjustScroll {
|
2019-10-23 02:20:35 +02:00
|
|
|
tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: false)
|
2019-09-10 23:38:59 +02:00
|
|
|
} else {
|
|
|
|
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:22:22 +01:00
|
|
|
func updateFeedSelection(animated: Bool) {
|
2019-09-12 20:49:23 +02:00
|
|
|
if dataSource.snapshot().numberOfItems > 0 {
|
|
|
|
if let indexPath = coordinator.currentFeedIndexPath {
|
|
|
|
if tableView.indexPathForSelectedRow != indexPath {
|
2019-11-13 22:22:22 +01:00
|
|
|
tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: animated)
|
2019-09-12 20:49:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-13 22:22:22 +01:00
|
|
|
tableView.selectRow(at: nil, animated: animated, scrollPosition: .none)
|
2019-08-25 18:38:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
func reloadFeeds(initialLoad: Bool) {
|
2019-08-25 18:38:04 +02:00
|
|
|
updateUI()
|
2019-09-01 17:11:03 +02:00
|
|
|
|
2019-08-30 23:19:06 +02:00
|
|
|
// We have to reload all the visible cells because if we got here by doing a table cell move,
|
|
|
|
// then the table itself is in a weird state. This is because we do unusual things like allowing
|
|
|
|
// drops on a "folder" that should cause the dropped cell to disappear.
|
2019-11-27 03:23:12 +01:00
|
|
|
applyChanges(animated: !initialLoad) { [weak self] in
|
|
|
|
if !initialLoad {
|
|
|
|
self?.reloadAllVisibleCells()
|
|
|
|
}
|
2019-09-01 17:11:03 +02:00
|
|
|
}
|
2019-08-25 18:38:04 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 20:14:14 +02:00
|
|
|
func ensureSectionIsExpanded(_ sectionIndex: Int, completion: (() -> Void)? = nil) {
|
|
|
|
guard let sectionNode = coordinator.rootNode.childAtIndex(sectionIndex) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
if !coordinator.isExpanded(sectionNode) {
|
2019-09-11 23:53:27 +02:00
|
|
|
coordinator.expand(sectionNode)
|
2019-11-19 18:16:43 +01:00
|
|
|
self.applyChanges(animated: true) {
|
2019-09-05 20:14:14 +02:00
|
|
|
completion?()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
completion?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:38:53 +01:00
|
|
|
func discloseFeed(_ webFeed: WebFeed, animated: Bool, completion: (() -> Void)? = nil) {
|
2019-08-19 22:45:52 +02:00
|
|
|
|
2019-12-19 17:38:53 +01:00
|
|
|
func discloseFeedInAccount() {
|
|
|
|
guard let node = coordinator.rootNode.descendantNodeRepresentingObject(webFeed as AnyObject) else {
|
2019-11-26 23:33:11 +01:00
|
|
|
completion?()
|
2019-12-19 17:38:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let indexPath = dataSource.indexPath(for: node) {
|
|
|
|
coordinator.selectFeed(indexPath, animated: animated) {
|
|
|
|
completion?()
|
|
|
|
}
|
|
|
|
return
|
2019-11-26 23:33:11 +01:00
|
|
|
}
|
2019-08-19 22:45:52 +02:00
|
|
|
|
2019-12-19 17:38:53 +01:00
|
|
|
// It wasn't already visable, so expand its folder and try again
|
2019-12-28 18:58:15 +01:00
|
|
|
guard let parent = node.parent, parent.representedObject is Folder else {
|
2019-12-19 17:38:53 +01:00
|
|
|
completion?()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinator.expand(parent)
|
|
|
|
reloadNode(parent)
|
2019-08-29 03:08:30 +02:00
|
|
|
|
2019-12-19 17:38:53 +01:00
|
|
|
applyChanges(animated: true, adjustScroll: true) { [weak self] in
|
|
|
|
if let indexPath = self?.dataSource.indexPath(for: node) {
|
|
|
|
self?.coordinator.selectFeed(indexPath, animated: animated) {
|
|
|
|
completion?()
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
}
|
2019-08-29 03:08:30 +02:00
|
|
|
}
|
2019-08-19 22:45:52 +02:00
|
|
|
}
|
2019-12-19 17:38:53 +01:00
|
|
|
|
|
|
|
// If the account for the feed is collapsed, expand it
|
|
|
|
if let account = webFeed.account,
|
|
|
|
let accountNode = coordinator.rootNode.childNodeRepresentingObject(account as AnyObject),
|
|
|
|
!coordinator.isExpanded(accountNode) {
|
|
|
|
|
|
|
|
coordinator.expand(accountNode)
|
2020-01-08 18:35:52 +01:00
|
|
|
applyChanges(animated: true) {
|
2019-12-19 17:38:53 +01:00
|
|
|
discloseFeedInAccount()
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
discloseFeedInAccount()
|
|
|
|
}
|
|
|
|
|
2019-08-19 22:45:52 +02:00
|
|
|
}
|
2019-08-15 19:42:25 +02:00
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
func focus() {
|
|
|
|
becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 02:58:18 +02:00
|
|
|
// MARK: UIContextMenuInteractionDelegate
|
|
|
|
|
|
|
|
extension MasterFeedViewController: UIContextMenuInteractionDelegate {
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
|
|
|
|
guard let sectionIndex = interaction.view?.tag,
|
|
|
|
let sectionNode = coordinator.rootNode.childAtIndex(sectionIndex),
|
2019-10-24 03:22:31 +02:00
|
|
|
let account = sectionNode.representedObject as? Account
|
2019-10-24 02:58:18 +02:00
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-24 05:15:29 +01:00
|
|
|
return UIContextMenuConfiguration(identifier: sectionIndex as NSCopying, previewProvider: nil) { suggestedActions in
|
2019-10-24 02:58:18 +02:00
|
|
|
let accountInfoAction = self.getAccountInfoAction(account: account)
|
2019-10-24 03:22:31 +02:00
|
|
|
let deactivateAction = self.deactivateAccountAction(account: account)
|
2019-12-30 09:33:06 +01:00
|
|
|
|
|
|
|
var actions = [accountInfoAction, deactivateAction]
|
|
|
|
|
|
|
|
if let markAllAction = self.markAllAsReadAction(account: account) {
|
|
|
|
actions.insert(markAllAction, at: 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UIMenu(title: "", children: actions)
|
2019-10-24 02:58:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 05:15:29 +01:00
|
|
|
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForHighlightingMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
|
|
|
|
guard let sectionIndex = configuration.identifier as? Int,
|
|
|
|
let cell = tableView.headerView(forSection: sectionIndex) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return UITargetedPreview(view: cell, parameters: CroppingPreviewParameters(view: cell))
|
|
|
|
}
|
2019-10-24 02:58:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
// MARK: MasterTableViewCellDelegate
|
|
|
|
|
|
|
|
extension MasterFeedViewController: MasterFeedTableViewCellDelegate {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
func disclosureSelected(_ sender: MasterFeedTableViewCell, expanding: Bool) {
|
|
|
|
if expanding {
|
|
|
|
expand(sender)
|
|
|
|
} else {
|
|
|
|
collapse(sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension MasterFeedViewController {
|
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
func configureToolbar() {
|
|
|
|
guard let refreshProgressView = Bundle.main.loadNibNamed("RefreshProgressView", owner: self, options: nil)?[0] as? RefreshProgressView else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.refreshProgressView = refreshProgressView
|
|
|
|
let refreshProgressItemButton = UIBarButtonItem(customView: refreshProgressView)
|
2020-01-08 01:39:45 +01:00
|
|
|
toolbarItems?.insert(refreshProgressItemButton, at: 2)
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
func updateUI() {
|
2019-11-27 03:23:12 +01:00
|
|
|
if coordinator.isReadFeedsFiltered {
|
2020-01-09 22:38:25 +01:00
|
|
|
setFilterButtonToActive()
|
2019-11-27 03:23:12 +01:00
|
|
|
} else {
|
2020-01-09 22:38:25 +01:00
|
|
|
setFilterButtonToInactive()
|
2019-11-27 03:23:12 +01:00
|
|
|
}
|
2019-10-30 10:04:13 +01:00
|
|
|
refreshProgressView?.updateRefreshLabel()
|
|
|
|
addNewItemButton?.isEnabled = !AccountManager.shared.activeAccounts.isEmpty
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
2019-08-29 01:06:27 +02:00
|
|
|
|
2020-01-09 22:38:25 +01:00
|
|
|
func setFilterButtonToActive() {
|
|
|
|
filterButton?.image = AppAssets.filterActiveImage
|
|
|
|
filterButton?.accLabelText = NSLocalizedString("Selected - Filter Read Feeds", comment: "Selected - Filter Read Feeds")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFilterButtonToInactive() {
|
|
|
|
filterButton?.image = AppAssets.filterInactiveImage
|
|
|
|
filterButton?.accLabelText = NSLocalizedString("Filter Read Feeds", comment: "Filter Read Feeds")
|
|
|
|
}
|
|
|
|
|
2019-08-29 03:21:50 +02:00
|
|
|
func reloadNode(_ node: Node) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.reloadItems([node])
|
2019-08-29 21:35:18 +02:00
|
|
|
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
|
2019-09-10 23:38:59 +02:00
|
|
|
self?.restoreSelectionIfNecessary(adjustScroll: false)
|
2019-08-29 21:35:18 +02:00
|
|
|
}
|
2019-08-29 03:21:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:16:43 +01:00
|
|
|
func applyChanges(animated: Bool, adjustScroll: Bool = false, completion: (() -> Void)? = nil) {
|
2019-09-11 12:33:54 +02:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Node, Node>()
|
|
|
|
let sectionNodes = coordinator.rootNode.childNodes
|
|
|
|
snapshot.appendSections(sectionNodes)
|
2019-08-29 01:06:27 +02:00
|
|
|
|
2019-09-11 12:33:54 +02:00
|
|
|
for (index, sectionNode) in sectionNodes.enumerated() {
|
|
|
|
let shadowTableNodes = coordinator.shadowNodesFor(section: index)
|
|
|
|
snapshot.appendItems(shadowTableNodes, toSection: sectionNode)
|
2019-08-29 01:06:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:16:43 +01:00
|
|
|
dataSource.apply(snapshot, animatingDifferences: animated) { [weak self] in
|
2019-09-10 23:38:59 +02:00
|
|
|
self?.restoreSelectionIfNecessary(adjustScroll: adjustScroll)
|
2019-08-29 03:08:30 +02:00
|
|
|
completion?()
|
2019-08-29 01:06:27 +02:00
|
|
|
}
|
2019-08-29 21:35:18 +02:00
|
|
|
}
|
2019-08-15 19:42:25 +02:00
|
|
|
|
2019-09-11 12:33:54 +02:00
|
|
|
func makeDataSource() -> UITableViewDiffableDataSource<Node, Node> {
|
2019-11-22 22:23:21 +01:00
|
|
|
let dataSource = MasterFeedDataSource(tableView: tableView, cellProvider: { [weak self] tableView, indexPath, node in
|
2019-08-29 01:06:27 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MasterFeedTableViewCell
|
|
|
|
self?.configure(cell, node)
|
|
|
|
return cell
|
|
|
|
})
|
2019-11-22 22:23:21 +01:00
|
|
|
dataSource.defaultRowAnimation = .middle
|
|
|
|
return dataSource
|
2019-08-29 01:06:27 +02:00
|
|
|
}
|
2019-10-28 23:18:44 +01:00
|
|
|
|
|
|
|
func resetEstimatedRowHeight() {
|
|
|
|
let titleLabel = NonIntrinsicLabel()
|
|
|
|
titleLabel.text = "But I must explain"
|
|
|
|
|
|
|
|
let unreadCountView = MasterFeedUnreadCountView()
|
|
|
|
unreadCountView.unreadCount = 10
|
|
|
|
|
|
|
|
let layout = MasterFeedTableViewCellLayout(cellWidth: tableView.bounds.size.width, insets: tableView.safeAreaInsets, label: titleLabel, unreadCountView: unreadCountView, showingEditingControl: false, indent: false, shouldShowDisclosure: false)
|
|
|
|
tableView.estimatedRowHeight = layout.height
|
|
|
|
}
|
2019-08-29 01:06:27 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func configure(_ cell: MasterFeedTableViewCell, _ node: Node) {
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-17 17:34:10 +02:00
|
|
|
cell.delegate = self
|
2019-10-29 23:55:49 +01:00
|
|
|
if node.representedObject is Folder {
|
2019-04-20 18:25:02 +02:00
|
|
|
cell.indentationLevel = 0
|
2019-10-29 23:55:49 +01:00
|
|
|
} else {
|
|
|
|
cell.indentationLevel = 1
|
2019-04-20 18:11:09 +02:00
|
|
|
}
|
2019-11-25 01:29:00 +01:00
|
|
|
cell.setDisclosure(isExpanded: coordinator.isExpanded(node), animated: false)
|
2019-09-27 02:43:17 +02:00
|
|
|
cell.isDisclosureAvailable = node.canHaveChildNodes
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
cell.name = nameFor(node)
|
2019-08-21 20:10:08 +02:00
|
|
|
cell.unreadCount = coordinator.unreadCountFor(node)
|
2019-11-06 01:05:57 +01:00
|
|
|
configureIcon(cell, node)
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-10-29 01:52:50 +01:00
|
|
|
guard let indexPath = dataSource.indexPath(for: node) else { return }
|
|
|
|
let rowsInSection = tableView.numberOfRows(inSection: indexPath.section)
|
|
|
|
if indexPath.row == rowsInSection - 1 {
|
|
|
|
cell.isSeparatorShown = false
|
|
|
|
} else {
|
|
|
|
cell.isSeparatorShown = true
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
func configureIcon(_ cell: MasterFeedTableViewCell, _ node: Node) {
|
|
|
|
cell.iconImage = imageFor(node)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
func imageFor(_ node: Node) -> IconImage? {
|
2019-11-15 03:11:41 +01:00
|
|
|
if let webFeed = node.representedObject as? WebFeed {
|
2019-10-29 02:57:26 +01:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
let feedIconImage = appDelegate.webFeedIconDownloader.icon(for: webFeed)
|
2019-10-29 02:57:26 +01:00
|
|
|
if feedIconImage != nil {
|
|
|
|
return feedIconImage
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:17:00 +01:00
|
|
|
if let faviconImage = appDelegate.faviconDownloader.faviconAsIcon(for: webFeed) {
|
2019-10-29 02:57:26 +01:00
|
|
|
return faviconImage
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
if let smallIconProvider = node.representedObject as? SmallIconProvider {
|
|
|
|
return smallIconProvider.smallIcon
|
|
|
|
}
|
2019-10-29 02:57:26 +01:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameFor(_ node: Node) -> String {
|
|
|
|
if let displayNameProvider = node.representedObject as? DisplayNameProvider {
|
|
|
|
return displayNameProvider.nameForDisplay
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2019-08-21 20:10:08 +02:00
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
func configureCellsForRepresentedObject(_ representedObject: AnyObject) {
|
|
|
|
applyToCellsForRepresentedObject(representedObject, configure)
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func applyToCellsForRepresentedObject(_ representedObject: AnyObject, _ completion: (MasterFeedTableViewCell, Node) -> Void) {
|
2019-08-15 19:42:25 +02:00
|
|
|
applyToAvailableCells { (cell, node) in
|
|
|
|
if node.representedObject === representedObject {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(cell, node)
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func applyToAvailableCells(_ completion: (MasterFeedTableViewCell, Node) -> Void) {
|
2019-08-15 19:42:25 +02:00
|
|
|
tableView.visibleCells.forEach { cell in
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell), let node = dataSource.itemIdentifier(for: indexPath) else {
|
2019-08-15 19:42:25 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(cell as! MasterFeedTableViewCell, node)
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 23:19:06 +02:00
|
|
|
private func reloadAllVisibleCells() {
|
2019-09-11 12:45:35 +02:00
|
|
|
let visibleNodes = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
|
2019-08-30 23:19:06 +02:00
|
|
|
reloadCells(visibleNodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reloadCells(_ nodes: [Node]) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.reloadItems(nodes)
|
2019-09-06 18:22:35 +02:00
|
|
|
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
|
2019-09-10 23:38:59 +02:00
|
|
|
self?.restoreSelectionIfNecessary(adjustScroll: false)
|
2019-09-06 18:22:35 +02:00
|
|
|
}
|
2019-08-30 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 19:42:25 +02:00
|
|
|
private func accountForNode(_ node: Node) -> Account? {
|
|
|
|
if let account = node.representedObject as? Account {
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
if let folder = node.representedObject as? Folder {
|
|
|
|
return folder.account
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
if let feed = node.representedObject as? WebFeed {
|
2019-08-15 19:42:25 +02:00
|
|
|
return feed.account
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expand(_ cell: MasterFeedTableViewCell) {
|
2019-09-11 23:53:27 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell), let node = dataSource.itemIdentifier(for: indexPath) else {
|
2019-08-15 19:42:25 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-11 23:53:27 +02:00
|
|
|
coordinator.expand(node)
|
2019-11-19 18:16:43 +01:00
|
|
|
applyChanges(animated: true) { [weak self] in
|
2019-10-29 01:52:50 +01:00
|
|
|
self?.reloadNode(node)
|
|
|
|
}
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func collapse(_ cell: MasterFeedTableViewCell) {
|
2019-09-11 23:53:27 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell), let node = dataSource.itemIdentifier(for: indexPath) else {
|
2019-08-15 19:42:25 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-11 23:53:27 +02:00
|
|
|
coordinator.collapse(node)
|
2019-11-19 18:16:43 +01:00
|
|
|
applyChanges(animated: true) { [weak self] in
|
2019-10-29 01:52:50 +01:00
|
|
|
self?.reloadNode(node)
|
|
|
|
}
|
2019-08-15 19:42:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 05:15:29 +01:00
|
|
|
func makeFeedContextMenu(node: Node, indexPath: IndexPath, includeDeleteRename: Bool) -> UIContextMenuConfiguration {
|
|
|
|
return UIContextMenuConfiguration(identifier: node.uniqueID as NSCopying, previewProvider: nil, actionProvider: { [ weak self] suggestedActions in
|
2019-08-19 22:45:52 +02:00
|
|
|
|
|
|
|
guard let self = self else { return nil }
|
2019-08-15 20:19:02 +02:00
|
|
|
|
|
|
|
var actions = [UIAction]()
|
|
|
|
|
2019-09-28 14:00:18 +02:00
|
|
|
if let inspectorAction = self.getInfoAction(indexPath: indexPath) {
|
|
|
|
actions.append(inspectorAction)
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
if let homePageAction = self.homePageAction(indexPath: indexPath) {
|
|
|
|
actions.append(homePageAction)
|
2019-06-06 00:42:35 +02:00
|
|
|
}
|
2019-08-15 20:19:02 +02:00
|
|
|
|
2019-08-15 22:19:23 +02:00
|
|
|
if let copyFeedPageAction = self.copyFeedPageAction(indexPath: indexPath) {
|
|
|
|
actions.append(copyFeedPageAction)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let copyHomePageAction = self.copyHomePageAction(indexPath: indexPath) {
|
|
|
|
actions.append(copyHomePageAction)
|
|
|
|
}
|
2019-12-30 09:33:06 +01:00
|
|
|
|
|
|
|
if let markAllAction = self.markAllAsReadAction(indexPath: indexPath) {
|
|
|
|
actions.append(markAllAction)
|
|
|
|
}
|
2019-08-15 20:19:02 +02:00
|
|
|
|
2020-01-02 08:16:29 +01:00
|
|
|
if includeDeleteRename {
|
|
|
|
actions.append(self.renameAction(indexPath: indexPath))
|
|
|
|
actions.append(self.deleteAction(indexPath: indexPath))
|
|
|
|
}
|
|
|
|
|
2019-08-19 22:49:42 +02:00
|
|
|
return UIMenu(title: "", children: actions)
|
2019-08-15 20:19:02 +02:00
|
|
|
|
|
|
|
})
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 05:15:29 +01:00
|
|
|
func makeFolderContextMenu(node: Node, indexPath: IndexPath) -> UIContextMenuConfiguration {
|
|
|
|
return UIContextMenuConfiguration(identifier: node.uniqueID as NSCopying, previewProvider: nil, actionProvider: { [weak self] suggestedActions in
|
2019-08-16 00:46:42 +02:00
|
|
|
|
2019-08-19 22:45:52 +02:00
|
|
|
guard let self = self else { return nil }
|
|
|
|
|
2019-08-16 00:46:42 +02:00
|
|
|
var actions = [UIAction]()
|
|
|
|
actions.append(self.deleteAction(indexPath: indexPath))
|
|
|
|
actions.append(self.renameAction(indexPath: indexPath))
|
2019-12-30 09:33:06 +01:00
|
|
|
|
|
|
|
if let markAllAction = self.markAllAsReadAction(indexPath: indexPath) {
|
|
|
|
actions.append(markAllAction)
|
|
|
|
}
|
2019-08-16 00:46:42 +02:00
|
|
|
|
2019-08-19 22:49:42 +02:00
|
|
|
return UIMenu(title: "", children: actions)
|
2019-08-16 00:46:42 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-30 09:33:06 +01:00
|
|
|
func makePseudoFeedContextMenu(node: Node, indexPath: IndexPath) -> UIContextMenuConfiguration? {
|
|
|
|
guard let markAllAction = self.markAllAsReadAction(indexPath: indexPath) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return UIContextMenuConfiguration(identifier: node.uniqueID as NSCopying, previewProvider: nil, actionProvider: { suggestedActions in
|
|
|
|
return UIMenu(title: "", children: [markAllAction])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
func homePageAction(indexPath: IndexPath) -> UIAction? {
|
2019-09-04 23:24:16 +02:00
|
|
|
guard coordinator.homePageURLForFeed(indexPath) != nil else {
|
|
|
|
return nil
|
2019-06-06 00:42:35 +02:00
|
|
|
}
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
let title = NSLocalizedString("Open Home Page", comment: "Open Home Page")
|
2019-09-04 23:24:16 +02:00
|
|
|
let action = UIAction(title: title, image: AppAssets.safariImage) { [weak self] action in
|
|
|
|
self?.coordinator.showBrowserForFeed(indexPath)
|
2019-08-15 20:19:02 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func homePageAlertAction(indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
|
2019-09-04 23:24:16 +02:00
|
|
|
guard coordinator.homePageURLForFeed(indexPath) != nil else {
|
|
|
|
return nil
|
2019-08-16 02:46:31 +02:00
|
|
|
}
|
2019-09-04 23:24:16 +02:00
|
|
|
|
2019-08-16 02:46:31 +02:00
|
|
|
let title = NSLocalizedString("Open Home Page", comment: "Open Home Page")
|
2019-09-04 23:24:16 +02:00
|
|
|
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
|
|
|
|
self?.coordinator.showBrowserForFeed(indexPath)
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-08-16 02:46:31 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:19:23 +02:00
|
|
|
func copyFeedPageAction(indexPath: IndexPath) -> UIAction? {
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath),
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = node.representedObject as? WebFeed,
|
2019-08-15 22:19:23 +02:00
|
|
|
let url = URL(string: feed.url) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Copy Feed URL", comment: "Copy Feed URL")
|
2019-08-16 20:27:41 +02:00
|
|
|
let action = UIAction(title: title, image: AppAssets.copyImage) { action in
|
2019-08-15 22:19:23 +02:00
|
|
|
UIPasteboard.general.url = url
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func copyFeedPageAlertAction(indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath),
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = node.representedObject as? WebFeed,
|
2019-08-16 02:46:31 +02:00
|
|
|
let url = URL(string: feed.url) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Copy Feed URL", comment: "Copy Feed URL")
|
|
|
|
let action = UIAlertAction(title: title, style: .default) { action in
|
|
|
|
UIPasteboard.general.url = url
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-08-16 02:46:31 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:19:23 +02:00
|
|
|
func copyHomePageAction(indexPath: IndexPath) -> UIAction? {
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath),
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = node.representedObject as? WebFeed,
|
2019-08-15 22:19:23 +02:00
|
|
|
let homePageURL = feed.homePageURL,
|
|
|
|
let url = URL(string: homePageURL) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Copy Home Page URL", comment: "Copy Home Page URL")
|
2019-08-16 20:27:41 +02:00
|
|
|
let action = UIAction(title: title, image: AppAssets.copyImage) { action in
|
2019-08-15 22:19:23 +02:00
|
|
|
UIPasteboard.general.url = url
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func copyHomePageAlertAction(indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath),
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = node.representedObject as? WebFeed,
|
2019-08-16 02:46:31 +02:00
|
|
|
let homePageURL = feed.homePageURL,
|
|
|
|
let url = URL(string: homePageURL) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Copy Home Page URL", comment: "Copy Home Page URL")
|
|
|
|
let action = UIAlertAction(title: title, style: .default) { action in
|
|
|
|
UIPasteboard.general.url = url
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-08-16 02:46:31 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
func deleteAction(indexPath: IndexPath) -> UIAction {
|
|
|
|
let title = NSLocalizedString("Delete", comment: "Delete")
|
2019-10-05 05:03:31 +02:00
|
|
|
|
|
|
|
let action = UIAction(title: title, image: AppAssets.trashImage, attributes: .destructive) { [weak self] action in
|
2019-08-16 20:54:19 +02:00
|
|
|
self?.delete(indexPath: indexPath)
|
2019-08-15 20:19:02 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameAction(indexPath: IndexPath) -> UIAction {
|
|
|
|
let title = NSLocalizedString("Rename", comment: "Rename")
|
2019-08-16 20:54:19 +02:00
|
|
|
let action = UIAction(title: title, image: AppAssets.editImage) { [weak self] action in
|
|
|
|
self?.rename(indexPath: indexPath)
|
2019-08-15 20:19:02 +02:00
|
|
|
}
|
|
|
|
return action
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-09-28 14:00:18 +02:00
|
|
|
func getInfoAction(indexPath: IndexPath) -> UIAction? {
|
2019-11-15 03:11:41 +01:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath), let feed = node.representedObject as? WebFeed else {
|
2019-09-28 14:00:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Get Info", comment: "Get Info")
|
|
|
|
let action = UIAction(title: title, image: AppAssets.infoImage) { [weak self] action in
|
|
|
|
self?.coordinator.showFeedInspector(for: feed)
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-10-24 02:58:18 +02:00
|
|
|
func getAccountInfoAction(account: Account) -> UIAction {
|
|
|
|
let title = NSLocalizedString("Get Info", comment: "Get Info")
|
|
|
|
let action = UIAction(title: title, image: AppAssets.infoImage) { [weak self] action in
|
|
|
|
self?.coordinator.showAccountInspector(for: account)
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-10-24 03:22:31 +02:00
|
|
|
func deactivateAccountAction(account: Account) -> UIAction {
|
|
|
|
let title = NSLocalizedString("Deactivate", comment: "Deactivate")
|
|
|
|
let action = UIAction(title: title, image: AppAssets.deactivateImage) { action in
|
|
|
|
account.isActive = false
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getInfoAlertAction(indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
|
2019-11-15 03:11:41 +01:00
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath), let feed = node.representedObject as? WebFeed else {
|
2019-09-28 14:00:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-05 05:03:31 +02:00
|
|
|
let title = NSLocalizedString("Get Info", comment: "Get Info")
|
2019-09-28 14:00:18 +02:00
|
|
|
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
|
|
|
|
self?.coordinator.showFeedInspector(for: feed)
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(true)
|
2019-09-28 14:00:18 +02:00
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
2019-12-30 09:33:06 +01:00
|
|
|
|
|
|
|
func markAllAsReadAction(indexPath: IndexPath) -> UIAction? {
|
|
|
|
guard let node = dataSource.itemIdentifier(for: indexPath),
|
|
|
|
coordinator.unreadCountFor(node) > 0 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let articleFetcher = node.representedObject as? Feed,
|
|
|
|
let fetchedArticles = try? articleFetcher.fetchArticles() else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let articles = Array(fetchedArticles)
|
|
|
|
return markAllAsReadAction(articles: articles, nameForDisplay: articleFetcher.nameForDisplay)
|
|
|
|
}
|
|
|
|
|
|
|
|
func markAllAsReadAction(account: Account) -> UIAction? {
|
|
|
|
guard let fetchedArticles = try? account.fetchArticles(FetchType.unread) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let articles = Array(fetchedArticles)
|
|
|
|
return markAllAsReadAction(articles: articles, nameForDisplay: account.nameForDisplay)
|
|
|
|
}
|
|
|
|
|
|
|
|
func markAllAsReadAction(articles: [Article], nameForDisplay: String) -> UIAction? {
|
|
|
|
guard articles.canMarkAllAsRead() else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let localizedMenuText = NSLocalizedString("Mark All as Read in “%@”", comment: "Command")
|
|
|
|
let title = NSString.localizedStringWithFormat(localizedMenuText as NSString, nameForDisplay) as String
|
|
|
|
|
2020-01-07 02:07:04 +01:00
|
|
|
let action = UIAction(title: title, image: AppAssets.markAllAsReadImage) { [weak self] action in
|
2019-12-30 09:33:06 +01:00
|
|
|
self?.coordinator.markAllAsRead(articles)
|
|
|
|
}
|
|
|
|
|
|
|
|
return action
|
|
|
|
}
|
2019-09-28 14:00:18 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
func rename(indexPath: IndexPath) {
|
|
|
|
|
2019-09-11 12:45:35 +02:00
|
|
|
let name = (dataSource.itemIdentifier(for: indexPath)?.representedObject as? DisplayNameProvider)?.nameForDisplay ?? ""
|
2019-04-15 22:10:30 +02:00
|
|
|
let formatString = NSLocalizedString("Rename “%@”", comment: "Feed finder")
|
|
|
|
let title = NSString.localizedStringWithFormat(formatString as NSString, name) as String
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .alert)
|
|
|
|
|
|
|
|
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
|
|
|
|
alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel))
|
|
|
|
|
|
|
|
let renameTitle = NSLocalizedString("Rename", comment: "Rename")
|
|
|
|
let renameAction = UIAlertAction(title: renameTitle, style: .default) { [weak self] action in
|
|
|
|
|
2019-09-11 12:45:35 +02:00
|
|
|
guard let node = self?.dataSource.itemIdentifier(for: indexPath),
|
2019-04-15 22:03:05 +02:00
|
|
|
let name = alertController.textFields?[0].text,
|
|
|
|
!name.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
if let feed = node.representedObject as? WebFeed {
|
2019-06-26 13:23:08 +02:00
|
|
|
feed.rename(to: name) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-08-29 03:21:50 +02:00
|
|
|
self?.reloadNode(node)
|
2019-06-26 13:23:08 +02:00
|
|
|
case .failure(let error):
|
|
|
|
self?.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
} else if let folder = node.representedObject as? Folder {
|
2019-06-26 13:23:08 +02:00
|
|
|
folder.rename(to: name) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-08-29 03:21:50 +02:00
|
|
|
self?.reloadNode(node)
|
2019-06-26 13:23:08 +02:00
|
|
|
case .failure(let error):
|
|
|
|
self?.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
alertController.addAction(renameAction)
|
|
|
|
|
|
|
|
alertController.addTextField() { textField in
|
2020-01-08 20:48:37 +01:00
|
|
|
textField.text = name
|
2019-04-15 22:03:05 +02:00
|
|
|
textField.placeholder = NSLocalizedString("Name", comment: "Name")
|
|
|
|
}
|
|
|
|
|
|
|
|
self.present(alertController, animated: true) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-04-18 16:42:41 +02:00
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
func delete(indexPath: IndexPath) {
|
|
|
|
guard let undoManager = undoManager,
|
2019-09-11 12:45:35 +02:00
|
|
|
let deleteNode = dataSource.itemIdentifier(for: indexPath),
|
2019-09-01 21:56:27 +02:00
|
|
|
let deleteCommand = DeleteCommand(nodesToDelete: [deleteNode], undoManager: undoManager, errorHandler: ErrorHandler.present(self))
|
2019-08-15 20:19:02 +02:00
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-28 18:30:40 +02:00
|
|
|
if let folder = deleteNode.representedObject as? Folder {
|
2019-09-01 02:30:21 +02:00
|
|
|
ActivityManager.cleanUp(folder)
|
2019-11-15 03:11:41 +01:00
|
|
|
} else if let feed = deleteNode.representedObject as? WebFeed {
|
2019-09-01 02:30:21 +02:00
|
|
|
ActivityManager.cleanUp(feed)
|
2019-08-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
pushUndoableCommand(deleteCommand)
|
2019-08-29 01:06:27 +02:00
|
|
|
deleteCommand.perform()
|
2019-09-08 15:40:15 +02:00
|
|
|
|
|
|
|
if indexPath == coordinator.currentFeedIndexPath {
|
2019-11-13 22:22:22 +01:00
|
|
|
coordinator.selectFeed(nil, animated: false)
|
2019-09-08 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:19:02 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|