Export/Import - how to implement?
iSapozhnik opened this issue ยท 6 comments
In my app, I have some user-defined actions. Let's say this action is a codable struct that I can save as binary data on the disk. I'm planning also to use KeyboardShortcuts so that those actions could be triggered by the shortcut. Is there a way to export and then import those shortcuts attached to the action?
This is how I do it in one of my apps:
struct Folder: Codable {
let id: UUID
var title: String?
var toggleMenuShortcutName: KeyboardShortcuts.Name { .init(id.uuidString) }
}
And it can then be used like this:
KeyboardShortcuts.Recorder("Toggle menu:", name: folder.toggleMenuShortcutName)
Thanks for the reply. If I understand correctly this will save the name of the shortcut, right? Imagine the following flow... I encode Folder
into data, store it on the disk, and share it with someone. If this other person imports the file into the app โ he won't be able to use the shortcut I set before the export action. Is there a way to achieve that?
Ok. Then you need to store the Shortcut
too. It's Codable
.
The ideal solution would be for the recorder to accept a binding so you could store the shortcut directly in the struct on changes: Recorder(shortcut: $shortcut)
However, this opens a can of worms as then it would loose out on a lot of the utility methods provided for the keyboard name identifier, like events.
The easiest workaround for now would probably be to use the onChange
handler in Recorder
to update your struct shortcut on changes.
Thanks @sindresorhus.
So I extended my struct and added one more property - shortcut. The good thing is that both the struct and the shortcut are codable so I can make a binary, save it to the file, import it on another machine, and use it. In order to use I need to again establish a connection between the shortcut name and the shortcut itself and of course setup handlers.
Just tried this approach and it works great!
Thanks again! We can close it now.