Skip to content

Commit

Permalink
Make toggle completed rows use the responder chain
Browse files Browse the repository at this point in the history
  • Loading branch information
vincode-io committed Aug 8, 2024
1 parent b06bae0 commit 452fecc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 59 deletions.
25 changes: 1 addition & 24 deletions Zavala/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
modifierFlags: [.control, .command])

let toggleCompleteRowsCommand = UIKeyCommand(title: .completeControlLabel,
action: #selector(toggleCompleteRowsCommand(_:)),
action: .toggleCompleteRows,
input: "\n",
modifierFlags: [.command])

let completeRowsCommand = UIKeyCommand(title: .completeControlLabel,
action: #selector(toggleCompleteRowsCommand(_:)),
input: "\n",
modifierFlags: [.command])

let uncompleteRowsCommand = UIKeyCommand(title: .uncompleteControlLabel,
action: #selector(toggleCompleteRowsCommand(_:)),
input: "\n",
modifierFlags: [.command])

let rowNotesCommand = UIKeyCommand(title: .addNoteControlLabel,
action: #selector(rowNotesCommand(_:)),
input: "-",
Expand Down Expand Up @@ -418,10 +408,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
mainCoordinator?.duplicateRows()
}

@objc func toggleCompleteRowsCommand(_ sender: Any?) {
mainCoordinator?.toggleCompleteRows()
}

@objc func rowNotesCommand(_ sender: Any?) {
if mainCoordinator?.isCreateRowNotesUnavailable ?? true {
if mainCoordinator?.isEditingTopic ?? false {
Expand Down Expand Up @@ -556,15 +542,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if mainCoordinator?.isDuplicateRowsUnavailable ?? true {
command.attributes = .disabled
}
case #selector(toggleCompleteRowsCommand(_:)):
if mainCoordinator?.isCompleteRowsAvailable ?? false {
command.title = .completeControlLabel
} else {
command.title = .uncompleteControlLabel
}
if mainCoordinator?.isToggleRowCompleteUnavailable ?? true {
command.attributes = .disabled
}
case #selector(rowNotesCommand(_:)):
if mainCoordinator?.isCreateRowNotesUnavailable ?? true {
if mainCoordinator?.isEditingTopic ?? false {
Expand Down
52 changes: 29 additions & 23 deletions Zavala/Editor/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extension Selector {
static let moveCurrentRowsRight = #selector(EditorViewController.moveCurrentRowsRight(_:))
static let moveCurrentRowsUp = #selector(EditorViewController.moveCurrentRowsUp(_:))
static let moveCurrentRowsDown = #selector(EditorViewController.moveCurrentRowsDown(_:))
static let toggleCompleteRows = #selector(EditorViewController.toggleCompleteRows(_:))
}

@MainActor
Expand Down Expand Up @@ -55,18 +56,18 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
var keyCommands = [UIKeyCommand]()

if !isEditingNote {
let shiftTab = UIKeyCommand(input: "\t", modifierFlags: [.shift], action: #selector(moveCurrentRowsLeft))
let shiftTab = UIKeyCommand(input: "\t", modifierFlags: [.shift], action: .moveCurrentRowsLeft)
shiftTab.wantsPriorityOverSystemBehavior = true
keyCommands.append(shiftTab)

let tab = UIKeyCommand(action: #selector(moveCurrentRowsRight), input: "\t")
let tab = UIKeyCommand(action: .moveCurrentRowsRight, input: "\t")
tab.wantsPriorityOverSystemBehavior = true
keyCommands.append(tab)
}

// We need to have this here in addition to the AppDelegate, since iOS won't pick it up for some reason
if !isToggleRowCompleteUnavailable {
let commandReturn = UIKeyCommand(input: "\r", modifierFlags: [.command], action: #selector(toggleCompleteRows))
if UIResponder.valid(action: .toggleCompleteRows) {
let commandReturn = UIKeyCommand(input: "\r", modifierFlags: [.command], action: .toggleCompleteRows)
commandReturn.wantsPriorityOverSystemBehavior = true
keyCommands.append(commandReturn)
}
Expand Down Expand Up @@ -128,16 +129,6 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
return delegate?.editorViewControllerIsGoForwardUnavailable ?? true
}

var isToggleRowCompleteUnavailable: Bool {
guard let outline, let rows = currentRows else { return true }
return outline.isCompleteUnavailable(rows: rows) && outline.isUncompleteUnavailable(rows: rows)
}

var isCompleteRowsAvailable: Bool {
guard let outline, let rows = currentRows else { return true }
return !outline.isCompleteUnavailable(rows: rows)
}

var isCreateRowNotesUnavailable: Bool {
guard let outline, let rows = currentRows else { return true }
return outline.isCreateNotesUnavailable(rows: rows)
Expand Down Expand Up @@ -657,13 +648,28 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
} else {
return false
}
case .toggleCompleteRows:
if let outline, let currentRows, !(outline.isCompleteUnavailable(rows: currentRows) && outline.isUncompleteUnavailable(rows: currentRows)) {
return true
} else {
return false
}
default:
return super.canPerformAction(action, withSender: sender)
}
}

override func validate(_ command: UICommand) {
switch command.action {
case .toggleCompleteRows:
if let outline, let currentRows, !outline.isCompleteUnavailable(rows: currentRows) {
command.title = .completeControlLabel
} else {
command.title = .uncompleteControlLabel
}
if !UIResponder.valid(action: command.action) {
command.attributes = .disabled
}
default:
break
}
Expand Down Expand Up @@ -1292,6 +1298,15 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
moveRowsDown(rows)
}

@objc func toggleCompleteRows(_ sender: Any?) {
guard let outline, let rows = currentRows else { return }
if !outline.isCompleteUnavailable(rows: rows) {
completeRows(rows)
} else if !outline.isUncompleteUnavailable(rows: rows) {
uncompleteRows(rows)
}
}

@objc func insertNewline() {
currentTextView?.insertNewline(self)
}
Expand Down Expand Up @@ -1365,15 +1380,6 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
}
}

@objc func toggleCompleteRows() {
guard let outline, let rows = currentRows else { return }
if !outline.isCompleteUnavailable(rows: rows) {
completeRows(rows)
} else if !outline.isUncompleteUnavailable(rows: rows) {
uncompleteRows(rows)
}
}

}

// MARK: UICollectionViewDelegate, UICollectionViewDataSource
Expand Down
12 changes: 0 additions & 12 deletions Zavala/MainCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ extension MainCoordinator {
return editorViewController?.isDuplicateRowsUnavailable ?? true
}

var isToggleRowCompleteUnavailable: Bool {
return editorViewController?.isToggleRowCompleteUnavailable ?? true
}

var isCompleteRowsAvailable: Bool {
return editorViewController?.isCompleteRowsAvailable ?? false
}

var isCreateRowNotesUnavailable: Bool {
return editorViewController?.isCreateRowNotesUnavailable ?? true
}
Expand Down Expand Up @@ -159,10 +151,6 @@ extension MainCoordinator {
editorViewController?.toggleNotesFilter()
}

func toggleCompleteRows() {
editorViewController?.toggleCompleteRows()
}

func moveCursorToCurrentRowTopic() {
editorViewController?.moveCursorToCurrentRowTopic()
}
Expand Down

0 comments on commit 452fecc

Please sign in to comment.