Implement keyboard shortcut infrastructure and first shortcuts

This commit is contained in:
Maurice Parker
2019-09-04 16:24:16 -05:00
parent 00a84ce6b5
commit 68143aa2c5
5 changed files with 88 additions and 22 deletions

View File

@@ -22,10 +22,15 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
var undoableCommands = [UndoableCommand]()
weak var coordinator: SceneCoordinator!
lazy var keyboardManager = KeyboardManager(type: .sidebar, coordinator: coordinator)
override var keyCommands: [UIKeyCommand]? {
return keyboardManager.keyCommands
}
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
@@ -253,6 +258,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
becomeFirstResponder()
coordinator.selectFeed(indexPath)
}
@@ -374,6 +380,12 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
}
}
// MARK: Keyboard shortcuts
@objc func openInBrowser(_ sender: Any?) {
coordinator.showBrowserForCurrentFeed()
}
// MARK: API
func updateFeedSelection() {
@@ -651,31 +663,25 @@ private extension MasterFeedViewController {
}
func homePageAction(indexPath: IndexPath) -> UIAction? {
guard let node = coordinator.nodeFor(indexPath),
let feed = node.representedObject as? Feed,
let homePageURL = feed.homePageURL,
let url = URL(string: homePageURL) else {
return nil
guard coordinator.homePageURLForFeed(indexPath) != nil else {
return nil
}
let title = NSLocalizedString("Open Home Page", comment: "Open Home Page")
let action = UIAction(title: title, image: AppAssets.safariImage) { action in
UIApplication.shared.open(url, options: [:])
let action = UIAction(title: title, image: AppAssets.safariImage) { [weak self] action in
self?.coordinator.showBrowserForFeed(indexPath)
}
return action
}
func homePageAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let node = coordinator.nodeFor(indexPath),
let feed = node.representedObject as? Feed,
let homePageURL = feed.homePageURL,
let url = URL(string: homePageURL) else {
return nil
guard coordinator.homePageURLForFeed(indexPath) != nil else {
return nil
}
let title = NSLocalizedString("Open Home Page", comment: "Open Home Page")
let action = UIAlertAction(title: title, style: .default) { action in
UIApplication.shared.open(url, options: [:])
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
self?.coordinator.showBrowserForFeed(indexPath)
completionHandler(true)
}
return action

View File

@@ -24,10 +24,15 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
weak var coordinator: SceneCoordinator!
var undoableCommands = [UndoableCommand]()
lazy var keyboardManager = KeyboardManager(type: .timeline, coordinator: coordinator)
override var keyCommands: [UIKeyCommand]? {
return keyboardManager.keyCommands
}
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
@@ -261,6 +266,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
becomeFirstResponder()
coordinator.selectArticle(indexPath, automated: false)
}
@@ -644,7 +650,7 @@ private extension MasterTimelineViewController {
}
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
let action = UIAction(title: title, image: AppAssets.safariImage) { [weak self] action in
self?.coordinator.showBrowser(for: indexPath)
self?.coordinator.showBrowserForArticle(indexPath)
}
return action
}
@@ -655,7 +661,7 @@ private extension MasterTimelineViewController {
}
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
let action = UIAlertAction(title: title, style: .default) { [weak self] action in
self?.coordinator.showBrowser(for: indexPath)
self?.coordinator.showBrowserForArticle(indexPath)
completionHandler(true)
}
return action

View File

@@ -0,0 +1,21 @@
//
// RootSplitViewController.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 9/4/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
class RootSplitViewController: UISplitViewController {
var coordinator: SceneCoordinator!
// MARK: Keyboard Shortcuts
@objc func openInBrowser(_ sender: Any?) {
coordinator.showBrowserForCurrentArticle()
}
}

View File

@@ -26,7 +26,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
private var activityManager = ActivityManager()
private var rootSplitViewController: UISplitViewController!
private var rootSplitViewController: RootSplitViewController!
private var masterNavigationController: UINavigationController!
private var masterFeedViewController: MasterFeedViewController!
private var masterTimelineViewController: MasterTimelineViewController?
@@ -222,7 +222,10 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
func start() -> UIViewController {
rootSplitViewController = UISplitViewController.template()
rootSplitViewController = RootSplitViewController()
rootSplitViewController.coordinator = self
rootSplitViewController.preferredDisplayMode = .automatic
rootSplitViewController.viewControllers = [ThemedNavigationController.template()]
rootSplitViewController.delegate = self
masterNavigationController = (rootSplitViewController.viewControllers.first as! UINavigationController)
@@ -707,7 +710,29 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
masterFeedViewController.present(addViewController, animated: true)
}
func showBrowser(for indexPath: IndexPath) {
func homePageURLForFeed(_ indexPath: IndexPath) -> URL? {
guard let node = nodeFor(indexPath),
let feed = node.representedObject as? Feed,
let homePageURL = feed.homePageURL,
let url = URL(string: homePageURL) else {
return nil
}
return url
}
func showBrowserForFeed(_ indexPath: IndexPath) {
if let url = homePageURLForFeed(indexPath) {
UIApplication.shared.open(url, options: [:])
}
}
func showBrowserForCurrentFeed() {
if let ip = currentMasterIndexPath, let url = homePageURLForFeed(ip) {
UIApplication.shared.open(url, options: [:])
}
}
func showBrowserForArticle(_ indexPath: IndexPath) {
guard let preferredLink = articles[indexPath.row].preferredLink, let url = URL(string: preferredLink) else {
return
}