frenzy-ios/Reader/Screens/AppSplitViewController.swift

104 lines
3.4 KiB
Swift
Raw Normal View History

2022-01-15 19:09:30 +00:00
//
// AppSplitViewController.swift
// Reader
//
// Created by Shadowfacts on 1/14/22.
//
import UIKit
#if targetEnvironment(macCatalyst)
import AppKit
#endif
class AppSplitViewController: UISplitViewController {
private let fervorController: FervorController
private var secondaryNav: UINavigationController!
init(fervorController: FervorController) {
self.fervorController = fervorController
super.init(style: .doubleColumn)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
preferredDisplayMode = .oneBesideSecondary
preferredSplitBehavior = .tile
presentsWithGesture = true
showsSecondaryOnlyButton = true
primaryBackgroundStyle = .sidebar
let sidebarHome = HomeViewController(fervorController: fervorController)
sidebarHome.enableStretchyMenu = false
sidebarHome.itemsDelegate = self
let sidebarNav = UINavigationController(rootViewController: sidebarHome)
sidebarNav.navigationBar.prefersLargeTitles = true
setViewController(sidebarNav, for: .primary)
secondaryNav = UINavigationController()
secondaryNav.isNavigationBarHidden = true
secondaryNav.view.backgroundColor = .appBackground
setViewController(secondaryNav, for: .secondary)
let home = HomeViewController(fervorController: fervorController)
let nav = AppNavigationController(rootViewController: home)
setViewController(nav, for: .compact)
}
#if targetEnvironment(macCatalyst)
@objc func toggleItemRead(_ item: NSToolbarItem) {
guard let nav = viewController(for: .secondary) as? UINavigationController,
let read = nav.topViewController as? ReadViewController else {
return
}
read.item.read = !read.item.read
updateImage(toolbarItem: item)
}
private func updateImage(toolbarItem: NSToolbarItem) {
if let nav = viewController(for: .secondary) as? UINavigationController,
let read = nav.topViewController as? ReadViewController {
toolbarItem.image = UIImage(systemName: read.item.read ? "checkmark.circle.fill" : "checkmark.circle")
} else {
toolbarItem.image = UIImage(systemName: "checkmark.circle")
}
}
#endif
}
extension AppSplitViewController: ItemsViewControllerDelegate {
func showReadItem(_ item: Item) {
secondaryNav.setViewControllers([ReadViewController(item: item, fervorController: fervorController)], animated: false)
#if targetEnvironment(macCatalyst)
if let titlebar = view.window?.windowScene?.titlebar,
let toggleRead = titlebar.toolbar?.items.first(where: { $0.itemIdentifier == .toggleItemRead }) {
updateImage(toolbarItem: toggleRead)
}
#endif
}
}
#if targetEnvironment(macCatalyst)
extension AppSplitViewController {
override func responds(to aSelector: Selector!) -> Bool {
if aSelector == #selector(toggleItemRead) {
guard let nav = viewController(for: .secondary) as? UINavigationController else {
return false
}
return nav.topViewController is ReadViewController
} else {
return super.responds(to: aSelector)
}
}
}
#endif