Files
NetNewsWire/Mac/MainWindow/Timeline/TimelineContainerViewController.swift
T

231 lines
7.2 KiB
Swift
Raw Normal View History

//
2019-02-14 21:58:45 -08:00
// TimelineContainerViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/14/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import AppKit
import Account
import Articles
2025-11-18 13:25:47 -08:00
@MainActor protocol TimelineContainerViewControllerDelegate: AnyObject {
func timelineSelectionDidChange(_: TimelineContainerViewController, articles: [Article]?, mode: TimelineSourceMode)
2025-11-03 08:55:25 -08:00
func timelineRequestedFeedSelection(_: TimelineContainerViewController, feed: Feed)
func timelineInvalidatedRestorationState(_: TimelineContainerViewController)
}
2019-02-16 22:14:06 -08:00
final class TimelineContainerViewController: NSViewController {
2025-11-18 22:22:38 -08:00
@IBOutlet var viewOptionsPopUpButton: NSPopUpButton!
@IBOutlet var newestToOldestMenuItem: NSMenuItem!
@IBOutlet var oldestToNewestMenuItem: NSMenuItem!
@IBOutlet var groupByFeedMenuItem: NSMenuItem!
2025-11-05 11:22:10 -08:00
2025-11-18 22:22:38 -08:00
@IBOutlet var readFilteredButton: NSButton!
2019-02-16 22:14:06 -08:00
@IBOutlet var containerView: TimelineContainerView!
2019-02-18 12:38:14 -08:00
var currentTimelineViewController: TimelineViewController? {
didSet {
let view = currentTimelineViewController?.view
if containerView.contentView === view {
return
}
containerView.contentView = view
view?.window?.recalculateKeyViewLoop()
2019-02-18 12:38:14 -08:00
}
}
2025-11-05 11:22:10 -08:00
var windowState: TimelineWindowState? {
return currentTimelineViewController?.windowState
}
2019-02-18 12:38:14 -08:00
weak var delegate: TimelineContainerViewControllerDelegate?
var isReadFiltered: Bool? {
guard let currentTimelineViewController = currentTimelineViewController, mode(for: currentTimelineViewController) == .regular else { return nil }
return regularTimelineViewController.isReadFiltered
}
var isCleanUpAvailable: Bool {
guard let currentTimelineViewController = currentTimelineViewController, mode(for: currentTimelineViewController) == .regular else { return false }
return regularTimelineViewController.isCleanUpAvailable
}
2025-11-05 11:22:10 -08:00
lazy var regularTimelineViewController = {
2019-02-16 22:14:06 -08:00
return TimelineViewController(delegate: self)
}()
private lazy var searchTimelineViewController: TimelineViewController = {
let viewController = TimelineViewController(delegate: self)
viewController.showsSearchResults = true
return viewController
2019-02-16 22:14:06 -08:00
}()
override func viewDidLoad() {
super.viewDidLoad()
2019-02-17 19:38:17 -08:00
setRepresentedObjects(nil, mode: .regular)
showTimeline(for: .regular)
2025-11-05 11:22:10 -08:00
makeMenuItemTitleLarger(newestToOldestMenuItem)
makeMenuItemTitleLarger(oldestToNewestMenuItem)
makeMenuItemTitleLarger(groupByFeedMenuItem)
updateViewOptionsPopUpButton()
2025-11-05 11:22:10 -08:00
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()
}
}
}
2025-11-05 11:22:10 -08:00
// MARK: - Notifications
2025-11-05 11:22:10 -08:00
2025-11-18 20:22:48 -08:00
func userDefaultsDidChange() {
updateViewOptionsPopUpButton()
}
2025-11-05 11:22:10 -08:00
2019-02-16 22:14:06 -08:00
// MARK: - API
2019-02-17 19:38:17 -08:00
func setRepresentedObjects(_ objects: [AnyObject]?, mode: TimelineSourceMode) {
timelineViewController(for: mode).representedObjects = objects
updateReadFilterButton()
2019-02-16 22:14:06 -08:00
}
func showTimeline(for mode: TimelineSourceMode) {
2019-02-18 12:38:14 -08:00
currentTimelineViewController = timelineViewController(for: mode)
}
func regularTimelineViewControllerHasRepresentedObjects(_ representedObjects: [AnyObject]?) -> Bool {
// Use this to find out if the regular timeline view already has the specified representedObjects.
// This is used in determining whether a search should end.
// The sidebar may think that the selection has changed, and therefore search should end —
// but it could be that the regular timeline already has these representedObjects,
// and therefore the selection hasn’t actually changed,
// and therefore search shouldn’t end.
// https://github.com/brentsimmons/NetNewsWire/issues/791
if representedObjects == nil && regularTimelineViewController.representedObjects == nil {
return true
}
guard let currentObjects = regularTimelineViewController.representedObjects, let representedObjects = representedObjects else {
return false
}
if currentObjects.count != representedObjects.count {
return false
}
for object in representedObjects {
2026-01-02 17:50:35 -08:00
if currentObjects.firstIndex(where: { $0 === object }) == nil {
return false
}
}
return true
2019-02-16 22:14:06 -08:00
}
2025-11-05 11:22:10 -08:00
2020-03-15 18:02:55 -05:00
func cleanUp() {
regularTimelineViewController.cleanUp()
}
2025-11-05 11:22:10 -08:00
func toggleReadFilter() {
regularTimelineViewController.toggleReadFilter()
updateReadFilterButton()
}
2025-11-05 11:22:10 -08:00
// MARK: State Restoration
2025-11-05 11:22:10 -08:00
func restoreState(from state: TimelineWindowState?) {
guard let state else { return }
2025-11-05 11:22:10 -08:00
regularTimelineViewController.restoreState(from: state)
updateReadFilterButton()
}
/// Restore state using legacy state restoration data.
///
/// TODO: Delete for NetNewsWire 7.
func restoreLegacyState(from state: [AnyHashable: Any]) {
regularTimelineViewController.restoreLegacyState(from: state)
updateReadFilterButton()
}
2019-02-16 22:14:06 -08:00
}
extension TimelineContainerViewController: TimelineDelegate {
func timelineSelectionDidChange(_ timelineViewController: TimelineViewController, selectedArticles: [Article]?) {
delegate?.timelineSelectionDidChange(self, articles: selectedArticles, mode: mode(for: timelineViewController))
2019-02-16 22:14:06 -08:00
}
2025-11-03 09:32:50 -08:00
func timelineRequestedFeedSelection(_: TimelineViewController, feed: Feed) {
delegate?.timelineRequestedFeedSelection(self, feed: feed)
}
2025-11-05 11:22:10 -08:00
func timelineInvalidatedRestorationState(_: TimelineViewController) {
delegate?.timelineInvalidatedRestorationState(self)
}
2025-11-05 11:22:10 -08:00
2019-02-16 22:14:06 -08:00
}
private extension TimelineContainerViewController {
func makeMenuItemTitleLarger(_ menuItem: NSMenuItem) {
menuItem.attributedTitle = NSAttributedString(string: menuItem.title,
attributes: [NSAttributedString.Key.font: NSFont.controlContentFont(ofSize: NSFont.systemFontSize)])
}
2025-11-05 11:22:10 -08:00
2019-02-16 22:14:06 -08:00
func timelineViewController(for mode: TimelineSourceMode) -> TimelineViewController {
switch mode {
case .regular:
return regularTimelineViewController
case .search:
return searchTimelineViewController
}
}
func mode(for timelineViewController: TimelineViewController) -> TimelineSourceMode {
if timelineViewController === regularTimelineViewController {
return .regular
2026-01-02 11:57:55 -08:00
} else if timelineViewController === searchTimelineViewController {
return .search
}
assertionFailure("Expected timelineViewController to match either regular or search timelineViewController, but it doesn’t.")
return .regular // Should never get here.
}
2025-11-05 11:22:10 -08:00
2025-11-17 22:09:53 -08:00
func updateViewOptionsPopUpButton() {
if AppDefaults.shared.timelineSortDirection == .orderedAscending {
newestToOldestMenuItem.state = .off
oldestToNewestMenuItem.state = .on
viewOptionsPopUpButton.setTitle(oldestToNewestMenuItem.title)
} else {
newestToOldestMenuItem.state = .on
oldestToNewestMenuItem.state = .off
viewOptionsPopUpButton.setTitle(newestToOldestMenuItem.title)
}
2025-11-05 11:22:10 -08:00
if AppDefaults.shared.timelineGroupByFeed == true {
groupByFeedMenuItem.state = .on
} else {
groupByFeedMenuItem.state = .off
}
}
2025-11-05 11:22:10 -08:00
func updateReadFilterButton() {
2020-05-02 13:25:48 -05:00
guard currentTimelineViewController == regularTimelineViewController else {
readFilteredButton.isHidden = true
return
}
2025-11-05 11:22:10 -08:00
guard let isReadFiltered = regularTimelineViewController.isReadFiltered else {
readFilteredButton.isHidden = true
return
}
2025-11-05 11:22:10 -08:00
readFilteredButton.isHidden = false
2025-11-05 11:22:10 -08:00
if isReadFiltered {
2025-11-19 10:38:18 -08:00
readFilteredButton.image = Assets.Images.filterActive
} else {
2025-11-19 10:38:18 -08:00
readFilteredButton.image = Assets.Images.filterInactive
}
}
2025-11-05 11:22:10 -08:00
}