Skip to content

Commit

Permalink
SpiceKeyData supports Codable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Nov 4, 2022
1 parent 48d4287 commit f0536bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<key>SpiceKeyDemo_AppKit.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>SpiceKeyDemo_SwiftUI.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ spiceKey.unregister()

- Save shortcut

SpiceKeyData supports NSCoding & Codable.

```swift
let key = Key.a
let flags = ModifierFlags.optCmd
Expand Down
39 changes: 35 additions & 4 deletions Sources/SpiceKey/SpiceKeyData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
// Copyright © 2019 Takuto Nakamura. All rights reserved.
//

import Foundation.NSObject
import Foundation

open class SpiceKeyData: NSObject, NSCoding, Codable {
public enum CodingKeys: String, CodingKey {
case primaryKey
case keyCode
case control
case option
case shift
case command
}

open class SpiceKeyData: NSObject, NSCoding {
public var primaryKey: String
public var keyCode: CGKeyCode
public var control: Bool
Expand Down Expand Up @@ -56,7 +65,8 @@ open class SpiceKeyData: NSObject, NSCoding {
self.command = modifierFlags.containsCommand
self.spiceKey = spiceKey
}


// NSCoding
required public init?(coder: NSCoder) {
primaryKey = (coder.decodeObject(forKey: "primaryKey") as? String) ?? ""
keyCode = coder.decodeObject(forKey: "keyCode") as! CGKeyCode
Expand All @@ -65,7 +75,7 @@ open class SpiceKeyData: NSObject, NSCoding {
shift = coder.decodeBool(forKey: "shift")
command = coder.decodeBool(forKey: "command")
}

public func encode(with coder: NSCoder) {
coder.encode(primaryKey, forKey: "primaryKey")
coder.encode(keyCode, forKey: "keyCode")
Expand All @@ -74,4 +84,25 @@ open class SpiceKeyData: NSObject, NSCoding {
coder.encode(shift, forKey: "shift")
coder.encode(command, forKey: "command")
}

// Codable
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
primaryKey = try container.decode(String.self, forKey: .primaryKey)
keyCode = try container.decode(CGKeyCode.self, forKey: .keyCode)
control = try container.decode(Bool.self, forKey: .control)
option = try container.decode(Bool.self, forKey: .option)
shift = try container.decode(Bool.self, forKey: .shift)
command = try container.decode(Bool.self, forKey: .command)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(primaryKey, forKey: .primaryKey)
try container.encode(keyCode, forKey: .keyCode)
try container.encode(control, forKey: .control)
try container.encode(option, forKey: .option)
try container.encode(shift, forKey: .shift)
try container.encode(command, forKey: .command)
}
}

0 comments on commit f0536bc

Please sign in to comment.