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-04-23 15:00:27 +02:00
|
|
|
class MasterFeedViewController: ProgressTableViewController, UndoableCommandRunner {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-05-19 23:08:48 +02:00
|
|
|
@IBOutlet private weak var markAllAsReadButton: UIBarButtonItem!
|
|
|
|
@IBOutlet private weak var addNewItemButton: UIBarButtonItem!
|
2019-04-23 11:35:48 +02:00
|
|
|
|
2019-04-17 01:25:55 +02:00
|
|
|
var undoableCommands = [UndoableCommand]()
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
let navState = NavigationStateController()
|
2019-04-17 01:25:55 +02:00
|
|
|
override var canBecomeFirstResponder: Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-04-17 03:56:02 +02:00
|
|
|
navigationItem.rightBarButtonItem = editButtonItem
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
tableView.register(MasterFeedTableViewSectionHeader.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
|
2019-04-18 14:24:55 +02:00
|
|
|
|
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)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(feedSettingDidChange(_:)), name: .FeedSettingDidChange, object: nil)
|
2019-04-18 16:42:41 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil)
|
2019-05-20 02:18:28 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange(_:)), name: .AccountsDidChange, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange(_:)), name: .AccountStateDidChange, object: nil)
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(backingStoresDidRebuild(_:)), name: .BackingStoresDidRebuild, object: navState)
|
2019-04-22 23:25:16 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(masterSelectionDidChange(_:)), name: .MasterSelectionDidChange, object: navState)
|
2019-04-28 17:31:35 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
|
2019-04-22 23:25:16 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
refreshControl = UIRefreshControl()
|
|
|
|
refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged)
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
updateUI()
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
2019-05-20 01:39:15 +02:00
|
|
|
clearsSelectionOnViewWillAppear = true
|
2019-04-29 01:53:57 +02:00
|
|
|
navigationController?.title = NSLocalizedString("Feeds", comment: "Feeds")
|
2019-04-15 22:03:05 +02:00
|
|
|
super.viewWillAppear(animated)
|
|
|
|
}
|
2019-04-17 01:25:55 +02:00
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
super.viewWillDisappear(animated)
|
|
|
|
resignFirstResponder()
|
|
|
|
}
|
2019-04-17 20:35:16 +02:00
|
|
|
|
|
|
|
// MARK: Notifications
|
2019-04-17 01:25:55 +02:00
|
|
|
|
2019-04-21 21:47:23 +02:00
|
|
|
@objc dynamic func backingStoresDidRebuild(_ notification: Notification) {
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2019-04-18 14:24:55 +02:00
|
|
|
|
2019-04-28 19:37:53 +02:00
|
|
|
updateUI()
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
guard let representedObject = note.object else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-18 14:24:55 +02:00
|
|
|
|
|
|
|
if let account = representedObject as? Account {
|
2019-04-22 00:42:26 +02:00
|
|
|
if let node = navState.rootNode.childNodeRepresentingObject(account) {
|
|
|
|
let sectionIndex = navState.rootNode.indexOfChild(node)!
|
2019-04-23 15:00:27 +02:00
|
|
|
if let headerView = tableView.headerView(forSection: sectionIndex) as? MasterFeedTableViewSectionHeader {
|
2019-04-23 01:00:26 +02:00
|
|
|
headerView.unreadCount = account.unreadCount
|
|
|
|
}
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:52:40 +02:00
|
|
|
guard let node = navState.rootNode.descendantNodeRepresentingObject(representedObject as AnyObject),
|
2019-04-28 19:37:53 +02:00
|
|
|
let indexPath = navState.indexPathFor(node) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tableView.reloadRows(at: [indexPath], with: .automatic)
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func faviconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
applyToAvailableCells(configureFavicon)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func feedSettingDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
guard let feed = note.object as? Feed, let key = note.userInfo?[Feed.FeedSettingUserInfoKey] as? String else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == Feed.FeedSettingKey.homePageURL || key == Feed.FeedSettingKey.faviconURL {
|
|
|
|
configureCellsForRepresentedObject(feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:42:41 +02:00
|
|
|
@objc func userDidAddFeed(_ notification: Notification) {
|
|
|
|
|
|
|
|
guard let feed = notification.userInfo?[UserInfoKey.feed],
|
2019-04-22 00:42:26 +02:00
|
|
|
let node = navState.rootNode.descendantNodeRepresentingObject(feed as AnyObject) else {
|
2019-04-18 16:42:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
if let indexPath = navState.indexPathFor(node) {
|
2019-04-18 16:42:41 +02:00
|
|
|
tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// It wasn't already visable, so expand its folder and try again
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let parent = node.parent, let indexPath = navState.indexPathFor(parent) else {
|
2019-04-18 16:42:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.expand(indexPath) { [weak self] indexPaths in
|
2019-04-21 22:18:09 +02:00
|
|
|
self?.tableView.beginUpdates()
|
|
|
|
self?.tableView.insertRows(at: indexPaths, with: .automatic)
|
|
|
|
self?.tableView.endUpdates()
|
|
|
|
}
|
2019-04-18 16:42:41 +02:00
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
if let indexPath = navState.indexPathFor(node) {
|
2019-04-18 16:42:41 +02:00
|
|
|
tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-20 02:18:28 +02:00
|
|
|
|
|
|
|
@objc func accountsDidChange(_ notification: Notification) {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func accountStateDidChange(_ notification: Notification) {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
2019-04-22 23:25:16 +02:00
|
|
|
@objc func masterSelectionDidChange(_ note: Notification) {
|
|
|
|
if let indexPath = navState.currentMasterIndexPath {
|
|
|
|
if tableView.indexPathForSelectedRow != indexPath {
|
|
|
|
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
@objc func contentSizeCategoryDidChange(_ note: Notification) {
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: Table View
|
|
|
|
|
2019-04-17 20:35:16 +02:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-04-22 01:13:39 +02:00
|
|
|
return navState.numberOfSections
|
2019-04-17 20:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-04-22 01:13:39 +02:00
|
|
|
return navState.rowsInSection(section)
|
2019-04-17 20:35:16 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
guard let nameProvider = navState.rootNode.childAtIndex(section)?.representedObject as? DisplayNameProvider else {
|
|
|
|
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-04-22 00:42:26 +02:00
|
|
|
guard let nameProvider = navState.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-04-22 00:42:26 +02:00
|
|
|
guard let sectionNode = navState.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-04-22 01:13:39 +02:00
|
|
|
headerView.disclosureExpanded = navState.isExpanded(sectionNode)
|
2019-04-21 01:20:25 +02:00
|
|
|
|
2019-04-18 18:38:38 +02:00
|
|
|
let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:)))
|
|
|
|
headerView.addGestureRecognizer(tap)
|
|
|
|
|
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, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MasterFeedTableViewCell
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let node = navState.nodeFor(indexPath) else {
|
2019-04-15 22:03:05 +02:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
configure(cell, node)
|
|
|
|
return cell
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let node = navState.nodeFor(indexPath), !(node.representedObject is PseudoFeed) else {
|
2019-04-15 22:03:05 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
|
|
|
|
|
|
|
// Set up the delete action
|
|
|
|
let deleteTitle = NSLocalizedString("Delete", comment: "Delete")
|
|
|
|
let deleteAction = UIContextualAction(style: .normal, title: deleteTitle) { [weak self] (action, view, completionHandler) in
|
|
|
|
self?.delete(indexPath: indexPath)
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAction.backgroundColor = UIColor.red
|
|
|
|
|
|
|
|
// Set up the rename action
|
|
|
|
let renameTitle = NSLocalizedString("Rename", comment: "Rename")
|
|
|
|
let renameAction = UIContextualAction(style: .normal, title: renameTitle) { [weak self] (action, view, completionHandler) in
|
|
|
|
self?.rename(indexPath: indexPath)
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
renameAction.backgroundColor = UIColor.gray
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: [deleteAction, renameAction])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
2019-04-17 20:35:16 +02:00
|
|
|
let timeline = UIStoryboard.main.instantiateController(ofType: MasterTimelineViewController.self)
|
2019-04-22 00:42:26 +02:00
|
|
|
timeline.navState = navState
|
2019-04-22 23:25:16 +02:00
|
|
|
navState.currentMasterIndexPath = indexPath
|
2019-04-17 20:35:16 +02:00
|
|
|
self.navigationController?.pushViewController(timeline, animated: true)
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-20 03:03:02 +02:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let node = navState.nodeFor(indexPath) else {
|
2019-04-20 03:03:02 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return node.representedObject is Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return proposedDestinationIndexPath
|
|
|
|
}()
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let draggedNode = navState.nodeFor(sourceIndexPath), let destNode = navState.nodeFor(destIndexPath), let parentNode = destNode.parent else {
|
2019-04-20 03:03:02 +02:00
|
|
|
assertionFailure("This should never happen")
|
|
|
|
return sourceIndexPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a folder and isn't expanded or doesn't have any entries, let the users drop on it
|
2019-04-22 01:13:39 +02:00
|
|
|
if destNode.representedObject is Folder && (destNode.numberOfChildNodes == 0 || !navState.isExpanded(destNode)) {
|
2019-04-20 16:07:54 +02:00
|
|
|
let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0
|
2019-04-20 03:03:02 +02:00
|
|
|
return IndexPath(row: destIndexPath.row + movementAdjustment, section: destIndexPath.section)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)!
|
|
|
|
|
|
|
|
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-04-22 00:42:26 +02:00
|
|
|
return navState.indexPathFor(parentNode)!
|
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
|
|
|
|
|
|
|
sortedNodes.remove(at: index)
|
|
|
|
|
|
|
|
let movementAdjustment = sourceIndexPath < destIndexPath ? 1 : 0
|
|
|
|
let adjustedIndex = index - movementAdjustment
|
|
|
|
if adjustedIndex >= sortedNodes.count {
|
2019-04-22 00:42:26 +02:00
|
|
|
let lastSortedIndexPath = navState.indexPathFor(sortedNodes[sortedNodes.count - 1])!
|
2019-04-20 16:07:54 +02:00
|
|
|
return IndexPath(row: lastSortedIndexPath.row + 1, section: lastSortedIndexPath.section)
|
|
|
|
} else {
|
2019-04-22 00:42:26 +02:00
|
|
|
return navState.indexPathFor(sortedNodes[adjustedIndex])!
|
2019-04-20 16:07:54 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 03:03:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let sourceNode = navState.nodeFor(sourceIndexPath), let feed = sourceNode.representedObject as? Feed else {
|
2019-04-20 15:46:58 +02:00
|
|
|
return
|
2019-04-20 03:03:02 +02:00
|
|
|
}
|
2019-04-20 15:46:58 +02:00
|
|
|
|
|
|
|
// Based on the drop we have to determine a node to start looking for a parent container.
|
|
|
|
let destNode: Node = {
|
|
|
|
if destinationIndexPath.row == 0 {
|
2019-04-22 00:42:26 +02:00
|
|
|
return navState.rootNode.childAtIndex(destinationIndexPath.section)!
|
2019-04-20 15:46:58 +02:00
|
|
|
} else {
|
|
|
|
let movementAdjustment = sourceIndexPath > destinationIndexPath ? 1 : 0
|
|
|
|
let adjustedDestIndexPath = IndexPath(row: destinationIndexPath.row - movementAdjustment, section: destinationIndexPath.section)
|
2019-04-22 00:42:26 +02:00
|
|
|
return navState.nodeFor(adjustedDestIndexPath)!
|
2019-04-20 15:46:58 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Now we start looking for the parent container
|
2019-04-20 03:03:02 +02:00
|
|
|
let destParentNode: Node? = {
|
2019-04-20 15:46:58 +02:00
|
|
|
if destNode.representedObject is Container {
|
2019-04-20 03:03:02 +02:00
|
|
|
return destNode
|
|
|
|
} else {
|
2019-04-20 15:46:58 +02:00
|
|
|
if destNode.parent?.representedObject is Container {
|
2019-04-20 03:03:02 +02:00
|
|
|
return destNode.parent!
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-20 15:46:58 +02:00
|
|
|
// Move the Feed
|
2019-05-31 00:41:56 +02:00
|
|
|
guard let source = sourceNode.parent?.representedObject as? Container, let destination = destParentNode?.representedObject as? Container else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
BatchUpdate.shared.start()
|
|
|
|
source.account?.moveFeed(feed, from: source, to: destination) { result in
|
2019-05-09 14:28:51 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-05-31 00:41:56 +02:00
|
|
|
BatchUpdate.shared.end()
|
2019-05-09 14:28:51 +02:00
|
|
|
case .failure(let error):
|
2019-05-31 00:41:56 +02:00
|
|
|
self.presentError(error)
|
2019-05-09 14:28:51 +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-04-17 14:00:32 +02:00
|
|
|
|
2019-04-26 01:06:53 +02:00
|
|
|
let settingsNavViewController = UIStoryboard.settings.instantiateInitialViewController() as! UINavigationController
|
|
|
|
settingsNavViewController.modalPresentationStyle = .formSheet
|
2019-04-17 14:00:32 +02:00
|
|
|
|
2019-04-26 01:06:53 +02:00
|
|
|
let settingsViewController = settingsNavViewController.topViewController as! SettingsViewController
|
|
|
|
settingsViewController.presentingParentController = self
|
|
|
|
|
|
|
|
self.present(settingsNavViewController, animated: true)
|
2019-04-17 14:00:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-17 01:25:55 +02:00
|
|
|
@IBAction func markAllAsRead(_ sender: Any) {
|
|
|
|
|
|
|
|
let title = NSLocalizedString("Mark All Read", comment: "Mark All Read")
|
|
|
|
let message = NSLocalizedString("Mark all articles in all accounts as read?", comment: "Mark all articles")
|
|
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
|
|
|
|
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
|
|
|
|
let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel)
|
|
|
|
alertController.addAction(cancelAction)
|
|
|
|
|
|
|
|
let markTitle = NSLocalizedString("Mark All Read", comment: "Mark All Read")
|
|
|
|
let markAction = UIAlertAction(title: markTitle, style: .default) { [weak self] (action) in
|
|
|
|
|
2019-05-02 13:01:30 +02:00
|
|
|
let accounts = AccountManager.shared.activeAccounts
|
2019-04-17 01:25:55 +02:00
|
|
|
var articles = Set<Article>()
|
|
|
|
accounts.forEach { account in
|
|
|
|
articles.formUnion(account.fetchUnreadArticles())
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let undoManager = self?.undoManager,
|
|
|
|
let markReadCommand = MarkStatusCommand(initialArticles: Array(articles), markingRead: true, undoManager: undoManager) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self?.runCommand(markReadCommand)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
alertController.addAction(markAction)
|
|
|
|
|
|
|
|
present(alertController, animated: true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:38:07 +02:00
|
|
|
@IBAction func add(_ sender: UIBarButtonItem) {
|
2019-04-25 13:05:49 +02:00
|
|
|
let addViewController = UIStoryboard.add.instantiateInitialViewController()!
|
2019-05-20 00:47:33 +02:00
|
|
|
addViewController.modalPresentationStyle = .formSheet
|
|
|
|
addViewController.preferredContentSize = AddContainerViewController.preferredContentSizeForFormSheetDisplay
|
2019-04-25 13:05:49 +02:00
|
|
|
addViewController.popoverPresentationController?.barButtonItem = sender
|
2019-05-20 00:47:33 +02:00
|
|
|
|
2019-04-25 13:05:49 +02:00
|
|
|
self.present(addViewController, animated: true)
|
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-04-22 00:42:26 +02:00
|
|
|
let sectionNode = navState.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-04-22 01:13:39 +02:00
|
|
|
if navState.isExpanded(sectionNode) {
|
2019-04-21 01:20:25 +02:00
|
|
|
headerView.disclosureExpanded = false
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.collapse(section: sectionIndex) { [weak self] indexPaths in
|
2019-04-21 22:18:09 +02:00
|
|
|
self?.tableView.beginUpdates()
|
|
|
|
self?.tableView.deleteRows(at: indexPaths, with: .automatic)
|
|
|
|
self?.tableView.endUpdates()
|
|
|
|
}
|
2019-04-18 18:38:38 +02:00
|
|
|
} else {
|
2019-04-21 01:20:25 +02:00
|
|
|
headerView.disclosureExpanded = true
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.expand(section: sectionIndex) { [weak self] indexPaths in
|
2019-04-21 22:18:09 +02:00
|
|
|
self?.tableView.beginUpdates()
|
|
|
|
self?.tableView.insertRows(at: indexPaths, with: .automatic)
|
|
|
|
self?.tableView.endUpdates()
|
|
|
|
}
|
2019-04-18 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: API
|
|
|
|
|
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-04-20 18:11:09 +02:00
|
|
|
if node.parent?.representedObject is Folder {
|
|
|
|
cell.indentationLevel = 1
|
2019-04-20 18:25:02 +02:00
|
|
|
} else {
|
|
|
|
cell.indentationLevel = 0
|
2019-04-20 18:11:09 +02:00
|
|
|
}
|
2019-04-22 01:13:39 +02:00
|
|
|
cell.disclosureExpanded = navState.isExpanded(node)
|
2019-04-18 01:16:33 +02:00
|
|
|
cell.allowDisclosureSelection = node.canHaveChildNodes
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
cell.name = nameFor(node)
|
2019-04-28 19:37:53 +02:00
|
|
|
cell.unreadCount = unreadCountFor(node)
|
2019-04-15 22:03:05 +02:00
|
|
|
configureFavicon(cell, node)
|
2019-04-20 22:41:15 +02:00
|
|
|
cell.shouldShowImage = node.representedObject is SmallIconProvider
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func configureFavicon(_ cell: MasterFeedTableViewCell, _ node: Node) {
|
2019-04-15 22:03:05 +02:00
|
|
|
cell.faviconImage = imageFor(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageFor(_ node: Node) -> UIImage? {
|
|
|
|
if let smallIconProvider = node.representedObject as? SmallIconProvider {
|
|
|
|
return smallIconProvider.smallIcon
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameFor(_ node: Node) -> String {
|
|
|
|
if let displayNameProvider = node.representedObject as? DisplayNameProvider {
|
|
|
|
return displayNameProvider.nameForDisplay
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func unreadCountFor(_ node: Node) -> Int {
|
|
|
|
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
|
|
|
|
return unreadCountProvider.unreadCount
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func delete(indexPath: IndexPath) {
|
2019-04-18 15:54:48 +02:00
|
|
|
|
|
|
|
guard let undoManager = undoManager,
|
2019-04-22 00:42:26 +02:00
|
|
|
let deleteNode = navState.nodeFor(indexPath),
|
|
|
|
let deleteCommand = DeleteCommand(nodesToDelete: [deleteNode], treeController: navState.treeController, undoManager: undoManager)
|
2019-04-18 15:54:48 +02:00
|
|
|
else {
|
|
|
|
return
|
2019-04-17 20:35:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 01:13:39 +02:00
|
|
|
navState.beginUpdates()
|
2019-04-18 15:54:48 +02:00
|
|
|
|
|
|
|
runCommand(deleteCommand)
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.rebuildShadowTable()
|
2019-04-17 20:35:16 +02:00
|
|
|
tableView.deleteRows(at: [indexPath], with: .automatic)
|
|
|
|
|
2019-04-22 01:13:39 +02:00
|
|
|
navState.endUpdates()
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-17 20:35:16 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
func rename(indexPath: IndexPath) {
|
|
|
|
|
2019-04-22 00:42:26 +02:00
|
|
|
let name = (navState.nodeFor(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-04-22 00:42:26 +02:00
|
|
|
guard let node = self?.navState.nodeFor(indexPath),
|
2019-04-15 22:03:05 +02:00
|
|
|
let name = alertController.textFields?[0].text,
|
|
|
|
!name.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let feed = node.representedObject as? Feed {
|
|
|
|
feed.editedName = name
|
|
|
|
} else if let folder = node.representedObject as? Folder {
|
|
|
|
folder.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
alertController.addAction(renameAction)
|
|
|
|
|
|
|
|
alertController.addTextField() { textField in
|
|
|
|
textField.placeholder = NSLocalizedString("Name", comment: "Name")
|
|
|
|
}
|
|
|
|
|
|
|
|
self.present(alertController, animated: true) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-04-18 16:42:41 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:34:10 +02:00
|
|
|
// MARK: MasterTableViewCellDelegate
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
extension MasterFeedViewController: MasterFeedTableViewCellDelegate {
|
2019-04-17 17:34:10 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func disclosureSelected(_ sender: MasterFeedTableViewCell, expanding: Bool) {
|
2019-04-18 01:16:33 +02:00
|
|
|
if expanding {
|
2019-04-18 16:42:41 +02:00
|
|
|
expand(sender)
|
2019-04-18 01:16:33 +02:00
|
|
|
} else {
|
2019-04-18 16:42:41 +02:00
|
|
|
collapse(sender)
|
2019-04-18 01:16:33 +02:00
|
|
|
}
|
2019-04-17 17:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: Private
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
private extension MasterFeedViewController {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-18 21:36:22 +02:00
|
|
|
@objc private func refreshAccounts(_ sender: Any) {
|
2019-05-26 18:54:32 +02:00
|
|
|
AccountManager.shared.refreshAll(errorHandler: ErrorHandler.present)
|
2019-04-23 14:26:35 +02:00
|
|
|
refreshControl?.endRefreshing()
|
2019-04-18 21:36:22 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:35:48 +02:00
|
|
|
func updateUI() {
|
|
|
|
markAllAsReadButton.isEnabled = navState.isAnyUnreadAvailable
|
2019-05-19 23:08:48 +02:00
|
|
|
addNewItemButton.isEnabled = !AccountManager.shared.activeAccounts.isEmpty
|
2019-04-23 11:35:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
func configureCellsForRepresentedObject(_ representedObject: AnyObject) {
|
|
|
|
|
|
|
|
applyToCellsForRepresentedObject(representedObject, configure)
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func applyToCellsForRepresentedObject(_ representedObject: AnyObject, _ callback: (MasterFeedTableViewCell, Node) -> Void) {
|
2019-04-15 22:03:05 +02:00
|
|
|
applyToAvailableCells { (cell, node) in
|
|
|
|
if node.representedObject === representedObject {
|
|
|
|
callback(cell, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func applyToAvailableCells(_ callback: (MasterFeedTableViewCell, Node) -> Void) {
|
2019-04-15 22:03:05 +02:00
|
|
|
tableView.visibleCells.forEach { cell in
|
2019-04-22 00:42:26 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell), let node = navState.nodeFor(indexPath) else {
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-23 15:00:27 +02:00
|
|
|
callback(cell as! MasterFeedTableViewCell, node)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-20 03:03:02 +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
|
|
|
|
}
|
|
|
|
if let feed = node.representedObject as? Feed {
|
|
|
|
return feed.account
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func expand(_ cell: MasterFeedTableViewCell) {
|
2019-04-18 01:16:33 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell) else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.expand(indexPath) { [weak self] indexPaths in
|
2019-04-21 22:18:09 +02:00
|
|
|
self?.tableView.beginUpdates()
|
|
|
|
self?.tableView.insertRows(at: indexPaths, with: .automatic)
|
|
|
|
self?.tableView.endUpdates()
|
2019-04-18 01:16:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 18:38:38 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func collapse(_ cell: MasterFeedTableViewCell) {
|
2019-04-18 01:16:33 +02:00
|
|
|
guard let indexPath = tableView.indexPath(for: cell) else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-22 00:42:26 +02:00
|
|
|
navState.collapse(indexPath) { [weak self] indexPaths in
|
2019-04-21 22:18:09 +02:00
|
|
|
self?.tableView.beginUpdates()
|
|
|
|
self?.tableView.deleteRows(at: indexPaths, with: .automatic)
|
|
|
|
self?.tableView.endUpdates()
|
2019-04-18 01:16:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 22:18:09 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|