Add some keyboard shortcuts

This adds the keyboard shortcuts for ⌘ + <number> to switch between tabs
and ⌘ + n to add a transaction
This commit is contained in:
William Brawner 2021-10-18 06:34:17 -06:00
parent 1d6693d0cc
commit a97e9f3102
2 changed files with 30 additions and 2 deletions

View file

@ -11,6 +11,21 @@ import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(title: "First Tab", action: #selector(handleKeyCommand(sender:)), input: "1", modifierFlags: .command, propertyList: 0),
UIKeyCommand(title: "Second Tab", action: #selector(handleKeyCommand(sender:)), input: "2", modifierFlags: .command, propertyList: 1),
UIKeyCommand(title: "Third Tab", action: #selector(handleKeyCommand(sender:)), input: "3", modifierFlags: .command, propertyList: 2),
UIKeyCommand(title: "Fourth Tab", action: #selector(handleKeyCommand(sender:)), input: "4", modifierFlags: .command, propertyList: 3),
UIKeyCommand(title: "Fifth Tab", action: #selector(handleKeyCommand(sender:)), input: "5", modifierFlags: .command, propertyList: 4),
]
}
@objc func handleKeyCommand(sender: UIKeyCommand) {
if let tabTag = sender.propertyList as? Int {
NotificationCenter.default.post(name: .init("switchTabs"), object: tabTag)
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

View file

@ -23,6 +23,7 @@ struct TabbedBudgetView: View {
Text("Overview")
}
.tag(0)
.keyboardShortcut("1")
TransactionListView(self.budget)
.sheet(isPresented: $isAddingTransaction,
onDismiss: {
@ -37,18 +38,19 @@ struct TabbedBudgetView: View {
Text("transactions")
}
.tag(1)
.keyboardShortcut("2")
CategoryListView(self.budget).tabItem {
Image(systemName: "chart.pie.fill")
Text("categories")
}
.tag(2)
.keyboardShortcut("3")
ProfileView().tabItem {
Image(systemName: "person.circle.fill")
Text("profile")
}
.tag(3)
.keyboardShortcut("4")
}.navigationBarItems(
trailing: HStack {
if tabSelection == 1 {
@ -58,9 +60,20 @@ struct TabbedBudgetView: View {
Image(systemName: "plus")
.padding()
}
.keyboardShortcut("n")
}
}
)
.onReceive(NotificationCenter.default.publisher(for: Notification.Name("switchTabs"))) { notification in
if let tabTag = notification.object as? Int {
if 0...3 ~= tabTag {
self.tabSelection = tabTag
print("Updating tabSelection to \(tabTag)")
} else {
print("Ignoring value \(tabTag)")
}
}
}
}
init (_ budget: Budget) {