Skip to content

Commit

Permalink
Remove the UserDefaults key for a shortcut completely if the `default…
Browse files Browse the repository at this point in the history
…` parameter on `Name` is not used (#114)
  • Loading branch information
kaunteya authored Dec 13, 2022
1 parent a105b13 commit a99680c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Sources/KeyboardShortcuts/KeyboardShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ public enum KeyboardShortcuts {
You would usually not need this as the user would be the one setting the shortcut in a settings user-interface, but it can be useful when, for example, migrating from a different keyboard shortcuts package.
*/
public static func setShortcut(_ shortcut: Shortcut?, for name: Name) {
guard let shortcut else {
userDefaultsRemove(name: name)
return
if let shortcut {
userDefaultsSet(name: name, shortcut: shortcut)
} else {
if name.defaultShortcut != nil {
userDefaultsDisable(name: name)
} else {
userDefaultsRemove(name: name)
}
}

userDefaultsSet(name: name, shortcut: shortcut)
}

/**
Expand Down Expand Up @@ -355,7 +358,7 @@ public enum KeyboardShortcuts {
userDefaultsDidChange(name: name)
}

static func userDefaultsRemove(name: Name) {
static func userDefaultsDisable(name: Name) {
guard let shortcut = getShortcut(for: name) else {
return
}
Expand All @@ -365,6 +368,16 @@ public enum KeyboardShortcuts {
userDefaultsDidChange(name: name)
}

static func userDefaultsRemove(name: Name) {
guard let shortcut = getShortcut(for: name) else {
return
}

UserDefaults.standard.removeObject(forKey: userDefaultsKey(for: name))
unregister(shortcut)
userDefaultsDidChange(name: name)
}

static func userDefaultsContains(name: Name) -> Bool {
UserDefaults.standard.object(forKey: userDefaultsKey(for: name)) != nil
}
Expand Down

0 comments on commit a99680c

Please sign in to comment.